Abstract Factory Pattern in Python
Create whole families of matching objects, such as a dark-theme button and checkbox, without naming their classes.
- Product families
- One factory per family
- Swapping the whole family at once
theme factoryDarkThemeFactoryDarkButtonDarkCheckbox
LightThemeFactoryLightButtonLightCheckbox
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.
DarkThemeFactory()🪵 Choose the dark collectionfactory.create_button()🪑 A piece from that collectionfactory.create_checkbox()🛋️ Another piece, guaranteed to match.render()🏠 Use the pieces without knowing their exact classThe 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
DarkThemeFactoryhas one method per product:create_buttonandcreate_checkbox.- Each method returns the dark version of that widget.
- The client only talks to
factory, so swapping in a light theme factory would change every widget at once. 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
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())
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.
- Replace the sample domain while preserving the pattern structure.
- Compare the pattern with a simpler implementation and explain the tradeoff.
- Write tests for each participant before adding another implementation.
Common mistakes
Creating products outside the factory
Creating a widget directly bypasses the theme and can mix families.
button = factory.create_button()
checkbox = LightCheckbox() # mixed themes
button = factory.create_button()
checkbox = factory.create_checkbox()
Finished this lesson?
Mark it as learned to track your progress.