Abstract Factory Pattern in Python: Crafting Families of Related Objects

Abstract Factory Pattern in Python: Crafting Families of Related Objects

ยท

2 min read

The Abstract Factory pattern is a creational design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. It allows you to create objects that work together seamlessly, ensuring compatibility and consistency.

Key Components:

  1. Abstract Factory (Interface): This is an interface that declares a set of factory methods for creating related products. It defines a family of product objects.

  2. Concrete Factory: Implementations of the Abstract Factory interface, each responsible for creating a family of related products.

  3. Abstract Product (Interface): Defines the interface for individual products in a family. Each concrete product must implement this interface.

  4. Concrete Product: These are the concrete implementations of Abstract Product interfaces. Each Concrete Factory produces a family of Concrete Products.

Example Implementation:

Here's a simplified example of the Abstract Factory pattern in Python:

from abc import ABC, abstractmethod

# Abstract Product interfaces
class Button(ABC):
    @abstractmethod
    def click(self):
        pass

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

# Concrete Product classes
class WindowsButton(Button):
    def click(self):
        return "Windows button clicked."

class MacButton(Button):
    def click(self):
        return "Mac button clicked."

class WindowsCheckbox(Checkbox):
    def select(self):
        return "Windows checkbox selected."

class MacCheckbox(Checkbox):
    def select(self):
        return "Mac checkbox selected."

# Abstract Factory interface
class GUIFactory(ABC):
    @abstractmethod
    def create_button(self):
        pass

    @abstractmethod
    def create_checkbox(self):
        pass

# Concrete Factory classes
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()

# Client code
def create_gui(factory):
    button = factory.create_button()
    checkbox = factory.create_checkbox()
    return button, checkbox

windows_factory = WindowsFactory()
mac_factory = MacFactory()

windows_button, windows_checkbox = create_gui(windows_factory)
mac_button, mac_checkbox = create_gui(mac_factory)

print(windows_button.click())  # Output: Windows button clicked.
print(mac_checkbox.select())   # Output: Mac checkbox selected.

In this example, GUIFactory is the Abstract Factory interface with methods for creating buttons and checkboxes. Concrete factories like WindowsFactory and MacFactory implement this interface to produce a family of related products (WindowsButton and WindowsCheckbox or MacButton and MacCheckbox).

Why It Matters:

The Abstract Factory pattern encourages designing families of related objects together, ensuring that they work seamlessly and adhere to the same interface. It promotes code modularity, flexibility, and scalability, making it easier to introduce new families of products or switch between different implementations. This pattern is particularly useful when you need to ensure compatibility and consistency in complex systems.

Did you find this article valuable?

Support Karun's Blog by becoming a sponsor. Any amount is appreciated!