您的位置:首页 > 产品设计 > UI/UE

小哥哥带你撸设计模式之---Builder模式

2017-07-20 11:46 204 查看

引言

继续走我的设计模式模式的进度条,今天轮到了Builder模式,这个模式吧我不知道该说些什么,它使用的还是挺广的,在码代码的过程中经常可以看到各种builder,可是吧我真没觉得它有多好,只是那种链式调用会显得酷酷的吧,看了一些其他的资料上面介绍的它的优点,但是这些优点使用一般的set和get方法一样可以做到。

介绍

之前有介绍过单例模式,那个单例模式的结果是拿到类的对象,这个Builder模式其实也是为了拿到类的对象,Builder拿到的对象是通过内部类Builder构造出来的,在构造的时候设置设置一些属性,参数等等,而不是new出来的。它将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

使用

代码:test类

public class Test {
private static final String TAG = "test";

private int a;
private int b;
private int c;
private int d;
private int f;
private int g;
private int h;
private String A;
private String B;
private String C;
private String D;
private String E;
private String F;
private String G;
private String H;

private Test(Build build) {
this.a = build.a;
this.b = build.b;
this.c = build.c;
this.d = build.d;
this.f = build.f;
this.g = build.g;
this.h = build.h;
this.A = build.A;
this.B = build.B;
this.C = build.C;
this.D = build.D;
this.E = build.E;
this.F = build.F;
this.G = build.G;
this.H = build.H;
}

public void log(){
Log.d(TAG, "log: a:"+a+",b:"+b+",c:"+c+",d:"+d+",f:"+f+",g:"+g+",h:"+h+",A:"+A+",B:"+B+",C:"+C+",D:"+D+",E:"+E+",F:"+F+",G:"+G+",H:"+H+",");
}
public static class Build {

private int a = 1;
private int b = 2;
private int c = 3;
private int d = 4;
private int f = 5;
private int g = 6;
private int h = 7;
private String A = "A";
private String B = "B";
private String C = "C";
private String D = "D";
private String E = "E";
private String F = "F";
private String G = "G";
private String H = "H";

//在这个方法里才会产生test的实例
public Test build(){
return new Test(this);
}

//这样子的构造方法保证了a和A必然会有值得
public Build(int a, String a1) {
this.a = a;
A = a1;
}

public int getA() {
return a;
}

public int getB() {
return b;
}

public int getC() {
return c;
}

public int getD() {
return d;
}

public String getE() {
return E;
}

public int getF() {
return f;
}

public int getG() {
return g;
}

public int getH() {
return h;
}

public Build setA(int a) {
this.a = a;
return this;
}

public Build setB(int b) {
this.b = b;
return this;
}

public Build setC(int c) {
this.c = c;
return this;
}

public Build setD(int d) {
this.d = d;
return this;
}

public Build setF(int f) {
this.f = f;
return this;
}

public Build setG(int g) {
this.g = g;
return this;
}

public Build setH(int h) {
this.h = h;
return this;
}

public Build setA(String a) {
A = a;
return this;
}

public Build setB(String b) {
B = b;
return this;
}

public Build setC(String c) {
C = c;
return this;
}

public Build setD(String d) {
D = d;
return this;
}

public Build setE(String e) {
E = e;
return this;
}

public Build setF(String f) {
F = f;
return this;
}

public Build setG(String g) {
G = g;
return this;
}

public Build setH(String h) {
H = h;
return this;
}
}

}


代码分析:test类

这部分代码有点长,主要是get、set占了地方,本身这段代码并不复杂。

首先看test类,它有很多的变量,还有一个private构造方法,因此不能被new,他还有一个log方法用来打印日志。

然后它有一个内部类Builder,这个类的所有set方法返回的都是Builder,这是它能够进行链式调用的原因。通过builder的set可以给很多属性赋值,不赋值的属性会使用默认的值,当某些属性必须赋值时,可以在Builder的构造方法中要求使用者强制赋值。

代码:调用test

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Test test = new Test.Build(123, "xxxx").setB(456).setB("++++").build();
test.log();
}
});


代码分析:

在这个button的点击事件中通过Test test = new Test.Build(123, “xxxx”).setB(456).setB(“++++”).build();链式调用产生的Test实例,

有一个与通常的bean类的区别就是,正常情况是new一个对象,然后用set方法设置对象的各种属性,而使用Builder模式后,用户必须先设置一些属性,然后在构建出对象,只有.build();被执行以后才会产生实例。

看一下 test.log();打印出来的结果:



可以看到,a,b,A,B,这几个我设置了值的变量都显示的我设置的值,而没有设置值的比如d,e,f,D,E,F之类都显示的默认值。

Android 中的Builder模式

上代码:

AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("确认清除吗?");
builder.setIcon(R.drawable.contact_dialog_deletecache);
builder.setNegativeButton("取消", null);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
settingClearCache.setOtherText("暂无");
deleteFolderFile(String.valueOf(getActivity().getCacheDir()), true);
}
});
builder.show();


显示出来是这个样子的



这是我之前写的一个AlertDialog对话框,一看到这种builder,这货跑不了的就是Builder模式。接下来看一下它的源码是不是这样子的。



可以看到在在AlertDialog类里面果然有一个Builder的内部类



它的settitle方法跟我的set方法一样也是用来设置属性也是返回this,因为返回的this,才可以链式调用。





这里源码用的show方法,show方法里面调用create(),在create方法里面new的AlertDialog实例,show和create组合起来就相当于我的Builder类里面的build方法,只不过它的实现过程比较复杂罢了。

小结

至此Builder模式就结束了,如果要初始化的对象有特别多的参数,这时候比较合适使用Builder模式,这种建造者模式具有更好的封装性,但是使用起来更复杂,更耗资源的说。本人能力有限,只能了解到这里,说不定还有其他更高深的用法。

本文所使用的代码可以在本人GitHub上找到 快来点我
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: