2.5.2 Representing Private Fields and Methods in Class Diagrams

Representing Private Fields and Methods in Class Diagrams

A class diagram shows which Player members callers can use and which belong to its implementation. Read the design before writing the equivalent C#.

Read visibility one member at a time
Visibility symbols
Symbol Meaning
+ Public: callable or accessible by other code that can use the class
- Private: accessible within the declaring class

The space after each sign is part of our formatting convention. Visibility belongs to each member; a public class can contain private fields and private methods.

Model a public request and an internal helper

In this notation example, Player stores a turn count and lets callers record a turn. Its helper increases the count. This small model does not yet enforce a three-turn limit.

  1. Create a Violet class diagram with one Player class.
  2. Enter - numberOfTurns : int in the attributes compartment.
  3. Enter + RecordTurn() : void and - IncreaseTurnCount() : void in the operations compartment.
  4. Keep the helper private even though a public operation uses it. Do not underline these instance members.
Player has private numberOfTurns, public RecordTurn, and private IncreaseTurnCount.
Private storage and a public action
Apply the idea
Check your understanding