I understand what composition is in OOP ,but I am not able to get a clear idea of what Aggregation is . Can someone explain ?
|
Simple rules:
Example 1: A Company is an aggregation of People. A Company is a composition of Accounts. When a Company ceases to do business its Accounts cease to exist but its People continue to exist. Example 2: (very simplified) A Text Editor owns a Buffer (composition). A Text Editor uses a File (aggregation). When the Text Editor is closed, the Buffer is destroyed but the File itself is not destroyed. |
|||||||||||||||
|
|
From http://en.wikipedia.org/wiki/Object_composition
So - while you have an ownership relationship with composition the owned object is also destroyed when the owner is - an aggregation (and the objects contained) can exist independently. -- Update: Apologies - this answer is far too simplistic in hindsight. c.batt provides an excellent definition in his answer: Aggregation vs Composition |
|||||||||||||||||
|
|
There is no single explanation. Different authors mean different things by aggregation. Most don't really mean anything specific by it. |
|||||
|
|
aggregation is a simple collection, like a bag of marbles composition implies internal/functional dependencies, like the hinges on a box cars aggregate passengers; they get in and out without breaking the car's functionality the tires are components; remove one and the car no longer functions correctly [note: the spare tire is an aggregate!] |
|||
|
|
|
I always look at composition as 'needs a', i.e. a car needs an engine, and I look at aggregation as 'things related for a purpose'. So staying with the car analogy, my aggregation may be to represent a journey which may involve bringing a car and passengers together. The journey does not own the car or the passengers, I'm aggregating data that is related for a specific scenario. When the journey is completed the car and the passengers go on. When a car is ended, the car and it's engine are normally destroyed together. |
|||
|
|
|
simple rule: we have object A and object Z. if we have a pointer from object A to object Z then its aggregation.(no pointing from Z to A ,since it maks a simple relation). if object Z cannot exists in the system without object A, then its composition. its not that hard, even more ITS NOT THAT USEFUL. why? beacuse no body uses this stuff to the fine lines, its a loose model that lets you grasp what you want from the system. |
|||||
|
