I was working with java code that is supposedly using the Factory pattern, but I'm not completely convinced by the pattern.
My code does this:
// the factory
class SomeFactoryImpl {
Set<SomeClass> getSomeListOfObjects();
}
And somewhere in the code:
{ ...
SomeFactory factory = new SomeFactoryImpl();
Set<SomeClass> list = factory.getSomeListOfObjects();
}
The point I'm pondering is, if factory classes don't have a static create() method, then one will need to instantiate a factory, which IMO should be just as complex as instantiating an object itself.
I don't consider the argument that such a factory can return collections of objects to be produced is good enough. I feel there can be cleaner workarounds, if a factory instance needs to be created before actually creating objects from the factory.
I feel that it is better if the create method a static method of the factory class. But I'm also sure that my opinion is not completely "correct".
So can the SO community give examples where instantiating a Factory object is better than using static create methods?
Also, I came across an answer to a similar question, which listed these links and the answer: so I need to know clearly the difference between FactoryMethodPattern, FactoryMethod and CreationMethod with code examples.
Using an instance of a factory shows the real benefits when combined with dependency injection.
So in your example, instead of:
{ ...
SomeFactory factory = new SomeFactoryImpl();
Set<SomeClass> list = factory.getSomeListOfObjects();
}
You would have:
public ThisClass(SomeFactory someFactory) {
this.factory = someFactory;
}
then later...
{ ...
Set<SomeClass> list = factory.getSomeListOfObjects();
}
Some points:
I know this only addresses a subset of your question...