2.4.3 Representing Lists in a Class Diagrams

Video: Lists on Object and Class Diagrams (6:31)
Representing Lists in Class Diagrams

A Team class can define a list-reference field whose items are Players. Its structure does not contain one named field for each current roster position.

Read the Code Before Drawing

This class definition uses System.Collections.Generic. Player is an existing class. The field initializer creates an empty list for each new Team instance.

Source to translate
public class Team
{
    public List<Player> Players = new List<Player>();
}
Rewrite the Meaning in Ordinary Words
The same design in ordinary words
Define a Team type
Give it a public Players field of type List of Player
Initialize that field with a new empty list for each Team
The declaration supports a changing number of current players

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 Team and Player.
  2. Inside Team's attributes enter +Players : List<Player>. Keep the angle brackets and Player element type.
  3. Leave Player's members empty in this focused view because their definitions are not part of this fragment.
  4. Draw a dashed dependency from Team toward Player and label it list element type. Do not add another Players association that would appear to describe separate storage.
  5. Save and read the field without current Count, indexes, or player identities. A zero-player and a twelve-player Team can follow the same 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.

Team has a public Players field of type List of Player. A dashed dependency to Player identifies the list element type.
The list field declares its element type
  • New
  • Changed
  • Removed
Read the Contract One Part at a Time

A Team with zero players and one with twelve players both follow this structure. Calling Players.Add changes a list instance, not the field declaration.

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