When objects get together, sometimes they behave differently in different relationships. You could say they have multiple personalities. We use the
For example, consider one of your authors as if he were an object (he won’t mind): He plays the role of Husband in the relationship with his wife—and in quite a different relationship to his job, he plays the role of Chief Technologist . (Sometimes he plays the role of Crash Dummy .) He plays many roles, depending on who or what he’s associating with. The same is true of objects in UML.
When adding roles to your association, consider the following:
A name is shown on the association line
You use roles to help clarify the nature of the behavior that an instance exhibits when it’s linked to an instance of another class.
UML2
In versions of UML previous to UML 2, the name at the end of an association was called a
role
. In UML 2, the word
role
has disappeared,
The
Figure 4-8:
Class diagram with roles.
You can think of a role as the name of an attribute
Tip
Association names are important to the readers of your diagrams. Role names are important to the code generators of your UML modeling tools. We recommend you provide role
Figure 4-9:
The Plane class with role-name attributes.
Tip Diagrams for modeling objects and classes come in two flavors—class diagrams and instance diagrams. The class diagram shows the static structure of classes and their associations. The object diagram shows objects (that is, instances) and their links. You use class diagrams most of the time—but now and then an instance diagram helps clarify a class diagram by providing an example. You build class diagrams to communicate the structure and behavior of each class. To show which classes can interact, you associate them together. When other developers have trouble understanding the meaning of your class diagram, use a instance diagram to show specific objects linked together. The instance diagram illustrates your class diagram.
When we put together the class diagram in Figure 4-8, for example, some developers didn’t understand what we
Figure 4-8 also shows a
Plane
class associated with (
moves through
) a
Cell
class. Here the
Cell
class plays a different role, that of
routeElement
. Again,
Figure 4-10 illustrates the meaning of the association (
moves through
) by showing the
p12
instance linked to three instances of the
Cell
class—
c45-20, c45-21, c45-22
. Each of these three
Cell
instances plays the role of
routeElement
, showing which other