设计模式之外观模式

来源:互联网 发布:西单有mac专柜吗 编辑:程序博客网 时间:2024/05/29 15:54

外观模式提供一个统一的接口,用来访问子系统中的一群接口。外观模式定义了一个高层接口,让子系统更容易使用。外观模式说白了就是把实现某个功能所需要的很多接口包装成一个接口,让客户想实现这个功能时直接调用这个接口就可以了。它的类图如下:


下面用一个具体的例子来说明外观模式的应用:我们想听一个高逼格的音乐的话,通常需要音响+CD播放机+灯光,但是每次打开或关闭音乐都要自己去操作这三件套未免有点

烦人了,所以我们需要将这三件套包装起来,实现智能一键开关音乐。下面是我们实现的具体 程序:

#include "stdafx.h"#include <iostream>#include <string>#include <memory>using namespace std;//因为Amplifier和CDplayer两个类包含了相互的对象指针,所以需要在这里声明CDplayerclass CdPlayer;class Amplifier {private:string description;shared_ptr<CdPlayer> cd;public:Amplifier(string description) {this->description = description;}void on() {cout<<description<<" on"<<endl;}void off() {cout<<description <<" off"<<endl;}void setVolume(int level) {cout<<description <<" setting volume to "<<level<<endl;}void setCd(shared_ptr<CdPlayer> cd) {cout<<description <<" setting CD player to " <<cd<<endl;this->cd = cd;}};class CdPlayer {private:string description;int currentTrack;shared_ptr<Amplifier> amplifier;string title;public:CdPlayer(string description, shared_ptr<Amplifier> amplifier) {this->description = description;this->amplifier = amplifier;}void on() {cout<<description <<" on"<<endl;}void off() {cout<<description <<" off"<<endl;}void eject() {cout<<description <<" eject"<<endl;} void play(string title) {this->title = title;currentTrack = 0;cout<<description<<" playing \""<< title <<"\""<<endl;} void play(int track) {if (title.empty()) {cout<<description + " can't play track " << currentTrack<<", no cd inserted"<<endl;} else {currentTrack = track;cout<<description <<" playing track "<<currentTrack<<endl;}}};  class TheaterLights { private:  string description;  public:  TheaterLights(string description) { this->description = description; }  void on() { cout<<description <<" on"<<endl; }  void off() { cout<<description <<" off"<<endl; }  void dim(int level) { cout<<description <<" dimming to " << level  <<"%"<<endl; } };  //外观包装后的类 class HomeTheaterFacade { private: //三个子系统对象的指针shared_ptr<Amplifier> amp;shared_ptr<CdPlayer> cd;shared_ptr<TheaterLights> lights; public: HomeTheaterFacade(shared_ptr<Amplifier> amp, shared_ptr<CdPlayer> cd, shared_ptr<TheaterLights> lights){this->amp = amp;this->cd = cd;this->lights = lights;} void listenToCd(string cdTitle) {cout<<"Get ready for an audiopile experence..."<<endl;lights->on();amp->on();amp->setVolume(5);amp->setCd(cd);cd->on();cd->play(cdTitle);} void endCd() {cout<<"Shutting down CD..."<<endl;amp->off();amp->setCd(cd);cd->eject();cd->off();}};int _tmain(int argc, _TCHAR* argv[]){shared_ptr<Amplifier> amp = shared_ptr<Amplifier>(new Amplifier("Top-O-Line Amplifier"));shared_ptr<CdPlayer> cd =shared_ptr<CdPlayer>(new CdPlayer("Top-O-Line CD Player", amp)) ;shared_ptr<TheaterLights> lights = shared_ptr<TheaterLights>(new TheaterLights("Theater Ceiling Lights"));shared_ptr<HomeTheaterFacade> homeTheater =shared_ptr<HomeTheaterFacade>(new HomeTheaterFacade(amp, cd, lights));homeTheater->listenToCd("演员");homeTheater->endCd();return 0;}
下面是程序的执行结果:



0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 xp电脑开机密码忘记了怎么办 电脑开机密码到期忘记改怎么办 电脑账户数据库密码忘了怎么办 微信不能拍摄了怎么办 华硕笔记本用户名密码忘了怎么办 学信网登录密码用户名搞忘怎么办 电脑密码输入错误会被锁怎么办 电脑被锁机软件设了密码怎么办 电脑密码被锁了怎么办 电脑xp密码忘了怎么办 三星账户账号密码忘了怎么办 三星账户密码忘记了怎么办 w7账号被禁用了怎么办 笔记本用户名密码忘记了怎么办 笔记本忘记用户名和密码怎么办 苹果手机忘记用户名和密码怎么办 w10电脑忘了密码怎么办 联想台式电脑密码忘记了怎么办 新电脑用一会就很烫怎么办 电脑忘了密码怎么办啊 戴尔笔记本账户密码忘记了怎么办 华硕台式电脑开不了机怎么办 手机号申请qq账号上限了怎么办 店铺不干了被告商标侵权怎么办 咸鱼违规被投诉侵犯商标权怎么办 房子买了70年后怎么办 买房付完首付后怎么办 手机b站忘记密码怎么办 护士电子注册没有激活码怎么办 大王卡变3g网是怎么办 无法解析服务器的dns地址怎么办 笔记本dns辅服务器未响应怎么办 原营业执照注销法人没在怎么办? 加盟骗局公安局工商局不管怎么办 发现老婆衣柜里有避孕套怎么办 2岁宝宝爱舔东西怎么办 痔疮手术后痛疼怎么办 长了内痔疮该怎么办 淘宝卖家遇到恶意买家怎么办 淘宝遇到恶意买家拍单怎么办 淘宝遇到买家恶意搞坏商品怎么办