Tuesday, March 12, 2013

Aggregation




Definition:
Aggregation is a relationship between two classes that is best described as a "has-a" and "whole/part" relationship. It is a more specialized version of the association relationship. The aggregate class contains a reference to another class and is said to have ownership of that class. Each class referenced is considered to be part-of the aggregate class
Ownership occurs because there can be no cyclic references in an aggregation relationship. If Class A contains a reference to Class B and Class B contains a reference to Class A then no clear ownership can be determined and the relationship is simply one of association.
For example, imagine a Student class that stores information about individual students at a school. Now let's say there is a Subject class that holds the details about a particular subject (e.g., history, geography). If the Student class is defined to contain a Subject object then it can be said that the Student object has-a Subject object. The Subject object also makes up part-of the Student object, after all there is no student without a subject to study. The Student object is therefore the owner of the Subject object.
Examples:
There is an aggregation relationship between Student class and the Subject class:
 public class Subject {
 
   private String name;
 
   public void setName(String name)
   {
     this.name = name;
   }
 
   public String getName()
   {
     return name;
   }
 }
 
 public class Student {
 
   private Subject[] studyAreas = new Subject[10];
 
   //the rest of the Student class
 }