[设计模式学习C++实现]---Facade模式
这个模式很简单,就是把好几类实现的功能用一个类整合起来,这样用户就只需要调用一个类中的这个方法就可以实现全部的过程。
实现代码(代码来自k_eckel):
1 //Subsystem1.h 2 class Subsystem1 3 { 4 public: 5 Subsystem1(); 6 ~Subsystem1(); 7 void Operation(); 8 protected: 9 private: 10 }; 11 12 //Subsystem1.cpp 13 #include <iostream> 14 using namespace std; 15 Subsystem1::Subsystem1() 16 { 17 } 18 Subsystem1::~Subsystem1() 19 { 20 } 21 void Subsystem1::Operation() 22 { 23 cout<<"Subsystem1 operation.."<<endl; 24 }
1 //Subsystem2.h 2 class Subsystem2 3 { 4 public: 5 Subsystem2(); 6 ~Subsystem2(); 7 void Operation(); 8 protected: 9 private: 10 }; 11 12 //Subsystem2.cpp 13 Subsystem2::Subsystem2() 14 { 15 } 16 Subsystem2::~Subsystem2() 17 { 18 } 19 void Subsystem2::Operation() 20 { 21 cout<<"Subsystem2 operation.."<<endl; 22 }
1 //Facade.h 2 class Facade 3 { 4 public: 5 Facade(); 6 ~Facade(); 7 void OperationWrapper(); 8 protected: 9 private: 10 Subsystem1* _subs1; 11 Subsystem2* _subs2; 12 }; 13 14 //Facade.cpp 15 Facade::Facade() 16 { 17 this->_subs1 = new Subsystem1(); 18 this->_subs2 = new Subsystem2(); 19 } 20 Facade::~Facade() 21 { 22 delete _subs1; 23 delete _subs2; 24 } 25 void Facade::OperationWrapper() 26 { 27 this->_subs1->Operation(); 28 this->_subs2->Operation(); 29 }
1 //main.cpp 2 #include "Facade.h" 3 #include <iostream> 4 using namespace std; 5 int main(int argc,char* argv[]) 6 { 7 Facade* f = new Facade(); 8 f->OperationWrapper(); 9 return 0; 10 }