{"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\/de\/2019\/05\/27\/decorator-pattern\/","title":{"rendered":"Musterdekorateur"},"content":{"rendered":"<p>Das Decorator-Muster bezieht sich auf strukturelle Designmuster.<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 \/>\nDer Dekorator wird als Alternative zur Vererbung verwendet, um die Funktionalit\u00e4t von Klassen zu erweitern.<br \/>Je nach Produkttyp besteht die Aufgabe, die Funktionalit\u00e4t der Anwendung zu erweitern. Der Kunde ben\u00f6tigt drei Arten von Produkten &#8211; Einfach, professionell, ultimativ.<br \/>Basic&#8211; z\u00e4hlt die Anzahl der Zeichen, Professional &#8211; Funktionen Basic + druckt Text in Gro\u00dfbuchstaben, Ultimate &#8211; Basic + Professional + druckt Text mit der Aufschrift ULTIMATE.<br \/>Wir implementieren es mithilfe der Vererbung:<\/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>Jetzt besteht die Anforderung, das Produkt \u201eUltimate Light\u201c umzusetzen &#8211; Basic + Ultimate, jedoch ohne die Funktionen der Professional-Version. Das erste OH! passiert, weil&#8230; Sie m\u00fcssen f\u00fcr eine so einfache Aufgabe eine separate Klasse erstellen und den Code duplizieren.<br \/>Lassen Sie uns die Implementierung mithilfe der Vererbung fortsetzen:<\/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>Das Beispiel kann zur Verdeutlichung weiterentwickelt werden, aber schon jetzt ist die Komplexit\u00e4t der Unterst\u00fctzung eines Systems auf Basis einer Vererbungsbasis sichtbar &#8211; umst\u00e4ndlich und mangelnde Flexibilit\u00e4t.<br \/>Ein Dekorator ist eine Reihe von Protokollen, die die Funktionalit\u00e4t beschreiben, eine abstrakte Klasse, die einen Verweis auf eine untergeordnete konkrete Instanz der Dekoratorklasse enth\u00e4lt, die die Funktionalit\u00e4t erweitert.<br \/>Schreiben wir das obige Beispiel mit dem Muster um:<\/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>Jetzt k\u00f6nnen wir Variationen jeder Art von Produkt erstellen &#8211; Es reicht aus, die kombinierten Typen beim Anwendungsstart zu initialisieren. Das folgende Beispiel zeigt die Erstellung der 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>Quellen<\/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>Quellcode<\/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>Das Decorator-Muster bezieht sich auf strukturelle Designmuster. Der Dekorator wird als Alternative zur Vererbung verwendet, um die Funktionalit\u00e4t von Klassen zu erweitern.Je nach Produkttyp besteht die Aufgabe, die Funktionalit\u00e4t der Anwendung zu erweitern. Der Kunde ben\u00f6tigt drei Arten von Produkten &#8211; Einfach, professionell, ultimativ.Basic&#8211; z\u00e4hlt die Anzahl der Zeichen, Professional &#8211; Funktionen Basic + druckt<a class=\"more-link\" href=\"https:\/\/demensdeum.com\/blog\/de\/2019\/05\/27\/decorator-pattern\/\">Continue reading <span class=\"screen-reader-text\">&#8220;Musterdekorateur&#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":"de","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\/de\/wp-json\/wp\/v2\/posts\/1998","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demensdeum.com\/blog\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demensdeum.com\/blog\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/de\/wp-json\/wp\/v2\/comments?post=1998"}],"version-history":[{"count":22,"href":"https:\/\/demensdeum.com\/blog\/de\/wp-json\/wp\/v2\/posts\/1998\/revisions"}],"predecessor-version":[{"id":3957,"href":"https:\/\/demensdeum.com\/blog\/de\/wp-json\/wp\/v2\/posts\/1998\/revisions\/3957"}],"wp:attachment":[{"href":"https:\/\/demensdeum.com\/blog\/de\/wp-json\/wp\/v2\/media?parent=1998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/de\/wp-json\/wp\/v2\/categories?post=1998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/de\/wp-json\/wp\/v2\/tags?post=1998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}