strategy-pattern-go
Installation
SKILL.md
Strategy Pattern (Go Backend)
Why: Strategy lets you define a family of algorithms, put each in a separate type, and make them interchangeable so the context stays stable while behavior is swapped at runtime (Refactoring.Guru).
Hard constraints: Context must depend only on a strategy interface, not concrete implementations. Use composition (context holds an interface value); avoid embedding or inheritance for variant behavior. Keep each strategy in its own type/package when it has real logic.
When to use
- Different variants of the same algorithm (e.g. payment methods, route builders, serializers) and you want to switch at runtime.
- A handler or service is bloated with conditionals (e.g.
switch method { case "stripe": ... case "paypal": ... }); extract each branch into a type that satisfies an interface. - You need to isolate algorithm details from the rest of the backend logic (Open/Closed: add strategies without changing context).