← Design Patterns learning pathDesign Patterns · Module 6 of 10
Observer and Strategy
Notify subscribers and swap algorithms.
📖 Key ideas
- Observer: subjects notify registered observers when state changes.
- Used in event systems, UI updates, pub/sub.
- Strategy: define a family of algorithms and switch them at runtime.
- Example: payment strategies (UPI, card, wallet) or sorting strategies.
- Strategy replaces long if-else chains.
💡 Real-life example: Observer is a YouTube subscription bell; Strategy is choosing route options (fastest, shortest, no tolls) in Maps.
ObserverSubscribers notified of changes
SubjectObject being observed
StrategyInterchangeable algorithms
ContextObject using a strategy