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.
Python code
71 linesfrom 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
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
- Use a dict-based registry to map factory types to instances for dynamic selection.
- 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
More from System design patterns
- Build a BFF (Backend for Frontend) Mock Aggregator in Python medium
- Builder pattern for mocking complex objects in Python easy
- Circuit Breaker Pattern in Python: Closed, Open, and Half-Open States medium
- Create a Data Helper Class in Python easy
- Domain Driven Design Aggregate Root Example in Python medium
- Facade Pattern in Python with Mock Simplification medium
Keep learning
Related tutorials and quizzes for this topic.