Skip to content

Commit 1aef348

Browse files
committed
The Builder Pattern
1 parent 1198889 commit 1aef348

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-5
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
### 创建型模式
1616
| 模式 | 描述 | 示例 |
1717
|------------------------------------------------------|------|------|
18-
| [单例 (Singleton)](src/main/java/creational/singleton) | 确保一个类只有一个实例 | 数据库连接池 |
19-
| [工厂方法 (Factory Method)]() | 创建对象而不指定具体类 | UI 跨平台组件创建 |
20-
| [抽象工厂 (Abstract Factory)]() | 创建相关对象族 | 跨平台 UI 组件套件 |
21-
| [建造者 (Builder)]() | 分步构建复杂对象 | SQL 查询构建器 |
22-
| [原型 (Prototype)]() | 通过克隆创建对象 | 游戏角色复制 |
18+
| [单例 (Singleton)](src/main/java/creational/singleton) | 确保一个类只有一个实例 | 数据库连接池 |
19+
| [工厂方法 (Factory Method)]() | 创建对象而不指定具体类 | UI 跨平台组件创建 |
20+
| [抽象工厂 (Abstract Factory)]() | 创建相关对象族 | 跨平台 UI 组件套件 |
21+
| [建造者 (Builder)](src/main/java/creational/builder) | 分步构建复杂对象 | SQL 查询构建器 |
22+
| [原型 (Prototype)]() | 通过克隆创建对象 | 游戏角色复制 |
2323

2424
### 结构型模式
2525
| 模式 | 描述 | 示例 |
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package creational.builder;
2+
3+
public class App {
4+
public static void main(String[] args) {
5+
House seaviewHouse = new House.Builder("Concrete", "Brick", "A")
6+
.hasSwimmingPool(true)
7+
.hasGarage(true)
8+
.hasGarden(true)
9+
.build();
10+
System.out.println(seaviewHouse);
11+
}
12+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package creational.builder;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public class House {
7+
private final String material;
8+
private final String structure;
9+
private final String roof;
10+
private final boolean hasGarage;
11+
private final boolean hasSwimmingPool;
12+
private final boolean hasGarden;
13+
14+
public House(Builder builder) {
15+
this.material = builder.material;
16+
this.structure = builder.structure;
17+
this.roof = builder.roof;
18+
this.hasGarage = builder.hasGarage;
19+
this.hasSwimmingPool = builder.hasSwimmingPool;
20+
this.hasGarden = builder.hasGarden;
21+
}
22+
23+
@Override
24+
public String toString() {
25+
return "House{" +
26+
"material='" + material + '\'' +
27+
", structure='" + structure + '\'' +
28+
", roof='" + roof + '\'' +
29+
", hasGarage=" + hasGarage +
30+
", hasSwimmingPool=" + hasSwimmingPool +
31+
", hasGarden=" + hasGarden +
32+
'}';
33+
}
34+
35+
public static class Builder {
36+
private final String material;
37+
private final String structure;
38+
private final String roof;
39+
private boolean hasGarage;
40+
private boolean hasSwimmingPool;
41+
private boolean hasGarden;
42+
43+
public Builder(String material, String structure, String roof) {
44+
this.material = material;
45+
this.structure = structure;
46+
this.roof = roof;
47+
}
48+
49+
public Builder hasGarage(boolean hasGarage) {
50+
this.hasGarage = hasGarage;
51+
return this;
52+
}
53+
54+
public Builder hasSwimmingPool(boolean hasSwimmingPool) {
55+
this.hasGarage = hasSwimmingPool;
56+
return this;
57+
}
58+
59+
public Builder hasGarden(boolean hasGarden) {
60+
this.hasGarden = hasGarden;
61+
return this;
62+
}
63+
64+
public House build() {
65+
return new House(this);
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)