samedi 28 novembre 2020

When to use factory pattern

I know this question has been asked many times, but I still have a very simple question, what is the purpose of using a factory pattern if the initiation of an object is simple.

Interface Animal{
    eat();
}

class Dog implements Animal{

     public void eat(){System.out.println("dog eat");}
}

Assume I have a concrete Cat class and fish class implement the Animal interface.

So in this case, is it necessary to make 3 Factory to create the animals? I think we only use the factory method when the initialization is difficult.

Aucun commentaire:

Enregistrer un commentaire