• 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 between delegation, composition and aggregation

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am really confused between those 3 concepts. I googled a lot and i found answers but i really got lost in the way they comapre those 3 concepts.
I even looked in coderanch http://faq.javaranch.com/java/AssociationVsAggregationVsComposition but got lost!
Can you guys please pass to me simple reference to understand the difference?

Thanks,
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any specific question that you have after reading the FAQ?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An association is when an object has a reference to another object that's not by definition a part of the first object, but instead performs some sort of function that the first object makes use of.
For instance, an instance of the class Person can have a reference to an instance of Car. The person is associated to that car, but the car isn't part of the person. As a matter of fact, there might be another Person who also has a reference to the same car.

Aggregation is when an object has a reference to another object that's inherently a part of it. A Car can have references to its Engine, the engine is part of the car. However, once the car is scrapped, they can salvage the engine and put it in another Car.

Composition is when an object has a reference to another object, that's inherently a part of it, and only it. When the first object is 'scrapped', so are the objects it is composed of. Let's just say that the Doors of a Car can not be reused. They are constructed when the Car is constructed, and when the Car is demolished, so are the Doors.

The lines are not clear cut. For instance, you can make Doors before you make a Car and then mount them on at a later time. In this case the entire Car would be an Aggregation of other parts.
In my experience, few relations in programming are aggregations. Often an object makes use of another separate object (an Association), or the other object is a part of it and ceases to exist when the first object reaches the end of its life time.
 
Mina Daoud
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Muhammad Khojaye wrote:Is there any specific question that you have after reading the FAQ?



Yea, as i mentioned in my post, i couldn't get it. Maybe my english is not that good, but it still confuses me
 
Mina Daoud
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:An association is when an object has a reference to another object that's not by definition a part of the first object, but instead performs some sort of function that the first object makes use of.
For instance, an instance of the class Person can have a reference to an instance of Car. The person is associated to that car, but the car isn't part of the person. As a matter of fact, there might be another Person who also has a reference to the same car.

Aggregation is when an object has a reference to another object that's inherently a part of it. A Car can have references to its Engine, the engine is part of the car. However, once the car is scrapped, they can salvage the engine and put it in another Car.

Composition is when an object has a reference to another object, that's inherently a part of it, and only it. When the first object is 'scrapped', so are the objects it is composed of. Let's just say that the Doors of a Car can not be reused. They are constructed when the Car is constructed, and when the Car is demolished, so are the Doors.

The lines are not clear cut. For instance, you can make Doors before you make a Car and then mount them on at a later time. In this case the entire Car would be an Aggregation of other parts.
In my experience, few relations in programming are aggregations. Often an object makes use of another separate object (an Association), or the other object is a part of it and ceases to exist when the first object reaches the end of its life time.



Thanks for that, i got it now. That was so simple definition indeed
but what about delegation?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Delegation is similar to simple Association, except the original object does nothing with the object it associates with, except pass its functionality on to a third object.

Why? A delegation usually happens in a wrapper class that makes another difficult to use class more accessible to your program.
Lets say you're using a class provided by a third party, with a very difficult to understand API, ugly method signatures, etc.

You can write your own class that wraps around this class which provides a nice clean appearance which simply redirects calls to it to the ugly class. This way you can comfortably write the rest of your program without worrying about ugly method calls, they're all contained by the wrapper class. This is delegation. The wrapper class simply delegates method invocations to another class, without doing anything special itself.
 
Mina Daoud
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Delegation is similar to simple Association, except the original object does nothing with the object it associates with, except pass its functionality on to a third object.

Why? A delegation usually happens in a wrapper class that makes another difficult to use class more accessible to your program.
Lets say you're using a class provided by a third party, with a very difficult to understand API, ugly method signatures, etc.

You can write your own class that wraps around this class which provides a nice clean appearance which simply redirects calls to it to the ugly class. This way you can comfortably write the rest of your program without worrying about ugly method calls, they're all contained by the wrapper class. This is delegation. The wrapper class simply delegates method invocations to another class, without doing anything special itself.



Thanks so much for the declaration in that simple way. But let me please ask another question, why they added all these terminologies? Or why as developer i need to know it? I mean, when any developer will develop an application, based on the buisiness logic will create the classes and hence by logic will have some aggregation/composition/delegation?
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it makes a difference to separate between these concepts when you're designing a system. When you decide ahead of time that some object needs to have the same life time as its containing object, then that is an important decision that while shape the system as a whole. It's good to have a standard set of terminology for these concepts, so when you are working on a system with a team, everyone understands what is expected when you're finished on the drawing board.
 
Mina Daoud
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Well, it makes a difference to separate between these concepts when you're designing a system. When you decide ahead of time that some object needs to have the same life time as its containing object, then that is an important decision that while shape the system as a whole. It's good to have a standard set of terminology for these concepts, so when you are working on a system with a team, everyone understands what is expected when you're finished on the drawing board.


I got your point, thanks for your help
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm glad I could help.
 
Yeast devil! Back to the oven that baked you! And take this tiny ad too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic