2.6.5 Representing Constructors in Class Diagrams

Representing Constructors in Class Diagrams

A constructor describes how a new Player begins life with required state. It belongs in the class's creation contract.

Read the Code Before Drawing

This complete Player definition requires a name and jersey number at creation. The constructor body assigns them to fields.

Source to translate
public class Player
{
    public string Name;
    public int JerseyNumber;

    public Player(string name, int jerseyNumber)
    {
        Name = name;
        JerseyNumber = jerseyNumber;
    }
}
Rewrite the Meaning in Ordinary Words
The same design in ordinary words
Define Player with stored Name and JerseyNumber fields
Require a name and jersey number when a Player is created
Use those inputs to initialize the fields
Describe the constructor contract, not one creation event

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 with one Player class node.
  2. In attributes enter +Name : string and +JerseyNumber : int.
  3. In operations enter +Player(name : string, jerseyNumber : int). Use the same class name and parameter order as the source.
  4. Do not add : void, a return statement, or an unprovided Player() overload. The declared constructor is the creation contract shown here.
  5. Save and test the signature mentally against new Player("Jordan", 7). Both actual inputs have a matching formal parameter.
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 public Name and JerseyNumber fields and a two-parameter constructor with no C# return type.
A constructor initializes a player
  • New
  • Changed
  • Removed
Read the Contract One Part at a Time

new Player("Jordan", 7) calls this constructor to initialize a new instance. It does not mean the class diagram itself contains an instance named Jordan.

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