strategy-pattern
Installation
SKILL.md
Strategy Pattern (Symfony)
Use when
- One operation has several interchangeable algorithms chosen at runtime, such as payment providers or export formats.
- A
switchormatchon a type string keeps growing. - New variants must be added without editing the code that selects them.
Default workflow
- Define the strategy interface, with a
supports()method or a key per strategy. - Tag every implementation through
#[AutoconfigureTag]on the interface or on each class. - Inject them with
#[AutowireIterator]to try each in order, or#[AutowireLocator]to fetch one by key. - Set priorities where evaluation order matters, and register a fallback strategy.
- Test each strategy alone, and the selector with fakes.
Guardrails
- The selector fails explicitly when no strategy supports the input.
- Keep strategies stateless and independent of each other.
- Two cases that will not grow do not need the pattern: a
matchis enough.