← Design Patterns learning path
Design Patterns · Module 6 of 10

Observer and Strategy

Notify subscribers and swap algorithms.

📖 Key ideas

  1. Observer: subjects notify registered observers when state changes.
  2. Used in event systems, UI updates, pub/sub.
  3. Strategy: define a family of algorithms and switch them at runtime.
  4. Example: payment strategies (UPI, card, wallet) or sorting strategies.
  5. 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