Design patterns: Abstract Factory

date: · 3 min
author: Mauto
series: Design Patterns/ part 4/24

The classic Gang of Four patterns, one per part, with practical PHP examples.

  1. Design Patterns
  2. Design patterns: Singleton
  3. Design patterns: Factory Method
  4. Design patterns: Abstract Factory
  5. Design patterns: Builder
  6. Design patterns: Prototype
  7. Design patterns: Adapter
  8. Bridge Pattern in PHP: What It Is, When to Use It, with Examples
  9. Design patterns: Composite
  10. Design patterns: Decorator
  11. Design patterns: Facade, What It Is and How to Use It (PHP)
  12. Design patterns: Flyweight
  13. Design patterns: Proxy
  14. Design patterns: Chain of Responsibility Explained (PHP)
  15. Design patterns: Command
  16. Design patterns: Interpreter
  17. Design patterns: Iterator
  18. Design patterns: Mediator
  19. Design patterns: Memento
  20. Design patterns: Observer
  21. Design patterns: State, What It Is and How to Use It (PHP)
  22. Design patterns: Strategy
  23. Design patterns: Template Method
  24. Design patterns: Visitor
the whole guide >

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. This pattern is useful when you want to make sure that a set of objects belongs to a given family and is used consistently.

Characteristics of the Abstract Factory pattern

  1. Creating families of objects: It lets you create groups of objects that belong to a given family or context, ensuring they are compatible with one another.
  2. Abstract interface: It provides an abstract interface that defines the methods for creating the objects. Concrete classes implement this interface to supply the specific creation logic.
  3. Independence from concrete classes: The client is unaware of the concrete classes being created. It can use the abstract factory’s interface without worrying about how the concrete classes are implemented.
  4. Easier extension: Adding a new product family is easy: you just create a new concrete factory that implements the abstract interface, without modifying existing code. Adding a new product type, on the other hand, requires changing the factory interface and every concrete factory.
  5. Consistency: It ensures that the objects created are consistent with one another. For example, when building a user interface, you can generate elements such as buttons and windows that belong to the same visual theme.
  6. Support for variability: It lets you change the family of objects being created without modifying the client code. You can switch to a different abstract factory implementation and get completely different objects.

Example in PHP:

Let’s imagine we have two types of car, each with two components: Seats and Engine. We can create two families of cars: Luxury Car and Sports Car.

// Interface for seats
interface Seats {
    public function getDescription();
}

// Interface for engines
interface Engine {
    public function getDescription();
}

// Seats for luxury cars
class LuxurySeats implements Seats {
    public function getDescription() {
        return "Luxury seats";
    }
}

// Seats for sports cars
class SportSeats implements Seats {
    public function getDescription() {
        return "Sports seats";
    }
}

// Engine for luxury cars
class LuxuryEngine implements Engine {
    public function getDescription() {
        return "Luxury engine";
    }
}

// Engine for sports cars
class SportEngine implements Engine {
    public function getDescription() {
        return "Sports engine";
    }
}

// Abstract Factory
interface CarFactory {
    public function createSeats(): Seats;
    public function createEngine(): Engine;
}

// Factory for luxury cars
class LuxuryCarFactory implements CarFactory {
    public function createSeats(): Seats {
        return new LuxurySeats();
    }

    public function createEngine(): Engine {
        return new LuxuryEngine();
    }
}

// Factory for sports cars
class SportCarFactory implements CarFactory {
    public function createSeats(): Seats {
        return new SportSeats();
    }

    public function createEngine(): Engine {
        return new SportEngine();
    }
}

// Usage
function buildCar(CarFactory $factory) {
    $seats = $factory->createSeats();
    $engine = $factory->createEngine();
    
    echo $seats->getDescription() . " with " . $engine->getDescription() . "\n";
}

$factoryLuxury = new LuxuryCarFactory();
$factorySport = new SportCarFactory();

buildCar($factoryLuxury); // Luxury seats with Luxury engine
buildCar($factorySport);  // Sports seats with Sports engine

Common use case

A classic example is an application that manages user interfaces for different operating systems (Windows, macOS, Linux). The abstract factory can provide methods for creating components such as windows, buttons and menus, ensuring that all the objects are designed to work together and follow the style of the specified operating system.

BACK TO THE GUIDE

Copied