Skip to content
StackDecode
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.

  1. 1Define the strategy interface
  2. 2Implement algorithms
  3. 3Context holds one
  4. 4Swap when needed
1/4

Step by step

  1. 1Define the strategy interface
  2. 2Implement algorithms
  3. 3Context holds one
  4. 4Swap when needed

Where it is used

Sorting, pricing, routing.

Watch out for

Clients must know the differences.

Related