构建者模式

构建者模式的定义

将一个复杂对象的构建和它的表示分离,使得同样的构建过程可以创建不同的表示。

构建者模式的使用场景

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

构建者模式实现方式(举例)

public class ConnectionConfig {

    private Context context;
    private String ip;
    private int port;
    private int readBufferSize;
    private long connectionTimeout;
    
    private ConnectionConfig(Builder builder){
        this.context = builder.context;
        this.ip = builder.ip;
        this.port = builder.port;
        this.connectionTimeout = builder.connectionTimeout;
        this.readBufferSize = builder.readBufferSize;
    }

    public Context getContext() {
        return context;
    }

    public String getIp() {
        return ip;
    }

    public int getPort() {
        return port;
    }

    public int getReadBufferSize() {
        return readBufferSize;
    }

    public long getConnectionTimeout() {
        return connectionTimeout;
    }

    public static class Builder{
        private Context context;
        private String ip = Constant.SERVER_ADDR_DEFAULT;
        private int port = Constant.TCP_PORT_DEFAULT;
        private int readBufferSize = 1024;
        private long connectionTimeout = 10000;

        public Builder(Context context){
            this.context = context;
        }

        public Builder setIp(String ip){
            this.ip = ip;
            return this;
        }

        public Builder setPort(int port) {
            this.port = port;
            return this;
        }

        public Builder setReadBufferSize(int size) {
            this.readBufferSize = size;
            return this;
        }

        public Builder setConnectionTimeout(long timeout) {
            this.connectionTimeout = timeout;
            return this;
        }

        private void applyConfig(ConnectionConfig config){
            config.context = this.context;
            config.ip = this.ip;
            config.port = this.port;
            config.connectionTimeout = this.connectionTimeout;
            config.readBufferSize = this.readBufferSize;
        }

        public ConnectionConfig builder(){
          /**写法1**/
          //ConnectionConfig config = new ConnectionConfig();
          // applyConfig(config);
          /**写法2**/
          ConnectionConfig config = new ConnectionConfig(this);
          return config;
        }
    }
}

反正就是有一个静态内部类通常叫做Builder,里面包含了外部要创建对象的所有属性,并且在Builder的构造方法或者声明所有属性时赋予默认初始值,然后给Builder添加每个改变自身属性的set方法(随便起其他名字的方法),最后在build或者create方法(或者随便起其他名字)中想办法把Builder中的属性值依次赋值给外部对象属性,上述例子中一个是在外部对象中定义了一个传入参数为Builder的构造方法,另外一个方法是在Builder创建了一个传入外部目标对象参数的applyConfig的方法,然后将自身的属性依次赋值给目标对象。

在Android中的构建者模式

Android的AlertDialog的实现。

多说一下AlertDialog的实现过程:AlertDialog通过构建者一步一步直到create方法调用,是将参数设置给了AlertController(可以粗略的理解为设置给AlertDialog),然后调用show过程会调用AlertDialog的生命周期(包括onCreate、onStart等)去创建并且添加到Window上展示给用户。

show过程:

(1)通过dispatchOnCreate方法来调用AlertDialog的onCreate方法;
(2)然后调用AlertDialog的onStart方法;
(3)最后将Dialog的DecorView添加到WindowManager中
在onCreate中最终其实就是调用了window的setContentView方法,这个和在Activity方法中调用setContentView方法之后也是调用window的setContentView过程是一模一样的。

你可能感兴趣的:(构建者模式)