Strategy
Description
When a context needs to perform an operation whose best implementation depends on circumstances that aren’t known until runtime, the strategy pattern encapsulates each candidate algorithm as an object behind a common interface and lets the context choose which to use. The choice can be driven by config, by input characteristics, by environment, by user role, by experiment assignment, by cost budget — the selection mechanism is part of the design. Structurally, strategy is the “explicit router with algorithm-shaped rivals” specialization ofrivals-into-router. The router is the context’s selection logic; the rivals are the strategy implementations; the shared interface is the contract that lets them be substituted. The Protocol pattern in modern Python — where a typed interface lets multiple concrete implementations be swapped in at the call site — is the same shape under a different name.
The wider cross-domain shape is “interchangeable plans selected based on context” — game-theoretic strategies, military doctrines, payment-method choices at checkout, compression algorithms keyed to file type, sorting algorithms keyed to input size.
Triggers
User-initiated: User says “make this pluggable” or “let me swap this out” or “we’ll want to try different X.” Vocabulary cues: “strategy,” “pluggable,” “interchangeable,” “policy,” “configurable,” “different implementations of the same thing.” Agent-initiated: Agent notices severalif/elif-shaped dispatch branches with similar bodies, or repeated near-duplicate algorithms that could share an interface. Candidate inference: “lift these into strategy objects with a common interface.”
Situation-shape signals: Multiple algorithms doing the same job differently. Code that already has branching on “type of X” with each branch implementing a variant. Need to defer the choice of algorithm to deployment, configuration, or runtime decision. Need to A/B test alternative implementations cleanly.
Exclusions
- Only one realistic implementation — if the algorithm family has effectively one member, encapsulating it as “a strategy” adds indirection without payoff. Strategy earns its keep when there are real interchangeable rivals.
- Algorithms with incompatible side effects — if the “strategies” have different return shapes or different effect profiles, they’re not truly interchangeable; calling them strategies hides a structural mismatch that should be surfaced.
- Performance-critical hot paths — dispatch through a strategy object often costs a virtual call or function-pointer indirection; in micro-benchmarks-shaped contexts, the indirection cost can dominate.
Structure
Relationships
- rivals-into-router — strategy is the algorithm-shaped case of rivals-into-router; the rivals are the strategies, the router is the selection logic.
- template-method — strategy and template-method are the two GoF answers to “make the algorithm pluggable” — strategy swaps the whole algorithm; template-method swaps only individual steps.
- doctrine — military / strategic doctrines are real-world strategies; the doctrine encodes both the strategy and its triggering conditions.
- asymmetric-gate — strategies often differ along a cheap/expensive or fast/precise axis; selecting strategies is selecting which asymmetry to accept.
- cost-cascade — cost-cascade is “try cheap strategy first, fall back to expensive strategy on failure”; strategy is the primitive that makes such cascades expressible.
Examples
Compression-algorithm selection · computer-science
Compression-algorithm selection · computer-science
Payment-method selection at checkout · computer-science
Payment-method selection at checkout · computer-science
A/B-test variant assignment · computer-science
A/B-test variant assignment · computer-science
`Comparator`/`Comparable` in Java/C++ STL · computer-science
`Comparator`/`Comparable` in Java/C++ STL · computer-science
Functional programming — strategies are just higher-order functions in disguise; the pattern is the OOP-shaped expressio · computer-science
Functional programming — strategies are just higher-order functions in disguise; the pattern is the OOP-shaped expressio · computer-science
Game AI behaviors · computer-science
Game AI behaviors · computer-science
Game-theoretic / military "strategy" — interchangeable plans selected based on opponent state; the structural ancestor of the software pattern · economics
Game-theoretic / military "strategy" — interchangeable plans selected based on opponent state; the structural ancestor of the software pattern · economics
Gang of Four (1994), *Design Patterns* — the OOP coinage. · computer-science
Gang of Four (1994), *Design Patterns* — the OOP coinage. · computer-science
GoF Strategy · computer-science
GoF Strategy · computer-science