{"id":76014,"date":"2024-05-06T12:39:37","date_gmt":"2024-05-06T16:39:37","guid":{"rendered":"https:\/\/bangla.sitestree.com\/?p=76014"},"modified":"2024-05-06T13:21:08","modified_gmt":"2024-05-06T17:21:08","slug":"java-decorator-design-pattern","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=76014","title":{"rendered":"Java : Decorator Design pattern"},"content":{"rendered":"<p><strong>Decorator Design pattern:<\/strong><\/p>\n<p><strong>Decorator<\/strong> is a structural design pattern that lets you attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors.<\/p>\n<p><strong>Example: <\/strong><\/p>\n<p>Assume: A notifier class\/object can send only email messages. But the application at a later time may want to use text\/SMS, FB, or similar messages. With decorator pattern: decorate the Notifier class with other behaviours such as send SMS, send fb message, send twitter message (i.e. may create additional classes\/objects (create link to original notifier class with interfaces\/inheritance and aggregations\/compositions) ). Then the client can use the original notifier class\/object but dynamically point to the other classes\/objects and use the corresponding sendMessage() method\/behaviour for SMS, Text, FB, Twitter or similar messaging\/notification.<\/p>\n<p>Note: Inheritance\/subclasses can be a choice but they have limitations. Aggregations\/Compositions\/interfaces will be used for the purpose of Decorator pattern.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/05\/image-8.png\" rel=\"attachment wp-att-76015\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/05\/image-8.png?resize=750%2C750\" alt=\"\" title=\"image-8-png\" width=\"750\" height=\"750\" class=\"alignnone size-full wp-image-76015\" srcset=\"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/05\/image-8.png?resize=150%2C150 150w, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/05\/image-8.png?resize=50%2C50 50w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/a><\/p>\n<p><strong>Note: \u201c<\/strong>When does a simple wrapper become the real decorator? As I mentioned, the wrapper implements the same interface as the wrapped object. That\u2019s why from the client\u2019s perspective these objects are identical.<strong>\u201d<\/strong><\/p>\n<p><strong>Ref: <a href=\"https:\/\/refactoring.guru\/design-patterns\/decorator\">https:\/\/refactoring.guru\/design-patterns\/decorator<\/a> <\/strong><\/p>\n<p><strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/Decorator_pattern\">https:\/\/en.wikipedia.org\/wiki\/Decorator_pattern<\/a> <\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Decorator Design pattern: Decorator is a structural design pattern that lets you attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors. Example: Assume: A notifier class\/object can send only email messages. But the application at a later time may want to use text\/SMS, FB, or similar messages. &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=76014\">Continue reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1962,182],"tags":[],"class_list":["post-76014","post","type-post","status-publish","format-standard","hentry","category-java-design-patterns","category---blog","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":78425,"url":"http:\/\/bangla.sitestree.com\/?p=78425","url_meta":{"origin":76014,"position":0},"title":"Factory Design Pattern: Examples in Java","author":"Sayed","date":"August 28, 2025","format":false,"excerpt":"Observe the code below: ShapeFactory factory = new ShapeFactory(); Shape shape1 = factory.getShape(\"circle\"); Shape shape2 = factory.getShape(\"square\"); Shape shape3 = factory.getShape(\"rectangle\"); shape1.draw(); shape2.draw(); shape3.draw(); We wanted to create objects such as shape1, shape2, and shape 3. We did not directly (create and) call the class for the required objects. We\u2026","rel":"","context":"In &quot;Java Design Patterns and OOP&quot;","block_context":{"text":"Java Design Patterns and OOP","link":"http:\/\/bangla.sitestree.com\/?cat=1962"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78427,"url":"http:\/\/bangla.sitestree.com\/?p=78427","url_meta":{"origin":76014,"position":1},"title":"Builder Pattern vs \u2699\ufe0f Factory Pattern in Java","author":"Sayed","date":"August 28, 2025","format":false,"excerpt":"From AI Tools\/Open AI\/ChatGPT \" Perfect \ud83d\udc4d Here\u2019s a copyright-free comparison table of the Builder Pattern vs Factory Pattern in Java, styled for easy posting on Facebook\/Blog: \ud83c\udfd7\ufe0f Builder Pattern vs \u2699\ufe0f Factory Pattern in Java AspectBuilder PatternFactory PatternTypeCreational design patternCreational design patternPurposeTo construct complex objects step by step with\u2026","rel":"","context":"In &quot;Java Design Patterns and OOP&quot;","block_context":{"text":"Java Design Patterns and OOP","link":"http:\/\/bangla.sitestree.com\/?cat=1962"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/08\/image-27.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/08\/image-27.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/08\/image-27.png?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/08\/image-27.png?resize=700%2C400 2x"},"classes":[]},{"id":78423,"url":"http:\/\/bangla.sitestree.com\/?p=78423","url_meta":{"origin":76014,"position":2},"title":"Builder Design Pattern: Example in Java","author":"Sayed","date":"August 28, 2025","format":false,"excerpt":"Observe these two examples of Object Creation from the Student Class Student student1 = new Student.Builder(\"Alice\") .age(12) .grade(\"7th\") .email(\"alice@email.com\") .build(); Student student2 = new Student.Builder(\"Bob\") .grade(\"5th\") .build(); We did not call the constructor with the property values, but we built the object step by step with only the properties and\u2026","rel":"","context":"In &quot;Java Design Patterns and OOP&quot;","block_context":{"text":"Java Design Patterns and OOP","link":"http:\/\/bangla.sitestree.com\/?cat=1962"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78099,"url":"http:\/\/bangla.sitestree.com\/?p=78099","url_meta":{"origin":76014,"position":3},"title":"Java Creational Design Patterns","author":"Sayed","date":"May 4, 2025","format":false,"excerpt":"5 types of creational design patterns: Factory Design Patterns: Purpose: Create Objects, Keep Object Creation Centralized Abstract Factory Design Patterns Singleton Design Pattern: Limit instantiation\u00a0 of a clas to only one instance Prototype Design Patterns: Object creation based on Prototype Object Instance; Simpler Object Creation than Factory. Builder Design Patterns:\u2026","rel":"","context":"In &quot;Root&quot;","block_context":{"text":"Root","link":"http:\/\/bangla.sitestree.com\/?cat=1"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/05\/image-3.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/05\/image-3.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/05\/image-3.png?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2025\/05\/image-3.png?resize=700%2C400 2x"},"classes":[]},{"id":78393,"url":"http:\/\/bangla.sitestree.com\/?p=78393","url_meta":{"origin":76014,"position":4},"title":"Java Design Patterns Simplified","author":"Sayed","date":"August 24, 2025","format":false,"excerpt":"From AI Tools\/OpenAI\/Internet \" Here\u2019s a short, copyright-free version suitable for Facebook or a blog post: Java Design Patterns Simplified Design patterns are proven solutions to common programming problems. They make code more reusable, flexible, and easier to maintain. 1\ufe0f\u20e3 Creational Patterns \u2013 How objects are created.Examples: Singleton, Factory, Builder,\u2026","rel":"","context":"In &quot;Anything JAVA&quot;","block_context":{"text":"Anything JAVA","link":"http:\/\/bangla.sitestree.com\/?cat=1975"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":76023,"url":"http:\/\/bangla.sitestree.com\/?p=76023","url_meta":{"origin":76014,"position":5},"title":"Proxy Pattern","author":"Sayed","date":"May 6, 2024","format":false,"excerpt":"Proxy Pattern: \u201cProxy is a structural design pattern that lets you provide a substitute or placeholder for another object. A proxy controls access to the original object, allowing you to perform something either before or after the request gets through to the original object.\u201d \u201cA credit card is a proxy\u2026","rel":"","context":"In &quot;Java Design Patterns and OOP&quot;","block_context":{"text":"Java Design Patterns and OOP","link":"http:\/\/bangla.sitestree.com\/?cat=1962"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/bangla.sitestree.com\/wp-content\/uploads\/2024\/05\/image-13.png?resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/76014","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=76014"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/76014\/revisions"}],"predecessor-version":[{"id":76016,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/76014\/revisions\/76016"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=76014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=76014"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=76014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}