Skip to main content
computer-science economics

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 of rivals-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 several if/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

Internal structure of strategy: a table of its component slots and the concepts that fill them.

Relationships

Relationship neighborhood of strategy: a graph of the concepts it connects to and the concepts it is a part of.
  • 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

Mainstream storage and transport systems routinely face a runtime choice among compression algorithms — gzip, zstd, lz4, snappy, brotli — each occupying a different point on the compression-ratio versus compression-speed versus decompression-speed surface. Production systems often select a different algorithm depending on the use case: lz4 for memory-bound inter-service messages where decompression latency dominates, zstd for archival storage where compression ratio dominates, gzip for HTTP traffic where ubiquity and tooling support dominate. The application code does not encode which algorithm it uses; it calls a shared compress interface and the underlying selection is configurable at runtime by deployment configuration, per-request headers, or per-file metadata.This is the strategy pattern in production: a family of genuinely interchangeable algorithms behind a single interface, selected by the calling context, with the application code remaining algorithm-agnostic. The cost of indirection is real (a virtual call, a configuration lookup) but trivial compared to the cost of either committing to one algorithm globally or hard-coding selection logic into every caller.Inference: When a system uses multiple algorithms for what is structurally the same operation, the strategy pattern is the architectural baseline — not “should we use strategy here?” but “what is the selection criterion, and where does it live?” Algorithm selection logic baked into call sites is a smell; centralizing it behind a strategy interface lets the selection move (config, header, metadata) without touching the callers.

Payment-method selection at checkout · computer-science

credit-card, PayPal, ACH, crypto: same purchase interface, different strategy.
each variant is a strategy; the experiment framework is the context.
sorting with pluggable order strategies.
Functional programming — strategies are just higher-order functions in disguise; the pattern is the OOP-shaped expression of “function as argument.”
aggressive vs. defensive vs. exploratory strategies switched based on game state.
the cross-domain reach into game theory and military planning establishes strategy as a structural primitive, not just a software trick
The Gang of Four’s Design Patterns introduced Strategy as a behavioral pattern: define a family of algorithms, encapsulate each one behind a common interface, and let the client select among them at runtime. The book’s canonical examples — pluggable layout algorithms in a text composition system, interchangeable validation policies — established the pattern’s vocabulary.The example instantiates strategy’s core move: separating the what (the operation invoked) from the how (which concrete algorithm is selected to perform it), so the choice can vary without disturbing the call sites. Functional-language analogues — passing a comparator function to a sort, passing a strategy as a higher-order function — collapse to the same structural shape with lighter syntactic weight.
the OOP coinage; example was layout strategies for a text editor.