How to Implement the Abstract Factory Pattern in Python

Implements the Abstract Factory pattern to create families of related GUI objects (buttons, checkboxes) without specifying their concrete classes.

Medium Python 3.9+ Aug 9, 2026 System design patterns 13 views 0 copies

Python code

71 lines
Python 3.9+
from abc import ABC, abstractmethod


class Button(ABC):
    @abstractmethod
    def render(self):
        pass


class Checkbox(ABC):
    @abstractmethod
    def render(self):
        pass


class WindowsButton(Button):
    def render(self):
        return "Rendering Windows-style button"


class WindowsCheckbox(Checkbox):
    def render(self):
        return "Rendering Windows-style checkbox"


class MacButton(Button):
    def render(self):
        return "Rendering Mac-style button"


class MacCheckbox(Checkbox):
    def render(self):
        return "Rendering Mac-style checkbox"


class GUIFactory(ABC):
    @abstractmethod
    def create_button(self):
        pass

    @abstractmethod
    def create_checkbox(self):
        pass


class WindowsFactory(GUIFactory):
    def create_button(self):
        return WindowsButton()

    def create_checkbox(self):
        return WindowsCheckbox()


class MacFactory(GUIFactory):
    def create_button(self):
        return MacButton()

    def create_checkbox(self):
        return MacCheckbox()


def create_gui(factory):
    button = factory.create_button()
    checkbox = factory.create_checkbox()
    return button.render(), checkbox.render()


if __name__ == "__main__":
    for name, factory in [("Windows", WindowsFactory()), ("Mac", MacFactory())]:
        button_output, checkbox_output = create_gui(factory)
        print(f"{name}: {button_output} | {checkbox_output}")

Output

stdout
Windows: Rendering Windows-style button | Rendering Windows-style checkbox
Mac: Rendering Mac-style button | Rendering Mac-style checkbox

How it works

The Abstract Factory pattern defines an interface for creating families of related objects without specifying their concrete classes. Here, GUIFactory declares factory methods create_button and create_checkbox, and each concrete factory (WindowsFactory, MacFactory) returns objects from the same family. The client code (create_gui) works with the abstract factory interface, making it easy to swap entire product families with minimal changes. This promotes consistency and decouples client code from concrete implementations.

Common mistakes

  • Forgetting to import ABC and abstractmethod before using them
  • Creating a factory for each individual product instead of a family
  • Hard-coding concrete product classes inside client code instead of using the factory

Variations

  1. Use a dict-based registry to map factory types to instances for dynamic selection.
  2. Combine with the Factory Method pattern for single-product creation in a base class.

Real-world use cases

  • Building cross-platform GUI toolkits that generate buttons, text fields, and menus for Windows, macOS, or Linux.
  • Switching between different database connection and query builders in an ORM based on the configured DB backend.
  • Creating themed UI components (light/dark) in a web frontend generator without changing the core rendering logic.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from System design patterns

Related tutorials and quizzes for this topic.