Builder模式

前言

之前在研究RxJava的时候,就深深的迷上了这种代码调用方式,简洁明了,逻辑也清晰。后台在使用Glide的时候发现这种.调用方法真的很舒服。

附上一段代码:

1
2
3
4
5
Glide.with(this)
.load(imageUrl)
.placeholder(image)
.error(image)
.into(imageView);

但后来才发现,最好的例子就是Android自带的AlertDialog.

1
2
3
4
5
6
7
8
9
new AlertDialog.Builder(MainActivity.this)
.setTitle("One Button")
.setMessage("Thanks for visiting The Code of a Ninja - codeofaninja.com")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {

dialog.cancel();
}
}).show();

实现

现在看一段代码
AlertDialog源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public Person(String name, int sex) {
this(name, sex, 0);
}

public Person(String name, int sex, int age) {
this(name,"",age,sex);
}

public Person(String firstName, String lastName, int age, int sex) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.sex = sex;
}

person有若干个属性,有三个构造方法。倘若某天person增加了几个字段,然后你就啪啪啪的把这几个字段加在了后面

1
2
3
4
5
6
7
public Person(String firstName, String lastName, int age, int sex,String arg1,String arg2) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.sex = sex;
this.arg1 = arg1;
this.arg2 = arg2;

这样看起来就很臃肿,如果参数再多呢,看起来就很难受了。调用的时候还需要去确认每一个位置的参数是什么。
两种方式可以解决:

  1. Set/Get
  2. Builder

Set/Get方法的确可以解决。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class Person {
String name;
String lastName;
...
public Person(Builder builder) {
this.firstName = builder.name;
this.lastName = builder.lastName;
...
}

// 请注意这里返回的是一个Person类而不是void,因为可以链式调用
public Person SetName(String name){
this.name = name;
return this;
}

public String getName(){
return this.name;
}

public Person SetLastName(String lastName){
this.name = lastName;
return this;
}

public String getLastName(){
return this.lastName;
}
}

Builder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public class Person {
String name;
String lastName;
...

public Person(Builder builder) {
this.firstName = builder.name;
this.lastName = builder.lastName;
...
}

// 请注意这里返回的是一个Person类而不是void,因为可以链式调用
public Person SetName(String name){
this.name = name;
return this;
}

public String getName(){
return this.name;
}

public Person SetLastName(String lastName){
this.name = lastName;
return this;
}

public String getLastName(){
return this.lastName;
}

public static class Builder{
String name;
String lastName;
...
public Builder SetName(String name){
this.name = name;
return this;
}

public String getName(){
return this.name;
}

public Builder SetLastName(String lastName){
this.name = lastName;
return this;
}

public String getLastName(){
return this.lastName;
}

public static Builder createBuilder(){
return new Builder();
}

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

调用

1
2
3
4
Person p = Builder.createBuilder()
.SetName("name")
.SetLastName("lastname")
.Build()

例子有很多,但不变的是思想。

-------------本文结束感谢您的阅读-------------
您的支持将是我最大的动力