设计模式(八)——建造者模式

建造者模式

讲一个复杂构建与他的表示分离,使得同样的构建可以创建不同的表示。

优点

  • 封装性:客户端不必知道产品内部组成的细节。
  • 建造者独立,容易扩展。例子中,惠普和苹果建造者互相独立。
  • 便于控制细节风险。

应用场景

  • 需要生成的产品对象有复杂的内部结构,这些产品对象具备共性。
  • 隔离复杂对象的创建和使用,并使得形同的创建过程可以创建不同的产品。
  • 相同方法,不同的执行顺序。

代码

GitHUb

新建一个产品类

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
public class Computer {

private String CPU;

private String capacity;

private String ram;

private String system;

public String getCPU() {
return CPU;
}

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

public String getCapacity() {
return capacity;
}

public void setCapacity(String capacity) {
this.capacity = capacity;
}

public String getRam() {
return ram;
}

public void setRam(String ram) {
this.ram = ram;
}

public String getSystem() {
return system;
}

public void setSystem(String system) {
this.system = system;
}

@Override
public String toString() {
return "Computer{" +
"CPU='" + CPU + '\'' +
", capacity='" + capacity + '\'' +
", ram='" + ram + '\'' +
", system='" + system + '\'' +
'}';
}
}

建造者接口:

1
2
3
4
5
6
7
8
9
10
11
public interface IBuildComputer {
void buildCPU();

void buildCapacity();

void buildRam();

void buildSystem();

Computer createComputer();
}

具体建造者:

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
public class HPComputer implements IBuildComputer {
private Computer computer;

public HPComputer(){
computer = new Computer();
}

@Override
public void buildCPU() {
computer.setCPU("惠普cpu");
}

@Override
public void buildCapacity() {
computer.setCapacity("560G");
}

@Override
public void buildRam() {
computer.setRam("16G");
}

@Override
public void buildSystem() {
computer.setSystem("windows10");
}

@Override
public Computer createComputer() {
return computer;
}
}



public class AppleComputer implements IBuildComputer {
private Computer computer;

public AppleComputer(){
computer = new Computer();
}

@Override
public void buildCPU() {
computer.setCPU("苹果cpu");
}

@Override
public void buildCapacity() {
computer.setCapacity("256G");
}

@Override
public void buildRam() {
computer.setRam("16G");
}

@Override
public void buildSystem() {
computer.setSystem("macOS");
}

@Override
public Computer createComputer() {
return computer;
}
}

Director导演类,负责安排已有模块顺序:

1
2
3
4
5
6
7
8
9
10
public class Director {

public Computer createComputerByDirector(IBuildComputer computer){
computer.buildCapacity();
computer.buildCPU();
computer.buildRam();
computer.buildSystem();
return computer.createComputer();
}
}

测试

1
2
3
4
5
6
7
8
9
10
11
public class Main {
public static void main(String[] args){

Director director = new Director();
Computer hp = director.createComputerByDirector(new HPComputer());
System.out.println(hp.toString());
System.out.println("-----------------------");
Computer apple = director.createComputerByDirector(new AppleComputer());
System.out.println(apple.toString());
}
}

结果

1
2
3
Computer{CPU='惠普cpu', capacity='560G', ram='16G', system='windows10'}
-----------------------
Computer{CPU='苹果cpu', capacity='256G', ram='16G', system='macOS'}

欢迎关注

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
显示 Gitment 评论