Saturday, June 16, 2012

composition vs aggregation


  • composition vs aggregation

Composition is more restrictive.
When there is a composition between two objects, the composed object cannot exist without the other object.

This restriction is not there in aggregation.

A Library contains students and books.
Relationship between library and student is aggregation.
Relationship between library and book is composition.
A student can exist without a library and therefore it is aggregation
A book cannot exist without a library and therefore its a composition.

http://javapapers.com/oops/association-aggregation-composition-abstraction-generalization-realization-dependency/


  • composition vs aggregation
composition is stronger than aggregation relation
in composition a part only belongs to a whole
if you destroy a building you destroy a room as well

in aggregation relation this is different because if you destroy a band musician can continue to exist




  • What is the difference between aggregation and composition?


Aggregation 
Aggregation is an association in which one class
belongs to a collection. This is a part of a whole
relationship where a part can exist without a whole.
For example a line item is a whole and product is a
part. If a line item is deleted then corresponding
product need not be deleted. So aggregation has a
weaker relationship.

Composition
Composition is an association in which one class belongs to a
collection. This is a part of a whole relationship where a part
cannot exist without a whole. If a whole is deleted then all parts are
deleted. For example An order is a whole and line items are parts.
If an order is deleted then all corresponding line items for that
order should be deleted. So composition has a stronger
relationship.


No comments:

Post a Comment