laravel-strategy-pattern

star 136

Use the Strategy pattern to select behavior at runtime; bind multiple implementations to a shared interface

jpcaparas By jpcaparas schedule Updated 11/3/2025

name: laravel:strategy-pattern description: Use the Strategy pattern to select behavior at runtime; bind multiple implementations to a shared interface

Strategy Pattern

Create a common interface and multiple implementations. Choose a strategy by key or context.

interface TaxCalculator { public function for(int $cents): int; }
final class NzTax implements TaxCalculator { /* ... */ }
final class AuTax implements TaxCalculator { /* ... */ }

final class TaxFactory {
  public function __construct(private array $drivers) {}
  public function forCountry(string $code): TaxCalculator { return $this->drivers[$code]; }
}

Register in a service provider and inject the factory where needed.

Install via CLI
npx skills add https://github.com/jpcaparas/superpowers-laravel --skill laravel-strategy-pattern
Repository Details
star Stars 136
call_split Forks 0
navigation Branch main
article Path SKILL.md
More from Creator