Bridge Pattern in PHP: What It Is, When to Use It, with Examples

date: · 3 min
author: Mauto
series: Design Patterns/ part 8/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 Bridge Pattern is a structural design pattern that decouples an abstraction from its implementation, allowing the two to vary independently. This pattern is useful when you want to avoid an explosion of subclasses caused by multiple dimensions of variation.

Characteristics of the Bridge pattern

  1. Separation between abstraction and implementation: The Bridge Pattern separates the abstraction (interface or high-level logic) from the implementation (concrete details), allowing them to vary independently.
  2. Flexibility and extensibility: It promotes extensibility, letting you add new abstractions and implementations without modifying existing code. This makes the system more flexible and easier to adapt to new requirements.
  3. Reduced complexity: It reduces the number of classes needed compared to the traditional approach, avoiding a proliferation of subclasses. It lets you combine different abstractions and implementations without creating multiple hierarchies.
  4. Support for component independence: The abstraction depends only on the implementation’s interface, not on its concrete classes. This means you can change or swap the concrete implementations without affecting the abstraction and vice versa, keeping the two components independent.
  5. Better maintainability: Separating responsibilities between abstraction and implementation makes the code easier to maintain. Changes can be made in one part without affecting the other, reducing the risk of introducing bugs.
  6. Dynamic combinations: It lets you dynamically combine different abstractions and implementations at runtime, improving operational flexibility.
  7. Use in multi-platform systems: The Bridge Pattern is especially useful when a system has to support multiple platforms, because it lets you keep a single abstract interface that can work with different platform-specific implementations.
  8. Reduced coupling: It promotes loose coupling between classes, since the abstraction knows the implementation only through its interface. This reduces direct dependencies between concrete components.

Example in PHP:

Let’s imagine we want to model different payment instruments (e.g. Credit Card and PayPal) for different kinds of account (e.g. Personal and Business). The Bridge Pattern lets us separate payment methods from accounts, giving us flexible combinations without having to create a class for every combination of account and payment method.

// Implementation: interface for payment methods
interface PaymentMethod {
    public function processPayment($amount);
}

// Concrete implementation: Credit Card
class CreditCard implements PaymentMethod {
    public function processPayment($amount) {
        echo "Payment of $amount processed via Credit Card.\n";
    }
}

// Concrete implementation: PayPal
class PayPal implements PaymentMethod {
    public function processPayment($amount) {
        echo "Payment of $amount processed via PayPal.\n";
    }
}

// Abstraction: the Account
abstract class Account {
    protected $paymentMethod;

    public function __construct(PaymentMethod $paymentMethod) {
        $this->paymentMethod = $paymentMethod;
    }

    abstract public function makePayment($amount);
}

// Refined Abstraction: Personal Account
class PersonalAccount extends Account {
    public function makePayment($amount) {
        echo "Personal Account: ";
        $this->paymentMethod->processPayment($amount);
    }
}

// Refined Abstraction: Business Account
class BusinessAccount extends Account {
    public function makePayment($amount) {
        echo "Business Account: ";
        $this->paymentMethod->processPayment($amount);
    }
}

// Usage
$personalAccountWithCreditCard = new PersonalAccount(new CreditCard());
$personalAccountWithCreditCard->makePayment(100);  // Output: Personal Account: Payment of 100 processed via Credit Card.

$businessAccountWithPayPal = new BusinessAccount(new PayPal());
$businessAccountWithPayPal->makePayment(250);  // Output: Business Account: Payment of 250 processed via PayPal.

BACK TO THE GUIDE

Copied