Association, Aggregation and Composition

Introduction

Association, Aggregation, and Composition are terms that represent relationships among objects. They are very basic stuff of Object Oriented Programming.

Association

Association is a relationship among the objects. Association is "*a*" relationship among objects. In Association, the relationship among the objects determines what an object instance can cause another to perform an action on its behalf. We can also say that an association defines the multiplicity among the objects. We can define a one-to-one, one-to-many, many-to-one and many-to-many relationship among objects. Association is a more general term to define a relationship among objects. Association means that an object "uses" another object.

For example Managers and Employees, multiple employees may be associated with a single manager and a single employee may be associated with multiple managers.

OOP1.jpg

Aggregation

Aggregation is a special type of Association. Aggregation is "*the*" relationship among objects. We can say it is a direct association among the objects. In Aggregation, the direction specifies which object contains the other object. There are mutual dependencies among objects.

For example, departments and employees, a department has many employees but a single employee is not associated with multiple departments.

UML Representation of Aggregation (white diamond):

OOP2.jpg

The UML representation of the example above (relation between employee and department):

OOP3.jpg

Here, the lives of both objects are independent of each other. That means that in this Association (Aggregation) the object has their own life cycle. Employees may exist without a department. Here, department can be called an owner object and the employee can be called a child object. The owner and child objects cannot belong to a different parent object.

Composition

Composition is special type of Aggregation. It is a strong type of Aggregation. In this type of Aggregation the child object does not have their own life cycle. The child object's life depends on the parent's life cycle. Only the parent object has an independent life cycle. If we delete the parent object then the child object(s) will also be deleted. We can define the Composition as a "Part of" relationship.

For example, the company and company location, a single company has multiple locations. If we delete the company then all the company locations are automatically deleted. The company location does not have their independent life cycle, it depends on the company object's life (parent object).

UML Representation of Composition (black diamond):

OOP4.jpg

UML representation of the example above (relation between Company and Company Location):

OOP5.jpg

Here, the lives of both objects are not independent. The life of the company location object can be determined by the life of the company object. The company object is responsible for creating and destroying company location objects.

Relationship among Association, Aggregation, and Composition

OOP6.jpg

Aggregation and Composition are a special type of Association. Composition is again a special type of Aggregation. We can define Aggregation and Composition as "has a" relationships. Composition is more restrictive or more specific. In Composition, composed objects cannot exist without the other object. This type of restriction does not exist in Aggregations. In Aggregation, the existence of a composed object is optional. In Aggregation, the child object can exist beyond the life cycle of its parent whereas in Composition the child object cannot exist beyond the life cycle of its parent.

Combined example of Aggregation and Composition:

OOP7.jpg

Aggregation VS Composition
 

Aggregation Composition
Aggregation is a special type of Association. Composition is a special type of Aggregation.
All objects have their own life cycle. In Composition, the child object does not have their own life cycle and it depends on the parent's life cycle.
A parent class is not responsible for creating or destroying the child class. The parent class is responsible for creating or destroying the child class.
Aggregation can be described as a "Has-a" relationship. Composition can be described as a "Has-a" relationship as well as a "Part of" relationship, but here the difference is the length of the relationship among the objects.
Aggregation is a weak Association. Composition is a strong Association.
Aggregation means one object is the owner of another object. Composition means one object is contained in another object.
The direction of a relation is a requirement in both Composition and Aggregation. The direction specifies which object contains the other one.
Both have a single direction of association.
Both have a single owner.


Conclusion

These three terms are more important in the object oriented world. They denote or represent the relations among objects. If you are confused or unable to decide whether a specific relation best describes an Association, Aggregation or Composition then it can be decribed as an Association.


Similar Articles