Thursday, October 4, 2012

UML Relations : Association, Aggregation, Composition, Dependency, Realization, Abstraction and Generalization.



We use some key terms to specify the relationship between objects. Those are Association, Aggregation, Composition, Dependency, Realization, Abstraction and Generalization. I have tried to put the relations in following chart as per their definations to remember easily.


Association
An Association is the basic term to represent the relation between objects and we can say it is parent of any relation between objects.

Aggregation
Aggregation is more specific than Association. It is a variant of the "has a" or association relationship; I.e. when an object holds other object then we called it as Aggregation.
Eg:
public class Library {
      private Student student;

      public Student getStudent() {
            return student;
      }

      public void setStudent(Student student) {
            this.student = student;
      }    
}

Composition
Composition is more specific than aggregation. It is a stronger variant of the "owns a" or association relationship i.e. contains object will not exists without the container class.
Eg:
public class Library {
      private Book book;

      public Book getBook() {
            return book;
      }

      public void setBook(Book book) {
            this.book = book;
      }    
}
There will be no book without library.

Dependency
Dependency is more specific than Composition. It is a weaker form of relationship which indicates that one class depends on another because it uses it at some point of time. I.e. One class depends on another.
public class Circle {
      public void calulateArea(Util util){
            util.area();
      }
}
Util is not useful without Circle and Circle will not provide calculateArea without Util.

Multiplicity
Multiplicity is more specific than Composition. This is association relationship indicates that (at least) one of the two related classes makes reference to the other. I.e. one contains other and vice-versa.
One:
public class One {
      private Two two;

      public Two getTwo() {
            return two;
      }

      public void setTwo(Two two) {
            this.two = two;
      }
     
}
Two:
public class Two {
      private One one;

      public One getOne() {
            return one;
      }

      public void setOne(One one) {
            this.one = one;
      }
     
}

Abstraction
Abstraction is an association used to hide the low level implementation. I.e. Considering the Category of the object is an abstraction.
Eg:
public interface Vehicle {
      public void forward ();
      public void reverse ();
      public void right ();
      public void left ();
}

Realization
A realization relationship between classes and interfaces and between components and interfaces shows that the class realizes the operations offered by the interface.
Eg:
public abstract class Car implements Vehicle{
      public void forward(){}
      public void reverse(){}
      public void right(){}
      public void left(){}   
}

Generalization:
Generalization uses a “is-a” relationship from a specialization to the generalization class. Common structure and behavior are used from the specialization to the generalized class.
Eg.
public class GenericCar extends Car{

      @Override
      public void forward() {      
            System.out.println("move forwad.");
      }

      @Override
      public void reverse() {
            System.out.println("move reverse.");
      }

      @Override
      public void right() {
            System.out.println("move right.");
           
      }

      @Override
      public void left() {
            System.out.println("move left.");
      }
}

No comments:

Post a Comment