Strategy Pattern
The Strategy pattern is a behavioral design pattern that defines a family of interchangeable algorithms, encapsulates each one behind a common interface, and lets a client select or swap the algorithm at runtime.
Definition
The Strategy pattern is a behavioral design pattern that defines a family of interchangeable algorithms, encapsulates each one behind a common interface, and lets a client select or swap the algorithm at runtime.
Overview
The Strategy pattern, one of the Gang of Four's behavioral patterns, addresses the problem of having multiple ways to perform a task — say, different sorting algorithms, different pricing rules, or different compression methods — where the choice needs to vary independently of the code that uses it. Rather than embedding conditional logic (a long if/else or switch statement) that selects behavior inline, Strategy extracts each algorithm into its own class implementing a shared interface, and the context object that needs the behavior holds a reference to a strategy object, delegating the actual work to it. This achieves the 'open/closed principle': new strategies can be added by writing a new class implementing the interface, without modifying the context class or any existing strategy. It also makes each algorithm independently testable in isolation, since strategies have no dependency on the context that uses them beyond the shared interface's inputs and outputs. The strategy in use can be chosen at construction time, injected via a setter, or selected dynamically at runtime based on configuration, user input, or context. Strategy is closely related to, and often confused with, the State pattern — structurally they look similar (a context holding a reference to an interchangeable object), but their intents differ: State models an object whose internal behavior changes with its own lifecycle and typically manages its own transitions, while Strategy is about the client actively choosing among interchangeable algorithms for a single operation. Strategy is also a foundational building block for higher-order-function-friendly languages: in languages with first-class functions (JavaScript, Python, Go, Rust closures), the 'strategy' is often just a function or closure passed as an argument, rather than a full class hierarchy, achieving the same decoupling with less ceremony. Common real-world applications include payment-processing systems that support multiple payment methods, sorting or comparison logic that varies by field, validation rule sets, and route-planning algorithms that can be swapped (shortest path vs. fastest route vs. avoid tolls).
Key Concepts
- Encapsulates interchangeable algorithms behind a common interface
- Lets the algorithm used by a context object be selected or swapped at runtime
- Replaces long conditional (if/else or switch) logic for algorithm selection
- Supports the open/closed principle — new strategies added without modifying existing code
- Each strategy is independently testable in isolation
- Often implemented with plain functions/closures in languages with first-class functions
- Structurally similar to, but conceptually distinct from, the State pattern
- Commonly configured via constructor injection, setters, or runtime configuration
Use Cases
Frequently Asked Questions
From the Blog
Go-to-Market Strategy: A Practical Guide to Launching Right
A go-to-market strategy is the plan that connects a product to the customers who need it, covering positioning, channels, and messaging before launch. This guide breaks down the core components and how to build one step by step.
Read More AI & TechnologyPricing Strategy 101: How Companies Set Prices
A pricing strategy is the method a company uses to set prices for its products based on costs, competition, and perceived customer value. This guide breaks down the main pricing models, when each one applies, and common mistakes to avoid.
Read More Cloud & CybersecurityBlue-green and canary deployments: choosing a rollout strategy
Choose a rollout strategy you can actually operate: how blue-green, canary and rolling updates differ in rollback speed, cost and the signals each one needs.
Read More Cloud & CybersecurityHow to build a cloud cost allocation tagging strategy
Make your bill answer "who owns this": choosing a minimal mandatory tag set, enforcing it at creation, handling untaggable spend and reporting per team.
Read More