Facade模式也叫外观模式,是由GoF提出的23种设计模式中的一种。 Facade模式为一组具有类似功能的类群,比如类库,子系统等等,提供一个一致的简单的界面。这个一致的简单的界面被称作facade
外部与一个子系统的通信必须通过一个统一的外观对象进行,为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用
属于结构型设计模式
在一个系统中,往往完成一个功能需要多个子系统一同执行才能完成,比如录制视频,捕捉视频图像用到摄像头,捕捉音频要使用到麦。
class SubSystem1 { public: void run(); }; void SubSystem1::run() { cout << "子系统1运行" << endl; } class SubSystem2 { public: void run(); }; void SubSystem2::run() { cout << "子系统2运行" << endl; } class SubSystem3 { public: void run(); }; void SubSystem3::run() { cout << "子系统3运行" << endl; } class Facade{ private: SubSystem1 *sys1; SubSystem2 *sys2; SubSystem3 *sys3; public: Facade(); ~Facade(); void run(); }; Facade::Facade() { sys1 = new SubSystem1; sys2 = new SubSystem2; sys3 = new SubSystem3; } Facade::~Facade() { if (sys1 != NULL) { delete sys1; } if (sys2 != NULL) { delete sys2; } if (sys3 != NULL) { delete sys3; } } void Facade::run() //三个子系统同时执行才能完成这个功能 { sys1->run(); sys2->run(); sys3->run(); } int main() { Facade *aFacade = new Facade; aFacade->run(); delete aFacade; return 0; }假设现有一家庭影院,有两种模式:
KTV模式:电视打开、灯关闭、音响打开、麦克风打开、DVD打开
游戏模式:电视打开、影响打开、游戏机打开、麦克风打开
void TV::turn_on(void) { cout << "电视打开" << endl; } void TV::turn_off(void) { cout << "电视关闭" << endl; } class Light { public: void turn_on(void); void turn_off(void); }; void Light::turn_on(void) { cout << "灯打开" << endl; } void Light::turn_off(void) { cout << "灯关闭" << endl; } class Sound { public: void turn_on(void); void turn_off(void); }; void Sound::turn_on(void) { cout << "音响打开" << endl; } void Sound::turn_off(void) { cout << "音响关闭" << endl; } class Microphone { public: void turn_on(void); void turn_off(void); }; void Microphone::turn_on(void) { cout << "麦克风打开" << endl; } void Microphone::turn_off(void) { cout << "麦克风关闭" << endl; } class DVD { public: void turn_on(void); void turn_off(void); }; void DVD::turn_on(void) { cout << "DVD打开" << endl; } void DVD::turn_off(void) { cout << "DVD关闭" << endl; } class Games { public: void turn_on(void); void turn_off(void); }; void Games::turn_on(void) { cout << "游戏机打开" << endl; } void Games::turn_off(void) { cout << "游戏机关闭" << endl; } class KtvFacadeMode { private: TV *mTv; DVD *mDvd; Sound *mSound; Light *mLight; Microphone *mMic; Games *mGames; public: KtvFacadeMode(); ~KtvFacadeMode(); void run(); }; KtvFacadeMode::KtvFacadeMode() { mTv = new TV; mDvd = new DVD; mSound = new Sound; mLight = new Light; mMic = new Microphone; mGames = new Games; } KtvFacadeMode::~KtvFacadeMode() { if (mTv != NULL){ delete mTv; } if (mDvd != NULL){ delete mDvd; } if (mSound != NULL){ delete mSound; } if (mLight != NULL) { delete mLight; } if (mMic != NULL) { delete mMic; } if (mGames != NULL){ delete mGames; } } void KtvFacadeMode::run() { mTv->turn_on(); mLight->turn_off(); mSound->turn_on(); mMic->turn_on(); mDvd->turn_on(); } class GameFacadeMode{ private: TV *mTv; DVD *mDvd; Sound *mSound; Light *mLight; Microphone *mMic; Games *mGames; public: GameFacadeMode(); ~GameFacadeMode(); void run(); }; GameFacadeMode::GameFacadeMode() { mTv = new TV; mDvd = new DVD; mSound = new Sound; mLight = new Light; mMic = new Microphone; mGames = new Games; } GameFacadeMode::~GameFacadeMode() { if (mTv != NULL) { delete mTv; } if (mDvd != NULL){ delete mDvd; } if (mSound != NULL){ delete mSound; } if (mLight != NULL) { delete mLight; } if (mMic != NULL) { delete mMic; } if (mGames != NULL){ delete mGames; } } void GameFacadeMode::run() { mTv->turn_on(); mLight->turn_on(); mSound->turn_on(); mGames->turn_on(); } int main() { string modeSelect; KtvFacadeMode *aKtvFacade = new KtvFacadeMode; GameFacadeMode *aGameFacade = new GameFacadeMode; while(1) { cin >> modeSelect; if (modeSelect == "KTV") { aKtvFacade->run(); } else if (modeSelect == "Game") { aGameFacade->run(); } else { cout << "退出系统" << endl; break; } } delete aKtvFacade; delete aGameFacade; return 0; }