Behavioral pattern · Visual logic

Strategy Pattern: Swap the Rule, Not the System

When one task has several valid ways to run, Strategy keeps the workflow stable while the decision-making rule can change independently.

Problem

A checkout, route planner, or game character keeps growing conditional branches whenever a new behavior is added.

Rule

Extract each interchangeable behavior behind one shared contract and inject the behavior the context should use.

Run
  1. INPUT selects the behavior required for this situation.
  2. CONTEXT delegates the work through a stable strategy contract.
  3. STRATEGY runs its own algorithm without changing the context.
Output

New algorithms can be added or selected without rewriting the workflow that consumes them.

Run it in the real world

A delivery quote keeps the same checkout flow while switching between bicycle, standard courier, and express pricing strategies.

Go deeper

This is an original explanation and example inspired by Head First Design Patterns. The book covers the complete pattern, design principles, and implementation detail.

Retail links are not affiliate links yet. If that changes, this page will clearly disclose it.