-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.cpp
80 lines (65 loc) · 1.91 KB
/
builder.cpp
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
//将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
//https://blog.csdn.net/naibozhuan3744/article/details/108230453
#include <iostream>
#include <string>
using namespace std;
#define DELETE(pointer) delete pointer; pointer=nullptr
class IProduct { //抽象产品类
public:
virtual string& getName() = 0;
virtual void show() = 0;
};
class Product1 : public IProduct { //具体产品类
public:
string& getName() override { return name; }
void show() override { cout << "name==" << name << endl; }
private:
string name; //要构建的具体产品名字(分为多个步骤才能构建完毕)
};
class IBuilder
{
public:
IBuilder() {}
virtual ~IBuilder() {}
virtual void buildPart1() = 0;
virtual void buildPart2() = 0;
virtual IProduct* getProduuct() = 0;
};
class ConcreteBuilder : public IBuilder //构建具体结构,细节的具体实现或者说表示层
{
public:
ConcreteBuilder(IProduct* product) : product(product) {}
~ConcreteBuilder() {}
void buildPart1() override { product->getName().append("buildPart1+"); }
void buildPart2() override { product->getName().append("buildPart2"); }
IProduct* getProduuct() override { return product; }
private:
IProduct* product;
};
class Director //构建者,用表示层的细节实现接口函数按照一定逻辑组合成某一款产品
{
public:
Director() {}
~Director() {}
IProduct* construct(IBuilder* builder) { //传入指针唯一目的是对该对象进行构建,构建前后指针地址一直不变
builder->buildPart1();
builder->buildPart2();
return builder->getProduuct();
}
};
void doBuilderPattern()
{
IProduct* product = new Product1(); //创建一个初始化的产品
IBuilder* builder = new ConcreteBuilder(product); //创建构造器,传入产品对象地址准备构造具体产品和功能
Director().construct(builder); //构建具体产品和功能
product->show();
DELETE(builder);
DELETE(product);
}
//int main()
//{
// doBuilderPattern();
//
// system("pause");
// return 1;
//}