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?

1
  • 1
    You can remove Enum and use different methods instead? Commented Dec 6, 2010 at 20:58

3 Answers 3

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.)

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

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.