In: Computer Science
subject pattern design (decorator)
Description • Suppose you were asked to implement a drawing tool • This drawing tool contains the shape supper class which includes draw() and getDescription functionalities • And you have two concrete class; Circle and Rectangle • This tool enables us to draw circles and rectangles but we want to extend this tool to enable the user to decorate the shapes with • Fill-color • border-color • border-thickness • border-style; dashed , dotted
Drawing Tool Exercise • We have a drawing tool, that has • Shape interface, which includes draw(), and description() methods • Colors and Border styles are defined as shown here • Requirement: • extend this tool to allow users to draw two concrete components; circle & rectangle • allow users to add features to shape (shape-decorators); such as fill-color, border-color, and border-thickness, border-style public enum Color { RED, GREEN, BLUE, WHITE, BLACK }
public enum BorderStyle { DASHED, SOLID, DOTTED, WHITE, BLACK }
What to Submit • Please show how decorator pattern can be applied on drawing tool, using class diagram • what is your main component, concrete components, main decorator, and concrete decorators • Write code to show the implementation • Write a class to test your code : • show in code how to draw circle, filled with red color, and has border with following properties, dashed border, in black, and has 2.0 as thickness
Shape.java
package design.decorator;
public interface Shape
{
    void draw();
    void resize();
    String description();
    boolean isHide();
}
Circle
package design.decorator;
public class Circle implements Shape
{
    @Override
    public void draw()
    {
        System.out.println("Drawing Circle");
    }
    @Override
    public void resize()
    {
        System.out.println("Resizing Circle");
    }
    @Override
    public String description()
    {
        return("Circle object");
    }
    @Override
    public boolean isHide()
    {
        return false;
    }
}
Rectangle
package design.decorator;
public class Rectangle implements Shape
{
    @Override
    public void draw()
    {
        System.out.println("Drawing Rectangle");
    }
    @Override
    public void resize()
    {
        System.out.println("Resizing Rectangle");
    }
    @Override
    public String description()
    {
        return("Rectangle object");
    }
    @Override
    public boolean isHide()
    {
        return false;
    }
}
ShapeDecorator
package design.decorator;
public abstract class ShapeDecorator implements Shape
{
    protected Shape decoratredShape;
    public ShapeDecorator(Shape decoratedShape)
    {
        super();
        this.decoratedShape = decoratedShape;
    }
}
enum Color
package design.decorator;
public enum Color 
{
    RED,
    GREEN,
    BLUE,
    YELLOW,
    WHITE,
    BLACK,
    ORANGE,
    MAROON
}
LineStyle
package design.decorator;
public enum LineStyle
{
    SOLID,
    DASH,
    DOT,
    DOUBLE_DASH,
    DASH_SPACE
}
FillColorDecorator
package design.decorator;
public class FillColorDecorator extends ShapeDecorator
{
    protected Color color;
    public FillColorDecorator(Shape decoratedShape, Color color)
    {
        super(decoratedShape);
        this.color=color;
    }
    @Override
    public void draw()
    {
        decoratedShape.draw();
        System.out.println("Fill Color: " + color);
    }
    @Override
    public String description()
    {
        return decoratedShape.decsription() + "filled with" + color + "color";
    }
    @Override
    public boolean isHide()
    {
        return decoratedShape.isHide();
    }
}
LineColorDecorator
package design.decorator;
public class LineColorDecorator extends ShapeDecorator
{
    protected Color color;
    public LineColorDecorator(Shape decoratedShape, Color color)
    {
        super(decoratedShape);
        this.color=color;
    }
    @Override
    public void draw()
    {
        decoratedShape.draw();
        System.out.println("Line Color: " + color);
    }
    @Override
    public void resize()
    {
        decoratedShape.resize();
    }
    @Override
    public String description()
    {
        return decoratedShape.decsription() + "drawn with" + color + "color";
    }
    @Override
    public boolean isHide()
    {
        return decoratedShape.isHide();
    }
}
LineThicknessDecorator
package design.decorator;
public class LineThicknessDecorator extends ShapeDecorator
{
    protected double thickness;
    public LineThicknessDecorator(Shape decoratedShape, double thickness)
    {
        super(decoratedShape);
        this.thickness=thickness;
    }
    @Override
    public void draw()
    {
        decoratedShape.draw();
        System.out.println("Line thickness: " + thickness);
    }
    @Override
    public void resize()
    {
        decoratedShape.resize();
    }
    @Override
    public String description()
    {
        return decoratedShape.decsription() + "drawn with line thickness" + thickness + ".";
    }
    @Override
    public boolean isHide()
    {
        return decoratedShape.isHide();
    }
}
LineStyleDecorator
package design.decorator;
public class LineStyleDecorator extends ShapeDecorator
{
    protected LineStyle style;
    public LineStyleDecorator(Shape decoratedShape, LineStyle style)
    {
        super(decoratedShape);
        this.style=style;
    }
    @Override
    public void draw()
    {
        decoratedShape.draw();
        System.out.println("Line style: "+ style);
    }
    @Override
    public void resize()
    {
        decoratedShape.resize();
    }
    @Override
    public String description()
    {
        return decoratedShape.decsription() + "drawn with" + style + "lines.";
    }
    @Override
    public boolean isHide()
    {
        return decoratedShape.isHide();
    }
}
Main.java
package design.decorator;
public class Main
{
        public static void main(String[] args) 
        {
                System.out.println("Creating Simple Shape Objects....");
                Shape rectangle= new Rectangle();
                Shape circle = new Circle();
                
                System.out.println("Drawing Simple Shape Objects....");
                rectangle.draw();
                System.out.println();
                circle.draw();
                System.out.println();
                
                System.out.println("Creating Decorated Circle with Red Color, Blue Lines in dash Pattern and thickness of 2.... ");
                Shape circle1 = new FillColorDecorator(new LineColorDecorator(new LineStyleDecorator(new LineThicknessDecorator(new Circle(), 2.0d), LineStyle.DASH), Color.BLUE), Color.RED);
        circle1.draw();
        System.out.println();
        // order of decorator is also not much important here since all are unique functionalities.
        // we can also do this nesting of functionalities in separate statements.
        System.out.println("creating object with similar functionalities in separate statements.");
        Circle c = new Circle();
        LineThicknessDecorator lt = new LineThicknessDecorator(c, 2.0d);
        LineStyleDecorator ls = new LineStyleDecorator(lt, LineStyle.DASH);
        LineColorDecorator lc = new LineColorDecorator(ls, Color.BLUE);
        FillColorDecorator fc = new FillColorDecorator(lc, Color.RED);
        Shape circle3 = fc;
        circle3.draw();
        System.out.println();
        
        System.out.println("Creating Decorated Circle with Green Color, Black Lines ...");
        Shape circle2 = new FillColorDecorator(new LineColorDecorator(new Circle(), Color.BLACK), Color.GREEN);
        circle2.draw();
        System.out.println();
        
        System.out.println("Creating Decorated Rectange with Yellow Color, Red Lines in double dash pattern...");
        Shape rectangle1 = new FillColorDecorator(new LineColorDecorator(new Rectangle(), Color.RED), Color.YELLOW);
        rectangle1.draw();
        System.out.println();
        }
}
NOTE: The above source code will help you in solving the problem. If in case you face any issue please do comment it.
UPVote!!! Please... Thank You!!!!!