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]

Monday, November 3, 2014

Decorator Pattern

Decorator pattern allows to add new functionality in an existing object without altering its structure.
Example:
Suppose in a sandwich shop, first customer order a sandwich with additional cheese and corn and chicken.
Now, the second customer needs a sandwich with cheese and third customer needs sandwich with chicken only.
Say price list is as follows,
                Sandwich รจ RS. 30/-
                Additional Chicken รจ RS. 20/-
                Additional Cheese รจ RS. 10/-
                Additional Corn รจ RS. 7/-

Now, for this we can use decorator pattern to make the billing software for sandwich shop.



package com.exmpl.decorator;

public interface Sandwich {
       public int getPrice();
}
============================================================
package com.exmpl.decorator;

public class PlaneSandwich implements Sandwich {

       @Override
       public int getPrice() {
              return 30;
       }
}
============================================================
package com.exmpl.decorator;

/* Make this decorator class as abstract, because no need to define/impleement interface's (Sandwich) "getPrice()" method under this decorator class. It will be overridden under each concrete classes (eg. CheesSandwich, ChickenSandwich etc). */
abstract public class SandwichDecorator  implements Sandwich {
       protected Sandwich specialSandwich;
      
       public SandwichDecorator(Sandwich specialSandwich){
              this.specialSandwich=specialSandwich;
       }
       /*public int getPrice(){
              return specialSandwich.getPrice();
       }*/
}
============================================================
package com.exmpl.decorator;

public class CheesSandwich extends SandwichDecorator{
       public CheesSandwich(Sandwich specialSandwich) {
              super(specialSandwich);
       }
       public int getPrice(){
              return specialSandwich.getPrice() + addCheesPrice();
       }
       private int addCheesPrice() {
              return 10;
       }
}
============================================================
package com.exmpl.decorator;

public class ChickenSandwich extends SandwichDecorator{

       public ChickenSandwich(Sandwich specialSandwich) {
              super(specialSandwich);
       }
       public int getPrice(){
              return specialSandwich.getPrice() + addChickenPrice();
       }
       private int addChickenPrice() {
              return 20;
       }
}
============================================================
package com.exmpl.decorator;

public class CornSandwich extends SandwichDecorator{
       public CornSandwich(Sandwich specialSandwich) {
              super(specialSandwich);
       }
       public int getPrice(){
              return specialSandwich.getPrice() + addCornPrice();
       }
       private int addCornPrice() {
              return 7;
       }
}
============================================================
package com.exmpl.decorator;

public class OrderPriceCheck {

       public static void main(String[] args) {
              // Plane sandwich ............
              Sandwich planeSandwich = new PlaneSandwich();
              System.out.println("Plane sandwich price ==>"
                           + planeSandwich.getPrice());
              // Sandwich with additional cheese and corn and chicken.
              Sandwich planeSandwichWithCheesCornChicken = new CheesSandwich(
                           new CornSandwich(new ChickenSandwich(new PlaneSandwich())));
              System.out
                           .println("Sandwich with additional cheese and corn and chicken ==>"
                                         + planeSandwichWithCheesCornChicken.getPrice());
              // Ssandwich with cheese.
              Sandwich planeSandwichWithChees = new CheesSandwich(new PlaneSandwich());
              System.out.println("Sandwich with additional cheese ==>"
                           + planeSandwichWithChees.getPrice());
              // Ssandwich with corn.
              Sandwich planeSandwichWithCorn = new CornSandwich(new PlaneSandwich());
              System.out.println("Sandwich with additional cheese ==>"
                           + planeSandwichWithCorn.getPrice());
       }
}
============================================================
Output รจ
Plane sandwich price ==>30
Sandwich with additional cheese and corn and chicken ==>67
Sandwich with additional cheese ==>40

Sandwich with additional cheese ==>37