{"id":9235,"date":"2020-11-09T11:00:26","date_gmt":"2020-11-09T02:00:26","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=9235"},"modified":"2020-11-06T20:07:56","modified_gmt":"2020-11-06T11:07:56","slug":"decorator-design-pattern","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/9235","title":{"rendered":"Decorator Design Pattern"},"content":{"rendered":"\n<p>Decorator is a structural design pattern that allows you to attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors.<\/p>\n\n\n\n<p>Extending a class is the first thing that comes to mind when you need to modify the behavior of an object. However, inheritance has several important caveats that you should be aware of. Inheritance is static. You cannot modify the behavior of an existing object at run time. You can only replace the entire object with one created from a different subclass. Subclasses can have only one main class. In most languages, inheritance does not allow a class to inherit behaviors from multiple classes at the same time. <\/p>\n\n\n\n<p>One of the ways to overcome these caveats is through aggregation or composition rather than inheritance. Both alternatives work in much the same way: one object has a reference to another and delegates some work to it, whereas with inheritance, the object itself can do that work, inheriting the behavior of its superclass.<\/p>\n\n\n\n<p>Lets look at the examples I created as below.<\/p>\n\n\n\n<h3>Sample PHP<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n\/**\n * The interface that must be implemented by concrete class or decorator class\n * Many methods might exist in a real world application.\n *\/\ninterface Home\n{\n    public function setup();\n}\n\n\/**\n * An example simple concrete class implementing the interface through the setup method.\n *\/\nclass ModernHome implements Home\n{\n    public $addon;\n\n    public function setup()\n    {\n        $this->addon[] = \"Added extra bed room\";\n    }\n}\n\n\/**\n * Here is the base decorator class referncing the home interface.\n * Implementations don't exist here as this is the scheme of the concrete classes that will extend to it.\n *\/\nabstract class ModernHomeDecorator implements Home\n{\n    protected $home;\n\n    public function __construct(Home $home)\n    {\n        $this->home = $home;\n    }\n\n    abstract public function setup();\n}\n\n\/**\n * Concrete decorator classes extending the  basedecorator(wrapping the original ModernHome Instance).\n *\/\nclass CinemaDecorator extends ModernHomeDecorator\n{\n    public function setup()\n    {\n        $this->home->setup();\n        $this->home->addon[] = \"added mini cinema\";\n        $this->addon = $this->home->addon;\n    }\n}\n\n\nclass PoolDecorator extends ModernHomeDecorator\n{\n    public function setup()\n    {\n        $this->home->setup();\n        $this->home->addon[] = \"added pool\";\n        $this->addon = $this->home->addon;\n    }\n}\n\n\n\/**\n * Clients just have to pass the modernhome instance to each decorator class that they want.\n * We can see here it is wrapping step by step. ModenHome is wrapped by cinema. Afterwards, cinema is wrapped by pool.\n * Array ( [0] => Added extra bed room [1] => added mini cinema [2] => added pool )\n *\/\n\n$home = new ModernHome();\n$home = new CinemaDecorator($home);\n$home = new PoolDecorator($home);\n$home->setup();\n\nprint_r($home->addon);\n<\/code><\/pre>\n\n\n\n<h3><a href=\"https:\/\/github.com\/HlaingTinHtun\/GOF-Design-Patterns\/blob\/master\/articles\/GOF_Patterns\/structural_patterns\/decorator.md#c\"><\/a>Sample C#<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\n\nnamespace HelloWorld\n{\n\n    \/**\n     * The interface that must be implemented by concrete class or decorator class\n     * Many methods might exist in a real world application.\n     *\/\n    abstract class Home\n    {\n        public abstract string setup();\n    }\n\n    \/**\n     * An example simple concrete class implementing the interface through the setup method.\n     *\/\n    class ModernHome : Home\n    {\n        public override string setup()\n        {\n            return \"ModernHome\";\n        }\n    }\n\n    \/**\n     * Here is the base decorator class referncing the home interface.\n     * Implementations don't exist here as this is the scheme of the concrete classes that will extend to it.\n     * we have a condition here to do the wrapping process.\n     *\/\n    abstract class Decorator : Home\n    {\n        protected Home _Home;\n\n        public Decorator(Home Home)\n        {\n            this._Home = Home;\n        }\n\n\n        public override string setup()\n        {\n            if (this._Home != null)\n            {\n                return this._Home.setup();\n            }\n            else\n            {\n                return string.Empty;\n            }\n        }\n    }\n\n    \/**\n     * Concrete decorator classes extending the  basedecorator(wrapping the original ModernHome Instance).\n     *\/\n    class CinemaDecorator : Decorator\n    {\n        public CinemaDecorator(Home comp) : base(comp){}\n\n        \n        public override string setup()\n        {\n            return $\"CinemaDecorator({base.setup()})\";\n        }\n    }\n\n    class PoolDecorator : Decorator\n    {\n        public PoolDecorator(Home comp) : base(comp){}\n\n        public override string setup()\n        {\n            return $\"PoolDecorator({base.setup()})\";\n        }\n    }\n\n\n    \/**\n     * Clients just have to pass the modernhome instance to each decorator class that they want.\n     * We can see here it is wrapping step by step. ModenHome is wrapped by cinema. Afterwards, cinema is wrapped by pool.\n     * PoolDecorator(CinemaDecorator(ModernHome))\n     *\/\n    class Program\n    {\n        static void Main(string[] args)\n        {\n\n            var moden = new ModernHome();\n            CinemaDecorator cinema = new CinemaDecorator(moden);\n            PoolDecorator pool = new PoolDecorator(cinema);\n            Console.WriteLine(pool.setup());\n            \n        }\n    }\n\n}\n<\/code><\/pre>\n\n\n\n<p>By Yuuma.<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9235\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/9235\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Decorator Design Pattern\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9235\" data-text=\"Decorator Design Pattern\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9235\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9235\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Decorator is a structural design pattern that allows you to attach new behaviors to objects by placing these o [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9235"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=9235"}],"version-history":[{"count":2,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9235\/revisions"}],"predecessor-version":[{"id":9238,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9235\/revisions\/9238"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=9235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=9235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=9235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}