Requests a route calculation
Backend & Runtime / Knowledge library
Strategy Pattern
Encapsulates interchangeable algorithms behind one interface
Why learn Strategy Pattern?
It selects behavior through composition rather than conditional sprawl
The problem it solves
It varies an algorithm independently from the object using it
Where it came from
Strategy Pattern is a software design pattern that encapsulates interchangeable algorithms behind one interface. Its guarantees and tradeoffs depend on the concrete implementation and operating context.
Build your understanding
Work through these exercises in order. Try the task before opening a reference.
- Define Strategy Pattern and distinguish it from a neighboring concept.
- Choose a realistic task where Strategy Pattern is appropriate.
- Identify a failure mode caused by misusing Strategy Pattern.
- Interpret a small design or result involving Strategy Pattern.
- Explain one tradeoff or limitation of Strategy Pattern.
Documentation and sources
- Patterns of Enterprise Application Architecture (martinfowler.com)
Connect the ideas
Explore next
Real World Example
Dynamic Logistics Routing Engine
Global logistics firms managing complex, multi-modal freight delivery networks.
The business problem
Hard-coded routing logic creates rigid systems where adding new carrier-specific calculation methods requires modifying core business logic, leading to high regression risks and deployment bottlenecks.
How this topic solves it
The Strategy Pattern encapsulates interchangeable routing algorithms behind a common interface, allowing the system to swap calculation logic at runtime without altering the client code that requests the route.
- Define a common RoutingStrategy interface with an execute method.
- Implement concrete classes for specific algorithms like AirFreight, SeaFreight, and GroundTransport.
- Configure a context object to hold a reference to a RoutingStrategy implementation.
- Inject the required strategy into the context based on user-selected criteria.
- Execute the strategy through the context interface to retrieve the calculated route.
Shows how the client interacts with the context to trigger a specific strategy.
Holds the current strategy
Interface defining the execute method
Specific algorithm implementation
- Client → sets strategy → RoutingContext
- RoutingContext → calls interface → RoutingStrategy
- RoutingStrategy → implements → ConcreteStrategy
Expected business outcome
Improved system maintainability and modularity, enabling developers to introduce new routing algorithms as independent components without risking the stability of existing delivery logic.
Limitations and checks
The primary limitation is the increased complexity caused by the proliferation of strategy classes; developers must monitor the number of objects created to ensure the system remains navigable and check for redundant logic by reviewing the strategy interface implementation count.