Factory Pattern

In part one of this series of design patterns articles we introduced design patterns, here we start with the first category of design patterns, as GOF describes , creational design patterns and the first one of them, is Factory Pattern .  

As the name describes, creational design patterns are about creating objects. They deal with objects creation mechanisms, trying to create objects in a way suitable to the situation. Creating objects using the basic form ( the new keyword in Java ) sometimes creates undesired designs like hard coded values, long constructors, and other complex situations. Creational design patterns tries to solve these problems in the best possible way.   


Factory Pattern represents an object that creates other objects.

The factory can return an instance of one of several possible classes (in a
subclass hierarchy), depending on the data provided to it. It is the one of the most used and easiest design patterns. Keep in mind that there is no single implementation of factory pattern, there are many. The main idea is still the same ! 


Lets say, you want to create a transportation objects. All of them have a the same function move objects from point A to point B. 


factory pattern

factory pattern

Here is a code example: 

A real example: 

Many articles online give only one abstract example of a design pattern that it describes. Most of these examples are abstract ones and doesn’t give a clear picture of the pattern. I am quoting a real code example here.

There are many example of Factory Pattern in Java, here I am listing some of them. It is important to keep in mind that there is not standard exact template to follow to create factory, neither there is for any Design Pattern.  In other words,  if some code is said to be a factory pattern, it doesn’t mean that it will implement the same number of classes and introduce the same number and type of relation. Design Patterns may vary in their implementations but the idea remains the same. In our case the idea is to create objects. 

Factory Pattern Use Cases: 

  1. java.util.Calendar#getInstance()
  2. java.util.ResourceBundle#getBundle()
  3. java.text.NumberFormat#getInstance()
  4. java.nio.charset.Charset#forName()
  5. java.net.URLStreamHandlerFactory#createURLStreamHandler(String) (Returns singleton object per protocol)
  6. java.util.EnumSet#of()
  7. javax.xml.bind.JAXBContext#createMarshaller() and other similar methods

As you can see all of the methods in the list create instances, this is the main idea of the this pattern, regardless of the actual real implementation of it.

When to use Factory Pattern

  • When a class can’t anticipate which kind of class of object it must create.
  • You want to localize the knowledge of which class you create.
  • When you have classes that is derived from the same sub-classes, or they may in fact be unrelated classes that just share the same interface. Either way, the methods in these class instances are the same and can be used interchangeably.
  • When you want to insulate the client from the actual type that is being instantiated.


  • The client does not need to know every subclass of objects it must create. It
    only need one reference to the abstract class/interface and the factory
  • The factory encapsulate the creation of objects. This can be useful if the
    creation process is very complex. 


There is no way to change an implementing class without a recompile. 



GoF Design Patterns – with examples using Java and UML2



Comments (1)

  1. Pingback: Design Patterns Examples in Java – Abstract Factory - The code rider

Leave a comment

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