Composition

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 and Object-Oriented Design,” discusses two 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 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.

Image

Figure 10.6. UML diagram representing composition.

As with the inheritance tree, there is no limit (theoretically) to the number of levels of aggregation you can represent. In the example shown 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.

Image

Figure 10.7. An expanded composition UML diagram.

Associations

Although aggregations represent parts of a whole, meaning that one class is logically built with parts of another, associations are 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).

Image

Figure 10.8. UML diagram representing an association.

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 an association exists 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).

Image

Figure 10.9. An expanded UML diagram representing association.

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, because it is made up of a motherboard, RAM, and so on (see Figure 10.10).

Image

Figure 10.10. UML representation of aggregation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.180.223