0

I'm using the factory pattern to create instances, and want to initialise these instances with different objects. At the moment I'm simply casting the object, but this doesn't feel right.

public interface IGenerator {
    public IGeneratedObject generate();
}

public class FirstGenerator implements IGenerator {

    private List<FirstObject> list;

    public FirstGenerator(List<FirstObject> list) {
        this.list = list;
    }

    public IGeneratedObject generate() {
        return doSomeStuff(list);
    }
}

public class SecondGenerator implements IGenerator {

    private UnrelatedObject obj;

    public SecondGenerator(UnrelatedObject obj) {
        this.obj = obj;
    }

    public IGeneratedObject generate() {
        return doOtherThingsStuff(obj);
    }
}

public class GeneratorFactory {

    public static IGenerator createGenerator(GeneratorType type, Object object) {

        switch (type) {

        case FIRST:
            return new FirstGenerator((List<FirstObject>) object);
        case SECOND:
            return new SecondGenerator((UnrelatedObject) object);
        }

        return null;
    }
}

Usage would then be as follows:

IGenerator gen1 = GeneratorFactory.createGenerator(GeneratorType.FIRST, listOfFirstObjects);

IGenerator gen2 = GeneratorFactory.createGenerator(GeneratorType.SECOND, unrelatedObj);

FirstObject and UnrelatedObject aren't related at the moment - no common base class or interfaces.

In the factory method, the compiler of course shows an 'Unchecked cast from...' warning.

Is there a better way? It seems like a situation where generics could be used, but is this unfeasible due to the objects not being related?

5
  • 1
    If the objects are completely unrelated, then why do they share a factory class? Wouldn't it be better to have separate factory classes for each that only handle related classes? Commented Nov 6, 2014 at 11:13
  • Not sure what you are trying to achieve exactly, but why don't you use generics? Commented Nov 6, 2014 at 11:14
  • @fge I really just want to get an instance of something that implements IGenerator regardless of the input data. I'm afraid I'm not familiar enough with generics on how to achieve this Commented Nov 6, 2014 at 11:26
  • @JonK If separate factory classes were required, wouldn't it just make sense to instantiate the constructor directly (as libik suggests in an answer below)? Commented Nov 6, 2014 at 11:27
  • 1
    Not necessarily, the factory pattern still allows you to control exactly how each class is instantiated, and allows you to do things like caching some common forms of those objects (see IntegerCache inside java.lang.Integer). It's probably not designed to cater for having one giant uberfactory that can instantiate anything in your application. Having multiple factories isn't a bad thing. Commented Nov 6, 2014 at 11:33

2 Answers 2

2

Could you create factory class so that the usage would be as below?

IGenerator gen1 = GeneratorFactory.createGeneratorFirst(listOfFirstObjects);

IGenerator gen2 = GeneratorFactory.createGeneratorSecond(unrelatedObj);
Sign up to request clarification or add additional context in comments.

2 Comments

If that were required, wouldn't it make more sense to just instantiate the constructor directly and not use the factory pattern?
I'll mark this as the accepted answer - although it will mean quite a few methods overall (about 15), it doesn't sound like the route I'd gone down will work
0

There is no reason to have method "create generator".

Creating generator is encapsulated by constructor and class type itself.

Using IGenerator gen1 = new FirstGenerator((List<FirstObject>) object); is completely fine.

1 Comment

So what you are saying is that factory design pattern is not necessary here?

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.