Thursday, October 25, 2018

Association, Aggregation And Composition in Java

When you model a system you can see that most of the classes collaborate with each other in many ways. In object-oriented modelling there are broadly three kind of relationships.

  1. dependencies
  2. generalizations
  3. associations
This post is about association and two types of associations:
  • Aggregation
  • Composition

Many people get confused about these terms so I’ll try to define Association, Aggregation and composition with Java examples, how these terms relate to each other and what are the differences among Association, Aggregation and Composition.


What is Association

An association is a structural relationship, in object oriented modelling, that specifies how objects are related to one another. This structural relationship can be shown in -

  • Class diagram associations
  • Use case diagram associations

An association may exist between objects of different types or between the objects of the same class. An association which connects two classes is called a binary association. If it connects more than two classes (not a very common scenario) it is called n-ary association.

An association in UML is drawn as a solid line connecting objects of different classes or two objects of same class.

If you want to show navigability then you can use arrow, at one end or at both end for bi-directional navigability.

Association Navigability
Association Navigability

Here B is navigable from A.

Association attributes

There are four attributes of association that you should know.

  1. Name– You use a name to describe the relationship with in the association. You can also give a direction to the name by using a triangle that points to the direction name should be read.
    association name
    Association Name
  2. Role– With in an association classes have specific role to play. You can explicitly state the role a class is playing in an association.

    As example– In the figure used above Factory is playing the role of employer where as worker is playing the role of employee.

  3. Multiplicity– In a structural relationship there may be zero, one or more objects connected with in an association. Multiplicity can be written as a range or as an explicit value. As example association end multiplicity can have one of the following values:
    • one (1): Indicates that exactly one entity type instance exists at the association end.
    • zero or one (0..1): Indicates that zero or one entity type instances exist at the association end.
    • One or more(1..*): Indicates that one or more entity type instances exist at the association end.
    association multiplicity
    Association Multiplicity
  4. Aggregation– Aggregation is a kind of association where you want to define whole/part kind of relationship. Since this post is about Aggregation and composition apart from association so let’s see aggregation in detail.

What is Aggregation

In a plain association, classes are considered at the same level with one class having no more importance than the other. Sometimes you do need to define a “whole/part” relationship where one class (whole) consists of smaller classes (part). This kind of relationship is called aggregation. It is also known as a “has-a” relationship as object of the whole has objects of the part.

An aggregation in UML is drawn as an open diamond at the whole end.

Aggregation Example

In the era of smart phones a very relevant example of the aggregation would be aggregating companies like Uber which is a Cab aggregator. Here Uber (company) is “whole” part and cab driver (Person) is “part”.

aggregation in Java
Aggregation

Here note that cab driver can also be aggregated by other class, a cab driver registered with Uber can also be registered with Ola (another cab aggregating company). That is why aggregation is also known as shared association.

Also to emphasize; in aggregation if there are two classes, class A (whole) and class B (part) then class A doesn’t own class B. Therefore, when specific instance of class A ceases to exist (garbage collected) the instance of class B doesn’t go out of scope. Class B object can still exist.

Aggregation Java example

If we have to show the aggregation in Java classes.

public class Company{
    private List<Person> cabDrivers;
    .............
    .............
}

public class Person{
    private String id;
    private String name;
    .........
    .........
}

Points about aggregation

  1. Aggregation is a type of association which represents whole/part kind of relationship.
  2. Aggregation is also known as "has-a" relationship.
  3. Aggregation doesn't represent a strong "whole/part" relationship and both the "whole" and "part" entities can survive without each other too.

What is Composition

As we have seen aggregation relationship makes a distinction between “whole” and “part” but doesn’t force an ownership and it also doesn’t link the “whole” and “part” object in such a way that if whole is destroyed, parts are also destroyed.

There is a variation of aggregation called composition which has strong ownership, thus the scope of the whole and part are related. In composition if there are two classes, class A (whole) and class B (part) then destruction of class A object will mean class B object will also cease to exist. Which also means class B object may be a part of class A object only.

Composition in UML is drawn as a filled diamond at the whole end.

Composition example

An example of composition would be a person whose body is composed of many body parts like hand, leg, eyes etc.

composition
Composition

Another example

Composition

Here School has one or more Departments and each department belongs to exactly one School. If school is closed all of its department will also be closed.

Composition Java example

In Composition the containing object is responsible for the creation and life cycle of the contained object (either directly or indirectly). We can do it with constructor initialization in Java code. Let’s say we have two classes Vehicle and Engine and Engine object is contained with in Vehicle class.

Class Engine {
 private String engineNum;
 .................
 .................
}

Class Vehicle{
 private Engine engine;
 // constuctor
 Vehicle(){
   engine = new Engine();
 }
}

Here it can be seen that Engine class object is created and destroyed along with the Vehicle class object.

Points about composition

  • composition is a strong form of aggregation.
  • Composition also represents a whole/part relationship but with an ownership.
  • In compostion parts are owned by the whole entity and contained entities can't exist without the owned entity. Which means if a composite (whole) is deleted, all of its contained parts are also deleted.
  • It is a binary association.

That's all for this topic Association, Aggregation And Composition in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Basics Tutorial Page


Related Topics

  1. Class in Java
  2. Object in Java
  3. Encapsulation in Java
  4. Polymorphism in Java
  5. Inheritance in Java

You may also like-

  1. Difference Between Abstract Class And Interface in Java
  2. Java pass by value or pass by reference
  3. Reflection in Java
  4. Overriding hashCode() and equals() method in Java
  5. varargs in Java
  6. ReentrantReadWriteLock in Java
  7. Lambda expressions in Java 8
  8. What if run() Method Called Directly Instead of start() Method - Java Multi-Threading