[结构型] 外观模式

外观模式的提出:

为复杂的子系统调用提供一个统一的入口,使子系统与客户的耦合度降低,且客户端调用非常方便。

就像在网站浏览时,开发者为我们设定的主页一样。这样我们就无需去记住所有的子网页的URL,只需要记得主页的URL。这样我们同样可以访问该网站的所有资源,而且还无需记得那么多复杂的URL。两者的道理是一样的。

View Code
 1 #include <iostream>
2 #include <string>
3 using namespace std;
4 // 外观模式
5 class Light
6 {
7 public:
8 void on()
9 {
10 cout << "灯开了..." << endl;
11 }
12 };
13
14 class TV
15 {
16 public:
17 void play()
18 {
19 cout << "电视机播放中..." <<endl;
20 }
21 };
22
23 class AirConditioner
24 {
25 public:
26 void on()
27 {
28 cout << "空调开了..." << endl;
29 }
30 };
31
32 class Facade // 外观类
33 {
34 private:
35 Light light;
36 TV t;
37 AirConditioner air;
38 public:
39 Facade(Light &l,TV &tv,AirConditioner &ac)
40 {
41 this->light = l;
42 this->t = tv;
43 this->air = ac;
44 }
45 void LightOn()
46 {
47 this->light.on();
48 }
49 void TVPlay()
50 {
51 this->t.play();
52 }
53 void AitConditionerOn()
54 {
55 this->air.on();
56 }
57
58 };
59 void main()
60 {
61 Light l;
62 TV tv;
63 AirConditioner ac;
64
65 Facade f(l,tv,ac);
66 f.LightOn();
67 f.TVPlay();
68 f.AitConditionerOn();
69
70 }
原文地址:https://www.cnblogs.com/xuxu8511/p/2406492.html