laravel-template-method-and-plugins について
Laravel開発向けに、コア処理を保ちながら小さなクラスで拡張する方法を扱うコーディングスキルです。Template Method、ストラテジーやプラグインの登録、実装ごとの分離テストを説明します。
主な機能
- 01Template Methodのアルゴリズム骨格
- 02サブクラスのフックによるカスタマイズ
- 03ストラテジーのインターフェースと実装
- 04キーや設定による登録と選択
- 05実装単位の分離テスト
活用シーン
- 01手順を差し替えられるインポート処理
- 02複数の決済ゲートウェイへの対応
- 03switch文を編集せずに機能を拡張
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ noartem/ skills --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 対応