Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Composite Pattern


A Composite Pattern says that just “allow clients to operate in generic manner on objects that may or may not represent a hierarchy of objects”.

Advantage of Composite Design Pattern

  • It defines class hierarchies that contain primitive and complex objects.
  • It makes easier to you to add new kinds of components.
  • It provides flexibility of structure with manageable class or interface.

Usage of Composite Pattern

It is used:

  • When you want to represent a full or partial hierarchy of objects.
  • When the responsibilities are needed to be added dynamically to the individual objects without affecting other objects. Where the responsibility of object may vary from time to time.

UML for Composite Pattern

compositeuml1

Elements used in Composite Pattern:

Let’s see the 4 elements of composte pattern.

1) Component
  • Declares interface for objects in composition.
  • Implements default behavior for the interface common to all classes as appropriate.
  • Declares an interface for accessing and managing its child components.
2) Leaf
  • Represents leaf objects in composition. A leaf has no children.
  • Defines behavior for primitive objects in the composition.
3) Composite
  • Defines behavior for components having children.
  • Stores child component.
  • Implements child related operations in the component interface.
4) Client
  • Manipulates objects in the composition through the component interface.

Note:The work flow of above general UML is as follows.

Client uses the component class interface to interact with objects in the composition structure. If recipient is the leaf then request will be handled directly. If recipient is a composite, then it usually forwards the request to its child for performing the additional operations.


Example of Composite Pattern

We can easily understand the example of composite design pattern by the UML diagram given below:

compositeuml2.jpg

Implementation of above UML:

Step 1

Create an Employee interface that will be treated as a component.

  1. // this is the Employee interface i.e. Component.
  2. public interface Employee {
  3.     public  int getId();
  4.     public String getName();
  5.     public double getSalary();
  6.        public void print();
  7.     public void add(Employee employee);
  8.        public void remove(Employee employee);
  9.        public Employee getChild(int i);
  10. }// End of the Employee interface.

Step 2

Create a BankManager class that will be treated as a Composite and implements Employee interface.

File: BankManager.java
  1. // this is the BankManager class i.e. Composite.
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. public class BankManager implements Employee {
  6.      private int id;
  7.      private String name;
  8.      private double salary;
  9.      public BankManager(int id,String name,double salary) {
  10.       this.id=id;
  11.       this.name = name;
  12.       this.salary = salary;
  13.      }
  14.          List<Employee> employees = new ArrayList<Employee>();
  15.      @Override
  16.      public void add(Employee employee) {
  17.         employees.add(employee);
  18.      }
  19.         @Override
  20.      public Employee getChild(int i) {
  21.       return employees.get(i);
  22.      }
  23.      @Override
  24.      public void remove(Employee employee) {
  25.       employees.remove(employee);
  26.      }
  27.      @Override
  28.      public int getId()  {
  29.       return id;
  30.      }
  31.      @Override
  32.      public String getName() {
  33.       return name;
  34.      }
  35.     @Override
  36.      public double getSalary() {
  37.       return salary;
  38.      }
  39.      @Override
  40.      public void print() {
  41.       System.out.println(“==========================”);
  42.       System.out.println(“Id =”+getId());
  43.       System.out.println(“Name =”+getName());
  44.       System.out.println(“Salary =”+getSalary());
  45.       System.out.println(“==========================”);
  46.       Iterator<Employee> it = employees.iterator();
  47.           while(it.hasNext())  {
  48.             Employee employee = it.next();
  49.             employee.print();
  50.          }
  51.      }
  52. }// End of the BankManager class.

Step 3

Create a Cashier class that will be treated as a leaf and it will implement to the Employee interface.

