Factory Method is known as a creational pattern. As the term present it is used to create something. To create objects such that it kept decoupled from implementor.

It is as simple as creator should hide the creation of object to others. Hence, any future changes in the object should not impact implementor.

“Don’t let others to interfere in your personal life”

The factory pattern really encourages coding to an interface in order to deal with future change.

Let’s take an example of Java Utils itself to understand Factory Pattern. getInstance() method of java.util.Calendar, ResourceBundle and NumberFormat uses the Factory pattern.

Example of Factory Method

In the above example, we are getting an instance of NumberFormat which can be NUMBERSTYLE, PERCENTSTYLE, CURRENCYSTYLE or INTEGERSTYLE based on input given by the implementor. Factory Pattern is mainly used in JDK, Open Source Library i.e. Logger Utility, JDBC.

Key Point: There will be one point where your code is depends on hard coded conditions. But it will not expose to implementor which is our target. Code written using factory pattern is easy to debug as it has centralized point of contact.

“We have to be dependent on something to achieve something. If not depends on anyone then god is always there.”

Keep watching this page for more information on Factory Design Pattern.

It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

2 Thoughts on “Factory Design Pattern in Java”

Leave a Reply

Your email address will not be published. Required fields are marked *