Android源码设计模式解析与实战之三Builder(建造者模式)

in Android with 0 comment

定义

将一个负责对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示

使用场景

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

UML类图

015117_9VOb_2003960.png

  1. Product产品类-产品的抽象类
  2. Builder-抽象Builder类,规范产品的组建,一般是由子类实现具体的组件过程
  3. ConcreteBuilder具体的Builder类
  4. Director-统一组装过程

构建计算机例子

package com.g.c.s.designmode.builder;

/**
 * Created by gaocai on 2017/12/5.
 */


/**
 * 计算机抽象类,即Product角色
 */
public 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 + "]";
    }

}

//具体的Computer类, Macbook
class Macbook extends Computer {


    protected Macbook() {
    }

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

//抽象Builder 类
abstract class Builder {

    //设置主板
    public abstract void builderBoard(String board);

    //设置显示器
    public abstract void builderDisplay(String display);

    //设置操作系统
    public abstract void builderOS();

    //创建Computer
    public abstract Computer create();
}

//具体的Builder类,
class MacbookBuilder extends Builder {

    private Computer mComputer = new Macbook();

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

    @Override
    public void builderDisplay(String display) {

        mComputer.setDisplay(display);
    }

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

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

//Director类, 负责构造Computer
class Director {

    Builder mBuilder = null;

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

    public void construct(String board, String display) {
        mBuilder.builderBoard(board);
        mBuilder.builderDisplay(display);
        mBuilder.builderOS();
    }
}

class Test {

    public static void main(String[] args) {
        Builder builder = new MacbookBuilder();
        Director pcDirector = new Director(builder);
        pcDirector.construct("英特尔主板", "Retina显示器");
        System.out.println("Computer Info: " + builder.create().toString());
    }
}

输出结果

Computer Info: Computer [mBoard=英特尔主板, mDisplay=Retina显示器, mOS= Mac OS X 11.12]

省去Director实现链式操作

package com.g.c.s.designmode.builder;

/**
 * Created by gaocai on 2017/12/5.
 */


/**
 * 计算机抽象类,即Product角色
 */
public 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 + "]";
    }

}

//具体的Computer类, Macbook
class Macbook extends Computer {


    protected Macbook() {
    }

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

//抽象Builder 类
abstract class Builder {

    //设置主板
    public abstract Builder builderBoard(String board);

    //设置显示器
    public abstract Builder builderDisplay(String display);

    //设置操作系统
    public abstract Builder builderOS();

    //创建Computer
    public abstract Computer create();
}

//具体的Builder类,
class MacbookBuilder extends Builder {

    private Computer mComputer = new Macbook();

    @Override
    public Builder builderBoard(String board) {
        mComputer.mBoard = board;
        return this;
    }

    @Override
    public Builder builderDisplay(String display) {
        mComputer.mDisplay = display;
        return this;
    }

    @Override
    public Builder builderOS() {
        mComputer.setOS();
        return this;
    }

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


class Test {

    public static void main(String[] args) {
        Builder builder = new MacbookBuilder();
        //链式操作
        Computer computer = builder.builderBoard("英特尔主板").builderDisplay("Retina显示器").builderOS().create();
        System.out.println("Computer Info: " + computer.toString());
    }
}

Computer Info: Computer [mBoard=英特尔主板, mDisplay=Retina显示器, mOS= Mac OS X 11.12]

Responses