构造者模式

1、定义:

将一个复杂对象的创建过程从它的表示分离,使得同样的构建过程可以创建不同的表示;
是一个很抽象,看不懂的概念,但是Android中经常能用到,比如AlertDialog。AlertDialog把对话框的构建和表示分离开来,使得同样的构建过程可以创建不同的表示。

2、自定义构造者模式

我们通过一个简单的例子,理解一下这个模式。

2.1、普通的实体类

创建一个实体类Person,有id、name、sex等属性,为了给属性赋值,通常的做法就是写get/set方法,或者通过重载他的的构造方法,如果需要的参数不固定的话,可能会有多个构造方法,那么在创建对象的时候也同样麻烦,可能需要传入不同的参数。

public class Person {
    private static final String TAG = "Person";
    private int id;
    private String name;
    private String sex;
    private int height;
    private String occupation;

    public Person(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public Person(int id, String name, String sex) {
        this.id = id;
        this.name = name;
        this.sex = sex;
    }

    public Person(int id, String name, String sex, int height, String occupation) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.height = height;
        this.occupation = occupation;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public String getOccupation() {
        return occupation;
    }

    public void setOccupation(String occupation) {
        this.occupation = occupation;
    }
}

创建对象时:

Person person1 = new Person(1,"zjf");
Person person2 = new Person(1,"zjf","male");
Person person3 = new Person(1,"zjf","male",180,"coder");

2.2、Builder的实体类

还是上面的Person类,首先创建一个静态Builder类,在Builder类里定义了一份与Person类一模一样的变量,通过一系列的成员函数进行设置属性值,但是返回值都是this,也就是都是Builder对象,最后提供了一个build函数用于创建Person对象,返回的是Person对象,对应的构造函数在Person类中进行定义,也就是构造函数的入参是Builder对象,然后依次对自己的成员变量进行赋值,对应的值都是Builder对象中的值。此外Builder类中的成员函数返回Builder对象自身的另一个作用就是让它支持链式调用,使代码可读性大大增强。

public class Person {
    private static final String TAG = "Person";
    private int id;
    private String name;
    private String sex;
    private int height;
    private String occupation;

    private Person(Builder builder) {
        this.id = builder.id;
        this.name = builder.name;
        this.sex = builder.sex;
        this.height = builder.height;
        this.occupation = builder.occupation;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public String getOccupation() {
        return occupation;
    }

    public void setOccupation(String occupation) {
        this.occupation = occupation;
    }


    static class Builder {
        private int id;
        private String name;
        private String sex;
        private int height;
        private String occupation;

        public Builder id(int id) {
            this.id = id;
            return this;
        }
        public Builder name(String name){
            this.name=name;
            return this;
        }
        public Builder sex(String sex){
            this.sex=sex;
            return this;
        }
        public Builder height(int height){
            this.height=height;
            return this;
        }

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

        public Person build(){
            return new Person(this);
        }
    }
}

创建对象时:

Person.Builder builder = new Person.Builder();
Person person1 = builder.id(1).name("zjf").build();
Person person2 = builder.id(1).name("zjf").sex("male").build();
Person person3 = builder.id(1).name("zjf").sex("male").occupation("coder").build();

2.3、AlertDialog.Builder

我们再对比一下构造AlertDialog的方法,相信对于构造者模式理解的会更深一些。

AlertDialog.Builder builder=new AlertDialog.Builder(this);
AlertDialog dialog=builder.setTitle("标题")
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setView(R.layout.myview)
        .setPositiveButton(R.string.positive, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
 
            }
        })
        .setNegativeButton(R.string.negative, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
 
            }
        })
        .create();
dialog.show();

3、总结

各大框架中大量的运用了Builder模式。最后总结一下步骤;
1、定义一个静态内部类Builder,内部的成员变量和外部类一样
2、Builder类通过一系列的方法用于成员变量的赋值,并返回当前对象本身(this)
3、Builder类提供一个build方法或者create方法用于创建对应的外部类,该方法内部调用了外部类的一个私有构造函数,该构造函数的参数就是内部类Builder
4、外部类提供一个私有构造函数供内部类调用,在该构造函数中完成成员变量的赋值,取值为Builder对象中对应的值