Association , Aggregation and Composition is
often confusing to developers . Although they have nearly same semantic but
they have subtle difference.
1) Association – Relationship between two or more objects.
2) Aggregation – Relationship between two or more objects where one
object exists even if another object destroys.
Example – Organization exist even if an Employee leaves.
Here Organization
is one object and Employee another object.
It is a special
form of Association where:
§ It
represents Has-A relationship.
§ It
is a unidirectional association i.e. a one way relationship.
For example, department can have students but vice versa is not possible and
thus unidirectional in nature.
§ In
Aggregation, both the entries can survive individually which means
ending one entity will not affect the other entity
3) Composition-
Relationship between two or more objects where one object destroys or become
meaningless if associated object destroys.
Example
-Car destroys if Engine destroys.
Here Car is one
object and Engine is another object.
In Java, we make
associated object as final if it is
very much required.
Composition is a
restricted form of Aggregation in which two entities are highly dependent on
each other.
§ It
represents part-of relationship.
§ In
composition, both the entities are dependent on each other.
§ When
there is a composition between two entities, the composed object cannot
exist without the other entity.
References
http://javarevisited.blogspot.in/2014/02/ifference-between-association-vs-composition-vs-aggregation.html
http://www.geeksforgeeks.org/association-composition-aggregation-java/
No comments:
Post a Comment