Strategy
The Strategy design pattern defines a family of interchangeable algorithms, encapsulates each one in its own class, and lets the algorithm vary independently from the code that uses it. The calling code depends only on a common interface, and the concrete strategy is selected and injected at runtime.
Strategy avoids long conditional structures (if/elseif or match) that pick between several algorithms, replacing them with polymorphism. It is closely related to the Decorator pattern, but a strategy replaces behaviour, while a decorator adds to it.
Common PHP use cases include payment processing (choosing a payment gateway), sorting or pricing algorithms, and validation rules that vary by context.
<?php
interface PaymentStrategy {
public function pay(float $amount): string;
}
class CreditCardPayment implements PaymentStrategy {
public function pay(float $amount): string {
return "Paid {$amount} by credit card";
}
}
class PaypalPayment implements PaymentStrategy {
public function pay(float $amount): string {
return "Paid {$amount} via PayPal";
}
}
class Checkout {
public function __construct(
private PaymentStrategy $strategy,
) {}
public function process(float $amount): string {
return $this->strategy->pay($amount);
}
}
$checkout = new Checkout(new PaypalPayment());
echo $checkout->process(42.0);
?>
See Also
- Strategy pattern - Wikipedia
- DesignPatternsPHP: Strategy
- Strategy vs Decorator in PHP: Choosing the Right Design Pattern