Search This Blog

Wednesday, August 07, 2013

Understanding Association, Aggregation, and Composition

In this article, we will try to understand three important concepts: association, aggregation, and composition. We will also try to understand in what kind of scenarios we need them. These three concepts have really confused a lot of developers and in this article.

For a little bit introduction on types of relationship visit this link

The UML Class diagram is used to visually describe the problem domain in terms of types of object (classes) related to each other in different ways. There are three primary inter-object relationships: association, aggregation, and composition. Using the right relationship line is important for placing implicit restrictions on the visibility and propagation of changes to the related classes, matter which play major role in reducing system complexity.

1. Association

The most abstract way to describe static relationship between classes(classifiers) is using the ‘Association’ link, which simply states that there is some kind of a link or a dependency between two classes or more.


An association defines a relationship between two or more classes. Binary associations are relationships between exactly two classes and a n-ary association is an association between three or more classes.

1.1. Weak Association

ClassA may be linked to ClassB in order to show that one of its methods includes parameter of ClassB instance, or returns instance of ClassB.


1.2. Strong Association

ClassA may also be linked to ClassB in order to show that it holds reference to ClassB instance.


2. Aggregation (Shared Association/Relation Aggregation)

Is a specialized association. It is specified by an aggregation association with a hollow diamond. A part in this type of aggregation can participate in several aggregates. In cases where there’s a part-of relationship between ClassA (whole) and ClassB (part), we can be more specific and use the aggregation link instead of the association link, taking special notice that ClassB can also be aggregated by other classes in the application.


Aggregation protects the integrity of an assembly of objects by defining a single point of control, called the aggregate, in the object that represents the assembly. Aggregation also uses the control object to decide how the assembled objects respond to changes or instructions that might affect the collection.


A part classifier can belong to more than one aggregate classifier and it can exist independently of the aggregate. For example, an Engine class can have an aggregation relationship with a Car class;at the same time an Engine class can have an aggregation relationship with an Order class which indicates that the engine is part of the Car and the Order. Aggregations are closely related to compositions.

3. Composition (Not-Shared Association/Real Aggregation)

Is a stronger form of aggregation where the parts cannot exist without the whole. The parts can only participate in one composite. In a composition association relationship, data usually flows in only one direction (that is, from the whole classifier to the part classifier). Composition is shown as an association with a filled solid diamond nearest the class constituting the whole.



In cases where in addition to the part-of relationship between class Person and booth class Hand and class Leg - there’s a strong life cycle dependency between those classes, meaning that when class Person deleted then booth class Hand and class Leg are also deleted as a result, we should be more specific and use the composition link instead of the aggregation link or the association link.

NB:

Unlike association and aggregation, in the composition relationship, the composed class cannot appear as a return type or parameter type of the composite class,  thus changes in the composed class cannot be propagated to the rest of the system. Consequently, usage of composition limits complexity growth as the system grows.

Aggregation v.s. Association:

The association link can replace the aggregation link in every situation, while aggregation cannot replace association in situations were there is only a ‘weak link’ between the classes.

Summarizing:

To avoid confusion henceforth for these three terms, I have put forward a table below which will help us compare them from three angles: owner, lifetime, and child object.

Association
Aggregation
Composition
Owner
No owner
Single or Multiple owner
Single owner
Life time
Have their own lifetime
Owner's life time
Child object
Child objects all are independent
Child objects belong to a single/multiple parent
Child objects belong to a single parent

Step by step Association, Aggregation, and Composition decision making

References:

ibm ,tutorialspoint, codeproject, sintef9013