[结构型] -- 外观模式
生活随笔
收集整理的這篇文章主要介紹了
[结构型] -- 外观模式
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
外觀模式的提出:
為復(fù)雜的子系統(tǒng)調(diào)用提供一個(gè)統(tǒng)一的入口,使子系統(tǒng)與客戶的耦合度降低,且客戶端調(diào)用非常方便。
就像在網(wǎng)站瀏覽時(shí),開發(fā)者為我們設(shè)定的主頁一樣。這樣我們就無需去記住所有的子網(wǎng)頁的URL,只需要記得主頁的URL。這樣我們同樣可以訪問該網(wǎng)站的所有資源,而且還無需記得那么多復(fù)雜的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 << "電視機(jī)播放中..." <<endl;
20 }
21 };
22
23 class AirConditioner
24 {
25 public:
26 void on()
27 {
28 cout << "空調(diào)開了..." << 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 }
?
轉(zhuǎn)載于:https://www.cnblogs.com/xuxu8511/archive/2012/03/19/2406492.html
總結(jié)
以上是生活随笔為你收集整理的[结构型] -- 外观模式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javaweb-服务器输出字符数据到浏览
- 下一篇: 图像算法面试题汇总