Composition

 <  Day Day Up  >  

Composition indicates that a has-a relationship is being used. When inheritance is not the proper design choice (because the is-a relationship is not appropriate), composition is normally used.

Chapter 9, "Building Objects," discusses two different types of composition: aggregations and associations. Composition is used when classes are built with other classes. This can happen with aggregation, when a class is actually a component of another class (as a tire is to a car). Or, it can happen with association, when a class needs the services of another class (for example, when a client needs the services of a server).

Aggregations

An aggregation is represented by a line with a diamond at the head. In the car example of Chapter 9, to represent that a steering wheel is part of a car, you use the notation shown in Figure 10.6.

Figure 10.6. UML diagram representing composition.

graphics/10fig06.gif

As with the inheritance tree, there is no limit (theoretically) to the number of levels of aggregation you can represent. In the example seen in Figure 10.7, there are four levels. Notice that the various levels can represent various aggregations. For example, although a stereo is part of the car, the radio is part of the stereo, and the tuner is part of the radio.

Figure 10.7. An expanded composition UML diagram.

graphics/10fig07.gif

Associations

Although aggregations represent parts of a whole, meaning that one class is logically built with parts of another, associations are simply services provided between classes.

As mentioned earlier, a client/server relationship fits this model. Although it is obvious that a client is not part of a server, and likewise a server is not part of a client, they both depend on each other. In most cases, you can say that a server provides the client a service. In UML notation, a plain line represents this service, with no shape on either end (see Figure 10.8).

Figure 10.8. UML diagram representing an association.

graphics/10fig08.gif

Note that because there is no shape on either end of the line, there is no indication about which way the service flows. The figure shows only that there is an association between the two classes.

To illustrate , consider the example of the computer system from Chapter 9. In this case, there are multiple components , such as a computer, monitor, scanner, keyboard, and mouse. Each is a totally separate component that interacts , to some degree, with the computer itself (see Figure 10.9).

Figure 10.9. An expanded UML diagram representing association.

graphics/10fig09.gif

The important thing to note here is that the monitor is technically part of the computer. If you were to create a class for a computer system, you could model it by using aggregation. However, the computer represents some form of aggregation, as it is made up of a motherboard, RAM, and so on (see Figure 10.10).

Figure 10.10. UML representation of aggregation.

graphics/10fig10.gif

 <  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