0.2.10 UML Object Diagrams

Video: Object Diagrams (10:02)
Object Diagrams: State at One Moment

Imagine the Wildcats coach takes a photograph just before a substitution. Jordan is wearing jersey 7 and is unavailable; Casey is wearing jersey 1 and is available. The photograph captures what is true now. It does not explain every action that produced that situation.

An object diagram is a selected snapshot of a software system. It answers: Which particular objects exist? What values do they hold? Which objects are connected? Together, an object's current values and connections describe its state. A later snapshot can show different state for the same objects.

Watch the Same Objects Change

The following 18 executable statements belong inside an existing method. For this reading example, Player is an existing class with public fields name (text), jerseyNumber and numberOfTurns (integers), and isAvailable (Boolean). The expression new Player() creates an instance; each assignment changes the named field. You do not need to write the class or learn method design to follow the snapshots.

The comments mark pause points, not extra commands. Each snapshot is taken after the statements above its marker and before the next executable statement.

One training session, four pause points
Player jordan = new Player();
jordan.name = "Jordan";
jordan.jerseyNumber = 7;
jordan.numberOfTurns = 0;
jordan.isAvailable = true;
// Pause A: before the next statement
Player casey = new Player();
casey.name = "Casey";
casey.jerseyNumber = 1;
casey.numberOfTurns = 0;
casey.isAvailable = true;
// Pause B: before the next statement
jordan.numberOfTurns = 1;
jordan.isAvailable = false;
casey.numberOfTurns = 1;
// Pause C: before the next statement
jordan.isAvailable = true;
jordan.numberOfTurns = 2;
casey.numberOfTurns = 2;
casey.isAvailable = false;
jordan.jerseyNumber = 9;
// Pause D: after the final assignment
Pause Before the Highlighted Statement

A breakpoint asks the debugger to pause before an executable statement runs. In Visual Studio, click the left margin beside Player casey = new Player(); to place a breakpoint for Pause A. Start debugging. The highlighted statement is next to execute: Casey has not been created yet. Compare Jordan's fields in Locals with the first diagram.

For Pause B, place a breakpoint beside jordan.numberOfTurns = 1;. For Pause C, place one beside jordan.isAvailable = true;. Continue between the breakpoints, or step over statements one at a time. To observe Pause D, step over the final jersey assignment and inspect the values while the local variables remain in scope. Source line numbers can change; use the exact statements to locate the pauses.

Pause A: One Initialized Player
Pause A: Casey creation is next; Jordan is initialized and Casey does not exist yet.
Illustrated debugger pause A
Jordan is a Player with jersey 7, zero turns, and availability true before training.
Pause A: one initialized player
  • New
  • Changed
  • Removed

Read the header first, then turn each row into a sentence: Jordan's turn count is currently zero. The type Player can describe many players, but this box describes one specific instance. Omitted facts, such as shoe color, are outside this model's purpose.

Pause B: Two Players, No Turns Yet
Pause B: Jordan turn update is next; both players exist, available, with zero turns.
Illustrated debugger pause B
At Pause B Jordan and Casey are distinct players, both available with zero turns. Their jerseys are 7 and 1.
Pause B: both players have been initialized
  • New
  • Changed
  • Removed

Casey's object now exists because its creation statement has executed. Jordan is the same instance as at Pause A. Creating Casey did not replace Jordan or copy Jordan's state.

Pause C: Same Identities, Changed State
Pause C: restoring Jordan availability is next; Jordan is unavailable and both players have one turn.
Illustrated debugger pause C
At Pause C both players have one turn. Jordan is unavailable, Casey is available, and jerseys remain 7 and 1.
Pause C: one turn each; Jordan is unavailable
  • New
  • Changed
  • Removed

Three assignments have executed since Pause B. Only Jordan's availability changed; Casey remains available. No new instance was created during those three statements.

Pause D: A Later State
Pause D: all eighteen displayed statements have executed; Jordan has jersey 9 and two turns, and Casey has two turns.
Illustrated debugger pause D
At Pause D both players have two turns. Jordan is available with jersey 9; Casey is unavailable with jersey 1.
Pause D: after the final assignment
  • New
  • Changed
  • Removed

Changing a jersey value did not create a new Jordan. A diagram needs both a clear snapshot time and stable instance labels so readers can distinguish a changed value from a different object.

An object diagram does not show the order of those assignments or the rule for selecting the next player. Use a process view when the question is what happens next; use this state view when the question is what exists and what is true at a pause.

Read the Evidence