186
Chapter 9 Building Objects
Similarly, in the stereo example, the relationship between the receiver, the speakers, and
the CD player is association; however, each of these components are complex objects that
are made up of other objects.
In the car example, although the engine, sparkplugs, and doors represent composition,
the stereo also represents an association relationship.
No One Right Answer
As usual, there isn’t a single absolutely correct answer when it comes to making a design
decision. Design is not an exact science. Although we can make general rules to live by,
these rules are not hard and fast.
Avoiding Dependencies
When using composition, it is desirable to avoid making objects highly dependent on one
another. One way to make objects very dependent on each other is to mix domains. In
the best of all worlds, an object in one domain should not be mixed with an object in an-
other domain, except under certain circumstances.We can return again to the stereo ex-
ample to explain this concept.
By keeping the receiver and the CD player in separate domains, the stereo system is
easier to maintain. For example, if the CD component breaks, you can send the CD player
off to be repaired individually. In this case, the CD player and the MP3 player have sepa-
rate domains.This provides flexibility such as buying the CD player and the MP3 player
from separate manufacturers. So, if you decide you want to swap out the CD player with a
brand from another manufacturer, you can.
Sometimes there is a certain convenience in mixing domains.A good example of this
pertains to the existence of TV/VCR combinations. Granted, it is convenient to have
both in the same module. However, if the TV breaks, the VCR is unusable—at least as part
of the unit it was purchased in.
You need to determine what is more important in specific situations: whether you
want convenience or stability.There is no right answer. It all depends on the application
and the environment. In the case of the TV/VCR combination, we decided that the
convenience of the integrated unit far outweighed the risk of lower unit stability
(see Figure 9.6).
Mixing Domains
The convenience of mixing domains is a design decision. If the power of having a TV/VCR
combination outweighs the risk and potential downtime of the individual components, the
mixing of domains may well be the preferred design choice.
Cardinality
Gilbert and McCarty describe cardinality as the number of objects that participate in an
association and whether the participation is optional or mandatory.To determine cardinal-
ity, ask the following questions: