Composition

 <  Day Day Up  >  

It is natural to think of objects as containing other objects. A television set contains a tuner and video display. A computer contains video cards, keyboards, and floppy drives . The computer can be considered an object unto itself, and the floppy drive is also considered a valid object. You could open up the computer and remove the floppy drive and hold it in your hand. In fact, you could take the floppy drive to another computer and install it; because it works in multiple computers, the fact that it is a standalone object is reinforced.

The classic example of object composition is the automobile. Many books, training classes, and articles seem to use the automobile as the essence of object composition. Besides the original interchangeable manufacture of the rifle, most people think of the automobile assembly line created by Henry Ford as the quintessential example of interchangeable parts . Thus, it seems natural that the automobile has become a primary reference point for designing OO software systems.

For example, most people would think it natural for a car to contain an engine. In fact, a car contains many objects besides an engine, including wheels, a steering wheel, and a stereo. Whenever a particular object is composed of other objects, and those objects are included as object fields, the new object is known as a compound , an aggregate , or a composite object (see Figure 7.6).

Figure 7.6. An example of composition.

graphics/07fig06.gif

Aggregation, Association, and Composition

From my perspective, there are really only two ways to reuse classes ”with inheritance or composition. In Chapter 9, "Building Objects," we will discuss composition in more detail; specifically , aggregation and association. In this book, I consider aggregation and association to be types of composition.


Representing Composition with UML

To represent the fact that the car object contains a steering wheel object, UML uses the notation shown in Figure 7.7.

Figure 7.7. Representing composition in UML.

graphics/07fig07.gif

Aggregation, Association, and UML

In this book, aggregations are represented in UML by lines with a diamond, such as an engine as part of a car. Associations are represented by just the line (no diamond), such as a standalone keyboard servicing a separate computer box.


Note that the line connecting the Car class to the SteeringWheel class has a diamond shape on the Car side of the line. This signifies that a Car contains (has-a) SteeringWheel .

Let's expand this example. Let's say that none of the objects used in this design use inheritance in any way. All the object relationships are strictly composition, and there are multiple levels of composition. Of course, this is a simplistic example, and there are many, many more object and object relationships in designing a car. However, this design is simply meant to be a simple illustration of what composition is all about.

Let's say that a car is composed of an engine, a stereo system, and a door.

How Many Doors and Stereos?

Note that a car normally has more than one door. Some have two, and some have four. You might even consider a hatchback a fifth door. In the same vein, it is not necessarily true that all cars have a stereo system. A car could have no stereo system or it could have one. I have even seen a car with two separate stereo systems. These situations are discussed in detail in Chapter 9. For the sake of this example, just pretend that a car has only a single door (perhaps a special racing car) and a single stereo system.


The fact that a car is made up of an engine, a stereo system, and a door is easy to understand because most people think of cars in this way. However, it is important to keep in mind when designing software systems, just like automobiles, that objects are made up of other objects. In fact, the number of nodes and branches that can be included in this tree structure of classes is virtually unlimited.

Figure 7.8 shows the object model for the car, with the engine, stereo system, and door included.

Figure 7.8. The Car class hierarchy.

graphics/07fig08.gif

Note that all three objects that make up a car are themselves composed of other objects. The engine contains pistons and spark plugs. The stereo contains a radio and a cassette. The door contains a handle. Also note that there is yet another level. The radio contains a tuner. We could have also added the fact that a handle contains a lock; the cassette contains a fast forward button, and so on. Additionally, we could have gone one level beyond the tuner and created an object for a dial. The level and complexity of the object model is, obviously, up to the designer.

Model Complexity

As with the inheritance problem of the barking and yodeling dogs, using too much composition can also lead to more complexity. There is a fine line between creating an object model that contains enough granularity to be sufficiently expressive, and a model that is so granular that it is difficult to understand and maintain.


 <  Day Day Up  >  


Object-Oriented Thought Process
Object-Oriented Thought Process, The (3rd Edition)
ISBN: 0672330164
EAN: 2147483647
Year: 2003
Pages: 164
Authors: Matt Weisfeld

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net