Thursday, 26 June 2014

Association Vs Aggregation Vs Composition


It represents a relationship between two or more objects where all objects have their own lifecycle and there is no owner. The name of an association specifies the nature of relationship between objects. This is represented by a solid line.
Let’s take an example of relationship between Professor and Book. Multiple Professor can associate with a single Book and a single Professor can associate with multiple Book. But there is no ownership between the objects and both have their own lifecycle. Both can be created and deleted independently.

Professor playing the role of author associated with textbook end typed as Book.
Professor playing the role of author associated with textbook end typed as Book.


It is a specialized form of Association where all object have their own lifecycle but there is ownership. This represents “whole-part or a-part-of” relationship. This is represented by a hollow diamond followed by a line.
Let’s take an example of relationship between Department(Container) and Teacher(Contained). A Teacher may belongs to multiple departments. Hence Teacher is a part of multiple departments. But if we destroy a Department(Container Object), Teacher Object(Contained Object ) will not destroy. Objects are not dependent.

         HAS A Relation ->   Department HAS A Teacher

An aggregation is used when life of object is independent of container object But still container object owns the aggregated object.

Example :  Suppose Organization has Person as employees, relationship between them is Aggregation. Here is how they look like in terms of Java classes

public class Organization {
    private List employees;

public class Person {
    private String name;   

Other Eg: Team has players, If team dissolve, Player will still exists.


It is a specialized form of Aggregation. It is a strong type of Aggregation. In this relationship child objects does not have their lifecycle without Parent object. If a parent object is deleted, all its child objects will also be deleted. This represents “death” relationship. This is represented by a solid diamond followed by a line.
Let’s take an example of relationship between House(Container) and rooms(Contained). House can contain multiple rooms there is no independent life of room and any room cannot belongs to two different house if we destroy  the house(Container Object)  then room(Contained Object) will be  automatically destroyed.
Objects are dependent.

       HAS A Relation ->  House HAS A Room

A composition is used where each part may belong to only one whole at a time.

Other Example  : Since Engine is 
part-of Car, relationship between them is Composition. Here is how they are implemented between Java classes.If we destroy the Car then engine will be destroyed. 

public class Car {
    //final will make sure engine is initialized
    private final Engine engine;  
    public Car(){
       engine  = new Engine();

class Engine {
    private String type;

UML Diagram of Association, Composition and Aggregation 

UML has different notations to denote aggregation, composition and association.  Association is denoted by simple arrow, while aggregation is denoted by  empty diamond head arrow and composition is denoted by filled diamond head arrow. When you draw UML diagram for two related class A and B, where A is associated with B then its denoted by A -> B. Similar way is used to show aggregation and composition between two classes. Here is UML notations for different kind of dependency between two classes.

Difference between Association, Aggregation and Composition

As, I said all three denotes relationship between object and only differ in their strength, you can also view them as below, where composition represent strongest form of relationship and association being the most general form.

Association vs Aggregation vs Composition

Association vs Composition vs Aggregation

Here is the list of differences between Composition and Aggregation in point format, for quick review. As I said key difference between them comes from the point that in case of Composition, One object is OWNER of other object, while in case of aggregation, one object is just a USER or another object.

1) If A and B two classes are related to each other such that, B ceased to exist, when A is destroyed, then association between two object is known as Composition. Example is Car and Engine. While if A and B are associated with each other, such that B can exist without being associated with A, then this association in known as Aggregation.

2) In case of Composition A owns B e.g. Person is owner of his HandMind and Heart, while  in case of Aggregation, A uses B e.g. Organization uses People as employee.

3) In UML diagram Association is denoted by normal arrow head, while Composition is represented by filled diamond arrow head, and Aggregation is represented by empty diamond arrow head, As shown in below and attached diagram in third paragraph.

Association  A---->B
Composition  A-----<filled>B
Aggregation  A-----<>B

4) Aggregation is a lighter form of Composition, where sub-part object can meaningfully exits without main objects.

5) In Java, you can use final keyword to represent Composition. Since in Composition, Owner object expect part object to be available and functions, by making it final, your provide guarantee that, when Owner will be created, this part object will exist. This is actually a Java idiom to represent strong form of association i.e. composition between two objects.

6) Another interesting word, which comes handy to understand difference between Composition and Aggregation in software design is "part-of" and "has". If one object is part-of another object e.g. Engine is part of Car, then association or relationship between them is Composition. On the other hand if one object just has another object e.g. Car has driver than it's Aggregation.

No comments:

Post a Comment