外观模式提供一个统一的接口,用来访问子系统中的一群接口。外观模式定义了一个高层接口,让子系统更容易使用。外观模式说白了就是把实现某个功能所需要的很多接口包装成一个接口,让客户想实现这个功能时直接调用这个接口就可以了。它的类图如下:
下面用一个具体的例子来说明外观模式的应用:我们想听一个高逼格的音乐的话,通常需要音响+CD播放机+灯光,但是每次打开或关闭音乐都要自己去操作这三件套未免有点
烦人了,所以我们需要将这三件套包装起来,实现智能一键开关音乐。下面是我们实现的具体 程序:
#include "stdafx.h" #include <iostream> #include <string> #include <memory> using namespace std; //因为Amplifier和CDplayer两个类包含了相互的对象指针,所以需要在这里声明CDplayer class 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; } 下面是程序的执行结果:
