一、介绍
定义
Builder(建造者模式):将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
使用场景
- 相同的方法,不同的执行顺序,产生不同的事件结果时;
- 多个部件或零件,都可以装配到一个对象中,但是产生的运行结果又不相同时;
- 产品类非常复杂,或者产品类中的调用顺序不同产生了不同的效能,这个时候使用建造者模式非常合适;
其实我只是感觉这个样子会方便一点(链式调用),至于定义(网上找的)也不怎么很理解
二、用一个Bean做示例代码
用一个JavaBean做示例代码,完了之后有一个封装HttpURLConnection的小例子(仅用来讨论Builder模式,自动忽略其他内容,捂脸)
给Student增加了一个静态内部类Builder,并修改了Student的构造函数
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
62
63
64
65
66
67
68
69
70
71
72public class Student {
private String id;
private String name;
private String age;
// 将公有改为私有,传递一个Builder对象
private Student(Builder builder) {
this.id = builder.id;
this.name = builder.name;
this.age = builder.age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String toString() {
return "Student{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
// Builder内部类
static class Builder{
private String id;
private String name;
private String age;
public Builder setId(String id) {
this.id = id;
return this;
}
public Builder setName(String name) {
this.name = name;
return this;
}
public Builder setAge(String age) {
this.age = age;
return this;
}
public Student build(){
return new Student(this);
}
}
}使用刚刚创建的这个类
1
2
3
4
5
6
7// 成员变量可以为null
Student student = new Student.Builder()
.setId("01")
.setName("name")
.build();
Log.e("tag", student.toString());总结:
- 定义一个静态内部类Builder,内部的成员变量和外部类一样
- Builder类通过一系列的方法用于成员变量的赋值,并返回当前对象本身(this)
- Builder类提供一个build方法或者create方法用于创建对应的外部类,该方法内部调用了外部类的一个私有构造函数,该构造函数的参数就是内部类Builder
- 外部类提供一个私有构造函数供内部类调用,在该构造函数中完成成员变量的赋值,取值为Builder对象中对应的值
参考链接:http://blog.csdn.net/jie1991liu/article/details/49640725
三、小Demo(有点low)
封装
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80public class HttpUtils {
// 自定义监听器
private onHttpListener mOnHttpListener;
private HttpUtils(Builder builder){
this.mOnHttpListener = builder.mOnHttpListener;
// 调用doPost方法请求数据
doPost(builder.urlStr,builder.param);
}
public void doPost(final String urlStr, final String param) {
new Thread(new Runnable() {
public void run() {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(urlStr);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(param);
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
InputStream in = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
if(connection.getResponseCode() == 200){
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
mOnHttpListener.onSuccess(builder.toString());
} else {
mOnHttpListener.onError(connection.getResponseCode());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
// Builder类
static class Builder{
private onHttpListener mOnHttpListener;
private String urlStr;
private String param;
public Builder setOnHttpListener(onHttpListener mOnHttpListener){
this.mOnHttpListener = mOnHttpListener;
return this;
}
public Builder doPost(String urlStr, String param){
this.urlStr = urlStr;
this.param = param;
return this;
}
public HttpUtils build(){
return new HttpUtils(this);
}
}
// 自定义监听器
public interface onHttpListener {
void onSuccess(String response);
void onError(int code);
}
}使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14new HttpUtils.Builder()
.doPost(path1, "{'BusStationId':1}")
.setOnHttpListener(new HttpUtils.onHttpListener() {
public void onSuccess(String response) {
Log.e("test", response);
}
public void onError(int code) {
}
})
.build();