-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFacadePattern.cpp
More file actions
106 lines (82 loc) · 2.02 KB
/
Copy pathFacadePattern.cpp
File metadata and controls
106 lines (82 loc) · 2.02 KB
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
/**************************************************************************
* * 外观模式
介绍
意图:为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
主要解决:降低访问复杂系统的内部子系统时的复杂度,简化客户端与之的接口。
何时使用: 1、客户端不需要知道系统内部的复杂联系,整个系统只需提供一个"接待员"即可。 2、定义系统的入口。
如何解决:客户端不与系统耦合,外观类与系统耦合。
关键代码:在客户端和复杂系统之间再加一层,这一层将调用顺序、依赖关系等处理好。
应用实例: 1、去医院看病,可能要去挂号、门诊、划价、取药,让患者或患者家属觉得很复杂,如果有提供接待人员,只让接待人员来处理,就很方便。 2、JAVA 的三层开发模式。
优点: 1、减少系统相互依赖。 2、提高灵活性。 3、提高了安全性。
缺点:不符合开闭原则,如果要改东西很麻烦,继承重写都不合适。
使用场景: 1、为复杂的模块或子系统提供外界访问的模块。 2、子系统相对独立。 3、预防低水平人员带来的风险。
注意事项:在层次化结构中,可以使用外观模式定义系统中每一层的入口。
**************************************************************************/
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
class Shape
{
public:
virtual void draw() = 0;
};
class Rectangle : public Shape
{
public:
void draw() override
{
cout << "Rectangle::draw()" << endl;
}
};
class Square: public Shape
{
public:
void draw() override
{
cout << "Square::draw()" << endl;
}
};
class Circle : public Shape
{
public:
void draw() override
{
cout << "Circle::draw()" << endl;
}
};
class ShapeMaker
{
public:
ShapeMaker() {
circle = new Circle();
rectangle = new Rectangle();
square = new Square();
}
void drawCircle() {
circle->draw();
}
void drawRectangle() {
rectangle->draw();
}
void drawSquare() {
square->draw();
}
private:
Shape* circle;
Shape* rectangle;
Shape* square;
};
void FacadePatternDemo()
{
ShapeMaker* shapeMaker = new ShapeMaker();
shapeMaker->drawCircle();
shapeMaker->drawRectangle();
shapeMaker->drawSquare();
}
int main()
{
FacadePatternDemo();
return 0;
}