Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
id behavioral/strategy
name Strategy
aliases
policy
guide_url
problem Make an algorithm interchangeable at runtime without the caller knowing which variant it got.
symptoms
swap algorithm at runtime
pricing rules
pluggable policy
if/elif chain choosing behavior
verdict prefer-alternative
caveats
In Python a strategy is just a function passed as an argument — the class-per-algorithm hierarchy is Java's workaround for lacking first-class functions.
Reach for the class form only when a strategy carries its own state or several related methods.
stdlib_sightings
sorted
list.sort
functools.cmp_to_key

Strategy

Swap the algorithm without touching the caller. Verdict: prefer an alternative — in Python a strategy is a function passed as an argument; the registry below is for families that grow.

Where What
pattern/ The importable code: StrategyRegistry, UnknownStrategyError
docs/ Fundamentals · Implementation guide · External examples
examples/promotions/ Mini-project: checkout pricing rules built on pattern/
tests/ Behavioral tests for the pattern and the mini-project
uv run python -m patterns.behavioral.strategy.examples.promotions.main