Why should one use factory method to create objects

Vaibhav Jain picture Vaibhav Jain · Jul 28, 2010 · Viewed 9.3k times · Source

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

Alex Martelli picture Alex Martelli · Jul 28, 2010

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;-).