0.2.11 Representing Object Instances in Object Diagrams

Representing Object Instances in Object Diagrams

Two Wildcats players can have the same name and jersey number while remaining two separate people. An object diagram must preserve that distinction.

Read the Code Before Drawing

This short excerpt runs inside an existing method. Player has public text field name and integer field jerseyNumber. Each new Player() creates a distinct instance.

If the underlying modeling idea is unfamiliar, use the lesson below while reading this example.

Source to translate
Player first = new Player();
first.name = "Alex";
first.jerseyNumber = 10;
Player second = new Player();
second.name = "Alex";
second.jerseyNumber = 10;
Rewrite the Meaning in Ordinary Words
The same snapshot in ordinary words
Create a first Player instance
Store Alex as its name and 10 as its jersey number
Create a second Player instance
Store Alex as its name and 10 as its jersey number
Pause after both players have been initialized

These words fix the snapshot moment and separate instances, values, and reference targets. Follow the source to that pause before deciding how many boxes to draw and which current values belong inside them.

Map the Source to Object Boxes and References
Build the Diagram in Violet
  1. In Violet, create an object diagram. Choose the snapshot after the final assignment so both creation expressions and all field assignments have executed.
  2. Place two object nodes with space between them. Set their underlined headers to first : Player and second : Player. Each header identifies one instance and its type.
  3. Edit the first object's fields: put name in a field-name area with value Alex, then jerseyNumber with value 10. Enter the text without quotation marks in Violet's separate value editor.
  4. Enter the same two field names and values in the second object. Keep a separate box even though the displayed values match.
  5. Do not connect the two boxes: the source creates two players but declares no object-to-object field reference between them. Save the editable object diagram and read each header before its values.
Compare the Completed Soccer Diagram

Compare this reference with the diagram you built in Violet. Layout may differ; the source meaning, labels, and relevant connections should agree.

Two distinct Player instances have identical displayed values: Alex and jersey 10.
Matching values, two distinct players
  • New
  • Changed
  • Removed
Trace the Snapshot One Reference or Value at a Time

Point to each box while naming the creation expression that produced it. Editing the second player's jersey would change the second box only.

Verify the Drawing Against the Source
Predict and Check a Small Change