{"id":1998,"date":"2019-05-27T21:00:04","date_gmt":"2019-05-27T18:00:04","guid":{"rendered":"http:\/\/demensdeum.com\/blog\/?p=1998"},"modified":"2024-12-16T22:32:37","modified_gmt":"2024-12-16T19:32:37","slug":"decorator-pattern","status":"publish","type":"post","link":"https:\/\/demensdeum.com\/blog\/hi\/2019\/05\/27\/decorator-pattern\/","title":{"rendered":"Pattern Decorator"},"content":{"rendered":"<p>The Decorator pattern is a structural design pattern.<br \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2012\" src=\"https:\/\/demensdeum.com\/blog\/wp-content\/uploads\/2019\/05\/pexels-photo-348601.jpeg\" alt=\"\" width=\"380\" height=\"252\" \/><br \/>\nDecorator is used as an alternative to inheritance to extend the functionality of classes.<br \/>There is a task to expand the functionality of the application depending on the type of product. The customer needs three types of product &#8211; Basic, Professional, Ultimate.<br \/>Basic &#8211; counts the number of characters, Professional &#8211; Basic + capabilities prints text in capital letters, Ultimate &#8211; Basic + Professional + prints text with the inscription ULTIMATE.<br \/>We implement it using inheritance:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>protocol Feature {\n\tfunc textOperation(text: String)\n}\n\nclass BasicVersionFeature: Feature {\n\tfunc textOperation(text: String) {\n\t\tprint(\"\\(text.count)\")\n\t}\n}\n\nclass ProfessionalVersionFeature: BasicVersionFeature {\n\toverride func textOperation(text: String) {\n\t\tsuper.textOperation(text: text)\n\t\tprint(\"\\(text.uppercased())\")\n\t}\n}\n\nclass UltimateVersionFeature: ProfessionalVersionFeature {\n\toverride func textOperation(text: String) {\n\t\tsuper.textOperation(text: text)\n\t\tprint(\"ULTIMATE: \\(text)\")\n\t}\n}\n\nlet textToFormat = \"Hello Decorator\"\n\nlet basicProduct = BasicVersionFeature()\nbasicProduct.textOperation(text: textToFormat)\n\nlet professionalProduct = ProfessionalVersionFeature()\nprofessionalProduct.textOperation(text: textToFormat)\n\nlet ultimateProduct = UltimateVersionFeature()\nultimateProduct.textOperation(text: textToFormat)\n<\/code><\/pre>\n<\/div>\n<p>Now there is a requirement to implement the product \u201cUltimate Light\u201d &#8211; Basic + Ultimate but without the capabilities of the Professional version. The first OUCH! happens, because you will have to create a separate class for such a simple task, duplicate the code.<br \/>Let&#8217;s continue the implementation using inheritance:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>protocol Feature {\n\tfunc textOperation(text: String)\n}\n\nclass BasicVersionFeature: Feature {\n\tfunc textOperation(text: String) {\n\t\tprint(\"\\(text.count)\")\n\t}\n}\n\nclass ProfessionalVersionFeature: BasicVersionFeature {\n\toverride func textOperation(text: String) {\n\t\tsuper.textOperation(text: text)\n\t\tprint(\"\\(text.uppercased())\")\n\t}\n}\n\nclass UltimateVersionFeature: ProfessionalVersionFeature {\n\toverride func textOperation(text: String) {\n\t\tsuper.textOperation(text: text)\n\t\tprint(\"ULTIMATE: \\(text)\")\n\t}\n}\n\nclass UltimateLightVersionFeature: BasicVersionFeature {\n\toverride func textOperation(text: String) {\n\t\tsuper.textOperation(text: text)\n\t\tprint(\"ULTIMATE: \\(text)\")\t\n\t}\n}\n\nlet textToFormat = \"Hello Decorator\"\n\nlet basicProduct = BasicVersionFeature()\nbasicProduct.textOperation(text: textToFormat)\n\nlet professionalProduct = ProfessionalVersionFeature()\nprofessionalProduct.textOperation(text: textToFormat)\n\nlet ultimateProduct = UltimateVersionFeature()\nultimateProduct.textOperation(text: textToFormat)\n\nlet ultimateLightProduct = UltimateLightVersionFeature()\nultimateLightProduct.textOperation(text: textToFormat)\n<\/code><\/pre>\n<\/div>\n<p>The example can be developed further for clarity, but even now the complexity of supporting a system based on inheritance is visible &#8211; it is cumbersome and lacks flexibility.<br \/>A decorator is a set of protocols describing functionality, an abstract class containing a reference to a child concrete instance of the decorator class that extends the functionality.<br \/>Let&#8217;s rewrite the example above using the pattern:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>protocol Feature {\n\tfunc textOperation(text: String)\n}\n\nclass FeatureDecorator: Feature {\n\tprivate var feature: Feature?\n\t\n\tinit(feature: Feature? = nil) {\n\t\tself.feature = feature\n\t}\n\t\n\tfunc textOperation(text: String) {\n\t\tfeature?.textOperation(text: text)\n\t}\n}\n\nclass BasicVersionFeature: FeatureDecorator {\n\toverride func textOperation(text: String) {\n\t\tsuper.textOperation(text: text)\n\t\tprint(\"\\(text.count)\")\n\t}\n}\n\nclass ProfessionalVersionFeature: FeatureDecorator {\n\toverride func textOperation(text: String) {\n\t\tsuper.textOperation(text: text)\n\t\tprint(\"\\(text.uppercased())\")\n\t}\n}\n\nclass UltimateVersionFeature: FeatureDecorator {\n\toverride func textOperation(text: String) {\n\t\tsuper.textOperation(text: text)\n\t\tprint(\"ULTIMATE: \\(text)\")\n\t}\n}\n\nlet textToFormat = \"Hello Decorator\"\n\nlet basicProduct = BasicVersionFeature(feature: UltimateVersionFeature())\nbasicProduct.textOperation(text: textToFormat)\n\nlet professionalProduct = ProfessionalVersionFeature(feature: UltimateVersionFeature())\nprofessionalProduct.textOperation(text: textToFormat)\n\nlet ultimateProduct = BasicVersionFeature(feature: UltimateVersionFeature(feature: ProfessionalVersionFeature()))\nultimateProduct.textOperation(text: textToFormat)\n\nlet ultimateLightProduct = BasicVersionFeature(feature: UltimateVersionFeature())\nultimateLightProduct.textOperation(text: textToFormat)\n<\/code><\/pre>\n<\/div>\n<p>Now we can create variations of any type of product &#8211; it is enough to initialize the combined types at the stage of application launch, the example below is the creation of the Ultimate + Professional version:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>ultimateProfessionalProduct.textOperation(text: textToFormat)\n<\/code><\/pre>\n<\/div>\n<h3>Sources<\/h3>\n<p><a href=\"https:\/\/refactoring.guru\/ru\/design-patterns\/decorator\" target=\"_blank\" rel=\"noopener\">https:\/\/refactoring.guru\/ru\/design-patterns\/decorator <\/a><\/p>\n<h3>Source code<\/h3>\n<p><a href=\"https:\/\/gitlab.com\/demensdeum\/patterns\" target=\"_blank\" rel=\"noopener\">https:\/\/gitlab.com\/demensdeum\/patterns<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Decorator pattern is a structural design pattern. Decorator is used as an alternative to inheritance to extend the functionality of classes.There is a task to expand the functionality of the application depending on the type of product. The customer needs three types of product &#8211; Basic, Professional, Ultimate.Basic &#8211; counts the number of characters,<a class=\"more-link\" href=\"https:\/\/demensdeum.com\/blog\/hi\/2019\/05\/27\/decorator-pattern\/\">Continue reading <span class=\"screen-reader-text\">&#8220;Pattern Decorator&#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":[104,95],"class_list":["post-1998","post","type-post","status-publish","format-standard","hentry","category-techie","category-tutorials","tag-decorator","tag-patterns","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\/1998","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=1998"}],"version-history":[{"count":22,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/1998\/revisions"}],"predecessor-version":[{"id":3957,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/1998\/revisions\/3957"}],"wp:attachment":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/media?parent=1998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/categories?post=1998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/tags?post=1998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}