Using the factory pattern for classes with differing parameters
Asked Answered
P

3

11

I have a very simple factory which takes an Enum as one of its parameters to determine the type of object that should be created, and a other parameter that's common to all the objects being created.

As I'm adding more types for the factory to create my object constructor's parameters are starting to differ, eg:

public class someFactory {
    public someFactory() {
    }

    public SomeObject newObject(Type type, Object data) {
        return this.newObject(type, data, "");
    }

    public SomeObject newObject(Type type, Object data, Object stringOrObject) {
        SomeObject someObject = null;

        if (type != null) {
             switch(type) {
                 case CREATE:
                     someObject = new CreateObject(data);
                     break;
                 case DELETE:
                     someObject = new DeleteObject(data, (String)stringOrObject);
                     break;
                 case EDIT:
                     someObject = new EditObject(data, (Object)stringOrObject);
                     break;
                 default:
                     break;
             }
        }

        return someObject;
    }
}

Should I not be using a factory and just instantiate the the different types with the right arguments or can the above be improved somehow to make it more flexible?

Pomerania answered 6/12, 2010 at 20:49 Comment(1)
You can remove Enum and use different methods instead?Grimsby
T
3

The standard Java thing to do is to add a method to the enum.

public enum Type {
    CREATE() {
        public SomeObject create(Object data, Object stringOrObject) {
            return new CreateObject(data);
        }
    },
    [...];
    public SomeObject create(Object data) {
        return create(data, "");
    }
    public abstract SomeObject create(Object data, Object stringOrObject);
}

As @Stas Kurilin points out, if you can avoid the enum and just call static creation methods of appropriate names and parameters, then you solve many problems.

(A few other random points: It's generally better to throw an exception than accept a null or unknown value. Try to use strong typing rather than Object. Stick with the Java coding conventions, such as capitalised type names.)

Thoracotomy answered 6/12, 2010 at 21:0 Comment(0)
T
1

I would create an interface that looks like

public interface IFactory
{
    SomeObject Create(Object data, String orObject);
    Boolean AppliesTo(Type type);
}

You can then have a Factory class that contains a list of three of these IFactories for Create, Delete, and Edit and can query the list of these factories for the first one that responds true to the AppliesTo method.

Tomika answered 6/12, 2010 at 20:56 Comment(0)
D
0

Create a interface with the following signature,

public interface IFactory
{
    GenricType Create(object data, string orObject);

}

and let other objects implement this interface. So that the creation remains with the object. Factory pattern is good. But, since you are using enums to identify the type, it would be better to use polymorphism, so that it becomes maintainable.

Detrimental answered 6/12, 2010 at 21:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.