Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Diff b/w composition & Agrigation

 
Mehdi Raza
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear JavaRanch's Fellow can u define me the composition and agrigation so that i can distinguish difference b/w these.
Wish u Best of Luck.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Composition means that this object has a member variable(s) that holds a reference to an object that was created in and by this class - this classes object "OWNS" that object. When this classes object dies, the other object dies also (well probably - unless you give away a reference to it).
class SomeComposition{
Thingy t = new Thingy();
}

Aggregation means that this object has member variables that hold references to objects that were created somewhere else and passed INTO this objects variables somehow. The "new" operator is in some other class.
In the second case the object that is being refered to exists separately from the aggregate. It was already there independant of the fact that this object now holds a reference to it. There will probably be some kind of a setter method that requires the reference to be passed into this class.
When this object dies, the other object will in all possibility still exist.

------------------
Cindy Glass
Sun Certified Programmer for the Java� 2 Platform
Co-author of Java 2 Certification Passport
 
Marium Hasan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello Cindy,
Can u give an example of aggregation?
So that it would be more clear
Thanks
Marium
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mehdi,
Craig Larman, in Applying UML and Patterns states that "aggregation is used to model whole-part relationships" and that the "whole" is called the "composite". The example he uses is that a 'Hand' aggregates 'Fingers'.
Martin Fowler, in UML Distilled and Larman both refer to this type of aggregation as "composite aggregation". Larman uses the example of a 'Sale' and 'SaleLineItems'. The line items only exist because a 'Sale' exists and the 'Sale' is a composite aggregation of all the 'SaleLineItems'.
A 'Sale' object would hold, say a Vector, that held references to multiple 'SaleLineItems'. If the 'Sale' is deleted then it is most likely that all the 'SaleLineItems' would also be deleted (there wouldn't be much reason to keep them around).
You could also have a composite aggregation where the parts are not deleted when the whole is deleted.
For example, if you designed a game in which there were a set number of players and the user formed a team by selecting players. In this case, the parts, the players, would be created separately from the whole, the 'team'. When the team was deleted the players could still continue to exist.
Hope that helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic