UML Class Relationship Diagrams

Steven Zeil

Last modified: Sep 20, 2018
Contents:

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 relationship diagrams.


Diagramming in Context

The diagrams are part of an overall documentation strategy.

The purpose of that strategy is communication.

1 Class Relationship Diagrams

A class relationship 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

 

We’ve already seen the three-part form of this.

But in the course of writing a full set of documentation, a class might appear multiple times in different diagrams. It could be distracting to list out all of tha attributes and operations every time it appears.

Instead, you can use the simpler name-only form when discussing a class whose attribute and operation details are given elsewhere, particularly if the attributes and operations of the class were not relevant to the idea you wanted that diagram to communicate.


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 or type is required. The others may be added when relevant:

 

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

 

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

 
This diagram above indicates that

 
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

There’s a relationship between 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

8 The Documentation Narrative

Remember, we don’t draw diagrams just for the sake of drawing diagrams. Diagrams should contribute to an overall narrative describing our model. (An example will be shown in the next set of notes.)

As we construct this narrative, we should merge the attribute and operation information from our isolated class diagrams into the diagrams so that no information is lost.