So I was reading along Design Patterns book to look up some details on the Factory Pattern. I was reading about two main variants of implementation:
- Using the abstract Factory and ConcreteFactories for each Type
- Using only one Factory with a parametrized Create(type) Function
Why should I avoid implementing one static Method for each Type in the Factory? For me this is similiar to the second approach but better due to autocomplete (when you don't have to reinstantiate classes based on the types as string from a .txt or something). I would even be able to pass different parameters to different types which is not possible in any of the two implementations.
Regards
No one stops you from doing that. The abstract factory makes a boundary around cohesive methods. It says:
"If you want to implement a factory for this concept, these methods must be implemented."
So the methods are related in meaning. If you look at the UML diagram in dofactory site example closely, it says that :
if you have a factory implementation, it does not randomly implement the creation of each product. The combination of products matters. The client chooses the concrete factory based on a condition once, but the chosen factory creates the products multiple times. You decide once and create multiple times. But with the factory method, every time you want to create a product, you must decide "how".