Builder

Builder模式的使用情景

  1. 相同的方法, 不同的执行顺序, 产生不同的事件结果
  2. 多个部件或零件, 都可以装配到一个对象中, 但是产生的运行结果又不相同
  3. 产品类比较复杂, 或者产品类中的调用顺序不同产生了不同的作用.
  4. 当初始化一个对象特别复杂, 如参数多, 且很多参数都具有默认值.

Builder模式的整体构造

产品类

abstract class Computer {
    protected String mBoard;
    protected String mDisplay;
    protected String mOS;

    protected Computer(){

    }

    public void setBoard(String board){
        mBoard=board;
    }

    public void setDisplay(String display){
        mDisplay=display;
    }

    public abstract void setOS();

    @Override
    public String toString() {
        return "Computer [mBoard="+ mBoard+", mDisplay="+mDisplay+", mOS="+mOS+"]";
    }
}


class Macbook extends Computer{
    protected Macbook(){

    }
    @Override
    public void setOS() {
        mOS="Mac OS X 10.10";
    }
}

Builder类

//抽象Builder类
abstract class Builder{

    public abstract void buildBoard(String board);
    public abstract void buildDispaly(String display);
    public abstract void buildOS();
    public abstract Computer create();
}

//具体Builder类
class MacbookBuilder extends Builder{

    private Computer mComputer=new Macbook();

    @Override
    public void buildBoard(String board) {
        mComputer.setBoard(board);
    }

    @Override
    public void buildDispaly(String display) {
        mComputer.setDisplay(display);
    }

    @Override
    public void buildOS() {
        mComputer.setOS();
    }

    @Override
    public Computer create() {
        return mComputer;
    }
}

导演类

导演类起到封装的作用, 避免高层模块深入到建造者内部的实现类

class Direcotr{
    Builder mBuilder=null;

    public Direcotr(Builder builder){
        mBuilder=builder;
    }

    public void construct(String board,String dispaly){
        mBuilder.buildBoard(board);
        mBuilder.buildDispaly(dispaly);
        mBuilder.buildOS();
    }

}
public class BuildTest{
    public static void main(String[] args){
        Builder builder=new MacbookBuilder();

        Direcotr pcDirector=new Direcotr(builder);

        pcDirector.construct("intel","retina");
        StdOut.println("Computer Info : "+builder.create().toString());
    }
}

忽略Director类

在开发过程中, 可以忽略Director类, 直接使用Builder来进行对象的组装, 可以使用setter方法进行链式调用.
new Builder().setA("A").setB("B").create()

public class BuildTest {
    public static void main(String[] args){
        ConcreteProductA A= (ConcreteProductA)new A_Buider().setArg_1("aa")
                                        .setArg_2("bb").create();
        StdOut.print(A);

    }
}

abstract class abstractProduct{
    protected String arg_1;
    protected String arg_2;

    protected abstract void setArg_1(String arg_1);

    protected abstract void setArg_2(String arg_2);

    @Override
    public String toString() {
        return "arg_1: "+arg_1+", arg_2: "+arg_2;
    }
}

class ConcreteProductA extends abstractProduct{

    protected void setArg_1(String arg_1){
        this.arg_1=arg_1;
    }

    protected void setArg_2(String arg_2){
        this.arg_2=arg_2;
    }
}

abstract class Builder {
    public abstract Builder setArg_1(String arg_1);

    public abstract Builder setArg_2(String arg_2);

    public abstract abstractProduct create();
}

class A_Buider extends Builder{
    private ConcreteProductA mConcreteProductA=new ConcreteProductA();

    @Override
    public Builder setArg_1(String arg_1) {
        mConcreteProductA.setArg_1(arg_1);
        return this;
    }

    @Override
    public Builder setArg_2(String arg_2) {
        mConcreteProductA.setArg_2(arg_2);
        return this;
    }

    @Override
    public abstractProduct create(){
        return mConcreteProductA;
    }
}

posted @ 2016-11-26 13:58  ivyxjc  阅读(235)  评论(0编辑  收藏  举报