laravel:template-method-and-plugins について
Laravel 開発向けのスキルです。コア処理を安定させながら拡張できるよう、テンプレートメソッドとプラグイン可能な戦略、登録と選択の方法を扱います。
主な機能
- 01テンプレートメソッドのアルゴリズム骨格
- 02サブクラスのフックメソッド
- 03インターフェースベースの戦略
- 04レジストリによる実装の選択
- 05実装ごとの独立したテスト
活用シーン
- 01インポート処理を維持しつつ手順を変更
- 02決済ゲートウェイの実装追加
- 03switch 文を登録クラスに置き換え
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ jpcaparas/ superpowers-laravel --skill laravel:template-method-and-pluginsインストール後、Claude Code で/laravel:template-method-and-pluginsを入力して読み込まれたか確認します。
詳細
テンプレートメソッドと差し替え可能なストラテジー
コアフローを安定させ、小さなクラスを通じて拡張できるようにします。
テンプレートメソッド
アルゴリズムの骨組みを定義する基底クラスを用意し、サブクラスでフックをオーバーライドします。
abstract class Importer {
final public function handle(string $path): void {
$rows = $this->load($path);
$data = $this->normalize($rows);
$this->validate($data);
$this->save($data);
}
abstract protected function load(string $path): array;
protected function normalize(array $rows): array { return $rows; }
protected function validate(array $data): void {}
abstract protected function save(array $data): void;
}
ストラテジー/プラグイン
インターフェースを定義し、実装を登録して、キーまたは設定で選択します。
interface PaymentGateway { public function charge(int $cents, string $currency): string; }
final class StripeGateway implements PaymentGateway { /* ... */ }
final class BraintreeGateway implements PaymentGateway { /* ... */ }
final class PaymentsRegistry {
/** @param array<string,PaymentGateway> $drivers */
public function __construct(private array $drivers) {}
public function for(string $key): PaymentGateway { return $this->drivers[$key]; }
}
switch 文を編集するより、クラスを追加することを優先してください。実装は個別にテストしてください。
コメント
Markdown 対応