建造者模式是运用于构建复杂对象时的一种设计模式

经典模式

经典的Builder模式分为以下四个角色:

  1. 要建造的产品Product
  2. 抽象的Builder
  3. Builder的具体实现ConcreteBuilder
  4. 使用者Director

Product

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
public class Computer {
/*CPU*/
private String CPU;
/*内存*/
private String memory;
/*硬盘*/
private String hardDisk;
/*键盘*/
private String keyboard;
/*鼠标*/
private String mouse;

public String getCPU() {
return CPU;
}

public void setCPU(String CPU) {
this.CPU = CPU;
}

public String getMemory() {
return memory;
}

public void setMemory(String memory) {
this.memory = memory;
}

public String getHardDisk() {
return hardDisk;
}

public void setHardDisk(String hardDisk) {
this.hardDisk = hardDisk;
}

public String getKeyboard() {
return keyboard;
}

public void setKeyboard(String keyboard) {
this.keyboard = keyboard;
}

public String getMouse() {
return mouse;
}

public void setMouse(String mouse) {
this.mouse = mouse;
}

@Override
public String toString() {
return "Computer{" +
"CPU='" + CPU + '\'' +
", memory='" + memory + '\'' +
", hardDisk='" + hardDisk + '\'' +
", keyboard='" + keyboard + '\'' +
", mouse='" + mouse + '\'' +
'}';
}
}

Builder

1
2
3
4
5
6
7
8
public interface ComputerConfigBuilder {
void setCPU();
void setMemery();
void setHardDisk();
void setKeyboard();
void setMouse();
Computer getComputer();
}

ConcreteBuilder

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
public class LowConfigBuilder implements ComputerConfigBuilder {

private Computer mComputer;

public LowConfigBuilder(){
this.mComputer = new Computer();
}

@Override
public void setCPU() {
mComputer.setCPU("i5");
}

@Override
public void setMemery() {
mComputer.setMemory("8G");
}

@Override
public void setHardDisk() {
mComputer.setHardDisk("500G");
}

@Override
public void setKeyboard() {
mComputer.setKeyboard("薄膜键盘");
}

@Override
public void setMouse() {
mComputer.setMouse("有线鼠标");
}

@Override
public Computer getComputer() {
return mComputer;
}
}
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
public class HighConfigBuider implements ComputerConfigBuilder {

private Computer mComputer;

public HighConfigBuider(){
this.mComputer = new Computer();
}

@Override
public void setCPU() {
mComputer.setCPU("i7");
}

@Override
public void setMemery() {
mComputer.setMemory("16G");
}

@Override
public void setHardDisk() {
mComputer.setHardDisk("1T");
}

@Override
public void setKeyboard() {
mComputer.setKeyboard("机械键盘");
}

@Override
public void setMouse() {
mComputer.setMouse("无线鼠标");
}

@Override
public Computer getComputer() {
return mComputer;
}
}

Director

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Director {
private ComputerConfigBuilder mBuilder;
public void setBuilder(ComputerConfigBuilder builder){
this.mBuilder = builder;
}
public void createComputer(){
mBuilder.setCPU();
mBuilder.setMemery();
mBuilder.setHardDisk();
mBuilder.setKeyboard();
mBuilder.setMouse();
}
public Computer getComputer(){
return mBuilder.getComputer();
}
}

如上所示,Director使用不同的Builder即可构造出完全不同的Product

变种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
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
public class Person {
/*名字*/
private String name;
/*性别*/
private final String gender;
/*年龄*/
private final String age;
/*鞋子*/
private final String shoes;
/*衣服*/
private final String clothes;
/*钱*/
private final String money;
/*房子*/
private final String house;
/*汽车*/
private final String car;
/*职业*/
private final String career;


private Person(Builder builder) {
this.name = builder.name;
this.gender = builder.gender;
this.age = builder.age;
this.shoes = builder.shoes;
this.clothes = builder.clothes;
this.money = builder.money;
this.house = builder.house;
this.car = builder.car;
this.career = builder.career;
}

public static class Builder {
private String name;
private String gender;
private String age;
private String shoes;
private String clothes;
private String money;
private String house;
private String car;
private String career;

public Builder(String name,String gender) {
this.name = name;
this.gender = gender;
}

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

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

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

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

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

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

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

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

使用构造者来构造对象:

1
2
3
4
5
Person person = new Person.Builder("张三","男")
.age("12")
.money("1000000")
.car("宝马")
.build();