外观模式:为一组具有类似功能的类群,比如类库,子系统等等,提供一个一致的简单的界面。
代码如下:
#include <iostream> using namespace std; class Television { public: void on() { cout << "Tv on" << endl; } void off() { cout << "Tv off" << endl; } }; class Light { public: void on() { cout << "Light on" << endl; } void off() { cout << "Light off" << endl; } }; class Audio { public: void on() { cout << "Audio on" << endl; } void off() { cout << "Audio off" << endl; } }; class Microphone { public: void on() { cout << "Microphone on" << endl; } void off() { cout << "Microphone off" << endl; } }; class Dvd { public: void on() { cout << "Dvd on" << endl; } void off() { cout << "Dvd off" << endl; } }; class KTVMode { public: KTVMode() { pTv = new Television; pLight = new Light; pAudio = new Audio; pMicrophone = new Microphone; pDvd = new Dvd; } void onKtv() { pTv->on(); pLight->on(); pAudio->on(); pMicrophone->on(); pDvd->on(); } void offKtv() { pTv->off(); pLight->off(); pAudio->off(); pMicrophone->off(); pDvd->off(); } private: Television *pTv; Light *pLight; Audio *pAudio; Microphone *pMicrophone; Dvd *pDvd; ~KTVMode() { delete pTv; delete pLight; delete pAudio; delete pMicrophone; delete pDvd; } }; void test01() { KTVMode *ktv = new KTVMode; ktv->onKtv(); } int main() { test01(); return 0; }
测试结果: