UML Class Relationship Diagrams

Steven Zeil

Last modified: Feb 19, 2014

Contents:
1. Class Diagrams
2. A Class, in Isolation
2.1 Attributes
2.2 Operations
3. Generalization Relationships
4. Associations
4.1 Naming Your Relationships
4.2 Multiplicity
4.3 Navigability
5. Specialized Associations
5.1 Aggregation
5.2 Composition
5.3 Qualification
5.4 Dependency
6. Other Class Diagram Elements
7. Drawing UML Class Diagrams

Next we turn to more formal documentation that you can expect to show outside your team and probably to retain for the lifetime of the project.

We will employ UML notation for this.


UML Diagrams

UML provides a number of different diagrams

We’ll look eventually at the ones shown in italics, starting with class diagrams.


Diagramming in Context

The diagrams are part of an overall documentation strategy

1. Class Diagrams

A class diagram describes the types of objects in the system and selected static relationships among them.

The relationships can be


Perspectives

A diagram can be interpreted from various perspectives:

The perspective affects the amount of detail to be supplied and the kinds of relationships worth presenting.

The choice of perspective depends on how far along you are in the development process. During the formulation of a domain model, for example, you would seldom move past the conceptual perspective. Analysis models will typically feature a mix of conceptual and specification perspectives. Design model development will typically start with heavy emphasis on the specification perspective, and evolve into the implementation perspective.

The choice of perspective also depends on the context of the document in which it appears. We don’t create diagrams just for the sake of creating diagrams. We create them to communicate with someone. Diagrams are a part of an overall documentation process. If you think about the diagrams that appear, for example, in one of your textbooks, you don’t see page after page of diagrams without supporting text. Instead, the diagrams are presented along with explanatory text, as a way of clarifying and emphasizing points being made in the overall narrative flow of the document. Good diagrams can reduce the amount of wordy and often technically stilted text that would otherwise be required.

The same is true when documenting a model. If you are trying to explain a concept in your model, an accompanying diagram should be at the conceptual level. If you are trying to explain the details of how objects interact, the specification perspective may be more appropriate. if you are trying to explain an unexpected feature of how you have implemented or plan to implement a feature, you are probably going to portray things from an implementation perspective. Even late in the development of the design model, you may find it necessary to switch back and forth from one perspective to another in order to be an effective communicator.

2. A Class, in Isolation

A class can be diagrammed as

You would use the simpler form in a conceptual perspective, or if the attributes and operations of the class were not relevant to the point of your diagram in specification or implementation perspectives.


Even an Empty Section has Meaning

Note that these are not equivalent:

2.1 Attributes

Attributes are given as

visibility name: type = defaultValue

Only the name is required. The others may be added when relevant:

When you are only talking about concepts or interface specifications, the idea of a “private” anything is irrelevant.

Attributes are usually single values.

2.2 Operations

Operations are given as

visibility name (parameterlist) : return-type

Again, the amount of detail depends on the perspective (and on how much has actually been decided)

3. Generalization Relationships

A class (the “subtype”) is considered to be a specialized form of another class (the “supertype”) or, alternatively, the supertype is a generalization of the subtype if


Diagramming Generalization

The UML symbol for this relationship is an arrow with an unfilled, triangular head.

We infer from the relationship that books and magazines have titles and dates of publication and that we can get their content. However, not all publications have ISBNs.


Multiple Specializations

4. Associations


Diagramming Associations


Decorations

4.1 Naming Your Relationships


Relationship Names

Most important of these are the decorations that name the relationship being shown.


Roles


Roles and Attributes



Roles versus Attributes

When should you use attributes within a class and when should you use associations with role names?


Unnamed Associations are Useless

As a rule, I would argue that every association should have either a name or roles


Unnamed Associations are Useless (cont.)

Not everyone agrees.

would default to

Neither of these is an attractive choice, but, in practice, there are generally better options.

4.2 Multiplicity

Multiplicity indicates how many instances of a class participate in the relationship.

Multiplicity is encoded as:


Diagramming Multiplicity

4.3 Navigability

Navigability

The diagram above suggests that,


Navigability and Role Names

There’s a relationship between navigability and role names.

Hence, we have no role name naming the cell related to a value or expression.


Navigability and Perspectives

5. Specialized Associations

Certain kinds of associations occur so frequently that they are given special symbols that replace names and (often) role labels.

5.1 Aggregation

Denoted by an arrowhead drawn as an unfilled diamond, aggregation can be read as “is part of” or, in the opposite direction as “has a”.


Aggregation Details


Example: Should This Be Aggregation?


Example: Should This Be Aggregation? (2)


Example: Should This Be Aggregation? (3)


Example: Should This Be Aggregation? (4)

5.2 Composition

Composition is stronger form of aggregation. It implies that the “lifetime” of the parts is bound to the lifetime of the whole.


Composition Example

However, destroying a spreadsheet does not necessarily destroy its cells.

5.3 Qualification

A qualified association describes a situation in which one class is related to multiple instances of another, but the collection of related instances is “indexed” by a third class.

5.4 Dependency

A class A depends on another class B if a change to the interface of B might require alteration of A.


Dependency Example


MVC


MVC


Why MVC?

6. Other Class Diagram Elements

We won’t use these as often, but you should be able to recognize them.


Parameterized Classes


Constraints


Stereotypes

Stereotypes are an extension mechanism built in to UML. Written within <<>>, these are labels used to indicate that you are deviating slightly from the standard interpretation of a UML construct.

interface

Satisfies / Realizes

Another specialized association, this is actually a combination of dependency with the arrowhead of generalization.

7. Drawing UML Class Diagrams

In theory, you can use almost any drawing tool.

On our systems, we have


Data Entry Rather than Direct Formatting


Exporting the Final Product