Skip to main content
Design Patterns design recommended

Strategy Pattern

The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Enables switching algorithms at runtime without modifying client code.

Difficulty
intermediate
Read time
1 min read
Version
v1.0.0
Confidence
established
Last updated

Quick Reference

Strategy Pattern: Define algorithm interface, implement variations as separate classes, inject strategy into context. Eliminates switch statements on type. Use for: pricing rules, validation, sorting, compression, payment methods. Select strategy at runtime based on config or user input.

Use When

  • Multiple algorithms for same task
  • Need to swap algorithms at runtime
  • Avoiding large switch/if-else blocks
  • Different behavior based on configuration

Skip When

  • Only one algorithm implementation exists
  • Algorithms never change at runtime
  • Simple boolean flags suffice

Strategy Pattern

The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Enables switching algorithms at runtime without modifying client code.

Tags

strategy design-patterns behavioral algorithms clean-code

Discussion