以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码
//Facade.h
#pragma once class Subsystem1 { public: Subsystem1(); virtual ~Subsystem1(); void Operation(); protected: private: }; class Subsystem2 { public: Subsystem2(); virtual ~Subsystem2(); void Operation(); }; class Facade { public: Facade(); virtual ~Facade(); void OperationWrapper(); protected: private: Subsystem1* _s1; Subsystem2* _s2; };
//Facade.cpp
#include"Facade.h" #include<iostream> Subsystem1::Subsystem1(){} Subsystem1::~Subsystem1(){} void Subsystem1::Operation() { std::cout << "Subsystem Operation I" << std::endl; } Subsystem2::Subsystem2(){} Subsystem2::~Subsystem2(){} void Subsystem2::Operation() { std::cout << "Subsystem Operation II" << std::endl; } Facade::Facade() { this->_s1 = new Subsystem1(); this->_s2 = new Subsystem2(); } Facade::~Facade() { delete _s1; delete _s2; } void Facade::OperationWrapper() { this->_s1->Operation(); this->_s2->Operation(); }
//main.cpp
#include"Facade.h" int main(int args, char* argv) { Facade* fac = new Facade(); fac->OperationWrapper(); return 0; }