Recent Posts

Friday, 7 October 2016

Composite Design Pattern Tutorial


Composite Design Pattern
     Before we go into the Composite design pattern, first we define composite objects (objects that contain other objects).
     Composite design pattern allows you to have a tree structure and ask each node in the tree structure to perform a task.You can take real life example of a organization. It have general managers and under general managers, there can be managers and  under managers there can be developers.Now you can set a tree structure and ask each node to perform common operation like getSalary().
    Composite design pattern treats each node in two ways Composite or leaf. Composite means it can have other objects below it. Leaf means it has no objects below it. It look like as follows,

Composite Design Pattern

Example

Employee.java(Component)
package com.ashok.designpatterns.composite;

public interface Employee {

   public void add(Employee employee);

   public void remove(Employee employee);

   public Employee getChild(int i);

   public String getName();

   public double getSalary();

   public void print();
}
Manager.java(Composite)
package com.ashok.designpatterns.composite;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Manager implements Employee {

   private String name;
   private double salary;

   public Manager(String name, double salary) {
      this.name = name;
      this.salary = salary;
   }

   List<Employee> employees = new ArrayList<Employee>();

   public void add(Employee employee) {
      employees.add(employee);
   }

   public Employee getChild(int i) {
      return employees.get(i);
   }

   public String getName() {
      return name;
   }

   public double getSalary() {
      return salary;
   }

   public void print() {
      System.out.println("Name =" + getName());
      System.out.println("Salary =" + getSalary());
      System.out.println("----------------------");

      Iterator<Employee> employeeIterator = employees.iterator();
      while (employeeIterator.hasNext()) {
         Employee employee = employeeIterator.next();
         employee.print();
      }
   }

   public void remove(Employee employee) {
      employees.remove(employee);
   }
}
Developer.java(Leaf)
package com.ashok.designpatterns.composite;

public class Developer implements Employee {

   private String name;
   private double salary;

   public Developer(String name, double salary) {
      this.name = name;
      this.salary = salary;
   }

   public void add(Employee employee) {
      // this is leaf node so this method is not applicable to this class.
   }

   public Employee getChild(int i) {
      // this is leaf node so this method is not applicable to this class.
      return null;
   }

   public String getName() {
      return name;
   }

   public double getSalary() {
      return salary;
   }

   public void print() {
      System.out.println("Name =" + getName());
      System.out.println("Salary =" + getSalary());
      System.out.println("-------------");
   }

   public void remove(Employee employee) {
      // this is leaf node so this method is not applicable to this class.
   }
}
TestCompositeDesignPattern.java
package com.ashok.designpatterns.composite;

public class TestCompositeDesignPattern {

   public static void main(String[] args) {
      Employee emp1 = new Developer("Ashok", 25000);
      Employee emp2 = new Developer("Dillesh", 45000);
      Employee manager1 = new Manager("PC Varma", 150000);
      manager1.add(emp1);
      manager1.add(emp2);
      Employee emp3 = new Developer("Anand", 25000);
      Manager generalManager = new Manager("Ambuj", 150000);
      generalManager.add(emp3);
      generalManager.add(manager1);
      generalManager.print();
   }
}

Output
Name =Ambuj
Salary =150000.0
-------------
Name =Anand
Salary =25000.0
-------------
Name =PC Varma
Salary =150000.0
-------------
Name =Ashok
Salary =25000.0
-------------
Name =Dillesh
Salary =45000.0
-------------

Next Tutorial  Decorator Design Pattern Tutorial

Previous Tutorial  Bridge Design Pattern Tutorial

No comments:

Post a Comment