Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real world examples of Factory Method pattern

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.

like image 990
softwarematter Avatar asked Mar 05 '10 10:03

softwarematter


People also ask

What is the Factory Method patterns explain with examples?

Factory Method Pattern. A Factory Pattern or Factory Method Pattern says that just define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate. In other words, subclasses are responsible to create the instance of the class.

What are the applications of factory pattern?

Factory Method Pattern allows the sub-classes to choose the type of objects to create. It promotes the loose-coupling by eliminating the need to bind application-specific classes into the code.

What is an example of the factory method pattern used in Java?

Best Example of Factory method design pattern is valueOf() method which is there in String and wrapper classes like Integer and Boolean and used for type conversion i.e. from converting String to Integer or String to double in java..


1 Answers

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.

like image 176
Sarfraz Avatar answered Sep 24 '22 12:09

Sarfraz