Monday, August 30, 2010

association, aggregation and composition in oops with real world examples

Association:
This is relation where each object has it own life cycle , independent and no owner.
Ex:-
1) Will take a common example of project and developer. A project will be having multiple developers and developer can work for different projects. There is a relationship but both are independent. Both can create and delete independently.

2) Let’s take an another 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.


Aggregation:
This is a specialize form of Association where all object have their own lifecycle has ownership on child object and the child object cannot belongs be related to another parent object.

Ex:-
1) Will take an example of Departments and developers. A developer can be only in one department like dotnet, java etc., but if we delete the Department object still the Developer object exists. This is a “Has- a” relation.
2) Let’s take an example of Department and teacher. A single teacher can not belongs to multiple departments, but if we delete the department teacher object will not destroy. We can think about “has-a” relationship.

Composition:

This is specialize form of Aggregation and can be called as strong Aggregations
Where the child object has no life if the parent object is deleted, because of this we call this as “death” relationship.

Ex:-
Best example is House and room. A house can have multiple rooms but if we delete the house the child i.e. rooms will also be deleted and has no life.