Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# How to differentiate among aggregation, association and composition?

Alibabra Sanjie
Ranch Hand
Posts: 147
How to differentiate among aggregation, association and composition?

Ilja Preuss
author
Sheriff
Posts: 14112
As this is a *very* frequently asked question, I composed a small explanation at http://faq.javaranch.com/view?AssociationVsAggregationVsComposition

Hope this helps...

Ranch Hand
Posts: 1873
Hi all,

I see all of those as,

1. Association -- Its a relation of association amongst objects. It could be Aggregation or Composition. Association is an abstract concept.

2. Composition -- If we think in terms of Databases then I would say that Strong Entity is "composed of" Weak Entities.

3. Aggregation -- Again w.r.t. Databases the "participating relationship" is Aggregation.

Regards
Maulin

Joe Nguyen
Ranch Hand
Posts: 161
Composite is a part-whole relationship with a strong relationship, whereas aggregation is also a part-whole relationship with a weak relationship.

One thing you may want to know is that UML2 is no longer supporting aggregation UML 2 Class Diagram Review

Dan Drillich
Ranch Hand
Posts: 1183
Alibabra,

Here is a nice presentation which covers the subject:

http://www.csc.liv.ac.uk/~igor/COMP201/files/SE_L22.ppt

Regards,
Dan

Mikey Alleblas
Greenhorn
Posts: 1
The way it seems to work to me is that an aggregation is purely conceptual. An Association links two classes which can be thought to be on the same level. In aggregation, it is just an association which identifies that the whole part (where the diamond is) is constituted of the parts (the other end) and this is how they associate. In programming this relationship, it doesn't seem much different to an association. I think an aggregation is redundant.

A good explanation from the book:
"The meaning of this simple form of aggregation is entirely conceptual. The open diamond distinguishes the 'whole' from the 'part', no more, no less. This means that simple aggregation does not change the meaning of navigation across the association between the whole and its parts, nor does it link the lifetimes of the whole and its parts."

A composition on the other hand is a strong association between the whole and its parts. When the whole class terminates, so do its parts. I think this wuold relate to programming as the instances of the "parts" classes being created within the "whole" object itself.
The book I got doesn't have composition in the part of the book where I got the defition for the aggregate.

A good explanation from the book:
"... in a composite aggreation, the whole is responsible for the disposition of its parts, which means that the composite must manage the creation and destruction of its parts. For example, when you create a 'Frame' in a windowing system, yuo must attach it to an enclosing 'Window'. Similarly, when you destroy the 'Window', the 'Window' object must in turn destroy its 'Frame' parts."

As for simple association, the book says:
"Use associations when yuo want to show structural relationships."
As for applications in code, I got no idea.

Do not take this information as being entirely correct, I am only learning it myself however I feel that I got it right since I was copying from the book .

-Mikey

Ps: The book I got for the curious is
The Unified Modeling Language User Guide
by
Grady Booch, James Rumbaugh and Ivar Jacobson
ISBN 0-201-57168-4
Was printed on recycled paper.

http://www.amazon.com/exec/obidos/tg/detail/-/0201571684/qid=1088208988/sr=8-1/ref=sr_8_xs_ap_i1_xgl14/102-7321209-0560162?v=glance&s=books&n=507846

I first tried SAMS learn UML in 24 hours and do not recommend it at all. It is I feel, better suited for management who have no idea about UML and have no need to get in too deep, but want to get a basic understanding of proposed systems and such which have been modelled in the UML.

Other books worth considering
http://www.amazon.com/exec/obidos/tg/detail/-/0596003447/qid=1088208961/sr=8-2/ref=pd_ka_2/102-7321209-0560162?v=glance&s=books&n=507846
I never read it, but O'Reilly books have always been my favourite technical reference and preferred reading material of any sort for that matter.

Ilja Preuss
author
Sheriff
Posts: 14112
Originally posted by Mikey Alleblas:
I think an aggregation is redundant.

As far as I know, it even was almost dropped from UML 2, but some tools vendors vetoed.

A composition on the other hand is a strong association between the whole and its parts. When the whole class terminates, so do its parts. I think this wuold relate to programming as the instances of the "parts" classes being created within the "whole" object itself.

The composite is also allowed to pass the responsibility for its parts to other composites. It's important, though, that there is always exactly one composite responsible for a part.

sarah Marsh
Ranch Hand
Posts: 282
I didn't get what the followings mean. Please explain more or give one example. Thanks!

Quote:
The composite is also allowed to pass the responsibility for its parts to other composites. It's important, though, that there is always exactly one composite responsible for a part.

Ilja Preuss
author
Sheriff
Posts: 14112
Originally posted by sarah Marsh:
I didn't get what the followings mean. Please explain more or give one example. Thanks!

Quote:
The composite is also allowed to pass the responsibility for its parts to other composites. It's important, though, that there is always exactly one composite responsible for a part.

It's not required that through the lifetime of the part, it's always associated to the same whole. You can have one whole creating the part, having the part passed around between several wholes and finally having the part destroyed by a whole different from the first one.

Does that help?

sarah Marsh
Ranch Hand
Posts: 282
Thanks, Ilja. I got it.

I have to re-understand
"A composition is a strong association between the whole and its parts."

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
I'm curious about WHY differentiate? What information are you trying to convey to what audience? I'm not challenging that you need to do it, just a friendly curiosity. I used to use solid diamonds to indicate one object "owned" another, controlled or defined the create, use and destroy life cycle and open diamonds to indicate pretty much any other relationship.

Ilja Preuss
author
Sheriff
Posts: 14112
Originally posted by Stan James:

Well, I guess Composition is quite an important concept for non-garbage-collected languages. In Java, I don't find that I use Composition at all.

Marlene Miller
Ranch Hand
Posts: 1392
In spite of the few semantics attached to aggregation, everybody thinks it is necessary (for different reasons). Think of it as a modeling placebo. The UML Reference Manual Aggregation pg 148

It's really not fair to present this quote without the context. You really should read the whole UML definition. But I thought this was amusing.

Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
Thanks, Ilja, that is why I used to care about that so much! Gettin old, forgot the last project already! Another point for GC.

Ilja Preuss
author
Sheriff
Posts: 14112

Warren Dew
blacksmith
Ranch Hand
Posts: 1332
2
I would use "composition" to include data members that are not shared with other objects.

Ilja Preuss
author
Sheriff
Posts: 14112
Originally posted by Warren Dew:
I would use "composition" to include data members that are not shared with other objects.

If you are aware of the fact that original Composition doesn't include this restriction and therefore your usage might confuse people not used to the convention...