设计模式
Nevermore 2024-01-13 Lang
# 面向对象设计原则
- 依赖倒置原则(DIP):高层模块应依赖于抽象(稳定);实现细节应该依赖于抽象。
- 开放封闭原则(OCP):对扩展开放,对更改封闭。
- 单一责任原则(SRP):一个类仅有一个引起变化的原因,变化的方向应包含类的责任
- Liskov替换原则(LSP):子类必须能替换它们的基类
- 接口隔离原则(ISP):不应该强迫客户依赖不用的方法,使用protect或private保护接口
- 优先使用组合
- 封装变化点:将不稳定的进行封装,实现稳定和不稳定的隔离
- 针对接口编程:将变量声明为接口,用户不用知道变量的具体类型
# 模式分类
- 创建型:对象创建
- 结构型:应对需求变化对结构的冲击
- 行为型:应对多个类交互
组件协作:Template Method; Strategy; Observer/Event
单一职责:Decorator; Bridge
对象创建:Factory Method; Abstract Factory; Prototype; Builder
对象性能:Singleton; Flyweight
接口隔离:Facade; Proxy; Mediator; Adapter
状态变化:Memento; State
数据结构:Composite; Iterator; Chain of Responsibility
行为变化:Command; Visitor
领域问题:Interpreter
# 1.模板方法Template Method
- 当所有步骤都是稳定或是不稳定的时候,设计模式不能被使用。
- 在变化点应用设计模式 ,在稳定和不稳定的隔离点使用设计模型来分离变化
class BaseMethod{
public:
//相对稳定,程序主流程
void Run{
step1;
step2;
step3;
}
virtual ~BaseMethod(){} // 一定要写虚析构,否则delete多态的对象会有问题
protected:
void step1() {} //稳定
void step2() {} //稳定
virtual void step3() = 0; //不稳定,用户自己实现
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 2.*策略模式Strategy
- 可以改写含有许多条件判断的语句
class CalStrategy {
public:
virtual void cal() = 0;
virtual ~CalStrategy() {}
};
class ACal : public CalStrategy {
public:
virtual void cal() override { std::cout << "执行了加法" << std::endl; } //子类实现不同的策略方式
};
class BCal : public CalStrategy {
public:
virtual void cal() override { std::cout << "执行了乘法" << std::endl; }
};
int main()
{
CalStrategy* strategy = new ACal;
strategy->cal();
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 3.*观察者模式Observer
- 当一个对象的状态发生改变,让依赖于它的所有对象得到通知,并进行相应的处理
class Observer{
public:
virtual void ExcuteObserver() = 0;
virtual ~Observer(){}
}
class Usage{ //保证了程序的复用性
Observer* m_info; //抽象通知
//vector<Observer*> m_infoList //建立多个观察者
public:
Usage(Observer* info):m_info(info)
{}
useSomething() {
m_info->ExcuteObserver(); //携带通知信息自动传播
//...
}
}
class Worker:public MainProgress, public Observer //多继承:主继承+一堆接口继承(抽象基类)
{
public:
void Use() {
Usage useage(this); //将本对象地址传回,使得Usage对象能够使用本对象重写的虚函数;订阅通知
useage.useSomething();
}
virtual void ExcuteObserver() {
// TrueAchieve... 用户自行实现
}
}
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
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
# 4.装饰模式Decorator
- 在某现有操作上进行其他类型的操作,如文件创建后的加密等
- 不需要在定义人物时对人物的穿着进行具体描述,在有需要的时候再添加。
class NakedPerson {
public:
NakedPerson () {}
NakedPerson(std::string name):m_name(name) {}
virtual ~NakedPerson() {}
std::string GetName() {
return m_name;
}
virtual void ShowInfo() = 0;
protected:
std::string m_name;
};
class Zhangsan : public NakedPerson {
public:
using NakedPerson::NakedPerson; //使用父类的构造函数
virtual void ShowInfo() {
std::cout << "张三的身体" << std::endl;
}
};
class Cloth : public NakedPerson { // 抽象类,执行穿衣服的操作
public:
void WearCloth(NakedPerson* nPerson) {
m_nPerson = nPerson;
}
protected:
NakedPerson* m_nPerson = nullptr;
};
class Shirt : public Cloth { //重写虚函数的方法
public:
virtual void ShowInfo() {
m_nPerson->ShowInfo();
std::cout << m_nPerson->GetName() << ":" << "穿上了衬衫" << std::endl;
}
};
class Pants : public Cloth {
public:
virtual void ShowInfo() {
m_nPerson->ShowInfo();
std::cout << m_nPerson->GetName() << ":" << "穿上了裤子" << std::endl;
}
};
int main()
{
NakedPerson* nPerson = new Zhangsan("小名二狗");
Cloth* shirt = new Shirt;
Cloth* pants = new Pants;
shirt->WearCloth(nPerson);
pants->WearCloth(shirt); //穿了衣服后再穿裤子
pants->ShowInfo();
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
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
# 5.桥模式Bridge
将实现的具体拆分成独立运作的模块(分离抽象的和具体实现)
构建一个想象的花园(Organization),需要植物、人等(Object),可以在一个名为花园的大类中定义花园中可能存在的所有成员,但是需要在这个类中对成员的动作和花园的结构进行描述,会造成这个类变得臃肿。通过桥将花园里的物和花园这个框架建立关联,对成员的动作和花园的描述分别进行管理。
struct DescribeObj //具体存在物的信息数据结构
{
DescribeObj(int id, std::string name, std::string OtherInfo)
: m_id(id), m_name(name), m_OtherInfo(OtherInfo)
{}
int m_id;
std::string m_name;
std::string m_OtherInfo;
};
class AbstractObject { //抽象的存在物
public:
AbstractObject(std::string name):m_name(name) {}
std::string GetObjName()
{
return m_name;
}
void AddMember(DescribeObj* pObj) {
m_Objmap.insert(std::make_pair(pObj->m_id, pObj));
}
void ShowInfo() {
for (auto item : m_Objmap) {
std::cout << "item ID:" << item.first << "; name:" \
<< item.second->m_name << "; Infomation:" << item.second->m_OtherInfo << std::endl;
}
}
virtual ~AbstractObject() {
for (auto item : m_Objmap) {
delete item.second;
}
}
virtual void TakingJob() = 0;
protected:
std::string m_name;
std::map<int, DescribeObj*> m_Objmap;
};
class Garderner : public AbstractObject //具象化的存在物
{
public:
using AbstractObject::AbstractObject; // 使用父类的构造函数传递抽象对象的名字
virtual void TakingJob() override
{
std::cout << this->GetObjName() << "园丁在修建花园" << std::endl;
}
};
class AbstractGarden //抽象的花园
{
public:
AbstractGarden(AbstractObject* pObj):m_pObj(pObj) //将抽象的对象(如动物、建筑物等)构建出抽象的花园
{}
void ShowInfo() {
m_pObj->ShowInfo();
m_pObj->TakingJob(); //建立花园中抽象对象的动作
}
virtual void PrintGardenInfo() = 0; //打印花园的信息由具体的类实现
protected:
AbstractObject* m_pObj;
};
class SkyGarden : public AbstractGarden // 构建具体的花园
{
public:
using AbstractGarden::AbstractGarden;
virtual void PrintGardenInfo() override
{
std::cout << "这是一个空中花园" << std::endl;
}
};
int main()
{
AbstractObject* gardener = new Garderner("园丁们:"); //多态:父类指针指向子类对象
DescribeObj* zhangsan = new DescribeObj(1, "Zhangsan", "一个老头");
DescribeObj* lisi = new DescribeObj(2,"lisi", "一个小孩");
gardener->AddMember(zhangsan); //向园丁的团体中添加具体的人
gardener->AddMember(lisi);
AbstractGarden* skygar = new SkyGarden(gardener); // 将园丁添加到花园中,建立关联
skygar->PrintGardenInfo();
skygar->ShowInfo();
delete gardener;
delete skygar;
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
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
# 6.工厂模式
class AbstractProduct
{
public:
virtual void ShowInfo() = 0;
virtual ~AbstractProduct() {}
//多态:使用父类的指针指向子类的对象,delete父类指针时,
//只会释放父类的空间,设为虚析构时才会判断对象是子类还是父类,从而对子类进行释放
};
class Iphone :public AbstractProduct
{
public:
virtual void ShowInfo() override
{
std::cout << "生产了一只Iphone" << std::endl;
}
};
class AppleTV :public AbstractProduct
{
public:
virtual void ShowInfo() override
{
std::cout << "生产了一只AppleTV" << std::endl;
}
};
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
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
# 简单工厂
- 仅有一个工厂函数,用来生产子类兄弟对象。
enum class ProductName :char
{
MobilePhone,
TV
};
class SimpleFactory
{
public:
AbstractProduct* createPruduct(ProductName prodN)
{
AbstractProduct* ptr = nullptr;
switch (prodN)
{
case ProductName::MobilePhone:
ptr = new Iphone;
break;
case ProductName::TV:
ptr = new AppleTV;
break;
default:
break;
}
return ptr;
}
};
int main()
{
SimpleFactory* factory = new SimpleFactory;
AbstractProduct* prodc = factory->createPruduct(ProductName::MobilePhone);
prodc->ShowInfo();
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
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
# 工厂模式
class AbstractFactory
{
public:
virtual AbstractProduct* createProduct() = 0;
virtual ~AbstractFactory() {}
};
class IphoneFactory : public AbstractFactory
{
public:
AbstractProduct* createProduct() override
{
return new Iphone;
}
};
int main()
{
AbstractFactory* fac = new IphoneFactory;
AbstractProduct* product = fac->createProduct();
product->ShowInfo();
delete fac;
delete product;
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 7.抽象工厂
- 如果工厂创建的对象具有很多属性,可建立每个属性的抽象属性,然后通过工厂进行建造。
- 生产一个如下对象
便宜的 | 贵的 | |
---|---|---|
屏幕 | LCD屏幕 | OLED屏幕 |
外壳 | 塑料 | 玻璃 |
// 屏幕
class Screen //抽象
{
public:
virtual void ShowInfo() = 0;
virtual ~Screen() {}
};
class OLEDScreen : public Screen
{
public:
virtual void ShowInfo() override
{
std::cout << "使用了OLED屏幕" << std::endl;
}
};
class LCDScreen : public Screen
{
public:
virtual void ShowInfo() override
{
std::cout << "使用了LCD屏幕" << std::endl;
}
};
// 外壳
class PhoneCase //抽象
{
public:
virtual void ShowInfo() = 0;
virtual ~PhoneCase() {}
};
class PlasticsCase :public PhoneCase
{
public:
virtual void ShowInfo()
{
std::cout << "使用了塑料外壳" << std::endl;
}
};
class GlassCase :public PhoneCase
{
public:
virtual void ShowInfo()
{
std::cout << "使用了玻璃外壳" << std::endl;
}
};
class MobilePhone
{
public:
MobilePhone(Screen* screen, PhoneCase* phonecase)
:m_screen(screen), m_PhoneCase(phonecase) {}
~MobilePhone() {
delete m_PhoneCase;
delete m_screen;
}
void ShowInfo() {
m_PhoneCase->ShowInfo();
m_screen->ShowInfo();
}
private:
Screen* m_screen;
PhoneCase* m_PhoneCase;
};
class AbstractFactory
{
public:
virtual MobilePhone* createPhone() = 0;
virtual ~AbstractFactory() {}
};
class CheapPhone : public AbstractFactory
{
virtual MobilePhone* createPhone() override
{
MobilePhone* pobj = new MobilePhone(new LCDScreen, new PlasticsCase);
std::cout << "创建了一只便宜的手机" << std::endl;
return pobj;
}
};
class ExpensivePhone : public AbstractFactory
{
virtual MobilePhone* createPhone() override
{
MobilePhone* pobj = new MobilePhone(new OLEDScreen, new GlassCase);
std::cout << "创建了一只贵的手机" << std::endl;
return pobj;
}
};
int main()
{
AbstractFactory* fac = new CheapPhone;
MobilePhone* product = fac->createPhone();
product->ShowInfo();
delete fac;
delete product;
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
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
# 8.原型模式
- 使用了子类的拷贝构造构造一个子类的实例,通过父类的指针去拷贝子类的对象,子类实现特定的功能。
- 保证类的各个属性(包括私有的)被克隆到了新的对象中。
class Prototype
{
public:
virtual Prototype* clone() = 0;
virtual void ShowInfo() = 0;
virtual ~Prototype() {}
};
class PrototypeA :public Prototype
{
public:
virtual Prototype* clone() override {
return new PrototypeA(*this);
}
virtual void ShowInfo() override {
std::cout << "创建了克隆A" << std::endl;
}
};
class PrototypeB :public Prototype
{
public:
virtual Prototype* clone() override {
return new PrototypeB(*this); //自定义克隆逻辑,注意深拷贝
}
virtual void ShowInfo() override {
std::cout << "创建了克隆B" << std::endl;
}
};
int main()
{
Prototype* pOrigin = new PrototypeA;
Prototype* newtype = pOrigin->clone();
newtype->ShowInfo();
delete pOrigin;
delete newtype;
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
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
# 9.建造者模式
- 抽象工厂的子类是兄弟关系,调用即可创建对象;Builder可以实现具体细节
- 建造一个如下对象
phone | 便宜的 | 贵的 |
---|---|---|
摄像头 | 无 | 有 |
指纹识别 | 无 | 有 |
//定义不同价位的手机
class PhoneAbstract
{
public:
virtual void AddPatrts(std::string name) = 0;
//virtual void AddPatrts(std::string name, int partsPrice) = 0;
virtual void ShowParts() = 0;
virtual ~PhoneAbstract() {}
};
class CheapPhone : public PhoneAbstract
{
public:
void AddPatrts(std::string name) override{
vec.push_back(name);
}
void ShowParts()override {
for (auto& item : vec) {
std::cout << item << " ";
}
std::cout << std::endl;
}
private:
std::vector<std::string> vec;
};
class ExpensivePhone : public PhoneAbstract
{
public:
virtual void AddPatrts(std::string name) override {}
void AddPatrts(std::string name, int partsPrice) {
Map.insert(std::make_pair(name, partsPrice));
}
void ShowParts() override {
for (auto& item : Map) {
std::cout << item.first << ":" << item.second << std::endl;
}
}
private:
std::map<std::string, int> Map;
};
//Builder
class PhoneBuilder
{
public:
virtual void reset() = 0;
virtual void BuildCamera() = 0;
virtual void BuildFingerPrint() = 0;
virtual ~PhoneBuilder() {}
};
class ExpansiveBuilder : public PhoneBuilder
{
public:
ExpansiveBuilder() {
reset();
}
void reset() override {
m_phone = new ExpensivePhone;
}
ExpensivePhone* GetCheapPhone() { //由该函数的返回指针来维护new的内存区域
ExpensivePhone* phone = m_phone;
m_phone = nullptr;
return phone;
}
void BuildCamera() override{
m_phone->AddPatrts("有摄像头",100);
}
void BuildFingerPrint() {
m_phone->AddPatrts("有指纹识别",200);
}
~ExpansiveBuilder() {
if (m_phone)
delete m_phone;
}
private:
ExpensivePhone* m_phone;
};
class CheapBuilder : public PhoneBuilder
{
public:
CheapBuilder() {
reset();
}
void reset() override {
m_phone = new CheapPhone;
}
CheapPhone* GetCheapPhone() { //由该函数的返回指针来维护new的内存区域
CheapPhone* phone = m_phone;
m_phone = nullptr;
return phone;
}
void BuildCamera() override {
m_phone->AddPatrts("无摄像头");
}
void BuildFingerPrint() {
m_phone->AddPatrts("无指纹识别");
}
~CheapBuilder() {
if (m_phone)
delete m_phone;
}
private:
CheapPhone* m_phone;
};
//Mannager
class Mannager
{
public:
void setBuilder(PhoneBuilder* builder){
m_builder = builder;
}
void BuildPhoneCamera() {
m_builder->BuildCamera();
}
void BuildPhoneAll() {
BuildPhoneCamera();
m_builder->BuildFingerPrint();
}
private:
PhoneBuilder* m_builder = nullptr;
};
int main()
{
Mannager* pman = new Mannager;
CheapBuilder* pcbuild = new CheapBuilder;
//只显示是否有摄像头
pman->setBuilder(pcbuild);
pman->BuildPhoneCamera();
CheapPhone* phone = pcbuild->GetCheapPhone();
phone->ShowParts();
delete phone;
//显示是否有摄像头和是否有指纹识别
pcbuild->reset();
pman->BuildPhoneAll();
phone = pcbuild->GetCheapPhone();
phone->ShowParts();
delete phone;
delete pman;
delete pcbuild;
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# 10.适配器Adapter
- 一个场景:如何使用英语和中文和狗说话?需要一个能将人语言翻译成狗语言的适配器。
class People
{
public:
virtual void SaySomething(std::string& words) = 0;
void WhatDogSay(std::string words) {
std::cout << words << std::endl;
}
virtual ~People() {}
};
class PeopleSayEnglish :public People
{
void SaySomething(std::string& words) override {
words = "Hello Dog";
}
};
class PeopleSayChinese :public People
{
void SaySomething(std::string& words) override {
words = "你好,小狗狗";
}
};
class Dog
{
public:
void SendMsg(std::string& words) {
words = "汪汪汪";
}
void RecvMsh(std::string msg) {
std::cout << msg << std::endl;
}
};
class AbstractAdapter
{
public:
AbstractAdapter(People* People):m_People(People) {}
virtual void translateToDogs() = 0;
virtual void translateToPeople() = 0;
~AbstractAdapter() {}
protected:
People* m_People;
Dog* m_dog;
};
class EnglishAdapter : public AbstractAdapter
{
public:
using AbstractAdapter::AbstractAdapter; //使用父类的构造函数
void translateToPeople(){
std::string msg;
m_dog->SendMsg(msg);
//msg执行转换
m_People->WhatDogSay("Dog说:" + msg);
}
void translateToDogs() {
std::string msg;
m_People->SaySomething(msg);
//msg执行转换
m_dog->RecvMsh("人说:" + msg);
}
};
int main()
{
People* p = new PeopleSayEnglish;
AbstractAdapter* adapter = new EnglishAdapter(p);
adapter->translateToDogs();
adapter->translateToPeople();
delete p;
delete adapter;
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
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
# 11.代理模式Proxy
- 服装商可以直接向顾客提供商品,也可以通过代理,两者卖的是同一个商品
class ClothAbstract
{
public:
virtual void ClothSelling() = 0;
virtual ~ClothAbstract() {}
};
class ClothFactory : public ClothAbstract //被代理的对象
{
public:
void ClothSelling() override {
std::cout << "服装商自己卖衣服" << std::endl;
}
};
class Broker :public ClothAbstract //代理类
{
public:
Broker() {
m_isProxy = true;
m_facotory = new ClothFactory;
}
bool isProxy() {
return m_isProxy;
}
void SetProxy(bool flag) {
m_isProxy = flag;
}
void ClothSelling() override {
if (m_isProxy) {
std::cout << "中间商参与的售卖" << std::endl;
m_facotory->ClothSelling();
}
}
~Broker() {
if (m_facotory) {
delete m_facotory;
}
}
private:
bool m_isProxy = false; //是否使用代理
ClothFactory* m_facotory = nullptr; //添加代理的对象,中间商为服装厂进行售卖
};
int main()
{
ClothAbstract* cloth = new Broker;
cloth->ClothSelling();
delete cloth;
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
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