1.2.2 Representing Fields in Class Diagrams

Representing Fields in Class Diagrams

A field declaration in C# and a field entry in a class diagram describe the same storage contract using different word order.

Read the Code Before Drawing

This is a complete class definition. Team is an existing custom class; its public fields are not needed for this excerpt.

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

Source to translate
public class Player
{
    public string name;
    public int jerseyNumber;
    public bool isAvailable;
    public Team team;
}
Rewrite the Meaning in Ordinary Words
The same design in ordinary words
Define a Player with a text name, integer jersey, and Boolean availability
Give Player a field that can refer to a Team
All four fields are public
Describe the reference type, without creating a Team instance

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 Player and Team. Leave space between them for the field-role label.
  2. In Player's attributes enter +name : string, +jerseyNumber : int, and +isAvailable : bool. The plus means public.
  3. Draw a directed association from Player toward Team. Label the line +team outside the boxes and put 0..1 beside the Team endpoint.
  4. Leave Team's member compartments empty in this focused view. Do not duplicate team as an additional attribute or create a particular team instance.
  5. Save and read the line from its owner: each Player can hold no Team reference yet, or a reference to one Team.
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 has three public simple fields and a public team reference that can be unassigned or point to one Team.
Fields and a custom-type reference
  • New
  • Changed
  • Removed
Read the Contract One Part at a Time

Read team from Player toward Team. The field declaration does not create a Team instance or prove the field is non-null at a particular pause.

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