{"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 We have a Rectangle and Circle which implement the shape interface.<\/p>\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 We now extend the 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 Output when public interface Shape {\n void draw();\n}\n<\/code><\/pre>\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
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
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
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
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