返回 Skill 列表
extension
分类: 开发与工程无需 API Key

laravel:strategy-pattern

使用策略模式在运行时选择行为;将多个实现绑定到共享接口

person作者: jakexiaohubgithub

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.