mardi 14 mars 2017

I don't really get the point of the factory pattern

I don't see the point. In the end is it just to split up logical groups into different classes?

If I have a group of similar product classes I new instantiated I can write an abstract factory class and derive concrete factories for each of those product classes I need.

But why a factory in the first place? The point of a constructor is initializing a class so all code I'd put into a factory's create method I could just as well put the constructor and all parameters the client class must provide like dependency injection or use specific parameters I have to provide inside the client class in every case and if anything about that changes I will have to adjust the client classes code no matter what.

I don't see a point in using factory methods (it's not a bad practice, but I don't see why it should be used over normal constructors) and the only point in an abstract factory I see is it being one of of the ways to organize similar classes, though over ways just are just as well..

The one real reason to use a factory pattern or method I see is if you want to limit the amount of instances of a class, like with a singleton pattern.

Aucun commentaire:

Enregistrer un commentaire