1.5.2 Representing Method Returns in Class Diagrams

Representing Method Returns in Class Diagrams

A class operation can promise a value back to its caller. The return type describes what kind of result a player query supplies.

Read the Code Before Drawing

This complete Player class reports its current turn count without changing it.

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

Source to translate
public class Player
{
    public int numberOfTurns;

    public int GetNumberOfTurns()
    {
        return numberOfTurns;
    }
}
Rewrite the Meaning in Ordinary Words
The same design in ordinary words
Define a Player with a stored integer turn count
Offer a public operation named GetNumberOfTurns
The operation accepts no inputs
It returns the current turn count as an integer
It does not change that count

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 with one Player box.
  2. Put +numberOfTurns : int in the attributes compartment.
  3. Put +GetNumberOfTurns() : int in the operations compartment. Preserve the empty parentheses and the result type after them.
  4. Leave the current turn count and the return statement out of the class box. They describe runtime state or implementation rather than the operation signature.
  5. Save and compare the result type against the C# declaration. Changing int to void would change what the method promises its caller.
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.

Player exposes GetNumberOfTurns with no parameters and an integer return type.
The return type follows the operation
  • New
  • Changed
  • Removed
Read the Contract One Part at a Time

An instance with count 2 can return 2; another with count 0 can return 0. Both follow the same operation declaration.

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