Section 18.10 Introducing Aggregation
Pointers enable a new form of relationship between objects known as aggregation. Both composition and aggregation are βhas-aβ relations. But they differ in terms of whether or not the containing object owns the related object.
Composition implies βexclusive ownershipβ. The contained object is a part of the one that is composed from it. If two Circles made with composition, each have a center Point at (2, 1), that would mean they both have their own Point object with those coordinates.
Aggregation implies that the contained object exists independently of any object(s) that aggregate it. If Circle is designed to have a center Point via aggregation, two Circles could share the exact same Point as their center. And that Point could persist even if the Circles went away.
Some implications of this βrelated by not ownedβ nature of aggregation:
-
One object can be related via aggregation to multiple other objects. One Person can be the friend of multiple other Persons.
-
The lifespan of the related object is not tied to the object that contains it. If a Person disappears, we donβt expect that means their friend will as well.
-
The contained object may be optional. A Person object might have a spouse that is another Person, but not all Persons will.
None of those things are true for composition. In composition, the contained object is owned by the container and its lifespan is tied to the container.
Sometimes, it is clear that exclusive ownership does not apply and we should think in terms of aggregation instead of composition. If we are representing friendship between Persons, and Anna is a friend of Nancy, does that mean that Jeff canβt also be a friend of Nancy? No, we donβt normally think of friendship that way.
Other times, we have to consider more deeply how the program makes of the objects. We might represent the relationship between a Point and a Circle as either composition or aggregation.
-
In a drawing program, we might use a Point to store information about the center of a Circle. But the Circle βownsβ the point. If a Circle goes away, so does its center Point. And two different Circleβs canβt share the same Point object as their center. (They might have the same coordinates, but they would each own a point with those coordinates.) That would be composition.
-
In a geometry program, we might want to define multiple Circles that all are centered on the same Point. None of those Circles own the Point and the Point should persist even if we delete all the Circles. That would be aggregation.
In UML, we represent aggregation with a line that ends in an open diamond on the side that is the aggregator (the one that contains the other). This looks almost the same as composition other than the diamond being open instead of filled.
classDiagram class Circle{ -m_radius : double -m_center : Point#42; +getRadius() double ...otherFunctions() } class Point{ -m_x : double -m_y : double +getX() double +getY() double ...otherFunctions() } Circle o-- Point
classDiagram class Person{ -m_spouse : Person#42; ...otherFunctions() } Person o-- Person
Checkpoint 18.10.2.
Identify the following as either aggregation or composition: We have a class that represents a Person and a class that represents Address. Each Person has an Address. The address is a separate class. Two people might live at the same location, but each would have its own Address record. When a Person goes away, so does the associated Address.
- Composition
- Aggregation
Checkpoint 18.10.3.
Identify the following as either aggregation or composition: Two Players each have access to a Deck of cards. Both players have access to the same exact Deck. If there are 10 cards in the Deck and one Player draws a card, there are only 9 cards left for both Players.
- Composition
- Aggregation
You have attempted of activities on this page.