Tuesday, November 4, 2014

Composite design Pattern

Composite Pattern
This pattern is used to create a tree structure. Suppose, we want to design a role hierarchies for an organization and say the hierarchy is as follows,

CEO
----- VPs
--------- Directors
---------------------- Managers


package com.exmpl.CompositePattern;

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

public class Employee {
       private String designation;
       private String empName;
       private String empId;
       private double empSalary;
       private List<Employee> listOfEmps;

       public Employee(final String empName, final String empId,
                     String designation, final double empSalary) {
              this.empName = empName;
              this.empId = empId;
              this.designation = designation;
              this.empSalary = empSalary;
              listOfEmps = new ArrayList<Employee>();
       }

       public void add(Employee emp) {
              listOfEmps.add(emp);
       }

       public List<Employee> getListOfEmps() {
              return listOfEmps;
       }

       @Override
       public String toString() {
              return "Employee [designation=" + designation + ", empName=" + empName
                           + ", empId=" + empId + ", empSalary=" + empSalary + "]";
       }
}
-----------------------------------------------------------------------
package com.exmpl.CompositePattern;

public class CompositePatternExample {
       public static void main(String args[]) {
              // One CEO of an organization.
              Employee CEO = new Employee("Mr. Mark Walls", "EMP-0001", "CEO",
                           250000.00);
              // Suppose two VPs under CEO
              Employee VP_ONE = new Employee("Mr. Patric Anderson", "EMP-0002", "VP",
                           200000.00);
              Employee VP_TWO = new Employee("Mr. Sent Willam", "EMP-0003", "VP",
                           200000.00);
              // Suppose four Directors under VP
              Employee DR_ONE = new Employee("Ms. Ena Richard", "EMP-0002",
                           "Director", 150000.00);
              Employee DR_TWO = new Employee("Mr. Aaron Hank", "EMP-0003",
                           "Director", 150000.00);
              Employee DR_THREE = new Employee("Mr. Barker Clive", "EMP-0002",
                           "Director", 150000.00);
              Employee DR_FOUR = new Employee("Mr. Freud Sigmund", "EMP-0003",
                           "Director", 150000.00);
              // Suppose eight Manages under Director
              Employee MANAGER_ONE = new Employee("Mr. Freund Peter", "EMP-0002",
                           "Manager", 100000.00);
              Employee MANAGER_TWO = new Employee("Mr. Frostrup Mariella",
                           "EMP-0003", "Manager", 100000.00);
              Employee MANAGER_THREE = new Employee("Mr. Gephardt Dick", "EMP-0002",
                           "Manager", 100000.00);
              Employee MANAGER_FOUR = new Employee("Mr. Giacometti Alberto",
                           "EMP-0003", "Manager", 100000.00);
              Employee MANAGER_FIVE = new Employee("Ms. Foch Ferdinand", "EMP-0002",
                           "Manager", 100000.00);
              Employee MANAGER_SIX = new Employee("Mr. Hayek Friedrich", "EMP-0003",
                           "Manager", 100000.00);
              Employee MANAGER_SEVEN = new Employee("Mr. Hazlitt William",
                           "EMP-0002", "Manager", 100000.00);
              Employee MANAGER_EIGHT = new Employee("Mr. Louis Joe", "EMP-0003",
                           "Manager", 100000.00);

              // VPs under CEO
              CEO.add(VP_ONE);
              CEO.add(VP_TWO);
              // Directors under VP
              VP_ONE.add(DR_ONE);
              VP_ONE.add(DR_TWO);
              VP_TWO.add(DR_THREE);
              VP_TWO.add(DR_FOUR);
              // Managers under Director.
              DR_ONE.add(MANAGER_ONE);
              DR_ONE.add(MANAGER_TWO);
              DR_TWO.add(MANAGER_THREE);
              DR_TWO.add(MANAGER_FOUR);
              DR_THREE.add(MANAGER_FIVE);
              DR_THREE.add(MANAGER_SIX);
              DR_FOUR.add(MANAGER_SEVEN);
              DR_FOUR.add(MANAGER_EIGHT);
             
              //Print the Employee Tree.
              System.out.println(CEO);
              for(Employee vp : CEO.getListOfEmps() ){
                     System.out.println("-------->"+vp);
                     for(Employee director : vp.getListOfEmps()){
                           System.out.println("---------------->"+director);
                           for(Employee manager : director.getListOfEmps()){
                                   System.out.println("------------------------>"+manager);
                           }
                     }
              }
       }
}
-----------------------------------------------------------------------
Output รจ
Employee [designation=CEO, empName=Mr. Mark Walls, empId=EMP-0001, empSalary=250000.0]
-------->Employee [designation=VP, empName=Mr. Patric Anderson, empId=EMP-0002, empSalary=200000.0]
---------------->Employee [designation=Director, empName=Ms. Ena Richard, empId=EMP-0002, empSalary=150000.0]
------------------------>Employee [designation=Manager, empName=Mr. Freund Peter, empId=EMP-0002, empSalary=100000.0]
------------------------>Employee [designation=Manager, empName=Mr. Frostrup Mariella, empId=EMP-0003, empSalary=100000.0]
---------------->Employee [designation=Director, empName=Mr. Aaron Hank, empId=EMP-0003, empSalary=150000.0]
------------------------>Employee [designation=Manager, empName=Mr. Gephardt Dick, empId=EMP-0002, empSalary=100000.0]
------------------------>Employee [designation=Manager, empName=Mr. Giacometti Alberto, empId=EMP-0003, empSalary=100000.0]
-------->Employee [designation=VP, empName=Mr. Sent Willam, empId=EMP-0003, empSalary=200000.0]
---------------->Employee [designation=Director, empName=Mr. Barker Clive, empId=EMP-0002, empSalary=150000.0]
------------------------>Employee [designation=Manager, empName=Ms. Foch Ferdinand, empId=EMP-0002, empSalary=100000.0]
------------------------>Employee [designation=Manager, empName=Mr. Hayek Friedrich, empId=EMP-0003, empSalary=100000.0]
---------------->Employee [designation=Director, empName=Mr. Freud Sigmund, empId=EMP-0003, empSalary=150000.0]
------------------------>Employee [designation=Manager, empName=Mr. Hazlitt William, empId=EMP-0002, empSalary=100000.0]

------------------------>Employee [designation=Manager, empName=Mr. Louis Joe, empId=EMP-0003, empSalary=100000.0]

No comments:

Post a Comment