Logical View  «Prev  Next»
Lesson 8 Modeling Aggregation and Composition
Objective Define the Notation for specifying aggregation and composition associations.

Modeling and Specifying Aggregation, Composition Associations

Aggregation is a special type of association used to indicate that objects are assembled together to create a new object. For example, a number of different parts are assembled to create a car, a boat, or a plane.

Aggregation

Aggregation Composition Modeling

Modeling Aggregation and Composition

Many modelers overuse aggregation. Remember that the aggregation and composition notation do not generate any unique code. Use the notation to convey your intent to the implementers.

Composition:

As we know, inheritance gives us an 'is-a' relationship. To make the understanding of composition easier, we can say that composition gives us a 'part-of' relationship. Composition is shown on a UML diagram as a filled diamond (see figure below).

Composition indicates that Engine is part of a car.
Composition indicates that Engine is part of a car.

Aggregation:

If inheritance gives us 'is-a' and composition gives us 'part-of', we could argue that aggregation gives us a 'has-a' relationship. Within aggregation, the lifetime of the part is not managed by the whole. To make this clearer, we need an example. For the past 12 months I have been involved with the implementation of a CRM system, so I am going to use part of this as an example. The CRM system has a database of customers and a separate database that holds all addresses within a geographic area. Aggregation would make sense in this situation, as a
Customer 'has-a' Address.

It would nor make sense to say that an Address is 'part-of' the Customer, because it i not. Consider it this way, if the customer ceases to exist, does the address? I would argue that it does not cease to exist.
Aggregation is shown on a UML diagram as an unfilled diamond (see figure below).

Aggregation where the address will continue to exist even if the person dies.
Aggregation where the address will continue to exist even if the person dies.

To identify aggregation, use a diamond on the end of the association attached to the aggregate class and attach the other end of the association to the component class. Remember to assign the appropriate multiplicity to each end of the association. In most cases, the multiplicity at the aggregate end is one, so many people do not bother to set this value.

1 Table Of Contents for each book, 2) 1 or more Chapters per book, 3) O to 1 Glossaries per book
1) 1 Table Of Contents for each book, 2) 1 or more Chapters per book, 3) O to 1 Glossaries per book

Composition

The UML has a new form of association called composition. Composition is used for aggregations where the life span of the parts depends on the life span of the aggregate. To indicate composition, simply make the diamond black.
Composition is the act of collecting several objects together to create a new one. Composition is usually a good choice when one object is part of another object. We've already seen a first hint of composition in the mechanic example. A car is composed of an engine, transmission, starter, headlights, and windshield, among numerous other parts. The engine, in turn, is composed of pistons, a crank shaft, and valves. In this example, composition is a good way to provide levels of abstraction. The car object can provide the interface required by a driver, while also providing access to its component parts, which offers the deeper level of abstraction suitable for a mechanic. Those component parts can, of course, be further broken down if the mechanic needs more information to diagnose a problem or tune the engine. This is a common introductory example of composition, but it's not overly useful when it comes to designing computer systems. Physical objects are easy to break into component objects. People have been doing this at least since the ancient Greeks originally postulated that atoms were the smallest units of matter (they, of course, didn't have access to particle accelerators). Computer systems are generally less complicated than physical objects, yet identifying the component objects in such systems does not happen as naturally.

Aggregation and composition
1) Aggregation and 2) composition: With composition, if the book is destroyed, the chapters no longer exist.
In the graphic above, the team example uses aggregation. A team is composed of players. But if the team is disbanded, the players live on. The book example uses composition because a book is composed of chapters. If the book is destroyed, the chapters are destroyed as well. In the next lesson, you will learn about composition context.

Model Aggregation Composition Associations - Exercise

Click the Exercise link below to model aggregation and composition associations.
Model Aggregation Composition Associations - Exercise

UML Aggregation Composition - Quiz

Now click the Quiz link below to take a short multiple-choice quiz on aggregation and composition.
UML Aggregation Composition - Quiz