samedi 29 février 2020

Best practice FactoryPattern (instantiation, static method or singleton )

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:

  1. 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?

  1. 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?

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