File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes Aggregation and Composition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "Aggregation and Composition" Watch "Aggregation and Composition" New topic

Aggregation and Composition

shai koren
Ranch Hand

Joined: Nov 04, 2001
Posts: 48
I am a bit confused about the two.
is that the way it goes?:
In aggregation there is a part - whole relationship where the part is dependant of the whole. destrying the whole and the part is destroyd.
Composition is a part-whole relationship where both can exist indepandantley.
or is it the other way around?

Shai koren<br />SCJP2 <br />SCEA (well yea only part 1 so far)
Rufus BugleWeed
Ranch Hand

Joined: Feb 22, 2002
Posts: 1551
In UML Distilled , Fowler says it's the other way around. He calls the concept a black beast in French, I believe. My French is no so good.
shai koren
Ranch Hand

Joined: Nov 04, 2001
Posts: 48
thanks rufus.
Krzysztof Swietlinski

Joined: May 11, 2002
Posts: 4
The way I think about it is that aggregation is just a regular association with additional information about whole-part thing - doesn't have any implcation on the implementation.
However composition being also whole-part thing additionally affects the life cycle of objects. Meaning that when you delete whole objects parts also get deleted and probably you can only create parts by adding them to the "whole" object (ie they cannot exist on their own).
In my practice I never use regular aggregations ... they just too confusing for developers. However I use compositions (composite aggragation is the whole name I believe) quite a lot ... especially with any O/R mapping.
Sanjay Raghavan
Ranch Hand

Joined: May 14, 2002
Posts: 148
This is my understanding...
1. Association (represented by a line between two classes, optionally an arrowhead represents direction): This is the weakest form of reference. I like to compare this with global variables or static members. If an object got a reference to a singleton, I would call that association. When the calling object goes out of scope, the singleton is still available for use by other clients.
2.Aggregation - stronger form of association (represented by a hollow diamond on the non-arrowhead side of the line): In aggregation, the instances of one class share a reference to the instance of the referenced class. In some programming languages, this is represented by shared variables.
Non-software example: Assume two circles are intersecting each other. At the point of intersection, the point is being shared by the two circles. If one circle goes out of scope, the point does not.
Good s/w example is when you use the prototype pattern and do "shallow cloning." When the prototypical object is cloned, the object references are shared.
3. Composition: Strongest form of association. Also a whole-part relationship (represented with a solid diamond on the non-arrowhead side): Here the instance of the reference belongs to the instance of the instantiator. Best examples are instance variables created by the instantiator (not just obtaining a reference to an object.)
A good example is "deep copy" using the prototype pattern. Each prototypical instance has it's own instances of object references.

Sanjay Raghavan<br />SCJP2, SCEA-J2EE<br />Moderator - <a href="" target="_blank" rel="nofollow">SCEA PREP</a><br />Co-Author - <a href="" target="_blank" rel="nofollow">SCEA@Whiz</a><br /><i>Where did you sip your Java Today?</i>
I agree. Here's the link:
subject: Aggregation and Composition
It's not a secret anymore!