← Back to PatternPip

STRATEGY / THE IDEA IN A MINUTE

Same job.
A different way to do it.

Strategy lets you swap how a job is done while the code requesting it stays the same.

Give each approach the same contract: what it accepts, what it returns, and what it promises. The object using it—the context—asks for the work through that contract.

What varies?
The algorithm used to do a job.
What stays stable?
The context that requests the work, and the contract it relies on.
Where is the choice made?
In the caller or configuration, outside the context.

THE STRUCTURE / FOUR ROLES

Choose outside. Delegate inside.

Application setup gives a Sorter an algorithm object. Sorter holds it through the SortStrategy interface and asks it to sort. Replacing that object changes the algorithm without changing Sorter’s code.

Swap the strategy, keep the context.

This example reuses one Sorter and the same input on every selection.

01 / CLIENTChooses the algorithm

Application setup

sorter.setStrategy(new BubbleSort())

Creates the chosen strategy and installs it on the existing Sorter. Then requests the job with sorter.sort(input).

The selection decision lives here. Sorter does not choose the “best” algorithm.

02 / CONTEXTSame object

Sorter

private strategy: SortStrategy

Holds one strategy at a time. Its sort method delegates through the shared interface:

return this.strategy.sort(input)

Currently holds BubbleSort. No algorithm-specific branch is needed inside Sorter.

03 / STRATEGY INTERFACESame contract

SortStrategy

sort(input: readonly number[]): SortRun

Promises ascending values and a trace, preserving the original input and duplicates. Every concrete strategy must honor those promises.

The interface describes the job. The selected object performs it.

04 / CONCRETE STRATEGIESReplaceable objects

Two ways to do the same job

  • BubbleSortIn use

    Compare neighbors. Swap those out of order.

  • QuickSort

    Partition around a pivot. Sort each side.

Both implement SortStrategy. Sorter holds one of these objects; it does not inherit from either class.

Setup supplies a strategy to the context. The context calls its interface. Each concrete strategy implements that interface with a different algorithm.

Same request. Same sorted values.

[5, 1, 4, 2, 3] → Sorter → BubbleSort → [1, 2, 3, 4, 5]

This result runs the selected algorithm. The algorithm’s comparison and swap steps can differ, while the context and contract stay the same.

Try the demo below to follow the request into the strategy and the result back to its caller.

WATCH THE HANDOFF

Change the strategy. Keep the Sorter.

Try it yourself

Choose an approach, then send the same sorting request. Only the selected strategy does the work.

1. Choose a strategy

This choice belongs to the caller.
Sorter only knows the contract.

THE CALLER

Sort this list

THE CONTEXT

Sorter

Same object. Same code.strategy.sort(input)

“I ask my current strategy.
It decides how to sort.”

Bubble Sort

Selected

Compare neighbors. Swap those out of order.

sort(input)

Quick Sort

Waiting

Partition around a pivot. Sort each side.

sort(input)

BACK TO THE CALLER

Same promise. Different approach.

Ascending values. Original list preserved.

The result appears here after the handoff.

Bubble Sort is installed. Ask the Sorter to sort the list.

Try this: run Bubble Sort, switch to Quick Sort and run again. The active connection changes; the Sorter and its request stay the same.

Reduced motion is on; results appear immediately.

Useful when behavior keeps changing.

Several approaches do the same job, and you want to add or replace one without repeatedly editing the context.

Keep the cost in mind.

Extra interfaces and classes add structure to maintain. One function or a small, stable conditional may be enough. Strategy organizes change; it does not make an algorithm faster.

Choose your next step.