建造者模式(Builder Pattern)
# 建造者模式(Builder Pattern)
# 概念
建造者模式是一种创建型设计模式,它允许通过将对象的构造过程分离成多个步骤来创建复杂的对象。通过使用相同的构造过程,可以构建不同的表示形式,使得构造过程更加灵活。
# 思想
建造者模式的思想是将一个复杂对象的构建过程与其表示分离,使得同样的构建过程可以创建不同的表示形式。通过定义一个独立的"建造者"类,该类封装了对象的构建过程,并提供方法来设置对象的各个部分,最后通过调用建造者的方法来构建对象。
# 角色
- Director(指导者): 负责调用建造者的方法来构建对象,它知道构建的顺序和步骤。
- Builder(建造者): 定义构建对象各个部分的抽象接口,并提供设置这些部分的方法。
- ConcreteBuilder(具体建造者): 实现建造者接口,负责具体的构建过程,以及返回构建后的对象。
- Product(产品): 表示被构建的复杂对象,包含多个部分。
# 优点
- 将复杂对象的构建过程与其表示分离,使得构建过程更加灵活。
- 可以使用相同的构建过程创建不同的表示形式。
- 使代码结构清晰,易于扩展和维护。
# 缺点
- 建造者模式会增加代码的复杂度,因为需要定义多个类来实现对象的构建过程。
# 类图
@startuml
class Director {
+ Construct(): void
}
interface Builder {
+ BuildPartA(): void
+ BuildPartB(): void
+ GetResult(): Product
}
class ConcreteBuilder1 {
+ BuildPartA(): void
+ BuildPartB(): void
+ GetResult(): Product
}
class ConcreteBuilder2 {
+ BuildPartA(): void
+ BuildPartB(): void
+ GetResult(): Product
}
class Product {
+ SetPartA(partA: string): void
+ SetPartB(partB: string): void
+ Show(): void
}
Director --> Builder
Director --> ConcreteBuilder1
Director --> ConcreteBuilder2
Builder ..> Product
ConcreteBuilder1 ..|> Builder
ConcreteBuilder2 ..|> Builder
Product --> "1" partA: string
Product --> "1" partB: string
@enduml
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
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
# 示例代码(C++)
#include <iostream>
#include <string>
// Product
class Pizza {
public:
void SetDough(const std::string& dough) {
dough_ = dough;
}
void SetTopping(const std::string& topping) {
topping_ = topping;
}
void SetSauce(const std::string& sauce) {
sauce_ = sauce;
}
void Show() {
std::cout << "Pizza with " << dough_ << " dough, " << topping_ << " topping, and " << sauce_ << " sauce." << std::endl;
}
private:
std::string dough_;
std::string topping_;
std::string sauce_;
};
// Builder
class PizzaBuilder {
public:
virtual void BuildDough() = 0;
virtual void BuildTopping() = 0;
virtual void BuildSauce() = 0;
virtual Pizza* GetPizza() = 0;
};
// ConcreteBuilder1
class HawaiianPizzaBuilder : public PizzaBuilder {
public:
void BuildDough() override {
pizza_ = new Pizza();
pizza_->SetDough("thin crust");
}
void BuildTopping() override {
pizza_->SetTopping("ham and pineapple");
}
void BuildSauce() override {
pizza_->SetSauce("tomato");
}
Pizza* GetPizza() override {
return pizza_;
}
private:
Pizza* pizza_;
};
// ConcreteBuilder2
class SpicyPizzaBuilder : public PizzaBuilder {
public:
void BuildDough() override {
pizza_ = new Pizza();
pizza_->SetDough("thick crust");
}
void BuildTopping() override {
pizza_->SetTopping("pepperoni and jalapeno");
}
void BuildSauce() override {
pizza_->SetSauce("spicy tomato");
}
Pizza* GetPizza() override {
return pizza_;
}
private:
Pizza* pizza_;
};
// Director
class Waiter {
public:
void Construct(PizzaBuilder* builder) {
builder->BuildDough();
builder->BuildTopping();
builder->BuildSauce();
}
};
int main() {
Waiter waiter;
HawaiianPizzaBuilder hawaiianBuilder;
waiter.Construct(&hawaiianBuilder);
Pizza* hawaiianPizza = hawaiianBuilder.GetPizza();
hawaiianPizza->Show();
SpicyPizzaBuilder spicyBuilder;
waiter.Construct(&spicyBuilder);
Pizza* spicyPizza = spicyBuilder.GetPizza();
spicyPizza->Show();
delete hawaiianPizza;
delete spicyPizza;
return 0;
}
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 代码运行结果
Pizza with thin crust dough, ham and pineapple topping, and tomato sauce.
Pizza with thick crust dough, pepperoni and jalapeno topping, and spicy tomato sauce.
1
2
2
编辑 (opens new window)
上次更新: 2023/06/09, 13:17:31
- 01
- Linux系统移植(五)--- 制作、烧录镜像并启动Linux02-05
- 03
- Linux系统移植(三)--- Linux kernel移植02-05