1.2.2 Class Diagrams

Video: Class Diagrams (15:19)
A UML Class Diagram Models Types

A UML class diagram describes the structural design of software types.

An object diagram answers:

A class diagram answers:

For a soccer system, a class diagram might contain:

Class diagram showing Player, then name, then jerseyNumber, then isAvailable.
Player.
  • New
  • Changed
  • Removed

The top compartment identifies the class.

The next compartment can list fields/attributes.

Methods are added later in PC1.

Class Diagrams Do Not Show Current Object Values

A class diagram might show:

jerseyNumber

An object diagram might show:

jerseyNumber = 7

The class diagram describes structure shared by Player objects.

The object diagram shows the current state of one specific Player instance.

Relationships Can Connect Classes

If Player objects can refer to Team objects, the class model can show a structural relationship between:

Player

and:

Team

At this stage, focus on recognizing that the two types are related.

More detailed relationship notation is not needed to learn the basic class model.

Class Diagrams Support Code Design

A class diagram can guide C# questions such as:

  • Which class files do we need?
  • Which fields belong to each class?
  • What types should those fields use?
  • Which classes need relationships?

The diagram is a design representation.

The C# class definition is the implementation representation.

Keep the Diagram Focused

Do not add:

  • methods not yet taught;
  • properties;
  • constructors;
  • inheritance;
  • unrelated data.

Model only the structure required by the current purpose.

A simple accurate class diagram is more useful than a complicated diagram containing future concepts.