51

From Martin Fowler's UML Distilled:

In the pre-UML days, people were usually rather vague on what was aggregation and what was association. Whether vague or not, they were always inconsistent with everyone else. As a result, many modelers think that aggregation is important, although for different reasons. So the UML included aggregation (Figure 5.3) but with hardly any semantics. As Jim Rumbaugh says, "Think of it as a modeling placebo" [Rumbaugh, UML Reference].

What I understand from this and answers I read on Stack Overflow is that it doesn't matter which one of these two relations I use, they're basically the same thing. Is this the case or are there some situations where the use of aggregation instead of association (and vice-versa) could be justified?

Mehdi Charife
  • 722
  • 1
  • 7
  • 22
Andna
  • 6,539
  • 13
  • 71
  • 120

8 Answers8

31

Maybe this can help you, but i don't think you will find the perfect explanation:

The difference is one of implication. Aggregation denotes whole/part relationships whereas associations do not. However, there is not likely to be much difference in the way that the two relationships are implemented. That is, it would be very difficult to look at the code and determine whether a particular relationship ought to be aggregation or association. For this reason, it is pretty safe to ignore the aggregation relationship altogether.
[Robert C. Martin | UML]

And an example for each situation:

a) Association is a relationship where all object have their own lifecycle and there is no owner. Let’s take an example of Teacher and Student. Multiple students can associate with a single teacher and single student can associate with multiple teachers, but there is no ownership between the objects and both have their own lifecycle. Both can create and delete independently.

