Vaibhav Jain Vaibhav Jain - 1 month ago 6
C# Question

Why should one use factory method to create objects


Possible Duplicates:

Factory Pattern. When to use factory methods?

Why do static Create methods exist?




Though I know what is Factory Design Pattern. But I am unable to comprehend what are the benefits of using it. Why should we create objects using Factory Design Pattern.

Answer

By creating objects through factories, you avoid making the subsystem's code depended on specific implementations of the interfaces it uses -- "program to an interface, not to an implementation" is the most important single phrase in the "Design Patterns" book, and factories are one crucial way to move your code towards that excellent goal (dependency injection is another key DP for that, which the classic book does not cover -- but then, often the dependencies you inject are factories anyway, so that omission is not too horrible;-).

Comments