Thursday, September 19

java composite design pattern , OO design pattern , composite tree design

              java composite design pattern , OO design pattern , composite tree design

-->


                                                           Composite Design Pattern

Composite design pattern is based on creating a tree structure in such a way that an individual leaf of the tree can be treated just like entire tree composition. Means all constituents of the tree consist of similar properties and can be represented with single interface.

 

For Example :

public interface Employee{

     public void addEmployee(Employee employee); 
     public void removeEmployee(Employee employee); 
     public Employee getEmployeeChild(int i); 
     public String getEmployeeName(); 
     public double getEmployeeSalary(); 
   
}




    public class Manager implements Employee{ 
     
     private String name; 
     private double salary; 
     
     public Manager(String name ,double salary){ 
      this.name = name; 
      this.salary = salary; 
     } 
      
     List employees = new ArrayList(); 
     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 remove(Employee employee) { 
      employees.remove(employee); 
     } 
     
    } 



    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 remove(Employee employee) { 
            //this is leaf node so this method is not applicable to this class. 
        } 
     
    } 



-->






public class CompositeDesignPatternMain { 
 
 public static void main(String[] args) { 
  Employee emp1=new Developer("John", 10000); 
  Employee emp2=new Developer("David", 15000); 
  Employee manager1=new Manager("Daniel",25000); 
  manager1.add(emp1); 
  manager1.add(emp2); 
  Employee emp3=new Developer("Michael", 20000); 
  Manager generalManager=new Manager("Mark", 50000); 
  generalManager.add(emp3); 
  generalManager.add(manager1); 
  generalManager.print(); 
 




Have a look at CompositeDesignPatternMain class carefully. Here using same interface Employee we are able to create a developer , manager and entire tree structure . In tree structure at top there is manager and below that that manager developers are added.

Similarly in other instance GeneralManager is at the top in the tree and manager and developer are added under it. Point to notice is that all tree structure and every individual leaf is being created using same interface . So the combination of Employee ,Developer,Manager is written is such a way  that reference Employee is sufficient to create a leaf of tree as well as entire structure . So here objects are composed in such a way that a single Employee reference can represent entire tree structure as well as single leaf of manager or developer or even any part of the tree .

Thus composition design pattern basically facilitate the concept of identifying commonality and designing various components around base structure in such a way that Base interface can suffice all leaf's requirement as well as can represent the entire tree structure.







-->


No comments:

Post a Comment