1.7.9 Representing Object Returns in Sequence Diagrams

Representing Object Returns in Sequence Diagrams

A returned player reference can give the controller another path to an existing object. Keep the call receiver and the returned object distinct.

Read the Code Before Drawing

Inside a Controller method, game1.CurrentPlayer already refers to jordan. GetCurrentPlayer() returns that field without creating or changing a player.

If the underlying C# feature is unfamiliar, use the lesson below while reading this example.

Source to translate
Player selected = game1.GetCurrentPlayer();
Rewrite the Meaning in Ordinary Words
Pseudocode for the same behavior
The controller asks game1 for its current player
game1 returns its existing reference to jordan
The controller stores that reference in selected
Jordan remains the same existing Player instance

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 existing lifelines controller : Controller, game1 : Game, and jordan : Player.
  2. Draw GetCurrentPlayer() from controller to game1 and activate game1 for the call.
  3. Draw a dashed result from game1 back to controller labeled jordan. End the game activation after that result.
  4. Add the note selected now refers to existing jordan near the caller and target. Do not draw selected as a fourth instance.
  5. Save and check that no create message or Player-to-controller return has been added. A later method call on selected would require a later message of its own.
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.

Game returns the existing Jordan reference to Controller, whose selected local now refers to Jordan.
Game returns a reference to an existing player
  • New
  • Changed
  • Removed
Trace One Interaction from Top to Bottom

The returned reference gives selected access to Jordan; it does not turn game1 into a Player. A later Player call would need its own message after this return.

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