Strategy Pattern
- In Turkish
- Strategy Deseni
In short
The strategy pattern is a behavioral design pattern that puts interchangeable algorithms behind one interface, so code can switch between them at run time.
What is the strategy pattern?
The strategy pattern takes a family of algorithms that do the same job in different ways, such as calculating shipping, sorting results, or compressing files, and puts each one behind a common interface. The code that uses them, called the context, holds a reference to one strategy and calls it without knowing which one it is. Changing the behavior then means passing a different strategy, not editing the context.
Without the pattern, this logic often grows into a long if/else or switch block that has to be edited every time a new option appears. With strategies, each option lives in its own class or function, can be tested on its own, and new ones can be added without touching existing code, which follows the open-closed principle from SOLID. In languages with first-class functions, a strategy is often just a function passed as an argument, such as the comparison function you give to a sort method.
A navigation app is a good analogy: you pick driving, cycling, or walking, and the app calculates a route to the same destination using a different algorithm for each mode. Real-world uses include pricing and discount rules, payment methods, authentication methods, retry policies, validation rules, and choosing a compression or serialization format.
The strategy pattern is often confused with the state pattern, because both swap objects behind an interface. In the strategy pattern the client chooses the algorithm from outside, while in the state pattern the object changes its own behavior as its internal state changes, such as an order moving from pending to shipped. It also pairs naturally with the factory pattern: a factory can pick the right strategy from configuration, and the strategy then does the work.
Key takeaways
- The strategy pattern puts interchangeable algorithms behind a shared interface.
- The context uses a strategy without knowing which concrete one it has.
- It replaces long conditional blocks and makes new options easy to add.
- In many languages a strategy can simply be a function passed as an argument.
- With strategies the caller chooses; in the state pattern the object switches behavior itself.
Example
// Each strategy calculates shipping in a different way
type ShippingStrategy = (weightKg: number) => number;
const standard: ShippingStrategy = (kg) => 5 + kg * 0.5;
const express: ShippingStrategy = (kg) => 15 + kg * 1.2;
const storePickup: ShippingStrategy = () => 0;
// The context doesn't care which strategy it receives
function shippingCost(weightKg: number, strategy: ShippingStrategy) {
return strategy(weightKg);
}
console.log(shippingCost(4, standard)); // 7
console.log(shippingCost(4, express)); // 19.8
console.log(shippingCost(4, storePickup)); // 0Readers ask
What is the difference between the strategy and state patterns?
Both delegate behavior to interchangeable objects. With the strategy pattern the caller picks the algorithm, while with the state pattern the object switches between states on its own as events happen, and each state defines different behavior.
Is passing a callback function the strategy pattern?
Essentially, yes. When you pass a comparison function to a sort method or a pricing function to checkout code, that function is a strategy; languages with first-class functions just don't need a separate class for each one.
When should you use the strategy pattern?
Use it when you have several ways of doing the same task and need to choose between them at run time, or when a conditional block keeps growing with new cases. If there are only two stable options, a simple if statement may be clearer.
See also
- Design PatternSoftware Architecture, p. 13A design pattern is a proven, reusable solution to a common problem in software design, described as a general template rather than as finished code.
- PolymorphismProgramming Fundamentals, p. 45Polymorphism is the ability of code to work with values of different types through one shared interface, with each type supplying its own behavior.
- InterfaceProgramming Fundamentals, p. 29An interface is a named set of method and property signatures that a type promises to provide, without saying how those members are implemented.
- Higher-Order FunctionProgramming Fundamentals, p. 25A higher-order function is a function that takes another function as an argument, returns a function, or both, so behavior can be passed around like data.
- Factory PatternSoftware Architecture, p. 19The factory pattern is a creational design pattern that moves object creation into a dedicated function or class, so callers never name the concrete class.
- SOLIDSoftware Architecture, p. 42SOLID is a set of five object-oriented design principles that help developers write code that is easier to understand, extend, test, and maintain.
Spotted a mistake or something missing on this page?Suggest an edit