Wednesday, 9 January 2013

Quick Summary : Object Associations



Composition

final class Company{
  private Employee emp;
  public Company(EmpDetails details) {
    emp= new Employee(details);
  }
   void assign() {
      emp.work();
   } 
}

Aggregation

final class Company{
  private Employee engine;
  void addEmployee(Employee emp) {
    this.emp = emp;
  }
  void assign() {
    if (emp != null)
      emp.work();
  }
}

Dependency

final class Company{
  void assign(Employee emp) {
    if (emp != null)
      emp.work();
  }
}

Abstraction

public interface Employee{
 public void work();
 public void off();
 public void quit();
}

Realisation

public abstract class Engineer implements Employee
{
 public void work();
 public void off();
 public void quit();
}

Generalization

public class SWEngineer extends Engineer
{
 public void work()
 {
  System.out.println("SW Engineer working");
 }
 public void off()
 {
  System.out.println("SW Engineer is off today");
 }
 
 public void quit()
 {
  System.out.println("SW Engineer is quitting");
 }
 
}
 

Relationships


Association
Defines a relationship between classes. Compositon and Aggregation are types of associations
Composition
the Employee is encapsulated within the Company . There is no way for the outside world to get a reference to the Employee. The Employee is created and destroyed with the company
Aggregation
The Company also performs its functions through an Employee, but the Employee is not always an internal part of the Company . Employees can be exchanged, or even completely removed. As the employee is injected, the Employee reference can live outside the  Company.
Dependency
The company does not hold the employee reference. It receives an employee reference only to the scope an operation. Company is  dependent on the Employee object to perform an operation
Abstraction
Defines  the basic operations the implementer should adher to.  Employee interface lists the general behavior of an employee
Realization
A class implements the behavior defined by the other other class or interface
Generalization
A class which is a special form of a parent class

 UML Relationship Pointers



References




 

2 comments:

  1. Nice, clean article, perfect for begginners. However, there's a typo in the first example. I think you meant:

    private Employee engine;

    (and there's an extra } there)

    ReplyDelete
  2. I guess the constructor got deleted while formatting and variable naming was wrong. Thanks for pointing it out. I corrected the example. Thanks

    ReplyDelete