Software engineering / Strategy pattern

Strategy pattern

A checkout has to price delivery in several ways. We pull each rule out of a chain of ifs into its own swappable piece, then shrink that piece to a plain function.

Beginner7 steps~6 min
Context — the checkout: needs a price, holds a strategy
Strategy — one pricing rule, same form as the others
Active strategy — the one plugged in right now
Price — the answer the strategy returns
// THE SHORT VERSION

In 30 seconds

The strategy pattern is for code that can do one job in several ways. Each way, a strategy, is written as a separate piece with the same form, so any of them can stand in for another.

The code that needs the job done, the context, holds one strategy and simply calls it. Swapping the strategy changes the behaviour without changing the context, and a new strategy can be added without editing old code.

In most modern languages a strategy is just a function passed as an argument, like the comparison function you give to sort.

Strategy pattern, step by step

STEPS01 / 07

Simplified: real strategies often need more than one input, and typed languages declare the shared form as an interface. The prices are made up for the example.

STEP 01 / 07 One function, many branches
THE MATH
if standard: 4 + 1 × kg if express: 9 + 2 × kg if pickup: 0
  1. Step 1: One function, many branches. A shop’s checkout works out the delivery price of an order. There are three delivery methods, each with its own rule, and all three rules sit inside one function behind a chain of ifs. It works, but the function grows with every method, and adding a fourth means opening and editing code that already works.
  2. Step 2: One box per rule. Take each rule out of the if chain and give it a box of its own. Every box answers the same question in the same form: given a weight, return a price. Because they share that form, any of them fits where another did. Each one is called a strategy, and together they are a family of interchangeable algorithms.
  3. Step 3: Plug one in. The checkout no longer contains any rule. It holds one strategy in its slot and, when it needs a price, asks whatever is there. With the standard card plugged in, an order of 2 kg costs 4 + 1 × 2 = €6. The checkout, which uses the strategy, is called the context.
  4. Step 4: Swap while running. The customer changes to express delivery. The program takes the standard card out and puts the express card in, while it is running. The checkout’s own line of code is exactly the same as before, yet the same 2 kg order now costs 9 + 2 × 2 = €13. Behaviour changed without a single if.
  5. Step 5: Add one, edit nothing. Drone delivery arrives, at a flat €15. In the if chain this meant editing the big function. Now it means writing one new card with the same form and plugging it in. The checkout and the three existing strategies are not touched, so nothing that already worked can break.
  6. Step 6: Just pass a function. The classic version wraps each strategy in a class with one method. In languages where a function can be stored in a variable and handed to other code, the function alone is enough. You have probably used the pattern already: the comparison function given to sort is a strategy for deciding which item comes first.
  7. Step 7: Who picks the strategy?. Something still has to turn the customer’s choice into a strategy. A table from names to strategies does it in one lookup, with no chain of ifs, and a new method is one new entry. The pattern earns its place when rules multiply or change often; for two fixed branches, a plain if is simpler and easier to read.
VALUES SO FAR
order2 kginput
branches3 ifsproblem
family····rule
standard····result
express····result
drone····added
edits····checkout
form····modern
chooser····lookup

Spotted a mistake on this page?