Visitor.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include <stdio.h>
  2. /*
  3. * C++ Design Patterns: VISITOR
  4. * http://www.plantuml.com/plantuml/uml/jLB1RjGm4BtxAqRYiAsgTDKULr5KKZZq0gXKlQBPPBerSkmm7aUgW7_Xck_8Zv6uTaNLPIK4-93OyzwyDthsXHTa2PrHwnNSNdsykdb_3QkrcbTZq7kuD9giAAMj4DzXGt2hlHR7y5N1l5x1i9pV6kh8okiZRG5XS-bimoIKoyTxkFN2tRxWriVgUpRFKB8yIvudV-1vyfUUv__LyzTSK_qfLsmQwcLf7c2JKzypQFPEZGNqh0SK-jIYO9fdlrO2ToHlKF3ezzhrlz5E69H5VbPFbMz5yUAs3wBdwb42YgBAqQdqVzwGypc8rafP9mJeuTrPdBtSLcNZZECDkmSqjGbK9Uzo7O6gN6-hpBrvw6cp5A2cUnoqOpGHkwQUfz6J5O9W2Lm_ZOoYdONf1ymG6ks98P9xPD127RG41i4BXqO2KpoltFOpoUaZymDnHxBKsscyqrPpLBSk36JCCvl8zsWzDFTeTyHBLxpdkNarZMOQOowz2saCWrz2L8cV-4Hnf-LKaMrXoKbTpBlGcPy0
  5. *
  6. */
  7. #include <iostream>
  8. class Element;
  9. class ConcreteElementA;
  10. class ConcreteElementB;
  11. class Visitor {
  12. public:
  13. virtual ~Visitor() {}
  14. virtual void visitorElementA(ConcreteElementA* const element) = 0;
  15. virtual void visitorElementB(ConcreteElementB* const element) = 0;
  16. };
  17. class ConcreteVisitor1 : public Visitor {
  18. public:
  19. ~ConcreteVisitor1() {}
  20. void visitorElementA(ConcreteElementA* const element);
  21. void visitorElementB(ConcreteElementB* const element);
  22. };
  23. class ConcreteVisitor2 : public Visitor {
  24. public:
  25. ~ConcreteVisitor2() {}
  26. void visitorElementA(ConcreteElementA* const element);
  27. void visitorElementB(ConcreteElementB* const element);
  28. };
  29. class Element {
  30. public:
  31. Element() {std::cout << "Constructor Element "<< std::endl;}
  32. virtual ~Element() {std::cout << "Destructor Element "<< std::endl;}
  33. virtual void accept(Visitor& v) = 0;
  34. };
  35. class ConcreteElementA : public Element {
  36. public:
  37. ConcreteElementA(const std::string& data)
  38. : private_data(data) {}
  39. ~ConcreteElementA() {}
  40. void accept(Visitor& v) {
  41. v.visitorElementA(this);
  42. }
  43. std::string getData() {
  44. return private_data;
  45. }
  46. private:
  47. std::string private_data;
  48. };
  49. class ConcreteElementB : public Element {
  50. public:
  51. ConcreteElementB(const std::string& data)
  52. : private_data(data) {}
  53. ~ConcreteElementB() {}
  54. void accept(Visitor& v) {
  55. v.visitorElementB(this);
  56. }
  57. std::string getData() {
  58. return private_data;
  59. }
  60. private:
  61. std::string private_data;
  62. };
  63. void ConcreteVisitor1::visitorElementA(ConcreteElementA *const element) {
  64. std::cout << "Concrete Visitor 1 : Element A visited, " << element->getData() << std::endl;
  65. }
  66. void ConcreteVisitor1::visitorElementB(ConcreteElementB *const element) {
  67. std::cout << "Concrete Visitor 1 : Element B visited, " << element->getData() << std::endl;
  68. }
  69. void ConcreteVisitor2::visitorElementA(ConcreteElementA *const element) {
  70. std::cout << "Concrete Visitor 2 : Element A visited, " << element->getData() << std::endl;
  71. }
  72. void ConcreteVisitor2::visitorElementB(ConcreteElementB *const element) {
  73. std::cout << "Concrete Visitor 2 : Element B visited, " << element->getData() << std::endl;
  74. }
  75. int main() {
  76. ConcreteVisitor1 visitor1;
  77. ConcreteVisitor2 visitor2;
  78. ConcreteElementA elementA("String ElementA ConcreteElementA");
  79. elementA.accept(visitor1);
  80. elementA.accept(visitor2);
  81. ConcreteElementB elementB("String ElementB ConcreteElementB");
  82. elementB.accept(visitor1);
  83. elementB.accept(visitor2);
  84. }