Saturday, March 22, 2014

Aggregation vs Composition through example in Java

There is a very thin line between Aggregation and Composition relationship between two classes. Both of these are a has-a relationship and that makes life difficult. Aggregation is a weak has-a relationship whereas Composition is a strong has-a relationship.

Let's take a simple example of three classes; Company, Department and Employee. Company consists of (or has ) Department and Department consists of Employee.

Aggregation vs Composition

The diamond end in both the cases points towards the whole class (or bigger and fatter class), with the only difference that Composition diamond is filled up (whereas Aggregation diamond is unfilled).

Another difference is that, in the case of Composition, the composite object has full responsibility for the component object. So a component object can't exist without the composite object in case of Composition. But in the case of Aggregation the coupling between the component and the composite is not so tight and thus component can enjoy life even without composite. 

What it means here is, Department can't exist without Company but Employee can exist even without Department. So, the association between Company and Department is stronger/tighter compared to that of between Department and Employee. 

Java Implementation

Aggregation, as well as composition, are implemented in the almost similar way in Java through Composition. The main difference lies in how you encapsulate the contained object. If the contained object is properly encapsulated inside the whole object then it will be composition. So in the case of Composition, the contained object doesn't have it's own identify. It comes and goes with the whole object.

To bring home the point, let's take a different example of two classes School and Student. The student is contained inside School. Also, School consists of more than one Student.

 /**  
  * Simple Student class     
  * @author Sid  
  */  
 class Student {  
      private String name;  
      private int standard;  
      public Student(String name, int standard) {  
           super();  
           this.name = name;  
           this.standard = standard;  
      }  
      //other attributes/methods  
 }  

 import java.util.ArrayList;  
 import java.util.List;  
 /**  
  * Simple School class  
  * @author Sid  
  */  
 public class School {  
      String name;  
      List<Student> students;  
      public School(String name) {  
           this.name = name;  
           this.students = new ArrayList<Student>();  
      }  
      // Enrolls new student  
      public boolean enrollStudent(String name, int standard) {  
           Student s = new Student(name, standard);  
           students.add(s);  
           return true;  
      }  
      // other attributes and methods  
 }  

 /**  
  * Class to test Student, School relationship  
  * @author Sid  
  */  
 public class SchoolClient {  
      public static void main(String[] args) {  
           School school = new School("DAV Public School");  
           school.enrollStudent("Sid Rai", 12);  
           school.enrollStudent("Rob D", 12);  
      }  
 }  

The relationship between Student and School is Aggregation or Composition?
Another way to put it is, Can an instance of Student exist without an instance of School?

Notice that, the creation of Student is encapsulated inside School class ( through enrollStudent(..) method).
By now, it should be obvious. Above code demonstrate Composition.

Just for completeness, How can you make the relationship as Aggregation in above example?
The simplest way is, refactor enrollStudent(..) method as :

       public boolean enrollStudent(Student s){....}

Similarly, you can achieve it in an Object Oriented Language.

References
---
do post your feedback !!!

No comments:

Post a Comment