b) Aggregation is a specialized form of Association where all object have their own lifecycle but there is ownership and child object can not belong to another parent object. Let’s take an example of Department and teacher. A single teacher can not belong to multiple departments, but if we delete the department, the teacher object will not be destroyed. We can think about “has-a” relationship.
[Maesh | GeeksWithBlogs]

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ChapMic
  • 26,954
  • 1
  • 21
  • 20
  • 3
    Interesting, but about second example, I think if we would swap aggregation with association, still there won't be any difference, and it all boils down to personal preference and readability of a diagram? – Andna Mar 09 '12 at 21:25
  • 1
    How can you *tell* whether a relationship is whole/part? – reinierpost Mar 13 '12 at 09:07
  • @ChapMic "child object can not belong to another parent object" this is plain wrong :) Please take a look at https://stackoverflow.com/questions/13935125/aggregation-multiplicity-uml and this https://en.wikipedia.org/wiki/Object_composition#Aggregation – igobivo Nov 08 '21 at 10:25
  • The GeeksWithBlogs reference is (effectively) broken. It would redirect to *[Geekswithblogs.net, the End of an Era](http://julian.farm/geekswithblogs/?url=/mtreadwell/archive/2004/06/06/6123.aspx)*. – Peter Mortensen Jan 25 '22 at 16:49
31

Rumbaugh's statement is the most telling and Uncle Bob's good advice. As I've said elsewhere, Aggregation is semantically so weak as to offer nothing practically beneficial. It only has one valid corner case (acyclicity of recursive relationships) however few people know and understand that. So you end up having to point out in comments anyway.

I just don't use it. And have never felt any loss. Stick with simple binary associations and focus on what really matters - getting the cardinality and naming right. You'll get far more from that than trying to decide the undecidable association vs. aggregation.

hth.

Community
  • 1
  • 1
sfinnie
  • 9,854
  • 1
  • 38
  • 44
  • What are your thoughts on the composition link? – Dennis Jun 01 '13 at 08:19
  • @Dennis: unlike Aggregation, Composition has clearly-defined semantics that usefully differentiate it from a straight binary association. More here: http://stackoverflow.com/questions/7834052/uml-class-diagram-association-vs-aggregation-composition-diamonds/7836357#7836357 – sfinnie Jun 03 '13 at 07:44
  • @sfinnie Can you further elaborate on the acyclicity of recursive relationships part? Sounds very intimidating. – Mehdi Charife May 08 '23 at 21:33
3

I tend to use Aggregation to show a relation that is the same as a Composition with one big distinction: the containing class is NOT responsible for the life-cycle of the contained object. Typically, a (non-null) pointer or reference to the object-to-be-contained is passed to the containing class's constructor. The containing object, for the duration of its life-cycle, depends upon the contained object existing. The containing object cannot do its job (fully) without the contained object. This is my interpretation of the "Part/Whole" relationship implied by Aggregation.

M.McKenzie
  • 31
  • 1
  • 1
    "The containing object cannot do its job (fully) without the contained object." Isn't this also true for associations and some forms dependencies? – Jupiter Sep 10 '13 at 16:15
1

This term often gets confused.

Aggregation and composition are some of the types of association. There is hardly a difference between aggregations and associations during implementation, and many will skip aggregation relations altogether in their diagrams with association relation.

You can get the idea from this analogy.

Class:A(person) and Class:B(car) has association relation, if Class:A has a Class:B declaration, and also Class:B(car) object is not essential to create a Class:A(person) object.

Class:A(car) and Class:B(tyre) has aggregation relation, if Class:A has a Class:B declaration, and also Class:B(tyre) object is essential to create a Class:A(car) object.

Cheers!

Mohan
  • 4,755
  • 2
  • 27
  • 20
0

They do not mean the same! I can put it in this way:

Association relationship: A class references another class. Actually it shows that a class is related to another class but they don't necessarily have attributes to show this relationship... e.g 'Teacher' and 'Student' classes, although 'Teacher' class has no attribute that refer to students, but we do know that in reality a teacher do have students... And also 'School' class has 'teachers' and 'students' properties that now make those two classes related to each other.

Aggregation relationship: A class contains another class. But if the container(ClassRoom) is destroyed, the contained(Chair) is not. Actually the ClassRoom owns the Chair. Aggregation is a more stronger relationship than the Association relationship.

Here is also a tutorial about it and the whole UML2.0 which explains everything easy and simple, you may find it useful: https://github.com/imalitavakoli/learn-uml2

TIP: Also let me mention that because the Association relationship exists between classes most of the times, we sometimes don't draw it to prevent unnecessary complexity.

Community
  • 1
  • 1
Ali
  • 1,268
  • 1
  • 13
  • 20
0

Implementation wise there is not much of a difference but conceptually there is big difference: aggregations are used to express a hierarchy. When you work with a hierarchy of components there are certain type of operations you need to have in the root interface:

  • find subcomponents in the hierarchy
  • add/remove subcomponents to/from the hierarchy
  • change common attributes of all components
  • traverse the hierarchy recursively (Visitor pattern)
  • reconfigure the hierarchy and the links (associations) between the components

Most of these operations are not needed when dealing with associations.

arpadf
  • 413
  • 3
  • 13
0

To add, I would just suggest to download the UML specification from the OMG site: best reference and see p 110.

None indicates that the property has no aggregation semantics.

Shared indicates that the property has shared aggregation semantics. Precise semantics of shared aggregation varies by application area and modeler.

Composite indicates that the property is aggregated compositely, i.e., the composite object has responsibility for the existence and storage of the composed objects (see the definition of parts in 11.2.3).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
granier
  • 1,739
  • 13
  • 25
0

In UML aggregation is under-defined and since they haven't got any clearly defined semantic. A valid use-case of an aggregation is the encapsulation of a several classes, as stated in "Domain Driven Design" by Eric Evans.

E.g. a car has four wheels. You might want to calculate the total amount of meters each wheel has driven, for each car. This calculation is done by the car-entity, since it knows which wheels it has and you don't care which wheels belong to which car.

The car is the aggregation-root for all it's parts, like wheels, and you can't access the parts of a car from outside the aggregation, just the root.

So basically an aggregation encapsulates a set of classes which belong to each other.

Jan Gräfen
  • 314
  • 2
  • 12
  • UML composition is semantically closer to DDD's aggregates. DDD Aggregates have some very well-defined semantics (actually stronger even than UML composition). Certainly much stronger than UML aggregation. It's unfortunate the terms don't match up :-( – sfinnie Mar 12 '12 at 20:47