Skip to content

Commit d2bc7e3

Browse files
committed
增加创建者模式
1 parent 11f5359 commit d2bc7e3

File tree

5 files changed

+287
-0
lines changed

5 files changed

+287
-0
lines changed

builder/pom.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
MIT License
5+
6+
Copyright (c) 2017 James
7+
8+
Permission is hereby granted, free of charge, to any person obtaining a copy
9+
of this software and associated documentation files (the "Software"), to deal
10+
in the Software without restriction, including without limitation the rights
11+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
copies of the Software, and to permit persons to whom the Software is
13+
furnished to do so, subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in all
16+
copies or substantial portions of the Software.
17+
18+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
SOFTWARE.
25+
26+
-->
27+
<project xmlns="http://maven.apache.org/POM/4.0.0"
28+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
29+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
30+
<parent>
31+
<artifactId>java_design_patterns</artifactId>
32+
<groupId>me.zbl</groupId>
33+
<version>1.11.9-SNAPSHOT</version>
34+
</parent>
35+
<modelVersion>4.0.0</modelVersion>
36+
37+
<artifactId>builder</artifactId>
38+
39+
40+
</project>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* MIT License
3+
* <p>
4+
* Copyright (c) 2017 James
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
* <p>
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package me.zbl.builder;
25+
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
/**
30+
* 创建者模式
31+
*/
32+
public class Application {
33+
34+
private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
35+
36+
public static void main(String[] args) {
37+
38+
Person personWang = new Person.Builder().name("小王").age(25).nationality(Nationality.CHINA).skinColor(SkinColor.YELLOW).build();
39+
Person personZhang = new Person.Builder().name("小张").age(28).nationality(Nationality.USA).skinColor(SkinColor.WHITE).build();
40+
Person personLiu = new Person.Builder().name("老王").age(48).nationality(Nationality.JAPAN).skinColor(SkinColor.YELLOW).build();
41+
42+
LOGGER.info(personWang.toString());
43+
LOGGER.info(personZhang.toString());
44+
LOGGER.info(personLiu.toString());
45+
46+
}
47+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* MIT License
3+
* <p>
4+
* Copyright (c) 2017 James
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
* <p>
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package me.zbl.builder;
25+
26+
/**
27+
* 国籍
28+
*/
29+
public enum Nationality {
30+
31+
CHINA("中国"), RUSSIA("俄罗斯"), USA("美国"), JAPAN("日本"), UK("英国");
32+
33+
private String name;
34+
35+
Nationality(String name) {
36+
this.name = name;
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return name;
42+
}
43+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* MIT License
3+
* <p>
4+
* Copyright (c) 2017 James
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
* <p>
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package me.zbl.builder;
25+
26+
/**
27+
* 人
28+
*/
29+
public final class Person {
30+
31+
private final String name;
32+
private final Integer age;
33+
private final Nationality nationality;
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public Integer getAge() {
40+
return age;
41+
}
42+
43+
public Nationality getNationality() {
44+
return nationality;
45+
}
46+
47+
public SkinColor getSkinColor() {
48+
return skinColor;
49+
}
50+
51+
private final SkinColor skinColor;
52+
53+
public Person(Builder builder) {
54+
this.name = builder.name;
55+
this.age = builder.age;
56+
this.skinColor = builder.skinColor;
57+
this.nationality = builder.nationality;
58+
}
59+
60+
@Override
61+
public String toString() {
62+
StringBuilder sb = new StringBuilder();
63+
sb.append(name);
64+
if (null != nationality) {
65+
sb.append(" 是来自 " + nationality.toString() + " 的");
66+
}
67+
if (null != age) {
68+
sb.append(" " + age + "岁的");
69+
}
70+
if (null != skinColor) {
71+
sb.append(" 有着" + skinColor + "皮肤的");
72+
}
73+
sb.append("一个人");
74+
return sb.toString();
75+
}
76+
77+
/**
78+
* 创建者
79+
*/
80+
public static class Builder {
81+
82+
private String name;
83+
private Integer age;
84+
private Nationality nationality;
85+
private SkinColor skinColor;
86+
87+
public Builder age(Integer age) {
88+
this.age = age;
89+
return this;
90+
}
91+
92+
public Builder name(String name) {
93+
if (null == name) {
94+
throw new IllegalArgumentException("人必须有名字!");
95+
}
96+
this.name = name;
97+
return this;
98+
}
99+
100+
public Builder nationality(Nationality nationality) {
101+
this.nationality = nationality;
102+
return this;
103+
}
104+
105+
public Builder skinColor(SkinColor skinColor) {
106+
this.skinColor = skinColor;
107+
return this;
108+
}
109+
110+
public Person build() {
111+
return new Person(this);
112+
}
113+
}
114+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* MIT License
3+
* <p>
4+
* Copyright (c) 2017 James
5+
* <p>
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
* <p>
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
* <p>
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package me.zbl.builder;
25+
26+
/**
27+
* 肤色
28+
*/
29+
public enum SkinColor {
30+
31+
YELLOW("黄色"), BLACK("黑色"), WHITE("白色");
32+
33+
private String color;
34+
35+
SkinColor(String color) {
36+
this.color = color;
37+
}
38+
39+
@Override
40+
public String toString() {
41+
return color;
42+
}
43+
}

0 commit comments

Comments
 (0)