1.Facade 模式在高层组合封装了子系统的接口,解耦了系统。隐藏了子系统的复杂性,使其更易使用。
2.Facade 模式结构图
3.实现
1 #ifndef _FACADE_H_ 2 #define _FACADE_H_ 3 4 class Subsystem1 5 { 6 public: 7 Subsystem1(); 8 ~Subsystem1(); 9 void Operation(); 10 protected: 11 private: 12 }; 13 14 class Subsystem2 15 { 16 public: 17 Subsystem2(); 18 ~Subsystem2(); 19 void Operation(); 20 protected: 21 private: 22 }; 23 24 class Facade 25 { 26 public: 27 Facade(); 28 ~Facade(); 29 void OperationWrapper(); 30 protected: 31 private: 32 Subsystem1* _subs1; 33 Subsystem2* _subs2; 34 }; 35 36 #endif
1 #include "Facade.h" 2 #include <iostream> 3 using namespace std; 4 5 Subsystem1::Subsystem1() 6 { 7 8 } 9 10 Subsystem1::~Subsystem1() 11 { 12 13 } 14 15 void Subsystem1::Operation() 16 { 17 cout<<"Subsystem1 operation.."<<endl; 18 } 19 20 Subsystem2::Subsystem2() 21 { 22 23 } 24 25 Subsystem2::~Subsystem2() 26 { 27 28 } 29 30 void Subsystem2::Operation() 31 { 32 cout<<"Subsystem2 operation.."<<endl; 33 } 34 35 Facade::Facade() 36 { 37 this->_subs1 = new Subsystem1(); 38 this->_subs2 = new Subsystem2(); 39 } 40 41 Facade::~Facade() 42 { 43 delete _subs1; 44 delete _subs2; 45 } 46 47 void Facade::OperationWrapper() 48 { 49 this->_subs1->Operation(); 50 this->_subs2->Operation(); 51 }
1 #include "Facade.h" 2 #include <iostream> 3 using namespace std; 4 5 int main(int argc,char* argv[]) 6 { 7 Facade* f = new Facade(); 8 f->OperationWrapper(); 9 return 0; 10 }