Thursday, 4 August 2016

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.

There are dependencies, there are generalizations and then there are 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, how they relate to each other and what are the differences among Association, Aggregation and Composition.

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.

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.

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.

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
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 emphasise; 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.

Java code

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;
    .........
    .........
}

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.

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 close.

Java code

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.

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!


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 -

>>>Go to Java Basics page

1 comment:

  1. I have definitely picked up anything new from right here. I did however expertise a few technical points using this site, since I experienced to reload the web site a lot of times previous to I could get it to load correctly.

    Corporate Training in Chennai

    ReplyDelete