Python Design Patterns

Abstract Factory Pattern in Python

Create whole families of matching objects, such as a dark-theme button and checkbox, without naming their classes.

  • Advanced
  • 2 min read
  • 1 runnable example
What you'll learn
  • Product families
  • One factory per family
  • Swapping the whole family at once
Theme factories
  • theme factory
    • DarkThemeFactory
      • DarkButton
      • DarkCheckbox
    • LightThemeFactory
      • LightButton
      • LightCheckbox

One factory, one matching family

The problem

A user interface with a dark theme must use dark buttons, dark checkboxes and dark menus together. If each widget is created separately, it is easy to mix a light checkbox into a dark screen. You need a way to create whole families of matching objects.

Think of it like this

An abstract factory is like buying furniture from one collection in a store. Choose the "dark oak" collection and the table, chairs and shelves you get all match. You never pick pieces one by one and risk mixing styles.

From a furniture collection to code
DarkThemeFactory()🪵 Choose the dark collection
factory.create_button()🪑 A piece from that collection
factory.create_checkbox()🛋️ Another piece, guaranteed to match
.render()🏠 Use the pieces without knowing their exact class
One factory creates a whole family of matching products.
In one sentence

The Abstract Factory pattern provides one factory object per product family, with a create method for each product, so all objects created through it belong together.

Key points

  1. DarkThemeFactory has one method per product: create_button and create_checkbox.
  2. Each method returns the dark version of that widget.
  3. The client only talks to factory, so swapping in a light theme factory would change every widget at once.
  4. render() is called the same way on every product.

When should you use it?

  • Themes and skins where all widgets must match.
  • Supporting several platforms or databases with matching sets of classes.
  • Swapping a whole family of implementations, for example real versus test doubles.

When not to use it

  • Only one product type exists: a plain factory function is enough.
  • Families rarely change: the extra classes add complexity without benefit.

Example code

Run code →
main.py
class DarkButton:
    # Abstract Factory: create products that belong to one family.
    def render(self): return "Dark button"


class DarkCheckbox:
    def render(self): return "Dark checkbox"


class DarkThemeFactory:
    # Produce a compatible family of UI objects.
    def create_button(self): return DarkButton()
    def create_checkbox(self): return DarkCheckbox()


factory = DarkThemeFactory()
print(factory.create_button().render())
print(factory.create_checkbox().render())
Get a Run button for this example

Result of running the code

Dark button
Dark checkbox

Code explanation

DarkButton and DarkCheckbox belong to the same family, and DarkThemeFactory is the only place that creates them. The client receives a factory and calls create_button() and create_checkbox() on it, so both widgets are guaranteed to be dark. To switch themes, only the factory object would change, not the client code.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Replace the sample domain while preserving the pattern structure.
  2. Compare the pattern with a simpler implementation and explain the tradeoff.
  3. Write tests for each participant before adding another implementation.
Run in the Python compiler →

Common mistakes

Creating products outside the factory

Creating a widget directly bypasses the theme and can mix families.

Problem
button = factory.create_button()
checkbox = LightCheckbox()  # mixed themes
Fix
button = factory.create_button()
checkbox = factory.create_checkbox()

Finished this lesson?

Mark it as learned to track your progress.