0
0
LLDsystem_design~7 mins

Abstract Factory pattern in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
When a system needs to create families of related objects without specifying their concrete classes, hardcoding object creation leads to rigid code that is difficult to extend or modify. This causes tight coupling between client code and specific implementations, making it hard to support new product variants or platforms without changing existing code.
Solution
The Abstract Factory pattern provides an interface to create families of related objects without specifying their concrete classes. It defines abstract factory interfaces and concrete factory classes that produce related objects, allowing the client to work with abstract types. This decouples the client from specific implementations and enables easy extension by adding new factories and products.
Architecture
Client
ConcreteFactoryA
(implements AF)
ProductA1
(implements

This diagram shows the client using an abstract factory interface to create related products. Concrete factories implement this interface to produce specific product variants.

Trade-offs
✓ Pros
Decouples client code from concrete classes, improving flexibility.
Supports easy addition of new product families without modifying existing code.
Ensures that related products are used together, maintaining consistency.
✗ Cons
Increases the number of classes and interfaces, adding complexity.
Can be overkill for simple systems with few product variants.
Requires upfront design to define abstract interfaces and factories.
Use when a system must create multiple families of related objects and needs to ensure that products from the same family are used together, especially when the system must support multiple platforms or variants.
Avoid when the system only needs to create a few unrelated objects or when product variants are unlikely to change, as the added complexity is not justified.
Real World Examples
Apple
Uses Abstract Factory to create UI components that adapt to different platforms like macOS and iOS, ensuring consistent look and feel.
Microsoft
Uses Abstract Factory in Windows to create families of related GUI elements for different themes and accessibility settings.
Google
Uses Abstract Factory in Android to generate UI widgets that vary by device type and OS version.
Code Example
Before applying Abstract Factory, the Application class directly creates platform-specific buttons, causing tight coupling and hard-to-maintain code. After applying Abstract Factory, the Application depends on an abstract factory interface to create buttons, allowing easy extension to new platforms by adding new factories without changing Application code.
LLD
### Before: Without Abstract Factory
class WindowsButton:
    def click(self):
        print("Windows button clicked")

class MacOSButton:
    def click(self):
        print("MacOS button clicked")

class Application:
    def __init__(self, os_type):
        if os_type == "Windows":
            self.button = WindowsButton()
        else:
            self.button = MacOSButton()

    def click_button(self):
        self.button.click()

app = Application("Windows")
app.click_button()

### After: With Abstract Factory
from abc import ABC, abstractmethod

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

class WindowsButton(Button):
    def click(self):
        print("Windows button clicked")

class MacOSButton(Button):
    def click(self):
        print("MacOS button clicked")

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

class WindowsFactory(GUIFactory):
    def create_button(self) -> Button:
        return WindowsButton()

class MacOSFactory(GUIFactory):
    def create_button(self) -> Button:
        return MacOSButton()

class Application:
    def __init__(self, factory: GUIFactory):
        self.button = factory.create_button()

    def click_button(self):
        self.button.click()

factory = WindowsFactory()
app = Application(factory)
app.click_button()
OutputSuccess
Alternatives
Factory Method
Factory Method creates one product per method and relies on subclassing, while Abstract Factory creates families of related products through multiple factory methods.
Use when: Choose Factory Method when you need to create one product type with variations, and Abstract Factory when you need to create multiple related products.
Builder
Builder focuses on constructing a complex object step-by-step, while Abstract Factory focuses on creating families of related objects.
Use when: Choose Builder when object construction is complex and needs stepwise control, Abstract Factory when you need to create related product families.
Summary
Abstract Factory decouples client code from concrete classes by creating families of related objects through abstract interfaces.
It ensures consistency among products and supports easy extension to new product variants or platforms.
The pattern adds complexity and should be used when multiple related products need to be created together.