Class Relationships
After the classes are in the UML, you can begin to turn your attention to the relation-
ships among the various classes. The principal relationships you’ll model are the
following:
- Generalization
- Association
- Aggregation
- Composition
The generalization relationship is implemented in C++ through public inheritance. From
a design perspective, however, you focus less on the mechanism and more on the seman-
tics: what it is that this relationship implies.
You examined the generalization relationship in the analysis phase, but now turn your
attention to the objects in your design rather than to just the objects in the domain. Your
efforts should now be to “factor out” common functionality in related classes into base
classes that can encapsulate the shared responsibilities.
When you “factor out” common functionality, you move that functionality out of the spe-
cialized classes and up into the more general class. Thus, if you notice that both your
checking and your savings account need methods for transferring money in and out,
you’ll move the TransferFunds()method up into the account base class. The more you
factor out of the derived classes, the more polymorphic your design will be.
One of the capabilities available in C++, which is not available in Java, is multiple inher-
itance(although Java has a similar, if limited, capability with its multiple interfaces).
Multiple inheritance allows a class to inherit from more than one base class, bringing in
the members and methods of two or more classes.
Experience has shown that you should use multiple inheritance judiciously because it can
complicate both your design and the implementation. Many problems initially solved
with multiple inheritance are today solved using aggregation. That said, multiple inheri-
tance is a powerful tool, and your design might require that a single class specializes the
behavior of two or more other classes.
Multiple Inheritance Versus Containment Is an object the sum of its parts? Does
it make sense to model a Carobject as a specialization of SteeringWheel,Door, and
Tire, as shown in Figure 11.14?
358 Day 11