I've been reading about FactoryPatterns and now i'm trying to grasp the idea, which implementation is best practice.
I guess there are a couple of options:
- Instantiate your factory
Factory factory = new Factory(); Obj obj = factory.createObj(TYPE);
Question 1: Do you really instantiate it everywere in the code?
Question 2: I've read in blogs and forums that the new operator is considered harmful. Isn't it the same for instantiating the Factory itself?
- Use static method
My preferred option, creating a static method inside my factory
public class Factory { public static void createObj(TYPE){ .. }}
Question 3: Due to the static method, am i shooting myself in my foot later on because the static method can't be overriden/adjusted in runtime?
- Make the Factory a singleton Make the entire Factory class a singleton.
I think this questions come closest to mine, and there are some good points in it. But i'd like to discuss it more.
Why should the getInstance() method in Factory pattern be static?
Factory Creation Methods Always Static?
Aucun commentaire:
Enregistrer un commentaire