2.5.3 Representing readonly Fields in Class Diagrams

Representing readonly Fields in Class Diagrams

The training turn limit is fixed for each Player. A diagram can show both who can access a field and whether its value may be reassigned after initialization.

Read a restriction separately from visibility

Use the UML annotation {readOnly} to show the assignment restriction. It is separate from the + or - visibility symbol. It does not mean the field is shared by all players.

Two field contracts
Diagram entry Meaning
- turnLimit : int {readOnly} Private limit whose assignment is restricted
- numberOfTurns : int Private count that may change
Draw the Player state
  1. Create a Player class box in Violet.
  2. Enter - turnLimit : int {readOnly} and - numberOfTurns : int as separate attributes.
  3. Keep the space after each visibility sign and retain the braces around readOnly.
  4. Do not underline either field. Underlining would describe static membership, a different idea.
Player stores a private readonly turnLimit and a private mutable numberOfTurns.
Fixed configuration and changing state
Apply the idea
Check your understanding