aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes How to differentiate among aggregation, association and composition? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "How to differentiate among aggregation, association and composition?" Watch "How to differentiate among aggregation, association and composition?" New topic
Author

How to differentiate among aggregation, association and composition?

Alibabra Sanjie
Ranch Hand

Joined: Feb 09, 2004
Posts: 147
How to differentiate among aggregation, association and composition?


Alibabra
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
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...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
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

Joined: Apr 20, 2001
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

Joined: Jul 09, 2001
Posts: 1180
Alibabra,

Here is a nice presentation which covers the subject:

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

Regards,
Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Mikey Alleblas
Greenhorn

Joined: Jun 25, 2004
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.aw-bc.com/catalog/academic/product/0,1144,0201571684,00.html
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

Joined: Jul 11, 2001
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

Joined: Mar 06, 2001
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

Joined: Jul 11, 2001
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

Joined: Mar 06, 2001
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

Joined: Jan 29, 2003
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Stan James:
I'm curious about WHY differentiate?


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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Jan 29, 2003
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

Joined: Jul 11, 2001
Posts: 14112
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
I would use "composition" to include data members that are not shared with other objects.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to differentiate among aggregation, association and composition?