Extend your project freely - Builder mode

Because a complex object has many and a large number of components, such as a car, there are wheels, steering wheels, engines, and various small parts, etc. How to assemble these parts into a car is a long and complicated assembly process. For this In this case, in order to hide the implementation details from the outside during the construction process, the Builder pattern can be used to separate the components from the assembly process, so that both the construction process and the components can be freely extended, and the coupling between the two is minimized.

1. Builder pattern definition

Separating the construction of a complex object from its representation allows the same construction process to create different representations.

2. Builder mode usage scenarios

  1. The same method, different execution order, produces different event results.
  2. Multiple components or parts can be assembled into an object, but the resulting operating results are not the same.
  3. The product class is very complex, or the call sequence in the product class has different effects, and the builder pattern is very suitable at this time.
  4. When initializing an object is particularly complex, such as when there are many parameters, and many parameters have default values.

3. Builder pattern UML diagram

write picture description here

4. Builder mode implementation

//计算机抽象类,即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 void setOS(){
        mOS = os;
    }

    @Override
    public String toString() {
        return "Computer [mBoard="+mBoard+",mDisplay="+mDisplay+",mOS="+mOS+"]";
    }
}
//具体的Computer类,Macbook
public class Macbook extends Computer {
    protected Macbook(){}

    @Override
    public void setOS() {
        mOS = "Mac OS X 10.10";
    }
}
//抽象Builder类
public abstract class Builder {
    //设置主机
    public abstract void buildBoard(String board);
    //设置显示器
    public abstract void buildDisplay(String diaplay);
    //设置操作系统
    public abstract void buildOS();
    //创建Comuter
    public abstract Computer create();
}
//具体的Builder类,MacbookBuilder
public class MacbookBuilder extends Builder {
    private Computer mComputer = new Macbook();

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

    @Override
    public void buildDisplay(String diaplay) {
        mComputer.setDisplay(diaplay);
    }

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

    @Override
    public Computer create() {
        return mComputer;
    }
}
//Director类,负责构建Computer
public class Director {
    Builder mBuilder = null;

    /**
     * @param builder
     */
    public Director(Builder builder){
        mBuilder = builder;
    }

    /**
     * 构建对象
     */
    public void construct(String board,String display){
        mBuilder.buildBoard(board);
        mBuilder.buildDisplay(display);
        mBuilder.buildOS();
    }
}
//测试类
public class Test {
    public static void main(String[] args){
        //构造器
        Builder builder = new MacbookBuilder();
        Director pcDirector = new Director(builder);
        //封装构建过程,4核、内存2GB、Mac系统
        pcDirector.construct("英特尔主板","Retina显示器");
        //构建计算机,输出相关信息
        System.out.println("Computer Info:"+builder.create().toString());
    }
}

5. Summary

The Builder pattern is also commonly used in Android development. It is usually used as the builder of the configuration class to separate the construction and presentation of the configuration. It also isolates the configuration from the target class and avoids too many setter methods. The more common form of the Builder pattern is implemented through a call chain, which makes the code more concise and easy to understand.

Advantages
1. Good encapsulation, using the builder pattern can make the client do not need to know the details of the internal composition of the product.
2. The builder is independent and easy to expand.

Disadvantages
It will generate redundant Builder objects and Director objects, consuming memory.

Remarks: Read the notes of "Analysis and Practice of Android Source Design Patterns".

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325860302&siteId=291194637