{"id":2121,"date":"2019-08-04T02:02:04","date_gmt":"2019-08-03T23:02:04","guid":{"rendered":"http:\/\/demensdeum.com\/blog\/?p=2121"},"modified":"2024-12-16T22:32:35","modified_gmt":"2024-12-16T19:32:35","slug":"proxy-pattern","status":"publish","type":"post","link":"https:\/\/demensdeum.com\/blog\/hi\/2019\/08\/04\/proxy-pattern\/","title":{"rendered":"Proxy Pattern"},"content":{"rendered":"<p>The Proxy pattern is a structural design pattern.<br \/>The pattern describes the technique of working with a class through a class interlayer &#8211; a proxy. The proxy allows you to change the functionality of the original class, with the ability to preserve the original behavior, while maintaining the original interface of the class.<br \/>Let&#8217;s imagine a situation &#8211; in 2015, one of the countries of Western Europe decides to record all requests to the websites of the country&#8217;s users, in order to improve statistics and gain a deeper understanding of the political moods of citizens.<br \/>Let&#8217;s present a pseudo-code of a naive implementation of a gateway that citizens use to access the Internet:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>class InternetRouter {\n\n    private let internet: Internet\n\n    init(internet: Internet) {\n        self.internet = internet\n    }\n\n    func handle(request: Request, from client: Client) -> Data {\n        return self.internet.handle(request)\n    }\n\n}\n<\/code><\/pre>\n<\/div>\n<p>In the code above, we create an Internet router class, with a pointer to an object providing access to the Internet. When a client requests a site, we return a response from the Internet.<\/p>\n<p>Using the Proxy pattern and the singleton anti-pattern, we will add the functionality of logging the client name and URL:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>class InternetRouterProxy {\n\n    private let internetRouter: InternetRouter\n\n    init(internet: Internet) {\n        self.internetRouter = InternetRouter(internet: internet)\n    }\n\n    func handle(request: Request, from client: Client) -> Data {\n\n        Logger.shared.log(\u201cClient name: \\(client.name), requested URL: \\(request.URL)\u201d)\n\n        return self.internetRouter.handle(request: request, from: client)\n    }\n\n}\n<\/code><\/pre>\n<\/div>\n<p>Due to the preservation of the original InternetRouter interface in the InternetRouterProxy proxy class, it is enough to replace the initialization class from InternetRouter to its proxy, no further changes to the code base are required.<\/p>\n<h3>Sources<\/h3>\n<p><a href=\"https:\/\/refactoring.guru\/ru\/design-patterns\/proxy\" rel=\"noopener noreferrer\" target=\"_blank\">https:\/\/refactoring.guru\/ru\/design-patterns\/ proxy<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Proxy pattern is a structural design pattern.The pattern describes the technique of working with a class through a class interlayer &#8211; a proxy. The proxy allows you to change the functionality of the original class, with the ability to preserve the original behavior, while maintaining the original interface of the class.Let&#8217;s imagine a situation<a class=\"more-link\" href=\"https:\/\/demensdeum.com\/blog\/hi\/2019\/08\/04\/proxy-pattern\/\">Continue reading <span class=\"screen-reader-text\">&#8220;Proxy Pattern&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[61,52],"tags":[95,111],"class_list":["post-2121","post","type-post","status-publish","format-standard","hentry","category-techie","category-tutorials","tag-patterns","tag-proxy","entry"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"hi","enabled_languages":["en","ru","zh","de","fr","ja","pt","hi"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"ru":{"title":true,"content":true,"excerpt":false},"zh":{"title":true,"content":true,"excerpt":false},"de":{"title":true,"content":true,"excerpt":false},"fr":{"title":true,"content":true,"excerpt":false},"ja":{"title":true,"content":true,"excerpt":false},"pt":{"title":true,"content":true,"excerpt":false},"hi":{"title":false,"content":false,"excerpt":false}}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/2121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/comments?post=2121"}],"version-history":[{"count":13,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/2121\/revisions"}],"predecessor-version":[{"id":3949,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/2121\/revisions\/3949"}],"wp:attachment":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/media?parent=2121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/categories?post=2121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/tags?post=2121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}