From a7768c2e9c1f5c41eac55da81a01f4fcf810f83a Mon Sep 17 00:00:00 2001 From: Jakub Vojvoda Date: Fri, 16 Sep 2016 14:23:30 +0200 Subject: [PATCH] add Facade pattern --- facade/Facade.cpp | 83 +++++++++++++++++++++++++++++++++++++++++++++++ facade/README.md | 1 + 2 files changed, 84 insertions(+) create mode 100644 facade/Facade.cpp diff --git a/facade/Facade.cpp b/facade/Facade.cpp new file mode 100644 index 0000000..499f851 --- /dev/null +++ b/facade/Facade.cpp @@ -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 + +/* + * 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; +} diff --git a/facade/README.md b/facade/README.md index 904e2a1..6cc4638 100644 --- a/facade/README.md +++ b/facade/README.md @@ -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