Build fluency in the vocabulary of extracting an interchangeable algorithm behind a common interface swappable at runtime.
0 / 5 completed
1 / 5
At standup, a dev mentions extracting an interchangeable algorithm into its own object behind a common interface, so calling code can swap in a different algorithm at runtime without changing the code that uses it. What is this design pattern called?
The strategy pattern is exactly this: the strategy pattern extracts an interchangeable algorithm into its own object behind a common interface, so calling code depends only on that interface and can swap in a different concrete algorithm at runtime without changing the code that uses it. A hash collision is an unrelated hash-table concept about two keys sharing a bucket. This extract-the-algorithm-behind-a-common-interface approach is exactly why the strategy pattern lets new algorithm variants be added without touching the code that selects and uses them.
2 / 5
During a design review, the team applies the strategy pattern to a feature that must support several interchangeable algorithms chosen at runtime, specifically because swapping algorithm objects behind a common interface avoids a long conditional branch selecting behavior by type. Which capability does this provide?
The strategy pattern here provides Runtime-swappable algorithms without conditional branching, since calling code depends only on the common interface, and adding a new algorithm variant means adding a new implementation of that interface instead of adding another branch to a growing conditional. A long conditional branch selecting behavior by type must be edited every time a new algorithm variant is added, and tends to grow unwieldy. This swap-behind-a-common-interface behavior is exactly why the strategy pattern is favored whenever several interchangeable algorithms must be selected at runtime.
3 / 5
In a code review, a dev notices a feature that must support several interchangeable algorithms selects behavior through a long conditional branch checking a type flag, instead of extracting each algorithm into its own object behind a common interface. What does this represent?
This is a missed strategy-pattern opportunity, since extracting each algorithm into its own object behind a common interface would let a new variant be added without touching the growing conditional branch. A cache eviction policy is an unrelated concept about discarded cache entries. This long-conditional-branch pattern is exactly the kind of maintenance risk a reviewer flags once several interchangeable algorithms are selected by a type check.
4 / 5
An incident report shows adding a new algorithm variant required editing a long, growing conditional branch and accidentally broke an unrelated existing branch, because behavior was selected through that conditional instead of extracting each algorithm behind a common interface. What practice would prevent this?
Introducing the strategy pattern lets a new algorithm variant be added as an independent implementation of the common interface, without touching existing branches. Continuing to select behavior through a long, growing conditional branch regardless of how many interchangeable algorithm variants the feature ends up supporting is exactly what caused the issue described in this incident. This strategy-based approach is the standard fix once a conditional branch selecting algorithm variants grows large enough to be risky to edit.
5 / 5
During a PR review, a teammate asks why the team reaches for the strategy pattern instead of simply adding another branch to the existing conditional whenever a new algorithm variant is needed. What is the reasoning?
The strategy pattern extracts each algorithm behind a common interface, so a new variant is added independently without touching existing code, while extending a conditional branch is simpler for one or two variants but grows unwieldy and riskier to edit as more variants are added. This is exactly why the strategy pattern is favored once several interchangeable algorithms are expected, while a simple conditional remains fine for just one or two fixed variants.