Real world examples of Factory Method pattern

softwarematter picture softwarematter · Mar 5, 2010 · Viewed 46.4k times · Source

I just read Factory Method. I understand that it provides a way to delegate the instantiation to sub-classes. But I couldn't understand the possible uses in a real-world scenario.

Can anyone give one typical example showing how Factory method pattern can be used so that I can relate to what I have read.

A problem statement for which factory method pattern is the best solution would be sufficient to make it clear.

Answer

Sarfraz picture Sarfraz · Mar 5, 2010

A class implementing factory design pattern works as bridge between multiple classes. Consider an example of using multiple database servers like SQL Server and Oracle. If you are developing an application using SQL Server database as backend, but in future need to change backend database to oracle, you will need to modify all your code, if you haven’t written your code following factory design pattern.

In factory design pattern you need to do very little work to achieve this. A class implementing factory design pattern takes care for you and lessen your burden. Switching from database server won’t bother you at all. You just need to make some small changes in your configuration file.