Tags

, , ,

In programming experience, I found confusion about Association, Aggregation and composition. So, Here I am trying to clearing the concepts.

Association is a relationship where all object have their own lifecycle and there is no owner. Let’s take an example of Teacher and Student. Multiple students can associate with single teacher and single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. Both can create and delete independently.

association.jpg

Aggregation is a specialize form of Association where all object have their own lifecycle but there is ownership and child object cannot belongs to another parent object. Let’s take an example of Department and teacher. A single teacher cannot belong to multiple departments, but if we delete the department teacher object will not destroy. We can think about “has-a” relationship.
Another example, Wheels are the part of Car. Car owns the wheels, but wheels can exist, if car is not available.

aggregation.jpg

Composition is again specialize form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. If owner object is destroyed the other one will no longer exist. Let’s take again an example of relationship between University and Department. University can contain multiple departments. There is no independent life of departments and any department cannot belong to two different universities. If we delete the university, departments will be automatically deleted.
Let’s take another example relationship between Questions and options. Single questions can have multiple options and option can not belong to multiple questions. If we delete questions options will be automatically deleted.

composition.jpg

So in summary, we can say that aggregation is a special kind of an association and composition is a special kind of an aggregation. (Association->Aggregation->Composition)

association_aggregation_composition.jpg

Figure : Association, Aggregation, Composition

Advertisements