///
// Facade pattern:
// - Provide a unified interface to a set of interfaces in a subsystem.
// Facade defines a higher-level interface that makes the subsystem
// easier to use.
//
// Author : ZAsia
// Date : 15/05/11
// Warning : In practice, declaration and implementation should
// be separated(.h and .cpp).
///
#include
using namespace std;
// subsystem classes
// - implement subsystem functionality.
// - handle work assigned by the Facade object
// - have no knowledge of the facade; that is, they keep on reference to it.
class SubsystemA
{
public:
void OperationA()
{
cout << "SubsystemA::OperationA..." << endl;
}
};
class SubsystemB
{
public:
void OperationB()
{
cout << "SubsystemB::OperationB..." << endl;
}
};
class SubsystemC
{
public:
void OperationC()
{
cout << "SubsystemC::OperationC..." << endl;
}
};
// Facade
// - knows which subsystem classes are responsible for a quest
// - delegates client requests to appropriate subsystem objects.
class Facade
{
public:
Facade() : _subsystemA(new SubsystemA),
_subsystemB(new SubsystemB),
_subsystemC(new SubsystemC) { }
~Facade()
{
delete _subsystemA; _subsystemA = nullptr;
delete _subsystemB; _subsystemB = nullptr;
delete _subsystemC; _subsystemC = nullptr;
}
void Operation()
{
_subsystemA->OperationA();
_subsystemB->OperationB();
_subsystemC->OperationC();
}
private:
SubsystemA *_subsystemA;
SubsystemB *_subsystemB;
SubsystemC *_subsystemC;
};
// Collaborations
// - Client communicate with the subsystem by sending request to Facade,
// which forwards them to the appropriate subsystem object(s). Although
// the subsystem objects perform the actual work, the facade may have to
// do work if its own to translate its interface to subsystem interfaces.
// - Client that use the facade don't have to access its subsystem object
// directly.
int main()
{
Facade *pFacadeObj = new Facade();
pFacadeObj->Operation();
return 0;
}
DesignPatterns_Facade
最新推荐文章于 2021-03-04 11:34:48 发布