0.2.12 Representing Fields in Object Diagrams

Video: Create Object Diagrams with Fields in Violet (15:09)
Representing Fields in Object Diagrams

A soccer snapshot needs to distinguish the player's jersey, availability, and turn count. Field names give those values their meaning.

Read the Code Before Drawing

This excerpt uses an existing Player with public fields name (string), jerseyNumber and numberOfTurns (int), and isAvailable (bool). Draw the pause after all assignments.

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

Source to translate
Player jordan = new Player();
jordan.name = "Jordan";
jordan.jerseyNumber = 7;
jordan.numberOfTurns = 0;
jordan.isAvailable = true;
jordan.numberOfTurns = 1;
Rewrite the Meaning in Ordinary Words
The same snapshot in ordinary words
Create Jordan as one Player
Store the name Jordan and jersey number 7
Start the turn count at 0 and availability at true
Record a turn by changing the turn count to 1
Pause after that update

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. Create an object diagram in Violet and place one object headed jordan : Player. Choose the pause after the last line, not immediately after the initializer.
  2. Add four field-name entries: name, jerseyNumber, numberOfTurns, and isAvailable. Use Violet's field/value areas rather than putting all information into the object heading.
  3. Enter values Jordan, 7, 1, and true beside their matching names. The turn count is 1 because its final update has executed.
  4. Keep one row per field. Do not retain a second numberOfTurns row holding the old zero, and do not change the header when a value changes.
  5. Save the diagram, then read each row as a sentence: this particular player currently has that value. Check it against the last executed assignment to the same field.
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.

Jordan has jersey 7, one recorded turn, and availability true after the final assignment.
Field values after the turn is recorded
  • New
  • Changed
  • Removed
Trace the Snapshot One Reference or Value at a Time

Read numberOfTurns = 1 as: this particular player currently has one recorded turn. The 7 means jersey number because its field name says so.

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