• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference b/w Association,Aggregation&Composition

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everybody,
I am not able to differentiate b/w these 3 terms. Any one can please provide me with good link and example (preferably Java) and some explaination.
Thanks in advance
Nakul
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Nakul,
Association, aggregation and composition can be mapped using attribute or method calling relatioship, it deppend of your model. Only in conceptually perpective you can differentiate between them.
Talking about composition and aggregation:
"...the difficult thing is considering what the difference is between agregation and association." [Martin Fowler]
So, one example should not be used as a rule. I understand like this:
- Association: one class calls methods from other. But this relationship can also be mapped as an attibute.
- Agregation: one class is "part of" the other. It is like saying that a monitor is part of a computer. I always map this as an attribute. Here, more than one object may have a reference for the part object.
- Composition: a particularly aggregation where the part object can not exist without the wrapper class. Also, the part object can only exist in one object and not in two object. The difference is like value and reference object concept [Fowler, page 93].
In Java, objects is passed by reference, and I would like to know how people map composition relationship. I would suggest to use part objects like attributes but not in more than one outer object. If we create objects only inside outer class and do not use its reference outside as other attribute, I understand it like a composite object.
Comments, please.
Adrian
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Robert Martin has a pretty good explanation of this at http://ootips.org/uml-hasa.html (although the code examples are in C++).
The interesting thing to note is that there isn't a precise mapping of Java syntax to these relationships. E.g. Any of the three could be represented (at least in part) with an instance variable.
(paraphrasing Martin into Java terminology) an association can be implemented through an instance variable, although it might also be implemented as a method argument, or the creation of a local variable.
Adrian in the prior post commented that he typically implements aggregations as an attribute (instance variable), but Martin also says that you need to avoid a cyclic relationship.
Adrian's suggestion of using an Inner Class for Composition is fairly common as well, but with some care, a Composition relationship could be implented with between a pair of regular classes as well.
Dave
------------------
david_kane@houseofyin.com
http://www.vraps.com
http://www.houseofyin.com
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David Kane:
[B]Robert Martin has a pretty good explanation of this at http://ootips.org/uml-hasa.html (although the code examples are in C++).

THE URL asks for a password !

 
David Kane
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's weird. The site has never asked me for one.
------------------
David Kane
david_kane@houseofyin.com
Author of Software Architecture: Organizational Principles and Patterns
http://www.vraps.com
http://www.houseofyin.com
 
Ranch Hand
Posts: 82
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you're using those particular terms, and all three in the same context, I'm guessing you're learning about UML diagrams. And since you categorized the question under Java, I'm guessing you want to know how to use Java to realize these concepts.

An association is just a reference. If my class has a property with getter/setter, let's say it's a StringBuffer, then my class has an association to StringBuffer.

Aggregation is multiple references. These are one-to-many, like an array. If my class has a List<StringBuffer> so StringBuffer objects can be added and removed, my class has an aggregation of StringBuffers.

Let's say I want to create a class that's just like StringBuffer but it has some other methods and features that make it special. I could have a private StringBuffer object which my class "wraps", by using the StringBuffer methods as the implementation of my class' methods (a concept called delegation), then I can state that my class is composed of a StringBuffer, or is a composition of StringBuffer

for more information follow the bellow link

https://stackoverflow.com/questions/885937/what-is-the-difference-between-association-aggregation-and-composition
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic