{"id":3246,"date":"2019-10-16T09:01:14","date_gmt":"2019-10-16T09:01:14","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=3246"},"modified":"2024-02-16T10:03:28","modified_gmt":"2024-02-16T10:03:28","slug":"composite-design-pattern","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=3246","title":{"rendered":"Composite (Design pattern)"},"content":{"rendered":"<p>That structural design pattern allows to create trees of objects and to interact with them as they are just a single object.<\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n#include &lt;iostream&gt;\r\n#include &lt;vector&gt;\r\n\r\n\/\/ Component\r\nclass IBird\r\n{\r\npublic:\r\n  virtual void fly() = 0;\r\n  virtual ~IBird() {}\r\n};\r\n\r\n\/\/ Leave\r\nclass Bird : public IBird\r\n{\r\npublic:\r\n  void fly() { std::cout &lt;&lt; &quot;flap flap&quot; &lt;&lt; std::endl; }\r\n};\r\n\r\n\/\/ Composite\r\nclass BirdFlock : public IBird\r\n{\r\npublic:\r\n  void fly() { for(auto bird : m_Flock){ if(bird){bird-&gt;fly();} } }\r\n  void add(IBird *bird) { m_Flock.push_back(bird); }\r\nprivate:\r\n  std::vector&lt;IBird*&gt; m_Flock;\r\n};\r\n\r\nint main()\r\n{\r\n  Bird bird1, bird2, bird3, bird4, bird5;\r\n\r\n  BirdFlock birdFlockPart;\r\n  birdFlockPart.add(&amp;bird1);\r\n  birdFlockPart.add(&amp;bird2);\r\n  birdFlockPart.add(&amp;bird3);\r\n\r\n  BirdFlock birdFlockAll;\r\n  birdFlockAll.add(&amp;birdFlockPart);\r\n  birdFlockAll.add(&amp;bird4);\r\n  birdFlockAll.add(&amp;bird5);\r\n\r\n  birdFlockAll.fly();\r\n}\r\n<\/pre>\n<pre>\r\nbirdFlockAll\r\n   |\r\n   |---> birdFlockPart\r\n   |        |\r\n   |        |---> bird1\r\n   |        |---> bird2\r\n   |        |---> bird3\r\n   |\r\n   |---> bird4\r\n   |---> bird5\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>That structural design pattern allows to create trees of objects and to interact with them as they are just a<\/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\/3246"}],"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=3246"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/3246\/revisions"}],"predecessor-version":[{"id":16697,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/3246\/revisions\/16697"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3246"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}