1

I'm working on a program right now that allows you to create shapes (square, rectangles and circles) you also have an option to create a compound shape by selecting already created shapes...I'm using two observers for the shapes. One for squares/rectangles and one for large circles and these observers list the reference points and sizes of the shapes. When a compound shape is created it should list the components in the square/rectangle frame if there are squares/rectangles in the compound shape.

I'm supposed to create the compound shape using composite pattern. So basically compound shapes and my circles, squares and rectangles need to be handled the same way

I have an array of objects called shapes, and compound shape is an object with an array of shapes in it.

My question is how can I check through the shapes array for an object of type compound shape and then check through the compound shapes array for an instance of rectangle or square?

Sorry for not including code but my program is somewhat large with a lot of classes

heres the methods I use to check for an instance of square or rectangle...these two methods are in two different classes. The shapes display in the right observer window when theyre just the simple shapes but the compound shapes dont display. If for example I had a shape list with 3 shapes...shape 1 is a large circle, shape 2 is a compound shape and shape 3 is a rectangle. And lets say the compound shape has 2 squares. Right now this would display the large circle and the rectangle but it wont display the compound shape components. I thought that once I got to the compoundShape, instanceof would look pick out an instanceof square or rectangle from the compundshape array

heres the compound shapes toString method

public String toString(){
    String output="";
    output += "Compound Shape: /n";
    for (int i = 0; i< numShapes; i++){
        output += shapes[i].toString();
    }
    return output;
}

heres the do method I use to look for an instance of square or rectangle

do {//squares rectangles
        currentShape = shapes.getShape();
        if (shapes.squareRectangleFinder())
            outputString1 += currentShape.toString();
    }while (shapes.next());

and heres the square finder method

public boolean squareRectangleFinder() {
    if ((shapes[currentShape] instanceof Square)||(shapes[currentShape] instanceof Rectangle)){
        return true;
    }
    return false;
}
7
  • Sounds like a homework assignment... consider that you should not be "checking through". What behaviors or attributes do you need to abstract and make available through the Compound classes public interface? Or perhaps through a shared interface all shapes share. Commented Nov 22, 2012 at 23:44
  • You can use the instanceof operator to check if the element in the array is an instance of the CompoundShape class. Commented Nov 22, 2012 at 23:45
  • That is true, but probably not in the spirit of what the homework is trying to teach. Commented Nov 22, 2012 at 23:46
  • "I'm using two observers for the shapes. One for squares/rectangles and one for large circles" That is another point that sounds like a problem with you class design. The squareRectangleFinder() problem for example can be solved if you add a method isRectangular() to Shape. If that is an important property of all shapes, make it one for all. Or add a class RectangularShape which defines this property and subclass Recangle and Square from there. Commented Nov 23, 2012 at 0:19
  • After reading the Wikipedia article it is pretty clear to me that composite pattern is about NOT using instanceof. - You just have to implement the same functionality on a CompoundShape that you do on a normal one. Commented Nov 23, 2012 at 0:22

1 Answer 1

1

I guess this is what "composite pattern" should do. Accordint to Wikipedia:

clients should ignore the difference between compositions of objects and individual objects

In my understanding it should be done like this (getters/setters, add/remove operations omitted):

interface Shape {
    public int getLeftmostCoordinate();
}

class Rectangle implements Shape {
    private int top;
    private int left;
    private int width;
    private int height;

    public int getLeftmostCoordinate() {
        return left;
    }
}

class Circle implements Shape {
    private int x;
    private int y;
    private int r;

    public int getLeftmostCoordinate() {
        return x - r;
    }
}

class CompoundShape implements Shape {
    private Shape[] shapes;

    public int getLeftmostCoordinate() {
        int left = shapes[0].getLeftmostCoordinate();

        for (int i=1; i<shapes.length; i++) {
            int candidate = shapes[i].getLeftmostCoordinate();
            if (candidate < left) {
                left = candidate;
            }
        }

        return left;
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

While what this answer says is true, it is not good object oriented practice, and almost certainly not what the homework assignment is trying to teach. A shared interface implemented by all Shapes, Compound, Circle, or whatever should abstract away the need for this technique, which really should only be used in the presence of reflection for abstract meta-coding.
@SAJ14SAJ Definitely, but the question itself is misleading, he asks how to distinguish between the classes. After checking wikipedia, it is the complete opposite what compound pattern should do.
@SAJ14SAJ How about this one?
I think that is the idea :-)
I edited my post to include pieces of the code that should be doing what I need here...Im really sorry if its not clear, but I have about 12 different classes and dont know how else to show what Im doing on here
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.