
Aggregate Relations
I’m tempted to say that aggregation is a stronger form of acquaintance, and that wouldn’t be far from wrong. In fact, in certain contexts it may be perfectly correct. Gamma, et al point out that the differences between the two is a matter of intent rather than explicit language mechanisms. That makes it a little tricky simply to show some code and pronounce, “See that’s what an aggregate looks like.” So bear with me as we look at aggregate relations.
Conjoined Participants
As can be seen in the red arrow symbols in the Participant Relations diagram at the top of this post, an aggregate relation is indicated by an arrow with a diamond at its base. The GoF illustrate the relationship as shown in Figure 1:

Figure 1: Aggregator and Aggregatee
The Aggregator creates an aggregate instance of the Aggregatee. As noted, GoF point out that no single code set can demonstrate what an aggregate relationship looks like, but I think that we can get a start by looking at a fairly simple relationship in the State design I created so that we could discuss both aggregate relations and delegation. (The Strategy design also provides a good example of aggregation with delegation.)
In looking at the aggregate relations between the Context class and State interface in the State example, we see the following aggregation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class Context { private var stoph:State; private var moveup:State; private var movedown:State; private var moveleft:State; private var moveright:State; private var stateNow:State; public var chopper:Sprite; public function Context( chopper:Sprite ) { this.chopper=chopper; trace("State is here"); moveup=new MoveUp(this); stoph=new Stop(this); movedown=new MoveDown(this); moveleft=new MoveLeft(this); moveright=new MoveRight(this); stateNow=stoph; } public function upMove():void { stateNow.doMoveUp(); } ...end snippet |
So what’s getting aggregated? In this instance the entire State interface and all of its implementations (classes) and methods are aggregated in the Context class. All of the requests coming from the Client are delegated to the State through such aggregation.
At this point you may better understand GoF’s statement,
Aggregation implies that an aggregate object and its owner have identical lifetimes.
The aggregate object is that created in the Context and the owner is the State interface and implementations. The Context creates the aggregate object by gathering up all of the concrete states. When the Client makes a request, it uses an instance of the Context:
private var contextNow:Context=new Context(chopper);
The instance, contextNow, essentially is the aggregate object. If you change the State implementations, it doesn’t matter because the Client makes the requests through the Context that creates an aggregate instance. That means if you change the implementations of the concrete States, everything still works because the requests are all through the Context class. As long as an aggregate relationship exists between participants in a design pattern, the aggregator creates an aggregate instance. With requests made through the aggregator, you decouple the request and the objects that carry out the requests. Further, through aggregation you can reuse objects all you want arranged in the way you want to employ them.
Closely Related Participants
You will find aggregate relationships in several design patterns, and the actual ActionScript 3.0 encoded aggregation has many faces. In coming to understand aggregation, take a look at several of the following classes that contain aggregation relationships.
One of the more interesting uses of aggregation is where the child classes aggregate the parent class or interface. You can see this in the Composite and Decorator patterns. Likewise, you will see many different ways that the aggregation process is designed to structure the relationships between design pattern participants. By looking at the code in different examples aggregation you will begin to see what aggregation means.
Aggregation for Change
After posting the State example with the Helicopter (extension of Sprite class) jumping around the screen, we received several comments on improving the design. By having a State design pattern in place, that should not be too difficult to change the states for the Helicopter. The structure can remain the same, but not only could we substitute just about any sprite for the Helicopter, we could change the algorithms using the current interfaces without touching the fundamental design pattern. So in the next post, we’ll look at the Helicopter movement with an eye to making it more state-like while maintaining the current structure, even adding helper classes if we want. As always, we value your comments.

The ActionsScript 3.0 Design Pattern Relations Part II: Aggregation by William B. Sanders, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.
Related posts:

Bill Sanders
One statement I saw recently (I think it was in an older text) suggested that when a composite’s owner object is destroyed, the composite’s children are likewise eliminated. In the case of an aggregate though, the children might survive, referenced by other objects. Aggregate children can be aggregated by more than one object.
I don’t think this conflicts with the GoF statement above, if we accept that aggregates can have more than one owner. And it kind of helped me see the distinction with a composition.
So I thought I’d share it here.
Hi Timbot,
A composition is a collected object made up of other objects, such as the Context compositing the States creating an aggregate object. The aggreateinstance in Figure 1 is an object created by the Aggregator with properties of the Agregatee. (I use the term properties broadly here to include both properties and methods.) So a composition has no child classes as do classes related by inheritance. Instead, a composition is an object made up of other objects without inheritance. I don’t mean to sound picky, but because a key principle is to favor composition over inheritance, we need to separate one from the other.
I hope that helps nudge the concept of aggregation (as a type of composition) closer to the light. As always, it’s great to hear from you!
Kindest regards,
Bill
Hi Bill!
I would like to share a concept that helps me better understand aggregation: It’s the “whole to its parts” idea. For instance, if you want to give emphasis that the “whole” (Context class) needs its “parts” (the State classes) to do what’s suppose to do. Sometimes classes are associated but the idea of ” whole to its parts” doesn’t apply so the aggregation symbols are not necessary, an Acquaintance would be just fine. I would even say there is no right or wrong, just a matter of giving more detail to the diagram.
Nice post! How can I rate it?
Tot ziens!
Hi William,
Keep in mind that both aggregation and acquaintance deal with composition. On a different topic, whole and part may be pivotal, but here the focus is on the relationships between participants. Acquaintance and aggregation constitute relationships between the participants. So that’s where we need to focus and try and understand the difference between the two. Unfortunately, there is a difference between them, but it is subtle and based on intent as much as specific details. That means there is a right and wrong insofar as using one or the other.
One thing that has helped me is to compare the relationship between a Client class and its acquaintance and an Aggregator class and the Aggregatee. I cannot think of a single design pattern where the Client has an aggregate relationship with a design pattern, but you will find lots of patterns where you’ll find aggregate relations between the pattern participants. If you look at the way in which those relations are implemented, you’ll begin to see how they are different.
As for me…Nenhum descanso para o mau.
Kindest regards,
Bill