0

I am experience some problems in understanding how the OO pattern works, My lecturer gave me the following question but I cannot solve it after thinking whole day

Scenario for my problems.

There is a class named "ShapeManager" which manages the Shape object. A class named "Shape" has two subclasses named "Circle" and "Rectangle"

The implementation of Shape class as follow

abstract public class Shape {
    private String id;
    private double length;

    public Shape() {

    }
    public Shape(String id , double length) {
        this.id = id;
        this.length = length;
    }
    public void setID(String id) {
        this.id = id;
    }
    public String getID() {
        return id;
    }
    public void setLength(double length) {
        this.length = length;
    }
    public double getLength() {
        return length;
    }
    public abstract String getDetails();

}

The subclass Square as follow

public class Square extends Shape{

    public Square() {
        super();
   }
    public Square(String id , double side) {
        super(id, side);
    }

    @Override
    public String getDetails() {
        return "Square => Id : "+getID() +", Side : "+ getLength() + ",Area : "+(getLength() * getLength());
    }
}

The subclass Circle as follow

public class Circle extends Shape{
    public Circle(){
        super();
    }
    public Circle (String id, double radius) {
        super(id, radius);

    }

    @Override
    public String details() {        
        return "Circle => Id : "+getID() + ", Radius : "+ getLength() + ",Area: "+(3.14*(getLength() * getLength()));
    }
}

The ShapeManager class as follow, this is not a completed class

public class ShapeManager {
    public Shape createShape() {

    }
    public void updateLength(String id ){

    }
    public void deleteShape(String id) {

    }
    public void listShapes() {

    }

}

ShapeManager have an association with Shape

 ShapeManager --1------0..*--> Shape

The design of this package (All the classes above) can not be changed, implementation must be following OCP (Open-Closed Principle).

My question is: How am I suppose to complete createShape method? Without parameter, it is seemingly impossible to create an object either a Rectangle or Circle.

Junior Programmer
  • 310
  • 1
  • 6
  • 20

4 Answers4

2

ShapeManager cannot create a shape if not knowing what this shape is (Square, Circle or something else). And it really doesn't know because you say the method createShare has no parameters. Either you misunderstood the question or the lecturer didn't explain it well. You should ask him/her for clarifications. If you look at the libraries of Java or any other OO language, I am pretty sure you won't find such scenario and implementation pattern as the one you gave in your example.


@croraf

You should find some other reading I think e.g. the classic book http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612. The main idea of a factory is that it returns something whose type the caller doesn't know, and doesn't care about. For example, if you have a method createSocket() in some SocketFactory, this method is usually defined to return an interface or an abstract class Socket. But actually it returns new SocketImpl1() and new SocketImpl2() which are concrete classes. What the factory returns may depend on many things - a system property, the underlying OS, anything you can think of. The main idea is that the factory centralizes the creation of Socket objects at one single place. This way, if you need to make a change, you can make it just in the factory. I think this book also has some decent Java counterparts too, you may look around. Other free good sources are referenced here.

Real world examples of Factory Method pattern

Community
  • 1
  • 1
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
2

I think you should have something like this, similar to how BorderFactory from java API works.

public class ShapeManager {
    public Shape createCircle() {
            ...
            return Circle;
    }
     public Shape createSquare() {
            ....
            return Square;
    }
      ...

   public void updateLength(String id ){

}
public void deleteShape(String id) {

}
public void listShapes() {

}

}
croraf
  • 4,332
  • 2
  • 31
  • 50
  • Thank you for my first +1 :). I was yesterday trying to understand static factory methods, and this is the 3rd point from explanation of their usefullness(am i right?). http://www.javapractices.com/topic/TopicAction.do?Id=21. But can you explain to me how to finish this pattern. – croraf Nov 30 '13 at 10:06
  • Good that you're willing to learn and willing to learn it well. I gave you another upvote and added something for you in my response. – peter.petrov Nov 30 '13 at 10:20
  • Ty sir for your explanation, +1. I took http://www.amazon.com/First-Design-Patterns-Elisabeth-Freeman/dp/0596007124/ref=sr_1_2?s=books&ie=UTF8&qid=1385822000&sr=1-2&keywords=design+patterns, hope they are about the same thing, and have a chapter about this pattern? – croraf Nov 30 '13 at 14:30
  • I hope so too. See also here. http://stackoverflow.com/questions/25331/best-book-resource-for-learning-java-design-patterns Good luck! – peter.petrov Nov 30 '13 at 14:51
1

You can't create shape without knowing type which shape would You like to create. You can define enumeration for types and pass the type value to the createShape(). And there You can switch between types and create the concrette shape You want.

Arsen Alexanyan
  • 3,061
  • 5
  • 25
  • 45
0

For me, Its classic Factory pattern.

public class ShapeFactory {
  public abstract Shape getShape(int shapeId);
}

 public interface Const {
  public static final int SHAPE_CIRCLE =1;
  public static final int SHAPE_SQUARE =2; 
}


public class SimpleShapeFactory extends ShapeFactory throws BadShapeException {
   public Shape getShape(int shapeTypeId){
    Shape shape = null;
    if(shapeTypeId == Const.SHAPE_CIRCLE) {

    //in future can reuse or cache objects.
      shape = new Circle();
    }
    else if(shapeTypeId == Const.SHAPE_SQUARE) {
      //in future can reuse or cache objects
      shape = new Square();
     }
    else throw new BadShapeException("ShapeTypeId="+ shapeTypeId);
    return shape;
  }
}

Calling:

ShapeFactory factory = new SimpleShapeFactory();

//returns a Shape but whether it is a Circle or a
//Square is not known to the caller.
Shape s = factory.getShape(1);
s.getDetails(); // circle details called
//returns a Shape but whether it is a Circle or a
//Square is not known to the caller.
s = factory.getShape(2);
s.getDetails(); //Square details called

References:

The Open Close Principle states that the design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code. The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible existing code unchanged.

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
  • I think added a link to the resource where you extracted the definition of open/closed principle would improve the response – Olimpiu POP Nov 30 '13 at 10:12