File: Cashier.java
  1. public  class Cashier implements Employee{
  2.     /*
  3.          In this class,there are many methods which are not applicable to cashier because
  4.          it is a leaf node.
  5.      */
  6.         private int id;
  7.             private String name;
  8.         private double salary;
  9.         public Cashier(int id,String name,double salary)  {
  10.             this.id=id;
  11.             this.name = name;
  12.             this.salary = salary;
  13.         }
  14.         @Override
  15.         public void add(Employee employee) {
  16.             //this is leaf node so this method is not applicable to this class.
  17.         }
  18.         @Override
  19.         public Employee getChild(int i) {
  20.             //this is leaf node so this method is not applicable to this class.
  21.             return null;
  22.         }
  23.         @Override
  24.         public int getId() {
  25.             // TODO Auto-generated method stub
  26.             return id;
  27.         }
  28.         @Override
  29.         public String getName() {
  30.             return name;
  31.         }
  32.         @Override
  33.         public double getSalary() {
  34.             return salary;
  35.         }
  36.         @Override
  37.         public void print() {
  38.             System.out.println(“==========================”);
  39.             System.out.println(“Id =”+getId());
  40.             System.out.println(“Name =”+getName());
  41.             System.out.println(“Salary =”+getSalary());
  42.             System.out.println(“==========================”);
  43.         }
  44.         @Override
  45.         public void remove(Employee employee) {
  46.             //this is leaf node so this method is not applicable to this class.
  47.         }
  48. }

Step 4

Create a Accountant class that will also be treated as a leaf and it will implement to the Employee interface.

File: Accountant.java
  1. public class Accountant implements Employee{
  2. /*
  3.     In this class,there are many methods which are not applicable to cashier because
  4.     it is a leaf node.
  5. */
  6.     private int id;
  7.     private String name;
  8.     private double salary;
  9.    public Accountant(int id,String name,double salary)  {
  10.        this.id=id;
  11.        this.name = name;
  12.        this.salary = salary;
  13.    }
  14.    @Override
  15.    public void add(Employee employee) {
  16.        //this is leaf node so this method is not applicable to this class.
  17.    }
  18.    @Override
  19.    public Employee getChild(int i) {
  20.        //this is leaf node so this method is not applicable to this class.
  21.        return null;
  22.    }
  23.    @Override
  24.     public int getId() {
  25.         // TODO Auto-generated method stub
  26.         return id;
  27.    }
  28.    @Override
  29.    public String getName() {
  30.        return name;
  31.    }
  32.    @Override
  33.    public double getSalary() {
  34.        return salary;
  35.    }
  36.    @Override
  37.    public void print() {
  38.        System.out.println(“=========================”);
  39.        System.out.println(“Id =”+getId());
  40.        System.out.println(“Name =”+getName());
  41.        System.out.println(“Salary =”+getSalary());
  42.        System.out.println(“=========================”);
  43.    }
  44.   @Override
  45.    public void remove(Employee employee) {
  46.        //this is leaf node so this method is not applicable to this class.
  47.    }
  48. }

Step 5

Create a CompositePatternDemo class that will also be treated as a Client and ii will use the Employee interface.

File: CompositePatternDemo.java
  1. public class CompositePatternDemo {
  2.     public static void main(String args[]){
  3.          Employee emp1=new Cashier(101,“Sohan Kumar”20000.0);
  4.          Employee emp2=new Cashier(102,“Mohan Kumar”25000.0);
  5.          Employee emp3=new Accountant(103,“Seema Mahiwal”30000.0);
  6.          Employee manager1=new BankManager(100,“Ashwani Rajput”,100000.0);
  7.           manager1.add(emp1);
  8.           manager1.add(emp2);
  9.           manager1.add(emp3);
  10.           manager1.print();
  11.         }
  12. }

download this composite pattern Example

Output

  1. ==========================
  2. Id =100
  3. Name =Ashwani Rajput
  4. Salary =100000.0
  5. ==========================
  6. ==========================
  7. Id =101
  8. Name =Sohan Kumar
  9. Salary =20000.0
  10. ==========================
  11. ==========================
  12. Id =102
  13. Name =Mohan Kumar
  14. Salary =25000.0
  15. ==========================
  16. =========================
  17. Id =103
  18. Name =Seema Mahiwal
  19. Salary =30000.0
  20. =========================

Comments

comments