Skip to main content

Command Palette

Search for a command to run...

Abstract Factory Pattern in Python: Crafting Families of Related Objects

Published
โ€ข2 min read
Abstract Factory Pattern in Python: Crafting Families of Related Objects
K

๐Ÿ Passionate Python Enthusiast | Educator | Blogger ๐Ÿ“

Welcome to my Python playground! ๐Ÿš€ I'm here to share my love for Python programming and help you master this versatile language. Whether you're just starting your coding journey or looking to level up your Python skills, you're in the right place.

๐Ÿ“š Dive into my tutorials and learn Python from scratch, one step at a time. From basic syntax to complex projects, I've got you covered.

๐ŸŽ“ As an educator, I believe in the power of sharing knowledge. Let's learn, grow, and conquer coding challenges together.

๐Ÿ”— Got a question or a cool project idea? Don't hesitate to reach out.

Remember, in the world of programming, a little indentation goes a long way. Happy coding, Pythonistas! ๐Ÿ๐Ÿ’ป

๐ŸŒ karun.hashnode.dev ๐Ÿ“ธ https://twitter.com/karunakarhv

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.

More from this blog

Karun's Blog

104 posts

A software engineer fluent in Python and Linux, I thrive in development. Beyond tech, I'm a fitness enthusiast and enjoying the journey of parenting.