Factory Method Design Pattern in Python — Explained With Code

Learn what the factory method pattern is, how to implement it in Python, and when to use it.

Ankush Kumar Singh
Python in Plain English

--

The factory method pattern is one of the famous, widely used, and one of my favorite design patterns used in programming. In my opinion, it’s a programmer’s version of performing magic. This blog will cover what is the factory method pattern, how to implement it in Python, and when to use this pattern.

As per definition, the factory method pattern defines an interface for creating an object but lets the subclass decide which class to instantiate. In other words, the factory method defers the instantiation to subclasses.

Let’s take an example to understand this better. Suppose we own software for cargo delivery systems. We started our business by delivering cargo that didn’t need a temperature-controlled environment. We developed code for it and the entire code was in the class NonFrozenDeliveryService. But as the demand started rising, customers started requesting products that need a temperature-controlled environment. To address this, we created another class FrozenDeliveryService which had the code for delivering products that need temperature control. But the issue with this approach is now the client has to know what object to create for different types of products.

This is where the factory method pattern is useful. It makes sense to decouple client and delivery service. The client should pass minimal information and based on that DeliveryService should create the object for the right class. To illustrate it further, if you order something from Amazon, as a customer Amazon’s delivery system should be a blackbox for you.

Following is the UML diagram for this:

As we see in the UML diagram, the interface Product is implemented by FrozenProduct and NonFrozenProduct. DeliveryService class is extended by FrozenDeliveryService and NonFrozenDeliveryService, representing type of product delivery. And going by definition subclasses FrozenDeliveryService and NonFrozenDeliveryService instantiate objects for FrozenProduct and NonFrozenProduct respectively.

Following is the code snippet for factory method design pattern:

Following is the sample output of the code:

Client isn't aware of different types delivery services.
Only information needed from client is `frozen` boolean flag.
Case 1: `frozen` boolean flag set to True
Client wants delivery of frozen product
This is a frozen Product
Delivering frozen Product
Case 2: `frozen` boolean flag set to False
Client wants delivery of non frozen product
This is a Non frozen Product
Delivering non-frozen Product

When to use factory method design pattern:

  1. Dependencies and exact types of the objects that the code will work with is not known.
  2. Extending core components of code/framework without it.
  3. Reusing the existing objects instead of rebuilding them to save system resources.

Thank you for reading the article. I hope you had a great time reading it!

Following are the links to other design pattern articles:

More content at plainenglish.io. Sign up for our free weekly newsletter. Get exclusive access to writing opportunities and advice in our community Discord.

--

--