Object-Oriented Analysis and Design 361
11
How are these models differentiated? As was stated, they are differentiated by the engine
size, body type, and performance characteristics. These various discriminating character-
istics can be mixed and matched to create various models. This can be modeled in the
UML with the discriminatorstereotype, as shown in Figure 11.18.
FIGURE11.18
Modeling the
discriminator.
Car
High Power Sedan Coupe Family Car
Low Power Sports Car
engine performance
body
The diagram in Figure 11.18 indicates that classes can be derived from Carbased on
mixing and matching three discriminating attributes. The size of the engine dictates how
powerful the car is, and the performance characteristics indicate how sporty the car is.
Thus, you can have a powerful and sporty station wagon, a low-power family sedan, and
so forth.
Each attribute can be implemented with a simple enumerator. Thus, in code, the body
type might be implemented with the following statement:
enum BodyType = { sedan, coupe, minivan, stationwagon };
It might turn out, however, that a simple value is insufficient to model a particular dis-
criminator. For example, the performance characteristic might be rather complex. In this
case, the discriminator can be modeled as a class, and the discrimination can be encapsu-
lated in an instance of that type.
Thus, the car might model the performance characteristics in a performancetype, which
contains information about where the engine shifts and how fast it can turn. The UML
stereotype for a class that encapsulates a discriminator, and that can be used to create
instancesof a class (Car) that are logically of different types (for example,SportsCar
versus LuxuryCar) is <<powertype>>. In this case, the Performanceclass is a powertype
for Car. When you instantiate Car, you also instantiate a Performanceobject, and you
associate a given Performanceobject with a given Car, as shown in Figure 11.19.