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.
supplies the chosen strategy
delegates through SortStrategy
algorithms implement SortStrategy
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.
supplies strategy
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.
holds and calls through SortStrategy
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.
both implement SortStrategy
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.
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.
This choice belongs to the caller. Sorter only knows the contract.
THE CALLER
Sort this list
51423
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.
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.