• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Aggregation and Composition

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1551
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks rufus.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic