{"id":26,"date":"2020-09-20T08:27:07","date_gmt":"2020-09-20T08:27:07","guid":{"rendered":"https:\/\/system.camp\/index.php\/2020\/09\/20\/the-decorator-design-pattern\/"},"modified":"2020-10-06T09:56:50","modified_gmt":"2020-10-06T09:56:50","slug":"the-decorator-design-pattern","status":"publish","type":"post","link":"https:\/\/system.camp\/tutorial\/the-decorator-design-pattern\/","title":{"rendered":"The Decorator Design Pattern"},"content":{"rendered":"\n

The decorator design pattern is a structural design pattern that is used for extending the functionality of existing classes without much of a change in the codebase. You can also look at this design pattern as a wrapper around the existing functionality.<\/p>\n\n\n\n

Let\u2019s dive into the example which will make things much clearer. We have some simple shapes for this example, now there\u2019s a business requirement for filling it with a colour.<\/p>\n\n\n\n

Existing classes and interface<\/em><\/p>\n\n\n\n

As a base, we have the Shape interface<\/p>\n\n\n\n

public interface Shape {\n    void draw();\n}\n<\/code><\/pre>\n\n\n\n

We have a Rectangle and Circle which implement the shape interface.<\/p>\n\n\n\n

public class Circle implements Shape {\n    @Override\n    public void draw() {\n        System.out.println(\"I have drawn a circle\");\n    }\n}\n<\/code><\/pre>\n\n\n\n
public class Rectangle implements Shape {\n    @Override\n    public void draw() {\n        System.out.println(\"I have drawn a Rectangle\");\n    }\n}\n<\/code><\/pre>\n\n\n\n

New additions to extend the functionality<\/em><\/p>\n\n\n\n

As per the new requirements, we have to fill the shapes with a colour. Thus, we add an abstract class that will help extend the functionality.<\/p>\n\n\n\n

public abstract class ColourFillerDecorator implements Shape {\n    protected Shape shape;\n    public ColourFillerDecorator(Shape shape) {\n        this.shape = shape;\n    }\n    public void draw() {\n        shape.draw();\n    }\n}\n<\/code><\/pre>\n\n\n\n

We now extend the ColourFillerDecorator<\/code> to have a concrete class which has a function to fill any colour inside the shape.<\/p>\n\n\n\n

public class ColourFiller extends ColourFillerDecorator {\n    private String colour;\n    public ColourFiller(Shape shape, String colour) {\n        super(shape);\n        this.colour = colour;\n    }\n    private void fill() {\n        System.out.println(\"Filled with: \" + colour);\n    }\n    @Override\n    public void draw() {\n        shape.draw();\n        fill();\n    }\n}\n<\/code><\/pre>\n\n\n\n

We now demonstrate the Decorator Pattern with the code below. If you want to use the decorator, keep the value of decorate as true, otherwise, you can change it to false<\/code>.<\/p>\n\n\n\n

public class DecoratorDemo {\n    public static void main(String[] args) {\n        Shape circle = new Circle();\n        Shape rectangle = new Rectangle();\n        boolean decorate = true;\n        if (decorate) {\n            ColourFiller circleColourFiller = new ColourFiller(circle, \"RED\");\n            ColourFiller rectangleColourFiller = new ColourFiller(rectangle, \"BLUE\");\n            circleColourFiller.draw();\n            rectangleColourFiller.draw();\n        } else {\n            circle.draw();\n            rectangle.draw();\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n

Output when decorate<\/code> value is true<\/code>:<\/p>\n\n\n\n

I have drawn a circle\nFilled with: RED\nI have drawn a Rectangle\nFilled with: BLUE\n<\/code><\/pre>\n\n\n\n

Output when decorate<\/code> value is false<\/code>:<\/p>\n\n\n\n

I have drawn a circle\nI have drawn a Rectangle\n<\/code><\/pre>\n\n\n\n

Github link<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

The decorator design pattern is a structural design pattern that is used for extending the functionality of existing classes without much of a change in the codebase. You can also look at this design pattern as a wrapper around the existing functionality. Let\u2019s dive into…<\/p>\n","protected":false},"author":2,"featured_media":66,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[35],"tags":[10,17,18,8],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts\/26"}],"collection":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":4,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"predecessor-version":[{"id":70,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/posts\/26\/revisions\/70"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/media\/66"}],"wp:attachment":[{"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/system.camp\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}