Skip to main content

Strategy Pattern

In Turkish
Strategy Deseni
Updated 3 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/strategy-pattern

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

Shipping strategies as interchangeable functionstypescript
// 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)); // 0

Readers 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

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings