From 1c9457115cd4b0ec0d1945916139bd93839891cd Mon Sep 17 00:00:00 2001 From: Jakub Vojvoda Date: Sat, 17 Sep 2016 18:05:19 +0200 Subject: [PATCH] add Bridge pattern --- bridge/Bridge.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 2 deletions(-) diff --git a/bridge/Bridge.cpp b/bridge/Bridge.cpp index 5564e57..ab73b3c 100644 --- a/bridge/Bridge.cpp +++ b/bridge/Bridge.cpp @@ -5,13 +5,80 @@ * * Source code is licensed under MIT licence * (for more details see LICENCE) - * + * */ #include +/* + * Implementor + * defines the interface for implementation classes + */ +class Implementor { +public: + virtual void action() = 0; + // ... +}; + +/* + * Concrete Implementors + * implement the Implementor interface and define concrete implementations + */ +class ConcreteImplementorA : public Implementor { +public: + void action() { + std::cout << "Concrete Implementor A" << std::endl; + } + // ... +}; + +class ConcreteImplementorB : public Implementor { +public: + void action() { + std::cout << "Concrete Implementor B" << std::endl; + } + // ... +}; + +/* + * Abstraction + * defines the abstraction's interface + */ +class Abstraction { +public: + virtual void operation() = 0; + // ... +}; + +/* + * RefinedAbstraction + * extends the interface defined by Abstraction + */ +class RefinedAbstraction : public Abstraction { +public: + RefinedAbstraction(Implementor *impl) + : implementor(impl) {} + + void operation() { + implementor->action(); + } + // ... + +private: + Implementor *implementor; +}; + + int main() { + Implementor *ia = new ConcreteImplementorA; + Implementor *ib = new ConcreteImplementorB; + + Abstraction *abstract1 = new RefinedAbstraction(ia); + abstract1->operation(); + + Abstraction *abstract2 = new RefinedAbstraction(ib); + abstract2->operation(); return 0; -} \ No newline at end of file +}