Strategy パターンは、互いに置き換え可能なアルゴリズム/振る舞いのファミリーを定義し、それぞれをカプセル化し、実行時に入れ替え可能にします。動的にアルゴリズムを選択でき、複雑な条件分岐を回避でき、柔軟で拡張可能なコードを促進します。
Strategy パターンの機能
STRATEGY → encapsulate INTERCHANGEABLE algorithms/behaviors behind a common interface:
→ define a family of strategies (each implements the same interface)
→ the context uses a strategy via the interface → can SWAP strategies at runtime
→ choose the behavior dynamically without changing the context
→ "compose" behavior instead of hardcoding it (favor composition over conditionals)
例
{ () { } }
{ () { } }
{
() { . = strategy; }
() { ..(amount); }
}
( ()).();
