设计模式-建造者模式(简化-链式)

最近在学习设计模式,之前断断续续的看过好多,自己也没有专门的练习和深入了解过,一直感觉对应设计模式相关的东西还是处于懵懂状态,这次下定决心好好的学习一下,所以做一个笔记用来督促自己。
资料收集与以下网站,如有冒犯,烦请告知

https://www.runoob.com/design-pattern/builder-pattern.html


定义

The intent of the Builder design pattern is to separate the construction of a complex object from its representation. By doing so the same construction process can create different representations.
将一个复杂对象的构建与其表示分离,使得同样的构建过程可以创建不同的表示

简单说明

1.当一个类的构建过于复杂的时候,就可以考虑builder模式了。

上手试一下,还是用coder的例子吧

1.程序员基类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Coder {
private String name;//名称
private String language;//使用的语言
private String coding;//编程能力
private String writeBug;//bug生成能力

public Coder(Builder builder) {
this.name = builder.getName();
this.language = builder.getLanguage();
this.coding = builder.getCoding();
this.writeBug = builder.getWriteBug();
}

public void draw() {
System.out.printf("%s程序员,使用%s编程语言,%s,%s。", name, language, coding, writeBug);
}
}
2.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

public class Builder {
private String name;
private String language;
private String coding;
private String writeBug;

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

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

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

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

public String getName() {
return name;
}

public String getLanguage() {
return language;
}

public String getCoding() {
return coding;
}

public String getWriteBug() {
return writeBug;
}

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

3.使用一下
1
2
3
4
5
6
7
8
9
10
11
public class Test {
public static void main(String[] args) {
Coder coder = new Builder()
.setCoding("写了一行代码")
.setLanguage("Java")
.setName("Android")
.setWriteBug("没有bug")
.build();
coder.draw();
}
}
4.输出信息
1
Android程序员,使用Java编程语言,写了一行代码,没有bug。

换一个写法

1.coder
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
public class Coder {
private String name;
private String language;
private String coding;
private String writeBug;

public Coder(Builder builder) {
this.name = builder.name;
this.language = builder.language;
this.coding = builder.coding;
this.writeBug = builder.writeBug;
}

public void draw() {
System.out.printf("%s程序员,使用%s编程语言,%s,%s。", name, language, coding, writeBug);
}

public static class Builder {
private String name;
private String language;
private String coding;
private String writeBug;

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

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

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

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


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

}
}
2.使用方式
1
2
3
4
5
6
7
8
9
10
11
public class Test {
public static void main(String[] args) {
Coder coder = new Coder.Builder()
.setCoding("写了一行代码")
.setLanguage("Java")
.setName("Android")
.setWriteBug("没有bug")
.build();
coder.draw();
}
}