System Design
15. Software Design PatternsAnimated walkthrough
Strategy Pattern
Swap algorithms at runtime.
The idea
Define a family of interchangeable algorithms behind one interface.
Watch it work
The steps light up one at a time. Use the controls to pause or step through.
- 1Define the strategy interface
- 2Implement algorithms
- 3Context holds one
- 4Swap when needed
1/4
Step by step
- 1Define the strategy interface
- 2Implement algorithms
- 3Context holds one
- 4Swap when needed
Where it is used
Sorting, pricing, routing.
Watch out for
Clients must know the differences.