0.4.9 UML Class Diagrams

Video: Class Diagrams vs. Object Diagrams (9:40)
Class Diagrams: The Structure Objects Follow

Before the Wildcats coach chooses today's lineup, the team needs a way to describe players and lineup roles. Every player needs a name, jersey number, and availability. A lineup needs a keeper, a forward, and a defender. These are design decisions about what information and connections a system supports.

A class diagram describes structure: kinds of objects, their attributes and operations when relevant, and relationships between those kinds. It can describe a design before any objects exist. Unlike an object snapshot, it does not answer which player is selected right now.

Model Three Named Player Fields

For this deliberately small training lineup, each Team must have exactly one player assigned to each of three roles. The role fields are named keeper, forward, and defense. A role describes how a Team refers to a Player; it is not a separate kind of player. This is a simplified teaching lineup, not a regulation soccer team.

Team has keeper, forward, and defense roles, each requiring one Player. The three repeated Player boxes are explicitly an expanded teaching view of one type.
An expanded view of three named fields with multiplicity
  • New
  • Changed
  • Removed

Read each line from Team toward Player: the keeper field refers to exactly one Player, and so do forward and defense. The 1 beside each Player endpoint is its multiplicity: how many Player values that role requires for one Team in this design. A missing assignment would violate this model's rule.

The expanded picture repeats Player so each role is easy to inspect. It does not define three different Player classes. Repeating the same attribute list makes the common structure visible, but is cumbersome as a design grows.

Consolidate the Same Type into One Box
One Team class has three separately named associations to one Player class. Each role has multiplicity one; the diagram defines a type, not a single runtime player.
One Player class, three distinct named roles
  • New
  • Changed
  • Removed

This is the compact class view of the same design. One Player class box describes all players. The three separate lines retain the three named fields; they must not collapse into an unlabeled connection that loses the roles.

A single Player class does not mean a single Player object. The model can have Casey as keeper, Jordan as forward, and Alex as defense. Also, multiplicity 1 on each role does not itself require three different objects. If the application requires different players in all roles, add that rule explicitly; the three lines alone do not enforce distinct identity.

Contrast the Design with One Lineup Snapshot
A Wildcats Team instance refers to Casey as keeper, Jordan as forward, and Alex as defense. Three Player instances follow the one Player class.
One possible current lineup
  • New
  • Changed
  • Removed

The class diagram says jerseyNumber : int because the design supports an integer jersey value. The object diagram says jerseyNumber = 7 for Jordan because that is the current value. A substitution changes a Team reference in a snapshot; it does not require another Player class definition.

Separate Roles, Types, and Objects