Association , Aggregation and Composition

Association

“Association represents the static relationship shared among the objects of two classes. “So any relationship between object of two classes is called association.

An association says there is some kind of dependency between the objects. It can either be a weak or Strong dependency.

Consider an example of Teacher and Student classes. Multiple students can associate with single teacher and single student can associate with multiple teachers. This is a kind of a weak relationship because there is no ownership between the student and teacher and the life cycle are maintained by themselves.

Composition

Composition is specialized form of aggregation and we can call this as a “life and death” relationship. It is a strong type of aggregation. Child object does not have their life-cycle and if parent object deletes all child objects will also be deleted.

Start exploring endless computing possibilities with your own Raspberry Pi computer and accessories. Perfect for beginners and students.

Consider an example of a set of classes Engine and Car. The object Engine is said to be composed with in the Car object as the Engine cannot leave without the Car object and the relationship is said to be whole because the Engine is a part of the Car object. Here the life cycle of Engine is managed by the Car object. If the Car gets destructed the associated composite object like Engine will also get destroyed.

Thus a composition relationship specifies that the lifetime of the part classifier is dependent on the lifetime of the whole classifier.

public class Engine
{
. . .
}

public class Car

{

Engine e = new Engine();

…….

}

Yet another example for Composite relationship is, consider a set of classed Person, Leg and Hand. The objects Leg and Hand are composed within the Person object. The life cycle of Leg and Hand objects are maintained by Person object. Also you can see the Leg and Hand object cannot leave independently without the Person object. And note there is a strong relationship of Leg and Hand object with the Person.

Composition class diagram
Composition class diagram

Aggregation

The difference between aggregation and composition is the object which can exists independently uses aggregation and an object that has no meaning outside of the relationship uses composition

Let’s take an example of Department and teacher. A single teacher cannot belong to multiple departments, but if we delete the department, the teacher object will not destroy. We can think about “has-a” relationship.

Let us take another example of Company and Employee classes. The employee is said to be aggregated with the Company. The Employee is working for a company and has a relationship with the company.  The employee object can still survive even if say the Company object does not exists.

Conclusion

I will conclude here by taking a real world example which has aggregation and compositions.

Consider a scenario of a University which owns various departments (e.g., physics, chemistry) and each department has a number of professors. If the university closes, the departments will no longer exist, but the professors in those departments will continue to exist. Therefore, a University can be seen as a composition of departments, whereas departments have an aggregation of professors. In addition, a Professor could work in more than one department, but a department could not be part of more than one university.


Leave a Reply