UML Class Relationship Diagrams

Steven Zeil

Last modified: Feb 19, 2014

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.

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.

Read this arrow as “is a specialization of”, “is a kind of”, or “is a”. (The latter can be a bit ambiguous however, as we might also say that Webster’s Dictionary “is a” Book, but that’s not a generalization relationship.

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

Some classes may participate in multiple generalization relationships. The arrows are grouped together to indicate related, mutually exclusive, divisions:

4. Associations

Diagramming Associations

Associations are shown as lines connecting classes.

Decorations

We make the associations meaningful by attaching various 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


In a specification or implementation perspective, roles often correspond to attribute names. These two representations are, in a sense, equivalent.

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

4.2 Multiplicity

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

Multiplicity is encoded as:

Diagramming Multiplicity

This diagram above indicates that

4.3 Navigability

Navigability arrows indicate whether, given one instance participating in a relationship, it is possible to determine the instances of the other class that are related to it.

The diagram above suggests that,

Navigability and Role Names

In perspectives and relationships where the role names are attribute names, we would not have a role name for an un-navigable direction of an association.

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”.

This diagram suggests that cells are part of a spreadsheet and that an expression and a value are each part of a cell.

Aggregation Details

Example: Should This Be Aggregation?

For example, this diagram tries to circumvent our rule that attributes are single-valued by having a “single” set as an attribute.

Example: Should This Be Aggregation? (2)

Example: Should This Be Aggregation? (3)

This one is no better in the earlier perspectives.

Example: Should This Be Aggregation? (4)

So, for conceptual or specification perspectives, I’d avoid aggregation in this case and fall back to a general association.

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

This diagram suggests that destroying a cell would also destroy its value and formula. That makes sense if the cell does not share those components with other objects.

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

This diagram shows a system that has been decomposed into three major subsystems.

MVC

The dependencies shown here are the defining characteristic of the MVC approach to user interface design:

MVC

The dependencies shown here are the defining characteristic of the MVC approach to user interface design:

Why MVC?

The advantage of this interface design is that we can radically change the user interface without altering the core computations (the model).

6. Other Class Diagram Elements

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

Parameterized Classes

Used to represent templates and similar concepts.

Constraints

Constraints can be added almost any place by writing them within brackets.

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.

For example, an interface is a collection of related operations.

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