File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Diff b/w composition & Agrigation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Diff b/w composition & Agrigation" Watch "Diff b/w composition & Agrigation" New topic
Author

Diff b/w composition & Agrigation

Mehdi Raza
Greenhorn

Joined: Nov 07, 2001
Posts: 2
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

Joined: Sep 29, 2000
Posts: 8521
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


"JavaRanch, where the deer and the Certified play" - David O'Meara
Marium Hasan
Greenhorn

Joined: Oct 03, 2001
Posts: 12

Hello Cindy,
Can u give an example of aggregation?
So that it would be more clear
Thanks
Marium
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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.


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Diff b/w composition & Agrigation