1.2.5 Class Diagrams

A UML Class Diagram Models Types

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

An object diagram answers:

Which specific objects exist right now?

A class diagram answers:

Which kinds of objects can exist, and what structure do they have?

For a soccer system, a class diagram might contain:

Plain text
Player
Team
Match

These are types, not current instances.

A Class Box Begins with the Class Name

A simple class can be represented as:

Plain text
+----------------------+
| Player               |
+----------------------+
| name                 |
| jerseyNumber         |
| isAvailable          |
+----------------------+

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:

Plain text
jerseyNumber

An object diagram might show:

Plain text
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:

Plain text
Player

and:

Plain text
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:

The diagram is a design representation.

The C# class definition is the implementation representation.

Keep the Diagram Focused

Do not add:

Model only the structure required by the current purpose.

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