Bridge.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <stdio.h>
  2. /*
  3. * C++ Design Patterns: BRIDGE
  4. * http://www.plantuml.com/plantuml/uml/bP7DIiD04CVl-nG37jegDUYnXD36YlJYGRtBTZDAO3-MsTb2KHyepv4NSxEO4b14zvBX_n5-mwm3I-9ej2XcK3_ijdUtC2j4UahB462p3qnISUiil0b8xmoSHvA3CbzaPGcU7ADYR9vRupKQj9m3C3zbTyOln8SGhMMa5waGTcFKqVTV9tEUyYF6ZREAa77IsNoLBlvJgdyfJLSzNowdV6BAMhudyoUWFwLg8QnZ11dW_YgpbFkgL4uxMf4xIbthY5LDRVEAPP7rgYpsrHXzEdfS3G8yCaN5aQJEthRlb8jU8-YPdApI88Jq6GxJTwVBv0eoQ0nm3HxZ8JdGTmSNaQ2rJTzvj8qqG-DIf146RB5EKpJw4m00
  5. *
  6. */
  7. #include <iostream>
  8. #include <string>
  9. class Implementor {
  10. public:
  11. Implementor() { std::cout << "Constructor Implementor" << std::endl; }
  12. virtual ~Implementor() {}
  13. virtual std::string operationImp() const = 0;
  14. };
  15. class ConcreteImplementorA : public Implementor {
  16. public:
  17. ConcreteImplementorA() { std::cout << "Constructor ConcreteImplementorA" << std::endl; }
  18. ~ConcreteImplementorA() {}
  19. std::string operationImp() const override { return "operationImp from ConcreteImplementorA\n"; }
  20. };
  21. class ConcreteImplementorB : public Implementor {
  22. public:
  23. ConcreteImplementorB() { std::cout << "Constructor ConcreteImplementorB" << std::endl; }
  24. ~ConcreteImplementorB() {}
  25. std::string operationImp() const override { return "operationImp from ConcreteImplementorB\n"; }
  26. };
  27. class Abstraction {
  28. protected:
  29. Implementor* protected_implementation;
  30. public:
  31. Abstraction(Implementor* implementation) : protected_implementation(implementation) {
  32. }
  33. virtual ~Abstraction() {}
  34. virtual std::string operation() const {
  35. return "Abstraction: Base operation with: " +
  36. this->protected_implementation->operationImp();
  37. }
  38. };
  39. class RefinedAbstraction : public Abstraction {
  40. public:
  41. virtual ~RefinedAbstraction() {}
  42. RefinedAbstraction(Implementor* implementation) : Abstraction(implementation) {
  43. }
  44. std::string operation() const {
  45. return "RefinedAbstraction: Extended operation with: " +
  46. this->protected_implementation->operationImp();
  47. }
  48. };
  49. int main() {
  50. Implementor* implementation = new ConcreteImplementorA;
  51. Abstraction* abstraction = new Abstraction(implementation);
  52. std::cout << abstraction->operation();
  53. std::cout << std::endl;
  54. delete implementation;
  55. delete abstraction;
  56. implementation = new ConcreteImplementorB;
  57. abstraction = new RefinedAbstraction(implementation);
  58. std::cout << abstraction->operation();
  59. std::cout << std::endl;
  60. delete implementation;
  61. delete abstraction;
  62. }