Facade - shoonie/StudyingDesignPattern GitHub Wiki
Provide a unified interface to as set of interface in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
- It shields clients from subsystem components, thereby reducing the number of objects that clients deal with and making the subsystem easier to use.
- It promotes weak coupling between the subsystem and its clients.
- It doesn't prevent application from using subsystem classes if they need to.
- Reducing client-subsystem coupling.
- Public versus private subsystem classes
// code from http://www.bogotobogo.com/DesignPatterns/facade.php
#include <iostream>
// Subsystem 1
class SubSystemOne
{
public:
void MethodOne() { std::cout << "SubSystem 1" << std::endl; };
};
// Subsystem 2
class SubSystemTwo
{
public:
void MethodTwo() { std::cout << "SubSystem 2" << std::endl; };
};
// Subsystem 3
class SubSystemThree
{
public:
void MethodThree() { std::cout << "SubSystem 3" << std::endl; }
};
// Facade
class Facade
{
public:
Facade()
{
pOne = new SubSystemOne();
pTwo = new SubSystemTwo();
pThree = new SubSystemThree();
}
void MethodA()
{
std::cout << "Facade::MethodA" << std::endl;
pOne->MethodOne();
pTwo->MethodTwo();
}
void MethodB()
{
std::cout << "Facade::MethodB" << std::endl;
pTwo->MethodTwo();
pThree->MethodThree();
}
~Facade()
{
delete pOne;
delete pTwo;
delete pThree;
}
private:
SubSystemOne * pOne;
SubSystemTwo *pTwo;
SubSystemThree *pThree;
};
int main()
{
Facade *pFacade = new Facade();
pFacade->MethodA();
pFacade->MethodB();
delete pFacade;
return 0;
}