创建型-建造者模式
# 建造者模式
- 分离复杂对象的构建和表示
- 同样的构建过程可以创建不同的表示
- 构造函数方法为 private,定义 Builder 类,通过一些方法完善对应属性,调用 build 方法创建对象。
# 为什么要使用建造者模式
- 对于一个对象构建参数特别多,如果用 new 可能会把参数传递错误。
- 如果类属性之间有一定的依赖关系,使用 Set 方法,无法进行依赖关系和约束条件校验。
- 需要创建不可变对象,对象创建之后,就不能再修改内部的属性值,因此不能暴露 set 方法。
# 流式建造者模式
public class User {
private final String firstName;
private final String lastName;
private final int age;
private final String phone;
private final String address;
private User(UserBuilder builder) {
this.firstName = builder.firstName;
this.lastName = builder.lastName;
this.age = builder.age;
this.phone = builder.phone;
this.address = builder.address;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public String getPhone() {
return phone;
}
public String getAddress() {
return address;
}
@Override
public String toString() {
return "User{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", age=" + age +
", phone='" + phone + '\'' +
", address='" + address + '\'' +
'}';
}
public static class UserBuilder {
private final String firstName;
private final String lastName;
private int age;
private String phone;
private String address;
public UserBuilder(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public UserBuilder age(int age) {
this.age = age;
return this;
}
public UserBuilder phone(String phone) {
this.phone = phone;
return this;
}
public UserBuilder address(String address) {
this.address = address;
return this;
}
public User builder() {
User user = new User(this);
validateUserObject(user);
return user;
}
private void validateUserObject(User user) {
}
}
}
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 经典建造者模式
public interface IMobilePhoneBuilder {
/**
* 组装电池
* @author HoldDie
* @email HoldDie@163.com
* @date 2018/5/25 10:35
*/
void buildBattery();
/**
* 组装操作系统
* @author HoldDie
* @email HoldDie@163.com
* @date 2018/5/25 10:35
*/
void buildOS();
/**
* 组装屏幕
* @author HoldDie
* @email HoldDie@163.com
* @date 2018/5/25 10:35
*/
void buildScreen();
/**
* 是否触屏
* @author HoldDie
* @email HoldDie@163.com
* @date 2018/5/25 10:36
*/
void buildStylus();
/**
* 获取实体
* @return MobilePhone
* @author HoldDie
* @email HoldDie@163.com
* @date 2018/5/25 10:50
*/
MobilePhone builder();
}
public class AndroidPhoneBuilder implements IMobilePhoneBuilder {
// 手机实体
private MobilePhone mobilePhone;
public AndroidPhoneBuilder() {
this.mobilePhone = new MobilePhone("Android Phone!");
}
/**
* 组装电池
* @author HoldDie
* @email HoldDie@163.com
* @date 2018/5/25 10:35
*/
@Override
public void buildBattery() {
mobilePhone.setPhoneBattery(BatteryType.MAH_1500);
}
/**
* 组装操作系统
* @author HoldDie
* @email HoldDie@163.com
* @date 2018/5/25 10:35
*/
@Override
public void buildOS() {
mobilePhone.setOperationSystemType(OperationSystemType.ANDROID);
}
/**
* 组装屏幕
* @author HoldDie
* @email HoldDie@163.com
* @date 2018/5/25 10:35
*/
@Override
public void buildScreen() {
mobilePhone.setPhoneScreenType(ScreenType.SCREEN_TYPE_TOUCH_RESISTIVE);
}
/**
* 是否触屏
* @author HoldDie
* @email HoldDie@163.com
* @date 2018/5/25 10:36
*/
@Override
public void buildStylus() {
mobilePhone.setStylusType(StylusType.YES);
}
/**
* 获取实体
* @return MobilePhone
* @author HoldDie
* @email HoldDie@163.com
* @date 2018/5/25 10:50
*/
@Override
public MobilePhone builder() {
return this.mobilePhone;
}
}
public class WindowsPhoneBuilder implements IMobilePhoneBuilder {
private MobilePhone mobilePhone;
public WindowsPhoneBuilder() {
this.mobilePhone = new MobilePhone("Windows Phone");
}
/**
* 组装电池
* @author HoldDie
* @email HoldDie@163.com
* @date 2018/5/25 10:35
*/
@Override
public void buildBattery() {
mobilePhone.setPhoneBattery(BatteryType.MAH_1000);
}
/**
* 组装操作系统
* @author HoldDie
* @email HoldDie@163.com
* @date 2018/5/25 10:35
*/
@Override
public void buildOS() {
mobilePhone.setOperationSystemType(OperationSystemType.WINDOWS_PHONE);
}
/**
* 组装屏幕
* @author HoldDie
* @email HoldDie@163.com
* @date 2018/5/25 10:35
*/
@Override
public void buildScreen() {
mobilePhone.setPhoneScreenType(ScreenType.SCREEN_TYPE_TOUCH_CAPACITIVE);
}
/**
* 是否触屏
* @author HoldDie
* @email HoldDie@163.com
* @date 2018/5/25 10:36
*/
@Override
public void buildStylus() {
mobilePhone.setStylusType(StylusType.YES);
}
/**
* 获取实体
* @return MobilePhone
* @author HoldDie
* @email HoldDie@163.com
* @date 2018/5/25 10:50
*/
@Override
public MobilePhone builder() {
return this.mobilePhone;
}
}
public class Manufacturer {
public void construct(IMobilePhoneBuilder phoneBuilder){
phoneBuilder.buildBattery();
phoneBuilder.buildOS();
phoneBuilder.buildScreen();
phoneBuilder.buildStylus();
}
}
@Data
public class MobilePhone {
private String phoneName;
private ScreenType phoneScreenType;
private BatteryType phoneBattery;
private OperationSystemType operationSystemType;
private StylusType stylusType;
public MobilePhone(String s) {
this.phoneName = s;
}
}
public static void main(String[] args) {
System.out.println("Hello World!");
Manufacturer manufacturer = new Manufacturer();
// 构建一个安卓系统
IMobilePhoneBuilder androidPhoneBuilder = new AndroidPhoneBuilder();
manufacturer.construct(androidPhoneBuilder);
System.out.println(androidPhoneBuilder.builder().getOperationSystemType());
// 构建一个 windows 系统
WindowsPhoneBuilder windowsPhoneBuilder = new WindowsPhoneBuilder();
manufacturer.construct(windowsPhoneBuilder);
System.out.println(windowsPhoneBuilder.builder().getPhoneName());
}
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# 和工厂模式的区别
- 两者主要是在创建对象的的关注点上不同。
- 工厂模式:用来创建不同但是相关类型的对象(继承同一父类或者接口的一组子类),由给定的参数来决定创建哪种类型的对象。
- 建造者模式:创建一种类型的复杂对象,通过设置不同的可选参数,“定制化”地创建不同的对象。
上次更新: 2020/09/08, 15:09:00