Skip to content
uencoder

Strategy Pattern

The strategy pattern encapsulates algorithms as independent strategy objects, switching algorithms at runtime through an interface and avoiding conditional branches.

Purpose

Make families of algorithms interchangeable, decoupling clients from algorithm implementations.

Mechanism

Define a strategy interface that each algorithm implements. The context holds a strategy reference and delegates to it. Swapping the strategy object at runtime changes behavior. Combined with the factory pattern to create strategy instances.

Use cases

  • Algorithm switching
  • Conditional elimination
  • Payment/sorting strategies

Related terms

References