Design Patterns · 9 min read · Beginner
Strategy & Factory Patterns
Swap algorithms dynamically at runtime with the Strategy pattern and centralize object creation with Factories.
What you'll learn
- Encapsulate interchangeable algorithms using the Strategy pattern
- Decouple object instantiation from business logic using a Factory
- Combine both patterns cleanly without giant switch blocks
The Strategy pattern lets you swap an algorithm (shipping rate, sorting rule, compression codec) at runtime by injecting an object that conforms to a shared contract. A Factory maps a configuration key ("express", "standard") to the right concrete strategy.
Fill-in-the-Blank Exercise
Strategy Registry Lookup
Look up the matching shipping strategy from a map by key.
const strategies = { standard: StandardShip, express: ExpressShip };
function createShipper(type) {
const Cls = [type] || StandardShip;
return Cls();
}Live OOP Playground
Shipping Strategy + Factory
Live Result
Console Output
No console output yet. Try calling console.log(...) in JS.
Check Your Understanding
Score: 0 / 1
1. What problem does the Strategy pattern solve?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.