This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Delegation Vs Aggregation and Composition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Delegation Vs Aggregation and Composition" Watch "Delegation Vs Aggregation and Composition" New topic
Author

Delegation Vs Aggregation and Composition

Gagan Sabharwal
Ranch Hand

Joined: Apr 23, 2006
Posts: 48
Hi,

I have been going through OOAD by Headfirst. I came across the importance of LSP, the need for Delegation and the importance of Association. The book is very comprehensive. But I still find myself juggling between Aggregation and Composition. I have seen a lot of posts in javaranch regarding the same. However after going through some, I intend to go through some more examples on the aforementioned concepts.

Could please somebody help me with good online articles on the same, which explain the sanctity of association and illustrate a clear difference between aggregation and composition, something which is other than the basic Car, Vehicle kind of examples?

Thanks
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
One of the key elements in the Unified Modeling Language is 'Relationships", e.g. Realization, Generalization, etc.

The Association relationship is used to indicate that two objects interact with each other in some way.

The Aggregation relationship is a type of Association relationship and is used to indicate a whole/part relationship betwen two objects.
This type of relationship applies to designs where the "part" object can exist in other places in the system and does not entirely depend upon
a particular "whole" object for its existence.

The Composition relationship is a type of Assocation relationship and is used to indicate a whole/part relationship between two objects.
This type of relationship applies to designs where the "part" object cannot exist in other places in the system and is entirely dependent upon
a particular "whole" object for its existence.

Hope this helps!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41124
    
  45
This may help: AssociationVsAggregationVsComposition


Ping & DNS - my free Android networking tools app
Gagan Sabharwal
Ranch Hand

Joined: Apr 23, 2006
Posts: 48
Thank you gentlemen!!!
Vlad Varnica
Ranch Hand

Joined: Dec 28, 2002
Posts: 40
Here is an example including getter, setters and associations attribute (e.g. icon with a little arrow) for :
- Association 0..1 to 0..1
- Composition 1 to *
- Aggregation 1 to *

You can't in the java code when reverse engineering a code see the difference between an aggregation or an composition because the only difference is provided by UML.
btw, this association type information is important this is why I think that adding a class diagram to a project will make it clearer


[Thumbnail for aggregation_with_setters_getters.png]

[Thumbnail for composition_setter_getters.png]

[Thumbnail for association_with_setter_getters.png]

Suresh Gokarakonda
Greenhorn

Joined: Jan 01, 2009
Posts: 13
In one of the design patterns book I am currently reading, the author says "in java language where GC happens automatically there is no difference between aggregation and composition ". Any idea what the author meant by this?


Regards,
Suresh
Vlad Varnica
Ranch Hand

Joined: Dec 28, 2002
Posts: 40
The java code is the same for both of them. Aggregation versus Compisition is only an UML information
This is one of the reasons why UML is important
Suresh Gokarakonda
Greenhorn

Joined: Jan 01, 2009
Posts: 13
How is it different in other languages?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Delegation Vs Aggregation and Composition
 
Similar Threads
Association, Aggregation and composition implementation in java
Dependency vs Association
Aggregation, association and composition
Is that an association?
Aggregation, association and composition