Creational Design Pattern: 2. Factory method

What is a factory method pattern?

Factory method method is also a creational design pattern which deals with instantiating objects of a class.

Note: This is part 2 of creational design pattern, if you haven’t read about singleton pattern yet, you can check it out here

Consider a situation where you own a pizza shop. In the shop there are different orders throughout a day. Some orders are of small sized pizza, some for large sized pizza and some for medium one. So, when an order comes, you first bake the base for the pizza then add the relevant toppings to it. When there are a lot of orders, then it becomes difficult to manage. It is better if we can delegate the responsibility of making pizza bases to someone else and specify what size base is needed. This is exactly where the factory method pattern comes to our rescue.

Factory here symbolizes a real world factory which manufactures some objects for us without giving us details on how the objects are created. There can be pizza base factory which in-turn can have multiple factories for making bases of different sizes. When a new size base is added to the factory, the client can directly ask for it without knowing the internal details of how it is made. The below mentioned UML diagram illustrates the factory method design pattern.

Simplest UML diagram for factory method pattern taken from internet

In our example, our final product we are getting from the factory is pizza base. Pizza base can be of different sizes, but they all have a same shape, same material(say for instance). So, the concrete products here represents three types of pizza bases of small, medium and large size.

Creator is our pizza base factory. It has an abstract factory method, which produces the pizza base of required size. The factory may have other operations like packaging and delivering which will be common for pizza bases of all the sizes.

Then there are three concrete creator classes that will extend or implement the pizza base factory (depending whether it is an abstract class or an interface respectively). These three classes will override the factory method and return a pizza base of the respective sizes.

This design pattern’s intent is to define an interface for creating objects, but let the sub-classes decide which class to instantiate.”

When to use?

  • The factory method pattern is generally used when the sub-classes decides that which type of object their parent will contain. This usually happens when object instantiation is done at runtime and we don’t know which object will be instantiated at the compile time itself.
  • This pattern is used to make the code for object creation more cleaner and at one place rather than duplicating it in the different parts of the code. Every time an object is needed, we can use the factory method.
  • By using factory method to instantiate the objects, client is also relieved from the logic of object creation and it gives a good layer of abstraction.
  • It is also easier to add a new base to the factory without affecting the client interaction.

Sample code

PizzaBaseFactorypublic abstract class PizzaBaseFactory {

public abstract PizzaBase getFactoryObject(String size);

public PizzaBase deliverBase(String size) {
PizzaBase base;
base = getFactoryObject(size);
//other operations like packaging and delivering
return base;
ThinCrustBaseFactorypublic class ThinCrustBaseFactory extends PizzaBaseFactory {

public PizzaBase getFactoryObject(String size) {
if(size == "S"){
return new ThinCrustSmallBase();
}else if(size == "M"){
return new ThinCrustMediumBase();
return new ThinCrustLargeBase();
PizzaBasepublic interface PizzaBase {
ThinCrustSmallBase, and similar for medium and large sized pizzapublic class ThinCrustSmallBase implements PizzaBase{

Clientpublic class Client {
public static void main(String[] args) {
PizzaBase base = new ThinCrustBaseFactory().deliverBase("S");

That’s all from my side. I hope it’s clear now, where, when and how to use the factory method design pattern.

Please let me know in the comments below, if you have any doubts. If you like this blog, please share it among your friends. Happy software engineering!

Note: Structural Pattern, Part 1



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store