Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factory in Java when concrete objects take different constructor parameters

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!

like image 314
Jjang Avatar asked Dec 14 '12 20:12

Jjang


People also ask

What is Factory Method design pattern in Java?

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.

What problem does factory pattern solve?

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.

Where is factory pattern used in Java?

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.

Which object oriented method is used by creational patterns to instantiate object?

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.


3 Answers

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();
like image 112
Random42 Avatar answered Oct 14 '22 05:10

Random42


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.

like image 9
djechlin Avatar answered Oct 14 '22 06:10

djechlin


All of your implementations must take the same number of arguments, you have three options:

  • have the factory store the addition arguments so you only need to provide the centre for example.
  • have the factory take all arguments even though some factories might ignore some of them.
  • have an argument be variable length. e.g. 'double...' the problem with this is the caller needs to know what the factory needs which defeats the purpose of a factory. IMHO.
like image 7
Peter Lawrey Avatar answered Oct 14 '22 07:10

Peter Lawrey