diff --git a/com/javadevjournal/design/structural/decorator/Circle.java b/com/javadevjournal/design/structural/decorator/Circle.java new file mode 100644 index 0000000..84248dc --- /dev/null +++ b/com/javadevjournal/design/structural/decorator/Circle.java @@ -0,0 +1,12 @@ +package javadevjournal.design.structural.decorator; + +/** + * @author Kunwar + */ +public class Circle implements Shape { + + @Override + public void drawShape() { + System.out.println("The shape is: Circle"); + } +} diff --git a/com/javadevjournal/design/structural/decorator/DecoratorPatternDemo.java b/com/javadevjournal/design/structural/decorator/DecoratorPatternDemo.java new file mode 100644 index 0000000..95140b6 --- /dev/null +++ b/com/javadevjournal/design/structural/decorator/DecoratorPatternDemo.java @@ -0,0 +1,29 @@ +package javadevjournal.design.structural.decorator;// DecoratorPatternDemo.java + +/** + * @author Kunwar + */ +public class DecoratorPatternDemo { + + public static void main(String[] args) { + // Creating objects of Shape interface + Shape circle = new Circle(); + Shape rectangle = new Rectangle(); + + // Creating objects of decorated classes + Shape redCircle = new RedShapeDecorator(new Circle()); + Shape redRectangle = new RedShapeDecorator(new Rectangle()); + + System.out.println("Circle with normal fill"); + circle.drawShape(); + + System.out.println("Rectangle with normal fill"); + rectangle.drawShape(); + + System.out.println("\nCircle of red fill"); + redCircle.drawShape(); + + System.out.println("\nRectangle of red fill"); + redRectangle.drawShape(); + } +} diff --git a/com/javadevjournal/design/structural/decorator/Rectangle.java b/com/javadevjournal/design/structural/decorator/Rectangle.java new file mode 100644 index 0000000..1b02282 --- /dev/null +++ b/com/javadevjournal/design/structural/decorator/Rectangle.java @@ -0,0 +1,12 @@ +package javadevjournal.design.structural.decorator;// Class 1 + +/** + * @author Kunwar + */ +public class Rectangle implements Shape { + + @Override + public void drawShape() { + System.out.println("The shape is: Rectangle"); + } +} diff --git a/com/javadevjournal/design/structural/decorator/RedShapeDecorator.java b/com/javadevjournal/design/structural/decorator/RedShapeDecorator.java new file mode 100644 index 0000000..8a49c58 --- /dev/null +++ b/com/javadevjournal/design/structural/decorator/RedShapeDecorator.java @@ -0,0 +1,27 @@ +package javadevjournal.design.structural.decorator; + +/** + * @author Kunwar + */ +public class RedShapeDecorator extends ShapeDecorator { + + public RedShapeDecorator(Shape decoratedShape) { + super(decoratedShape); + } + + @Override + public void drawShape() { + decoratedShape.drawShape(); + //additional method to change the behavior of the shape object + shapeFill(decoratedShape); + } + + /** + * This method will change the behavior of the shape object at runtime. + * + * @param decoratedShape + */ + private void shapeFill(Shape decoratedShape) { + System.out.println("Shape Fill color: Red"); + } +} diff --git a/com/javadevjournal/design/structural/decorator/Shape.java b/com/javadevjournal/design/structural/decorator/Shape.java new file mode 100644 index 0000000..ba2fd3d --- /dev/null +++ b/com/javadevjournal/design/structural/decorator/Shape.java @@ -0,0 +1,8 @@ +package javadevjournal.design.structural.decorator; + +/** + * @author Kunwar + */ +public interface Shape { + void drawShape(); +} diff --git a/com/javadevjournal/design/structural/decorator/ShapeDecorator.java b/com/javadevjournal/design/structural/decorator/ShapeDecorator.java new file mode 100644 index 0000000..3be7a9c --- /dev/null +++ b/com/javadevjournal/design/structural/decorator/ShapeDecorator.java @@ -0,0 +1,19 @@ +package javadevjournal.design.structural.decorator; + +/** + * @author Kunwar + */ +public abstract class ShapeDecorator implements Shape { + + //protected object of Shape Interface. + protected Shape decoratedShape; + + public ShapeDecorator(Shape decoratedShape) { + this.decoratedShape = decoratedShape; + } + + //calling the drawShape method on decoratedShape + public void drawShape() { + decoratedShape.drawShape(); + } +}