{"id":5405,"date":"2020-02-18T14:08:36","date_gmt":"2020-02-18T14:08:36","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=5405"},"modified":"2024-02-16T10:02:39","modified_gmt":"2024-02-16T10:02:39","slug":"decorator-design-pattern","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=5405","title":{"rendered":"Decorator (Design Pattern)"},"content":{"rendered":"<p>This structural pattern increases the functionality of a class.<\/p>\n<hr>\n<p><img decoding=\"async\" src=\"http:\/\/www.max-sperling.bplaced.net\/wp-content\/uploads\/2020\/11\/Decorator.svg\" class=\"aligncenter\" \/><br \/>\nLooks similar to a proxy, but it gets the object injected (aggregation).<\/p>\n<hr>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\n#include &lt;memory&gt;\r\n#include &lt;iostream&gt;\r\n\r\n\/\/ Interface\r\nclass IHuman {\r\npublic:\r\n    virtual void wakeUp() = 0;\r\n};\r\n\r\n\/\/ Normal class\r\nclass Human: public IHuman {\r\npublic:\r\n    void wakeUp() {\r\n        std::cout &lt;&lt; &quot;Eat breakfast.&quot; &lt;&lt; std::endl;\r\n    }\r\n};\r\n\r\n\/\/ Decorator\r\nclass Hero: public IHuman {\r\npublic:\r\n    Hero(std::unique_ptr&lt;IHuman&gt; human) : m_Human(std::move(human)) {}\r\n\r\n    void wakeUp() {\r\n        m_Human-&gt;wakeUp();\r\n        std::cout &lt;&lt; &quot;Save the world.&quot; &lt;&lt; std::endl;\r\n    }\r\nprivate:\r\n    std::unique_ptr&lt;IHuman&gt; m_Human;\r\n};\r\n\r\nint main() {\r\n    std::unique_ptr&lt;IHuman&gt; human(new Human());\r\n    std::unique_ptr&lt;IHuman&gt; hero(new Hero(std::move(human)));\r\n    hero-&gt;wakeUp();\r\n    \/\/ ...\r\n    return 0;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This structural pattern increases the functionality of a class. Looks similar to a proxy, but it gets the object injected<\/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\/5405"}],"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=5405"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/5405\/revisions"}],"predecessor-version":[{"id":16689,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/5405\/revisions\/16689"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5405"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}