mirror of
https://github.com/JakubVojvoda/design-patterns-cpp.git
synced 2025-12-17 21:04:36 +03:00
add Facade pattern
This commit is contained in:
83
facade/Facade.cpp
Normal file
83
facade/Facade.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* C++ Design Patterns: Facade
|
||||
* Author: Jakub Vojvoda [github.com/JakubVojvoda]
|
||||
* 2016
|
||||
*
|
||||
* Source code is licensed under MIT licence
|
||||
* (for more details see LICENCE)
|
||||
*
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
||||
/*
|
||||
* Subsystems
|
||||
* implement more complex subsystem functionality
|
||||
* and have no knowledge of the facade
|
||||
*/
|
||||
class SubsystemA {
|
||||
public:
|
||||
void suboperation() {
|
||||
std::cout << "Subsystem A method" << std::endl;
|
||||
// ...
|
||||
}
|
||||
// ...
|
||||
};
|
||||
|
||||
class SubsystemB {
|
||||
public:
|
||||
void suboperation() {
|
||||
std::cout << "Subsystem B method" << std::endl;
|
||||
// ...
|
||||
}
|
||||
// ...
|
||||
};
|
||||
|
||||
class SubsystemC {
|
||||
public:
|
||||
void suboperation() {
|
||||
std::cout << "Subsystem C method" << std::endl;
|
||||
// ...
|
||||
}
|
||||
// ...
|
||||
};
|
||||
|
||||
/*
|
||||
* Facade
|
||||
* delegates client requests to appropriate subsystem object
|
||||
* and unified interface that is easier to use
|
||||
*/
|
||||
class Facade {
|
||||
public:
|
||||
Facade() :
|
||||
subsystemA(), subsystemB(), subsystemC() {}
|
||||
|
||||
void operation1() {
|
||||
subsystemA->suboperation();
|
||||
subsystemB->suboperation();
|
||||
// ...
|
||||
}
|
||||
|
||||
void operation2() {
|
||||
subsystemC->suboperation();
|
||||
// ...
|
||||
}
|
||||
|
||||
// ...
|
||||
|
||||
private:
|
||||
SubsystemA *subsystemA;
|
||||
SubsystemB *subsystemB;
|
||||
SubsystemC *subsystemC;
|
||||
// ...
|
||||
};
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
Facade *facade = new Facade();
|
||||
facade->operation1();
|
||||
facade->operation2();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
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.
|
||||
The pattern has structural purpose and applies to objects.
|
||||
|
||||
### When to use
|
||||
|
||||
|
||||
Reference in New Issue
Block a user