Question

In: Computer Science

subject pattern design (decorator) Description • Suppose you were asked to implement a drawing tool •...

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

Solutions

Expert Solution

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!!!!!


Related Solutions

Draw a state diagram of the string pattern recognizer, implement it according to the design sequence...
Draw a state diagram of the string pattern recognizer, implement it according to the design sequence of the FSM, and draw a schematic diagram.
Network Design proposal for a University Problem: Suppose you are asked to design of a network...
Network Design proposal for a University Problem: Suppose you are asked to design of a network infrastructure for a university. The university has 7 departments namely, IT, Finance, HR, Management, Faculty, students and R&D. The university also has an ADSL internet connection which is shared for the different departments. It is required that all the departments should have intercommunication. The R&D department should not have access to the internet. Each of the department contain 50-100 users. Explain your design giving...
Implement a Composite Design Pattern for the code below that creates a family tree MAIN: public...
Implement a Composite Design Pattern for the code below that creates a family tree MAIN: public class Main { public static void main(String[] args) { /* Let's create a family tree (for instance like one used on genealogy sites). For the sake of simplicity, assume an individual can have at most two children. If an individual has 1-2 children, they are considered a "tree". If an individual does not have children, they are considered a "person". With that in mind,...
The tool in AutoCAD that allows you to adjust the properties of layers in one drawing...
The tool in AutoCAD that allows you to adjust the properties of layers in one drawing so that they match the properties of another drawing is the A. Layer Transform. B. Properties Transform. C. Layer Translator. D. Property Match.
Dependency injection is a design pattern used to implement Inversion of Control (IoC). Explain Dependency Injection...
Dependency injection is a design pattern used to implement Inversion of Control (IoC). Explain Dependency Injection as a fundamental aspect of spring framework. Discuss different ways to implemented DI in J2EE.
If you were a Public Safety Executive/Administrator, how would you design and implement a public education...
If you were a Public Safety Executive/Administrator, how would you design and implement a public education campaign concerning CBRNE hazards (the “new” hazards)? What information would you present to the public and how?
Implement a Factory Design Pattern for the code below: MAIN: import java.util.ArrayList; import java.util.List; import java.util.Random;...
Implement a Factory Design Pattern for the code below: MAIN: import java.util.ArrayList; import java.util.List; import java.util.Random; public class Main { public static void main(String[] args) { Character char1 = new Orc("Grumlin"); Character char2 = new Elf("Therae"); int damageDealt = char1.attackEnemy(); System.out.println(char1.name + " has attacked an enemy " + "and dealt " + damageDealt + " damage"); char1.hasCastSpellSkill = true; damageDealt = char1.attackEnemy(); System.out.println(char1.name + " has attacked an enemy " + "and dealt " + damageDealt + " damage");...
Suppose you were asked to give a presentation to a group of parents-to-be who are participating...
Suppose you were asked to give a presentation to a group of parents-to-be who are participating in a prepared-childbirth class. Your topic is “effective parenting” or, put another way, “how to raise an energetic, friendly, cooperative, independent, self-reliant child.” Based on the material presented on various styles of parenting and child behaviour outcomes, what points would you stress to these parents-to-be? Provide a rationale for each of your choices.
Suppose you were asked to compute national income and product for an economy. Explain the modifications...
Suppose you were asked to compute national income and product for an economy. Explain the modifications of the usual methods that you will apply to correct the existing problems. Do you think it will result in a larger or a smaller income?
Suppose you were asked to comment on a proposed policy to control oil spills. Since the...
Suppose you were asked to comment on a proposed policy to control oil spills. Since the average cost of an oil spill has been computed as $X, the proposed policy would require any firm responsible for a spill immediately to pay the government $X. Is this likely to result in the efficient amount of precaution against oil spills? Why or why not?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT