This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
hi 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
Shai koren<br />SCJP2 <br />SCEA (well yea only part 1 so far)
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. Cheers, Krzysztof
All, 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. HTH.
Sanjay Raghavan<br />SCJP2, SCEA-J2EE<br />Moderator - <a href="http://groups.yahoo.com/group/scea_prep" target="_blank" rel="nofollow">SCEA PREP</a><br />Co-Author - <a href="http://www.whizlabs.com/scea/scea.html" target="_blank" rel="nofollow">SCEA@Whiz</a><br /><i>Where did you sip your Java Today?</i>