{"id":3248,"date":"2019-10-16T15:06:00","date_gmt":"2019-10-16T15:06:00","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=3248"},"modified":"2024-02-16T10:03:14","modified_gmt":"2024-02-16T10:03:14","slug":"visitor-design-pattern","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=3248","title":{"rendered":"Visitor (Design pattern)"},"content":{"rendered":"<p>The purpose of that behavioral design pattern is to add a new operation without adding it to the existing object structure.<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n#include &lt;iostream&gt;\r\n\r\nclass Cat;\r\nclass Dog;\r\n\r\nclass IPetVisitor\r\n{\r\npublic:\r\n  virtual void visit(Dog* dog) = 0;\r\n  virtual void visit(Cat* cat) = 0;\r\n};\r\n\r\nclass Pet\r\n{\r\npublic:\r\n  virtual void accept(IPetVisitor* petVisitor) = 0;\r\n  virtual std::string getSound() = 0;\r\n  virtual ~Pet() {}\r\n};\r\n \r\nclass Cat : public Pet\r\n{\r\npublic:\r\n  void accept(IPetVisitor* petVisitor)\r\n    { if(petVisitor) {petVisitor-&gt;visit(this);} }\r\n  std::string getSound()\r\n    { return std::string(&quot;meow&quot;); }\r\n};\r\n \r\nclass Dog : public Pet\r\n{\r\npublic:\r\n  void accept(IPetVisitor* petVisitor)\r\n    { if(petVisitor) {petVisitor-&gt;visit(this);} }\r\n  std::string getSound()\r\n    { return std::string(&quot;woof&quot;); }\r\n};\r\n \r\nclass PetVisitor : public IPetVisitor\r\n{\r\npublic:\r\n  void visit(Cat* cat)\r\n    { std::cout &lt;&lt; &quot;The cat does &quot; &lt;&lt; cat-&gt;getSound() &lt;&lt; std::endl; }\r\n  void visit(Dog* dog)\r\n    { std::cout &lt;&lt; &quot;The dog does &quot; &lt;&lt; dog-&gt;getSound() &lt;&lt; std::endl; }\r\n};\r\n \r\nint main()\r\n{\r\n  PetVisitor petVisitor;\r\n\r\n  Cat cat;\r\n  cat.accept(&amp;petVisitor);\r\n  Dog dog;\r\n  dog.accept(&amp;petVisitor);\r\n \r\n  return 0;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The purpose of that behavioral design pattern is to add a new operation without adding it to the existing object<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[30],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/3248"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3248"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/3248\/revisions"}],"predecessor-version":[{"id":16695,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/3248\/revisions\/16695"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3248"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}