1.7.8 Representing Object Return Types in Class Diagrams

Representing Object Return Types in Class Diagrams

The Wildcats controller can ask which player is current. The operation promises a Player reference, not a numeric score or a new player.

Read the Code Before Drawing

This complete Game definition returns an existing field. Player is an existing custom class; the getter does not create an instance.

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

Source to translate
public class Game
{
    public Player CurrentPlayer;

    public Player GetCurrentPlayer()
    {
        return CurrentPlayer;
    }
}
Rewrite the Meaning in Ordinary Words
The same design in ordinary words
Define Game with a CurrentPlayer reference field
Offer GetCurrentPlayer with no input parameters
Return the field's existing Player reference
Do not create a Player during the query

These words describe the class contract, not a sequence of runtime actions. Identify the types, stored members, operation inputs and results, and relationships before choosing compartments and connectors.

Map the Source to Class Boxes and Connectors
Build the Diagram in Violet
  1. Create a class diagram containing Game and Player.
  2. In Game's operations enter +GetCurrentPlayer() : Player. Keep the parentheses empty because there is no input.
  3. Represent the stored field with an association from Game to Player labeled +CurrentPlayer. Put 0..1 at the Player endpoint for this possibly unassigned reference.
  4. Do not create a second association just because the method returns Player. The operation entry already states that result contract.
  5. Save and read the field line and operation independently. One says what can be stored; the other says what a caller can request.
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 can store a CurrentPlayer reference and return a Player reference through GetCurrentPlayer.
Stored player reference and object return type
  • New
  • Changed
  • Removed
Read the Contract One Part at a Time

If CurrentPlayer refers to Jordan, the method returns that same reference. If the field is unassigned in this simple model, the method can return null; the signature alone does not guarantee a non-null object.

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