🏗️Strategy Pattern
Also known as Strategy, Policy Pattern
Pick how a job is done while the program runs.
Think of it like
Think of it like picking how to get to school. Bike, bus or walk, you still end up at school.
Example
Emma's shop works out shipping three ways: standard, express and pickup. Checkout is handed the one the buyer picked and calls it, with no long if-else chain.
Why it matters
It replaces long chains of if and else with small parts that are easy to test and add to.
Definition
Common mistake
Key takeaways
- 1Same shape. Many ways to do it.
- 2Swap at run time. Pick the one you need.
- 3Add without editing. Main code stays put.