设计模式之外观模式

多个子系统封装到一个总系统中

  1 #include<iostream>
  2 using namespace std;
  3 #include<string>
  4 
  5 //外观模式
  6 //多个子系统封装到一个总系统中
  7 
  8 //家庭影院
  9 class facede{
 10 public:
 11     
 12 };
 13 
 14 //TV
 15 class TV{
 16     public: 
 17         void on(){
 18             cout<<"TV on"<<endl;
 19         }
 20         void off(){
 21             cout<<"TV off"<<endl;
 22         }
 23 };
 24 
 25 //Light
 26 class Light{
 27     public: 
 28         void on(){
 29             cout<<"Light on"<<endl;
 30         }
 31         void off(){
 32             cout<<"Light off"<<endl;
 33         }
 34 };
 35 
 36 //Audio
 37 class Audio{
 38     public: 
 39         void on(){
 40             cout<<"Audio on"<<endl;
 41         }
 42         void off(){
 43             cout<<"Audio off"<<endl;
 44         }
 45 };
 46 
 47 //Mic
 48 class Mic{
 49     public: 
 50         void on(){
 51             cout<<"Mic on"<<endl;
 52         }
 53         void off(){
 54             cout<<"Mic off"<<endl;
 55         }
 56 };
 57 
 58 //DVD
 59 class DVD{
 60     public: 
 61         void on(){
 62             cout<<"DVD on"<<endl;
 63         }
 64         void off(){
 65             cout<<"DVD off"<<endl;
 66         }
 67 };
 68 
 69 //Player
 70 class Player{
 71     public: 
 72         void on(){
 73             cout<<"Player on"<<endl;
 74         }
 75         void off(){
 76             cout<<"Player off"<<endl;
 77         }
 78 };
 79 //KTV模式
 80 class KTVMode{
 81 public:
 82     KTVMode(){
 83         pTV=new TV;
 84         pLight=new Light;
 85         pAudio=new Audio;
 86         pMic=new Mic;
 87         pDVD=new DVD;
 88     }
 89     ~KTVMode(){
 90         delete pTV;
 91         delete pLight;
 92         delete pAudio;
 93         delete pMic;
 94         delete pDVD;
 95     }
 96     void on(){
 97         pTV->on();
 98         pLight->on();
 99         pAudio->on();
100         pMic->on();
101         pDVD->on();
102     }
103     void off(){
104         pTV->off();
105         pLight->off();
106         pAudio->off();
107         pMic->off();
108         pDVD->off();
109     }
110 public:
111     TV* pTV;
112     Light* pLight;
113     Audio* pAudio;
114     Mic* pMic;
115     DVD* pDVD;
116 };
117 
118 int main()
119 {
120     KTVMode* km=new KTVMode;
121     km->off();
122     return 0;
123 }

 

posted @ 2020-04-07 17:01  njit-sam  阅读(147)  评论(0编辑  收藏  举报