2.7.2 Representing Properties in Class Diagrams

Representing Properties in Class Diagrams

Model the public access contract separately from the private field it uses. This course labels C# properties explicitly so readers can distinguish them from ordinary fields.

Show accessors in the property entry
Course notation
Entry Meaning
- jerseyNumber : int Private backing field
+ JerseyNumber : int <<property>> {get; set;} Public property with public read and write access

The annotation is a course mapping convention. Violet accepts it as diagram text. The two entries do not represent two independent stored jersey numbers: the accessors use the backing field.

Build the property contract in Violet
  1. Create a Player class box.
  2. Add the private backing-field entry above in the attributes compartment.
  3. Add the public property entry on another attribute line.
  4. Add a note explaining that <<property>> identifies a C# property and {get; set;} lists its accessors.
  5. Keep a space after each visibility sign. Do not invent methods named GetJerseyNumber or SetJerseyNumber when the source defines a property.
Player has a private jerseyNumber backing field and a public JerseyNumber property with get and set accessors, both using the same storage.
One stored value with a property interface
Read restricted access accurately

For a getter-only property use {get;}. For a public getter with a private setter, our annotation is {get; private set;}. A missing setter and a private setter are different contracts.

Apply the idea
Check your understanding