2.2.3 Representing Array Fields in Class Diagrams

Representing Array Fields in Class Diagrams

A Team can store a reference to an array of training scores. Its class diagram describes the field type, not today's scores.

Read the Code Before Drawing

This complete class fragment defines the field but does not allocate an array. It follows the introductory field style; reference-nullability warnings depend on project settings.

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

Source to translate
public class Team
{
    public int[] RoundGoals;
}
Rewrite the Meaning in Ordinary Words
The same design in ordinary words
Define a Team type
Give every Team a public field named RoundGoals
That field can refer to an integer array
The declaration does not create an array or specify current scores

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 in Violet and place one Team class node.
  2. In its attributes compartment enter +RoundGoals : int[]. Put the public marker, name, colon, and complete array type on one line.
  3. Leave the operation compartment empty. This source declares no method and no constructor to represent.
  4. Do not add [0], current scores, or Length to this class box. Those describe a particular array snapshot rather than the declared field.
  5. Save and compare int[] with the C# declaration. Removing [] would describe different storage.
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 one public RoundGoals field of type integer array; no array length or contents are specified.
An array field belongs in the class contract
  • New
  • Changed
  • Removed
Read the Contract One Part at a Time

Two Team objects can store references to arrays with different lengths and values while both follow this declaration. A new Team's field is initially null until assigned in this simple example.

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