2.6.3 Representing Object Creation and Constructor Arguments in Sequence Diagrams

Representing Object Creation and Constructor Arguments in Sequence Diagrams

A sequence diagram shows one particular creation event. The new Player lifeline begins at that event, and the creation message shows the actual arguments.

Follow the creation expression

Inside a controller method, use the two-parameter Player contract shown in the class-diagram lesson.

One creation expression
Player jordan = new Player("Jordan", 7);

The controller supplies Jordan and 7. The constructor stores those inputs in the new Player. When creation completes, jordan receives a reference to that same instance. The constructor body does not contain a return statement returning the Player.

Draw arguments and lifetime together
  1. Create the existing controller : Controller lifeline in Violet.
  2. Place jordan : Player so its heading begins where the creation message arrives.
  3. Label the creation message new Player("Jordan", 7). Show the constructor activation.
  4. Add a note identifying the initialized name as Jordan and jerseyNumber as 7.
  5. Show completion back to the controller. Do not draw a second Player to represent the returned reference.
Controller creates Jordan with name Jordan and jersey number 7; the new lifeline starts at creation and completion supplies the same instance reference.
Arguments travel with the creation request
Separate declaration and execution

The class diagram uses formal parameters such as name and jerseyNumber. This sequence uses the actual arguments Jordan and 7. A different creation call changes those values without changing the constructor signature.

Apply the idea
Check your understanding