Design patterns: Strategy

date: · 3 min
author: Mauto
series: Design Patterns/ part 22/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 Strategy Pattern is a behavioral pattern that lets you define a family of algorithms, encapsulate each one in a separate class, and make them interchangeable. The algorithm to use can be selected dynamically at runtime, making the code more flexible and easy to extend. This pattern is particularly useful when you want to avoid long conditional blocks (such as if-else or switch-case) to choose between different algorithms.

Strengths

  1. Code separation: Each algorithm is encapsulated in its own class, separating the algorithm’s logic from the context in which it’s used.
  2. Interchangeable algorithms: Algorithms can be swapped at runtime without changing the client code.
  3. Better maintainability: Adding or changing algorithms doesn’t affect the rest of the code.
  4. Extensibility: It’s easy to add new algorithms by simply implementing a new class that follows the same interface.
  5. Promotes cohesion: The context handles only the high-level logic, while the strategies take care of the specific implementation of the algorithms.

Use case: A payment system

Imagine a payment system that supports several methods: Credit Card, PayPal and Bank Transfer. Depending on the payment method chosen by the user, the algorithm for processing the payment changes.

PHP Code

<?php
// Payment Strategy interface
interface PaymentStrategy {
    public function pay($amount);
}

// Credit Card payment strategy
class CreditCardPayment implements PaymentStrategy {
    private $cardNumber;

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

    public function pay($amount) {
        // Show only the last 4 digits of the card
        $maskedNumber = '**** ' . substr($this->cardNumber, -4);
        echo "Payment of $amount made with Credit Card number: {$maskedNumber}.\n";
    }
}

// PayPal payment strategy
class PayPalPayment implements PaymentStrategy {
    private $email;

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

    public function pay($amount) {
        echo "Payment of $amount made with PayPal to the address: {$this->email}.\n";
    }
}

// Bank Transfer payment strategy
class BankTransferPayment implements PaymentStrategy {
    private $bankAccount;

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

    public function pay($amount) {
        echo "Payment of $amount made by Bank Transfer to the account: {$this->bankAccount}.\n";
    }
}

// Context class for payment
class PaymentContext {
    private $paymentStrategy;

    public function __construct(PaymentStrategy $paymentStrategy) {
        $this->paymentStrategy = $paymentStrategy;
    }

    public function setPaymentStrategy(PaymentStrategy $paymentStrategy) {
        $this->paymentStrategy = $paymentStrategy;
    }

    public function pay($amount) {
        $this->paymentStrategy->pay($amount);
    }
}

// Usage example
$context = new PaymentContext(new CreditCardPayment("1234-5678-9876-5432"));
$context->pay(100);  // Payment by Credit Card

$context->setPaymentStrategy(new PayPalPayment("user@example.com"));
$context->pay(200);  // Payment by PayPal

$context->setPaymentStrategy(new BankTransferPayment("IT60X0542811101000000123456"));
$context->pay(300);  // Payment by Bank Transfer

Explanation

  • PaymentStrategy interface: Defines the pay() method, which every payment strategy must implement.
  • Concrete Strategies (CreditCardPayment, PayPalPayment, BankTransferPayment): Implement the interface and handle the payment algorithm for their respective method.
  • PaymentContext class: Manages the current payment algorithm and lets you switch strategy at runtime.
  • Using the context: The user can change the payment method without modifying the context itself; the strategy is simply replaced.

Conclusion

The Strategy Pattern cleanly separates algorithms from the context in which they’re used, improving modularity and making algorithms interchangeable at runtime. This is particularly useful in complex systems that require a variety of algorithms or logic.

BACK TO THE GUIDE

Copied