您的位置:首页 > 其它

[结构型] -- 外观模式

2012-03-19 18:28 225 查看
外观模式的提出:

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

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

View Code

#include <iostream>
#include <string>
using namespace std;
// 外观模式
class Light
{
public:
void on()
{
cout << "灯开了..." << endl;
}
};

class TV
{
public:
void play()
{
cout << "电视机播放中..." <<endl;
}
};

class AirConditioner
{
public:
void on()
{
cout << "空调开了..." << endl;
}
};

class Facade    // 外观类
{
private:
Light light;
TV    t;
AirConditioner air;
public:
Facade(Light &l,TV &tv,AirConditioner &ac)
{
this->light = l;
this->t = tv;
this->air = ac;
}
void LightOn()
{
this->light.on();
}
void TVPlay()
{
this->t.play();
}
void AitConditionerOn()
{
this->air.on();
}

};
void main()
{
Light l;
TV tv;
AirConditioner ac;

Facade f(l,tv,ac);
f.LightOn();
f.TVPlay();
f.AitConditionerOn();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: