11
With the containment presentation, there is an issue as to how to show the multiplicity of the part (by
definition, the multiplicity on the whole end of a composition is exactly "1"). Since there is no line on
which to place the multiplicity, it is common to put the multiplicity in one of the upper corners of the part
class. This is called instance multiplicity. We see that the Power Subsystem contains either one or two
objects of type Switch, zero or more objects of type Charger, and zero to two objects of type Battery.
There is also the issue of how to show the role names. The common way is to use a class role name. A
class role name precedes the class name and a slash (/) separator. In the figure, instances of class Switch
have a class role name of PowerSwitch. As an aside, we can also show object names if we like,
independently from the class role names. An object name is shown as preceding the class name, with a
colon (:) separator. Thus,
PowerSwitch/ thePowerSwitch: Switch
shows a role called PowerSwitch that is played by an object named thePowerSwitch, which happens to be
an instance of class Switch.
The most common implementation of an association, as seen in the previous code examples, is an object
pointer (in C++) or an object reference (in Java). This is true regardless of which kind of association it is,
whether it is an ordinary association, an aggregation, or a composition. There are many other ways of
implementing an association—including nested class declaration, object identifier reference (as in a MS
Windows handle or a CORBA object ID), an operating system task ID, and so on—but using a pointer is
the most common.
A Word About Stereotypes Figure 1-2
has a couple of places where a class has a special adornment called a
stereotype. A stereotype is a way of tailoring the UML to meet a specific need or purpose. It is part of the
lightweight extension mechanism defined within the UML. A stereotype is a user-defined kind of element
that is based on some already defined element in the UML, such as Class, Operation, Association, and so
on. Stereotypes are usually shown by attaching the stereotype name in guillemets with the stereotyped
element or shown using a user-defined icon. In the example figure, a class box is used for a large-scale
element called a Subsystem. To indicate that this is that special kind of element, we attach the stereotype
«subsystem» to the class box. Subsystems are discussed later in this chapter.
1.3.2.4 Generalization
The generalization relation in the UML means that one class defines a set of features that is either
specialized or extended in another. Generalization may be thought of as "is a type of" relation and
therefore only has a design-time impact rather than a run-time impact.
Generalization has many uses in class models. First, generalization is used as a means to ensure interface
compliance, much in the same way that interfaces are used. Indeed, it is the most common way to
implement interfaces in languages that do not have interfaces as a native concept, such as in C++. Also,
generalization can simplify your class models because a set of features common to a number of classes can
be abstracted together into a single superclass, rather than having to redefine the same structure
independently in many different classes. In addition, generalization allows for different realizations to be
used interchangeably. For example, one realization subclass might optimize worst-case performance, while
another optimizes memory size, while yet another optimizes reliability because of internal redundancy.
Generalization in the UML means two things. First, it means inheritance—that subclasses have (at least)
the same attributes, operations, methods, and relations as the superclasses they specialize. Of course, if the
subclasses were identical with their superclasses, that would be boring, so subclasses can differ from their
superclasses in either or both of two ways: specialization or extension.
Subclasses can specialize operations or state machines of their superclasses. Specializing means that the
same operation (or action list on the statechart) is implemented differently than in the superclass. This is
commonly called polymorphism. In order to make this work, when a class has an association with another