2.6.6 Representing Object Creation in Sequence Diagrams

Representing Object Creation in Sequence Diagrams

A new object appears during an interaction. Its lifeline must begin where creation happens rather than pretending it was present all along.

Read the Code Before Drawing

Inside a Controller method, Player has a parameterless constructor that sets Name to "New player". The next assignment changes the new instance's public Name field.

Source to translate
Player selected = new Player();
selected.Name = "Jordan";
Rewrite the Meaning in Ordinary Words
Pseudocode for the same behavior
The controller creates one Player using the parameterless constructor
The constructor initializes Name to New player
The caller receives a reference in selected
The next statement changes that same object's Name to Jordan
No second Player is created

Use these words to follow one execution before drawing it. Keep the same order and the same alternatives as the source; simplify the wording without adding work that the code does not perform.

Map the Source to Lifelines and Messages
Build the Diagram in Violet
  1. Create a sequence diagram containing the existing controller : Controller lifeline.
  2. Place selected : Player with its heading at the incoming creation request rather than at the top of the interaction.
  3. Draw new Player() from controller to the new participant and show the constructor activation. Note Constructor sets Name to "New player".
  4. Show creation completion back to the caller. Beneath it, add a note Caller directly assigns selected.Name = "Jordan"; same object. Do not turn that public field assignment into an invented setter call.
  5. Save and count the creation messages. There should be one Player participant and one event beginning its lifetime.
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.

Controller creates one Player, then changes its Name field to Jordan on the same instance.
A lifeline starts at creation
  • New
  • Changed
  • Removed
Trace One Interaction from Top to Bottom

At the constructor's completion, Name is New player. After the following field assignment, it is Jordan. A sequence diagram can annotate these state facts without becoming a full object snapshot.

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