Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Object Oriented Design c) You review a design written by somebody else for an ap

ID: 3736920 • Letter: O

Question

Object Oriented Design

c) You review a design written by somebody else for an application
and you find these:

- an interface Shape with a method draw()
- a class Circle that implements Shape
- a class Rectangle that implements Shape
- a class CompoundShape that:
o implements interface Shape
o aggregates 0 or more Shape objects,
o has an extra method called add(Shape sh)
o for implementing method draw() calls the draw() method for all
aggregated Shape objects.
You assume that a CompoundShape object is made of multiple shapes.
What design pattern is at work in this application? Explain your answer.

d) The TitledBorder class can give a title to a border. Consider the code

panel.setBorder(new TitledBorder(new EtchedBorder(), "Enter Option"));

What design pattern(s) are at work? Explain your answer.
(a similar example is in the textbook/notes)

Explanation / Answer

package sample.designpattern.factory;

/**
* Factory provides a standard way of creating objects with common interface but different types.
* Factory itself is normally in singleton scope. In the following example the factory is in
* prototype scope.
*/
public class ShapeFactory {

    public Shape getShape(String shapeType) {
        if (shapeType == null) {
            return null;
        } else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
            return new Rectangle();
        } else if (shapeType.equalsIgnoreCase("CIRCLE")) {
            return new Circle();
        } else if (shapeType.equalsIgnoreCase("SQUARE")) {
            return new Square();
        }
        return null;
    }

    public static void main(String[] args) {
        ShapeFactory shapeFactory = new ShapeFactory();

        //get an object of Circle and call its draw method.
        Shape shape1 = shapeFactory.getShape("CIRCLE");

        //call draw method of Circle
        shape1.draw();

        //get an object of Rectangle and call its draw method.
        Shape shape2 = shapeFactory.getShape("RECTANGLE");

        //call draw method of Rectangle
        shape2.draw();

        //get an object of Square and call its draw method.
        Shape shape3 = shapeFactory.getShape("SQUARE");

        //call draw method of circle
        shape3.draw();
    }

}

package sample.designpattern.factory;


/* To change this template use File | Settings | File Templates.
*/
public class Circle implements Shape {
    @Override
    public void draw() {
        System.out.println("calling Circle::draw() method");
    }
}

package sample.designpattern.factory;

/* To change this template use File | Settings | File Templates.
*/
public class Rectangle implements Shape {
    @Override
    public void draw() {
        System.out.println("calling Rectangle::draw() method");
    }
}

package sample.designpattern.factory;

/*

To change this template use File | Settings | File Templates.
*/
public interface Shape {
    void draw();
}


package sample.designpattern.factory;

/**
* Factory provides a standard way of creating objects with common interface but different types.
* Factory itself is normally in singleton scope. In the following example the factory is in
* prototype scope.
*/
public class ShapeFactory {

    public Shape getShape(String shapeType) {
        if (shapeType == null) {
            return null;
        } else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
            return new Rectangle();
        } else if (shapeType.equalsIgnoreCase("CIRCLE")) {
            return new Circle();
        } else if (shapeType.equalsIgnoreCase("SQUARE")) {
            return new Square();
        }
        return null;
    }

    public static void main(String[] args) {
        ShapeFactory shapeFactory = new ShapeFactory();

        //get an object of Circle and call its draw method.
        Shape shape1 = shapeFactory.getShape("CIRCLE");

        //call draw method of Circle
        shape1.draw();

        //get an object of Rectangle and call its draw method.
        Shape shape2 = shapeFactory.getShape("RECTANGLE");

        //call draw method of Rectangle
        shape2.draw();

        //get an object of Square and call its draw method.
        Shape shape3 = shapeFactory.getShape("SQUARE");

        //call draw method of circle
        shape3.draw();
    }

}

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class TitledBorderTest
{
    private static void createAndShowUI()
    {
        UIManager.getDefaults().put("TitledBorder.titleColor", Color.RED);
        Border lowerEtched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
        TitledBorder title = BorderFactory.createTitledBorder(lowerEtched, "Enter option");
//      title.setTitleJustification(TitledBorder.RIGHT);
        Font titleFont = UIManager.getFont("TitledBorder.font");
        title.setTitleFont( titleFont.deriveFont(Font.ITALIC + Font.BOLD) );

        JPanel panel = new JPanel()
        {
            @Override
            public String getToolTipText(MouseEvent e)
            {
                Border border = getBorder();

                if (border instanceof TitledBorder)
                {
                    TitledBorder tb = (TitledBorder)border;
                    FontMetrics fm = getFontMetrics( tb.getTitleFont() );
                    int titleWidth = fm.stringWidth(tb.getTitle()) + 20;
                    Rectangle bounds = new Rectangle(0, 0, titleWidth, fm.getHeight());
                    return bounds.contains(e.getPoint()) ? super.getToolTipText() : null;
                }

                return super.getToolTipText(e);
            }
        };
        panel.setBorder( title );
        panel.setToolTipText("Title With ToolTip");

        JFrame frame = new JFrame("SSCCE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( panel );
        frame.setSize(200, 200);
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}