The factory method is used to avoid violation of Open-closed principle. Instead of creating an object by inheritage:
Product myProd = new ConcreteProduct1; // concreteProduct extends abstract product
myProd.doSomething();
we use a factory "interface" with relatives concrete factories:
Factory myFact = new ConcreteFact1; // (2)
Product myProd = ConcreteFact1.createProd(); // (1)
myProd.doSomething(); // (1)
I read much about factory method; I understood that, using factory method, it's possible to exclude vilations of the open-closed principle. But I still don't understand:
- with this design pattern we have still a dependency to class Product (myProd) (1)
- moreover.. we have a dependency with Concrete-Factories (client needs to instantiate a specific wanted concrete factory and client must know witch one) (2)
Tank you for any clarification.
Aucun commentaire:
Enregistrer un commentaire