{"id":4940,"date":"2024-02-18T14:28:16","date_gmt":"2024-02-18T14:28:16","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=4940"},"modified":"2024-11-09T08:30:12","modified_gmt":"2024-11-09T08:30:12","slug":"decorator-pattern","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/decorator-pattern\/","title":{"rendered":"Decorator Pattern"},"content":{"rendered":"<blockquote><p>Decorator pattern dynamically changes the functionality of an object at run-time without creating new object rather by adding behavior and attribute to existing Object.<\/p><\/blockquote>\n<p><strong class=\"ctaHeader3\">Why Decorator Pattern?<\/strong><\/p>\n<p>It allows adding new functionality to an existing object without altering its original class. This pattern involves wrapping the original object in a decorator class, which has the same interface as the object it decorates. The decorator class then adds its behavior to the object during runtime, allowing for increased flexibility and modularity in programming.<\/p>\n<p><strong class=\"ctaHeader3\">When Decorator Pattern?<\/strong><br \/>\nIf you want to add additional functionality to an existing object (i.e. already instantiated class at runtime), as opposed to object&#8217;s class and\/or subclass then Decorator pattern should be used. It is easy to add functionality to an entire class of objects by subclassing an object&#8217;s class, but it is impossible to extend a single object this way. With the Decorator Pattern, you can add functionality to a single object and leave others like it unmodified.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2024\/11\/decorator-pattern-structure.png\" alt=\"\" \/><\/p>\n<p><strong class=\"ctaHeader3\">How Decorator Pattern Implemented?<\/strong><\/p>\n<ol>\n<li>Create a Interface and a concrete base class which implements Interface. This base class has only default constructor<\/li>\n<li>Create Concrete Decorator Classes which implements Interface. The Constructor of Decorator takes BaseDecorator class(vanilla Object) as argument<\/li>\n<li>The Decorator keeps appending the attributes and behavior to this Vanilla Object<\/li>\n<\/ol>\n<p><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2024\/11\/DecoratorUML.png\" alt=\"\" \/><\/p>\n<p><strong>IGift.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic interface IGift {\r\n    public String addGift();\r\n}\r\n<\/pre>\n<p><strong>ConcreteGift.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class ConcreteGift implements IGift{\r\n\r\n    @Override\r\n    public String addGift() {\r\n        return &quot;Gift Item to Cherish&quot;;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>GiftFlowerDecorator.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class GiftFlowerDecorator implements IGift{\r\n    private IGift gift;\r\n\r\n    public GiftFlowerDecorator(IGift gift) {\r\n        this.gift = gift;\r\n    }\r\n\r\n    @Override\r\n    public String addGift() {\r\n        if(this.gift != null){\r\n            return this.gift.addGift() + &quot; with flowers&quot;;\r\n        }\r\n\r\n        return null;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>GiftMessageDecorator.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class GiftMessageDecorator implements IGift{\r\n    private String message;\r\n    private IGift gift;\r\n\r\n    public GiftMessageDecorator(IGift gift){\r\n        this.gift = gift;\r\n    }\r\n\r\n    public void setMessage(String message){\r\n        this.message = message;\r\n    }\r\n\r\n    @Override\r\n    public String addGift() {\r\n        if (this.gift != null) {\r\n            return this.gift.addGift() + this.message;\r\n        }\r\n        return null;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>GiftWrapperDecorator.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class GiftWrapperDecorator implements IGift{\r\n    private IGift gift;\r\n\r\n    public GiftWrapperDecorator(IGift gift) {\r\n        this.gift = gift;\r\n    }\r\n\r\n    @Override\r\n    public String addGift() {\r\n        if(this.gift != null){\r\n            return this.gift.addGift() + &quot; and fully Wrapped&quot;;\r\n        }\r\n\r\n        return null;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>BuyGiftOnline.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class BuyGiftOnline {\r\n    public static void main(String&#x5B;] args) {\r\n        IGift objGift = new ConcreteGift();\r\n        System.out.println(objGift.addGift());\r\n\r\n        GiftMessageDecorator objGiftMsgDec = new GiftMessageDecorator(objGift);\r\n        objGiftMsgDec.setMessage(&quot; with Message&quot;);\r\n        System.out.println(objGiftMsgDec.addGift());\r\n\r\n        GiftWrapperDecorator objWrapDec = new GiftWrapperDecorator(objGiftMsgDec);\r\n        System.out.println(objWrapDec.addGift());\r\n\r\n        GiftFlowerDecorator objFlowerDec = new GiftFlowerDecorator(objWrapDec);\r\n        System.out.println(objFlowerDec.addGift());\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>\r\nGift Item to Cherish\r\nGift Item to Cherish with Message\r\nGift Item to Cherish with Message and fully Wrapped\r\nGift Item to Cherish with Message and fully Wrapped with flowers\r\n<\/pre>\n<p>Below we have one more example of Decorator Pattern<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2024\/11\/LoggerDecorator.png\" alt=\"\" \/><\/p>\n<p><strong>ILogger.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic interface ILogger {\r\n    String log(String msg);\r\n}\r\n<\/pre>\n<p><strong>BasicConcreteLogger.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class BasicConcreteLogger implements ILogger{\r\n    public BasicConcreteLogger() {\r\n    }\r\n\r\n    @Override\r\n    public String log(String msg) {\r\n        System.out.println(msg);\r\n        return msg;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>LoggerWithTimeStampDecorator.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class LoggerWithTimeStampDecorator implements ILogger{\r\n    ILogger logger;\r\n\r\n    public LoggerWithTimeStampDecorator(ILogger logger) {\r\n        this.logger = logger;\r\n    }\r\n\r\n    @Override\r\n    public String log(String msg) {\r\n        msg = msg + &quot; TimeStamp:-&quot;+ new java.util.Date();\r\n        System.out.println(msg);\r\n        return msg;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>LoggerWithUUIDDecorator.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class LoggerWithUUIDDecorator implements ILogger{\r\n    ILogger logger;\r\n    public LoggerWithUUIDDecorator(ILogger logger) {\r\n        this.logger = logger;\r\n    }\r\n\r\n    @Override\r\n    public String log(String msg) {\r\n        msg = msg + &quot; UUID:- &quot;+ UUID.randomUUID().toString();\r\n        System.out.println(msg);\r\n        return msg;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>LoggerUtil.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class LoggerUtil {\r\n    public static void main(String&#x5B;] args) {\r\n        BasicConcreteLogger objLogger = new BasicConcreteLogger();\r\n        String msg = objLogger.log(&quot;Sample Log Message&quot;);\r\n\r\n        LoggerWithTimeStampDecorator objLTS = new LoggerWithTimeStampDecorator(objLogger);\r\n        msg = objLTS.log(msg);\r\n\r\n        LoggerWithUUIDDecorator objUUID = new LoggerWithUUIDDecorator(objLogger);\r\n        objUUID.log(msg);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nSample Log Message\r\nSample Log Message TimeStamp:-Sat Nov 09 13:44:31 IST 2024\r\nSample Log Message TimeStamp:-Sat Nov 09 13:44:31 IST 2024 UUID:- deacc0f1-8817-41c3-97d5-e05dd9909b57\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Decorator pattern dynamically changes the functionality of an object at run-time without creating new object rather by adding behavior and attribute to existing Object. Why Decorator Pattern? It allows adding new functionality to an existing object without altering its original class. This pattern involves wrapping the original object in a decorator class, which has the&hellip; <a href=\"https:\/\/codethataint.com\/blog\/decorator-pattern\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[355,358],"tags":[],"class_list":["post-4940","post","type-post","status-publish","format-standard","hentry","category-structural-pattern","category-decortator-pattern"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=4940"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4940\/revisions"}],"predecessor-version":[{"id":5326,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4940\/revisions\/5326"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=4940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=4940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=4940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}