2.6.4 Representing Constructor Parameters in Class Diagrams

Representing Constructor Parameters in Class Diagrams

The class diagram states what information every creation call must supply. Constructor parameter names are not the same thing as stored field names.

Read the Code Before Drawing

This complete Player class assigns two constructor inputs to its 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
Player creation requires two inputs
The first input is text named name
The second is an integer named jerseyNumber
The constructor copies them into Name and JerseyNumber fields
Describe the required input order and types

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 Player class box in a Violet class diagram.
  2. Enter +Name : string and +JerseyNumber : int in the attributes compartment.
  3. In the operation compartment enter +Player(name : string, jerseyNumber : int). Put the text parameter first and integer parameter second.
  4. Check the names inside the parentheses against the formal declarations, not the stored-field names on the assignment's left side.
  5. Save without current names or jersey values in the signature. The same constructor supports different creation arguments.
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.

Constructor inputs name and jerseyNumber are shown in the creation signature; stored Name and JerseyNumber are fields.
Inputs and storage occupy different compartments
  • New
  • Changed
  • Removed
Read the Contract One Part at a Time

A call with "Casey", 1 maps Casey to name and 1 to jerseyNumber. Changing those arguments does not change the constructor signature.

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