Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Strategy Design Pattern

Swap algorithms without rewriting the object that uses them.

The Strategy Pattern separates what a system does from how that behavior is chosen.


Business Framing

A pricing engine may need different rules for:

  • retail customers

  • wholesale buyers

  • VIP members

The checkout flow should not be rewritten every time pricing policy changes. Instead, the checkout uses a pricing strategy object.


Visual Intuition


Pattern Anatomy

PartResponsibility
ContextUses a strategy to perform work
Strategy interfaceDefines a common method
Concrete strategiesImplement the behavior differently

Worked Example: Pricing Strategies

What changed and what stayed stable?

The Checkout class stayed stable. Only the pricing behavior changed. That is exactly why Strategy reduces branching chaos.


Why Not Just Use if/elif?

Small decisions can use plain conditionals. Strategy becomes valuable when:

  • behavior families keep growing

  • rules change independently of the caller

  • you want easier testing

  • you want to inject behavior dynamically


When Strategy Fits Best

Good fitWeak fit
Pricing rulesOne tiny condition used once
Shipping logicHard-coded behavior that never changes
Recommendation rankingExtremely simple procedural scripts
Fraud-scoring policiesProblems with no interchangeable behavior

Mini Quiz

  1. What stays stable in the Strategy Pattern?

  2. What changes from one strategy to another?

  3. What is the biggest practical benefit of Strategy in business apps?

Answer Check
  1. The context object

  2. The algorithm or decision rule

  3. It allows business logic to evolve without rewriting the whole workflow


Practice Prompt

Build a ShippingStrategy system with StandardShipping, ExpressShipping, and PickupShipping.

Design Hint

Keep the order-processing workflow inside one context class, and move only shipping-cost computation into the strategy classes.


Interactive Code

What to notice

The workflow still runs through the same Checkout class. Only the policy-selection step changes which strategy gets injected.


Guided Practice

Which design choice best signals a Strategy pattern?

One class creates all concrete objects That points more toward Factory than Strategy.
A stable context can swap among interchangeable algorithms Strategy keeps the caller stable while the algorithm varies.
Features are layered around one object That describes Decorator.
One event triggers many subscribers That describes Observer.

Exercises

  1. Add a HolidayShipping strategy that applies a time-limited discount.

  2. Refactor a long if/elif decision tree into small strategy classes.

Exercise hint

Define a common cost() method first, then make the calculator depend only on that interface rather than on concrete shipping types.


Key Takeaway

Strategy helps you say: same workflow, different policy.


Quick Exercise Solution Hint

  • HolidayShipping above shows a ready implementation for Exercise 1 in the practice prompt. Try replacing the placeholder strategy with HolidayShipping() in the practice cell to verify behavior.