Wednesday, 16 December 2015

Design Pattern

1. builder pattern
  • instead of using many constructors, it use builder to handle multiple parameters
public class Pizza {
    private int size; 
    private boolean cheese; 
    private boolean pepperoni; 
    private boolean bacon; 

    public static class Builder { 
        private final int size; 
        private boolean cheese = false; 
        private boolean pepperoni = false; 
        private boolean bacon = false; 

        public Builder(int size) { this.size = size; } 
        public Builder cheese(boolean value) { cheese = value; return this; } 
        public Builder pepperoni(boolean value) { pepperoni = value; return this; } 
        public Builder bacon(boolean value) { bacon = value; return this; } 

        public Pizza build() { return new Pizza(this);}  //build pizza from builder
    }

    private Pizza(Builder builder) { 
        size = builder.size; 
        cheese = builder.cheese; 
        pepperoni = builder.pepperoni; 
        bacon = builder.bacon; 
    } 
}

Pizza pizza = new Pizza.Builder(12).cheese(true).pepperoni(true).bacon(true).build();




2. adapter pattern
  • translate one interface to another
public interface Duck { 
    public void quack();
    public void fly();
}

public interface Turkey { 
    public void gobble();
    public void fly();
}

public class TurkeyAdapter implements Duck {
    Turkey turkey;

    public TurkeyAdapter(Turkey turkey) {
        this.turkey = turkey;
    }

    public void quack() {
        turkey.gobble();
    }

    public void fly() {
        turkey.fly();
    }
}

public static void main(String[] args) {
    WildTurkey turkey = new WildTurkey();
    Duck turkeyAdapter = new TurkeyAdapter(turkey);

    turkeyAdapter.quack();
    turkeyAdapter.fly();
}


3. bridge pattern
  • decouple abstraction from its implementation
  • prefer composition over inheritance
  • very similar to strategy pattern
/** abstraction */ 
abstract class Shape { 
    protected DrawingAPI drawingAPI; 
    protected Shape(DrawingAPI drawingAPI){ this.drawingAPI = drawingAPI; } 
}

class CircleShape extends Shape {  
    public CircleShape(DrawingAPI drawingAPI) { super(drawingAPI); } 
}

/** implementor */
interface DrawingAPI { public void drawCircle(double x, double y, double radius); }

class DrawingAPI1 implements DrawingAPI { 
    public void drawCircle(double x, double y, double radius) {} 

class DrawingAPI2 implements DrawingAPI { 
    public void drawCircle(double x, double y, double radius) {} 

public static void main(String[] args) {  
    Shape s1 = new CircleShape(1, 2, 3, new DrawingAPI1()); 
    Shape s2 = new CircleShape(5, 7, 9, new DrawingAPI2()); 
}


4. command pattern
  • 'command' object include 'receiver' and an 'execute' method on 'receiver'
  • 'client' create 'command' object and store them in 'invoker' (a list or queue)
public interface Command { void execute(); }  //command interface

public class Light {   //receiver
    public void on() { }
    public void off() { }

public class onCommand implements Command {   //on command
    private Light light; 
    public onCommand (Light light) { this.light = light; } 
    @Override public void execute() { light.on(); } 
}

public class Switch {      //invoker
    private List<Command> l = new ArrayList<Command>();
    public void storeAndExecute(Command cmd) { 
        this.l.add(cmd); 
        cmd.execute(); }  // optional
}

public class offCommand implements Command {   //off command
    private Light light;
    public offCommand(Light light) { this.light = light; }
    @Override public void execute() { light.off(); }

public class TestSwitch { 
    public static void main(String[] args){ 
        Light lamp = new Light(); 
        Command on = new onCommand(lamp); 
        Command off = new offCommand(lamp); 

        Switch mySwitch = new Switch(); 
        switch(args[0]) { 
            case "ON": 
                mySwitch.storeAndExecute(on); 
                break; 
            case "OFF": 
                mySwitch.storeAndExecute(off); 
                break; 
            default: break; 
        } 
    } 
}


5. mvc pattern
  • controller 'observe' and interact with view
  • controller 'observe' and interact with model
  • for security reason, view not necessarily 'observe' and interact with model (e.g. secure database)
6. singleton pattern
  • using enum


reference:
1. when would you use the builder pattern?
2. bridge pattern
3. java singleton using enum

No comments:

Post a Comment