{"id":4184,"date":"2019-12-18T09:27:49","date_gmt":"2019-12-18T09:27:49","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=4184"},"modified":"2024-02-16T10:02:51","modified_gmt":"2024-02-16T10:02:51","slug":"proxy-design-pattern","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=4184","title":{"rendered":"Proxy (Design Pattern)"},"content":{"rendered":"<p>This structural pattern controls the access to a class.<\/p>\n<hr>\n<p><img decoding=\"async\" src=\"http:\/\/www.max-sperling.bplaced.net\/wp-content\/uploads\/2020\/11\/Proxy.svg\" class=\"aligncenter\" \/><br \/>\nLooks similar to a decorator, but it instantiates the object by itself (composition).<\/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\nclass IPictureReader {\r\npublic:\r\n    virtual void load(std::string name) = 0;\r\n};\r\n \r\nclass PictureReader : public IPictureReader {\r\npublic:\r\n    void load(std::string name) {\r\n        std::cout &lt;&lt; &quot;Loading the actual image.&quot; &lt;&lt; std::endl;\r\n        \/\/ ...\r\n    }\r\n};\r\n \r\nclass PictureReaderProxy : public IPictureReader {\r\npublic:\r\n    PictureReaderProxy() : m_Picture(new PictureReader()) {}\r\n \r\n    void load(std::string name) {\r\n        if (m_Name != name) {\r\n            m_Picture-&gt;load(name);\r\n            m_Name = name;\r\n        }\r\n    }\r\nprivate:\r\n    std::string m_Name;\r\n    std::unique_ptr&lt;IPictureReader&gt; m_Picture;\r\n};\r\n \r\nint main() {\r\n    std::unique_ptr&lt;IPictureReader&gt; picProxy(new PictureReaderProxy());\r\n    picProxy-&gt;load(&quot;img001.jpg&quot;); \/\/ loads the new picture\r\n    picProxy-&gt;load(&quot;img001.jpg&quot;); \/\/ uses the cached picture\r\n    \/\/ ...\r\n    return 0;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This structural pattern controls the access to a class. Looks similar to a decorator, but it instantiates the object by<\/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\/4184"}],"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=4184"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/4184\/revisions"}],"predecessor-version":[{"id":16691,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/4184\/revisions\/16691"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4184"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}