Abstract Factory Design Pattern

The Abstract Factory design pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. This pattern is useful when there are multiple families of related objects and you want to ensure that the objects created by a factory are compatible with each other. Let’s explain the Abstract Factory pattern with a code example in PHP:

// Abstract product classes
interface Button {
    public function render();
}

interface Checkbox {
    public function render();
}

// Concrete product classes for Windows theme
class WindowsButton implements Button {
    public function render() {
        return "Rendering Windows button...";
    }
}

class WindowsCheckbox implements Checkbox {
    public function render() {
        return "Rendering Windows checkbox...";
    }
}

// Concrete product classes for Mac theme
class MacButton implements Button {
    public function render() {
        return "Rendering Mac button...";
    }
}

class MacCheckbox implements Checkbox {
    public function render() {
        return "Rendering Mac checkbox...";
    }
}

// Abstract factory interface
interface GUIFactory {
    public function createButton(): Button;
    public function createCheckbox(): Checkbox;
}

// Concrete factory for Windows theme
class WindowsFactory implements GUIFactory {
    public function createButton(): Button {
        return new WindowsButton();
    }

    public function createCheckbox(): Checkbox {
        return new WindowsCheckbox();
    }
}

// Concrete factory for Mac theme
class MacFactory implements GUIFactory {
    public function createButton(): Button {
        return new MacButton();
    }

    public function createCheckbox(): Checkbox {
        return new MacCheckbox();
    }
}

// Client code
function renderGUI(GUIFactory $factory) {
    $button = $factory->createButton();
    $checkbox = $factory->createCheckbox();

    echo $button->render() . PHP_EOL;
    echo $checkbox->render() . PHP_EOL;
}

// Usage
echo "Rendering Windows GUI:" . PHP_EOL;
renderGUI(new WindowsFactory());

echo "Rendering Mac GUI:" . PHP_EOL;
renderGUI(new MacFactory());

Explanation:

  • We start by defining abstract product interfaces Button and Checkbox, which declare methods for rendering these UI elements.
  • Concrete product classes WindowsButton, WindowsCheckbox, MacButton, and MacCheckbox implement these interfaces for specific platforms (Windows and Mac).
  • The abstract factory interface GUIFactory declares methods for creating button and checkbox objects.
  • Concrete factory classes WindowsFactory and MacFactory implement the GUIFactory interface and provide platform-specific implementations for creating button and checkbox objects.
  • The client code (renderGUI function) accepts a factory object and uses it to create button and checkbox objects. It then renders these objects.
  • Finally, we demonstrate creating and rendering GUI elements for both Windows and Mac platforms using their respective factories.

The Abstract Factory pattern allows us to create families of related objects (e.g., buttons and checkboxes) without specifying their concrete classes. This promotes flexibility, maintainability, and scalability in the codebase, as new families of products can be added easily by introducing new concrete factories. Additionally, it ensures that the objects created by a factory are compatible with each other, making it suitable for building complex systems with multiple interrelated components.

Related Posts

Leave A Comment