I'm trying to implement a Factory pattern in Java. I have a class called Shape which Circle and Triangle extends. The problem is that Shape constructor gets only 2 parameters while Circle gets 3 parameters and so is Triangle (which I won't show in the code section because is identical to Circle). To demonstrate it better:
private interface ShapeFactory{
public Shape create(int x, int y);
}
private class CircleFactory implements ShapeFactory{
public Shape create(float radius, int x, int y){ //error
return new Circle(radius, x,y);
}
}
Any ideas how to overcome this problem? I must not recieve an input from user inside the factory (must be recieved from outside).
Thanks!
The factory design pattern says that define an interface ( A java interface or an abstract class) for creating object and let the subclasses decide which class to instantiate. The factory method in the interface lets a class defers the instantiation to one or more concrete subclasses.
The factory pattern aims to solve a fundamental problem in instantiation – i.e., the creation of a concrete object of a class – in object-oriented programming. In principle, creating an object directly within the class that needs or should use this object is possible, but very inflexible.
The factory design pattern is used when we have a superclass with multiple sub-classes and based on input, we need to return one of the sub-class. This pattern takes out the responsibility of the instantiation of a class from the client program to the factory class.
Factory Method Design Pattern The Factory Design Pattern or Factory Method Design Pattern is one of the most used design patterns in Java. According to GoF, this pattern “defines an interface for creating an object, but let subclasses decide which class to instantiate.
You have two options:
1) Abstract Factory:
RectangularShape extends Shape
RoundShape extends Shape
and RectangularShapeFactory
and RoundShapeFactory
2) Builder (see also Item 2 in Effective Java)
public Shape {
private final int x;
private final int y;
private final double radius;
private Shape(Builder builder) {
x = builder.x;
y = builder.y;
radius = builder.radius;
}
public static class Builder {
private final int x;
private final int y;
private double radius;
public Builder(int x, int y) {
this.x = x;
this.y = y;
}
public Builder radius(double radius) {
this.radius = radius;
return this;
}
public Shape build() {
return new Shape(this);
}
}
}
//in client code
Shape rectangle = new Shape.Builder(x,y).build();
Shape circle = new Shape.Builder(x,y).radius(radiusValue).build();
What you are trying to do is simply impossible. If the constructor arguments are different, then the client code will have to do different work for a Circle
as for a Square
and you can't solve this with uniform code. If there is other work the factory is doing besides handling the constructor arguments that you believe should happen in a factory, then you need to post this to your question and state the difficulty you are having in factoring out this common code-work.
All of your implementations must take the same number of arguments, you have three options:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With