Factory Pattern

Sean Bradley
Design Patterns In Python
4 min readApr 9, 2019

--

When developing code, you may instantiate objects directly in methods or in classes. While this is quite normal, you may want to add an extra abstraction between the creation of the object and where it is used in your project.

You can use the Factory pattern to add that extra abstraction. The Factory pattern is one of the easiest patterns to understand and implement.

Adding an extra abstraction will also allow you to dynamically choose classes to instantiate based on some kind of logic.

Before the abstraction, your class or method would directly create a concrete class. After adding the factory abstraction, the concrete class is now created outside of the current class/method, and now in a subclass.

Imagine an application for designing houses and the house has a chair already added on the floor by default. By adding the factory pattern, you could give the option to the user to choose different chairs, and how many at runtime. Instead of the chair being hard coded into the project when it started, the user now has the option to choose.

Adding this extra abstraction also means that the complications of instantiating extra objects can now be hidden from the class or method that is using it.

This separation also makes your code easier to read and document.

The Factory pattern is really about adding that extra abstraction between the object creation and where it is used. This gives you extra options that you can more easily extend in the future.

Terminology

  • Concrete Creator: The client application, class or method that calls the Creator (Factory method).
  • Product Interface: The interface describing the attributes and methods that the Factory will require in order to create the final product/object.
  • Creator: The Factory class. Declares the Factory method that will return the object requested from it.
  • Concrete Product: The object returned from the Factory. The object implements the Product interface.

Factory UML Diagram

Factory Pattern UML Diagram

Source Code

In this concept example, the client wants an object named b

Rather than creating b directly in the client, it asks the creator (factory) for the object instead.

The factory finds the relevant class using some kind of logic from the attributes of the request. It then asks the subclass to instantiate the new object that it then returns as a reference back to the client asking for it.

./factory/factory_concept.py

"""
The Factory Pattern Concept
https://sbcode.net/python/factory/#factoryfactory_conceptpy
"""
from abc import ABCMeta, abstractmethodclass IProduct(metaclass=ABCMeta):
"A Hypothetical Class Interface (Product)"
@staticmethod
@abstractmethod
def create_object():
"An abstract interface method"
class ConcreteProductA(IProduct):
"A Concrete Class that implements the IProduct interface"
def __init__(self):
self.name = "ConcreteProductA"
def create_object(self):
return self
class ConcreteProductB(IProduct):
"A Concrete Class that implements the IProduct interface"
def __init__(self):
self.name = "ConcreteProductB"
def create_object(self):
return self
class ConcreteProductC(IProduct):
"A Concrete Class that implements the IProduct interface"
def __init__(self):
self.name = "ConcreteProductC"
def create_object(self):
return self
class Creator:
"The Factory Class"
@staticmethod
def create_object(some_property):
"A static method to get a concrete product"
if some_property == 'a':
return ConcreteProductA()
if some_property == 'b':
return ConcreteProductB()
if some_property == 'c':
return ConcreteProductC()
return None
# The Client
PRODUCT = Creator().create_object('b')
print(PRODUCT.name)

Output

python ./factory/factory_concept.py 
ConcreteProductB

Factory Pattern Overview Video

Example Use Case

Visit Factory — Design Patterns In Python (sbcode.net) for an example use case of the Factory pattern.

An example use case is a user interface where the user can select from a menu of items, such as chairs.

The user has been given a choice using some kind of navigation interface, and it is unknown what choice, or how many the user will make until the application is actually running and the user starts using it.

So, when the user selected the chair, the factory then takes some property involved with that selection, such as an ID, Type or other attribute and then decides which relevant subclass to instantiate in order to return the appropriate object.

Real world example of the Factory Pattern being used

See the above gif for a very graphical example of a real world example of using the factory pattern to return chair objects. The Panel on the left contains chairs, and when you drag and drop a chair to the panel on the right, it calls the Chair Factory to return it a chair.

Video of a Factory Pattern use case

Summary

  • The Factory Pattern is an Interface that defers the creation of the final object to a subclass.
  • The Factory pattern is about inserting another layer/abstraction between instantiating an object and where in your code it is actually used.
  • It is unknown what or how many objects will need to be created until runtime.
  • You want to localize knowledge of the specifics of instantiating a particular object to the subclass so that the client doesn’t need to be concerned about the details.
  • You want to create an external framework, that an application can import/reference, and hide the details of the specifics involved in creating the final object/product.
  • The unique factor that defines the Factory pattern, is that your project now defers the creation of objects to the subclass that the factory had delegated it to.

Thankyou for reading my quick tutorial on the Factory Design Pattern.

To read more of my design patterns articles please consider becoming a Medium subscriber using the link https://sean-bradley.medium.com/membership

If you already are a Medium subscriber, then you can visit my series on Design Patterns in Python at https://medium.com/design-patterns-in-python

Sean

Design Patterns In Python (Book)

You can also buy this series in paperback.
Book provides FREE access to online instructional videos.
Design Patterns In Python : ASIN B08XLJ8Z2J

--

--

Sean Bradley
Design Patterns In Python

Developer of real time, low latency, high availability, asynchronous, multi threaded, remotely managed, fully automated and monitored solutions.