3.3.3 Representing Enums in Class Diagrams

Representing Enums in Class Diagrams

An enum gives the Wildcats a named set of role values. The class diagram should keep that type distinct from Player instances and free-form text.

Read the Code Before Drawing

These complete declarations define a PlayerRole enum and a Player field and method that use it.

Source to translate
public enum PlayerRole
{
    Keeper,
    Forward,
    Defense
}

public class Player
{
    public PlayerRole Role;

    public void SetRole(PlayerRole role)
    {
        Role = role;
    }
}
Rewrite the Meaning in Ordinary Words
The same design in ordinary words
Define PlayerRole as a named-value type
Its named values are Keeper, Forward, and Defense
Define a Player whose Role field uses PlayerRole
Its SetRole operation accepts one value of that same enum type
Do not create one Player class or instance per enum member

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 a Player class box and another box representing PlayerRole.
  2. Mark the latter with the visible stereotype <<enumeration>> and list Keeper, Forward, and Defense as named values, one per line. These are not operations.
  3. Inside Player enter attribute +Role : PlayerRole and operation +SetRole(role : PlayerRole) : void.
  4. Draw a dashed type-use dependency from Player toward PlayerRole, labeled field and parameter type. Do not draw a field-reference association to an imagined enum object.
  5. Save and read the same enum type in the field, parameter, and enumeration heading. Do not substitute string or a particular member into the declaration.
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 uses PlayerRole for a field and a method parameter. PlayerRole lists Keeper, Forward, and Defense.
An enum defines the role value type
  • New
  • Changed
  • Removed
Read the Contract One Part at a Time

Role may currently contain Forward for Jordan, but that current choice does not belong as a fixed value in the class operation signature.

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