2.2.12Design Principles

Design Patterns — Behavioral - Observer, Strategy, Command, Iterator, State, Template Method, Chain of Responsibility,

2,729 words12 min readdifficulty · medium

WHY do behavioral patterns exist?

The recurring trick in every pattern below is the same: replace a hard-coded if/switch or a direct method call with an object you can swap, store, queue, or notify.


The 10 patterns at a glance

Figure — Design Patterns — Behavioral -  Observer, Strategy, Command, Iterator, State, Template Method, Chain of Responsibility,

1. Observer


2. Strategy


3. Command


4. Iterator


5. State


6. Template Method


7. Chain of Responsibility


8. Mediator


9. Memento


10. Visitor


The 80/20 — what to actually remember


Recall Feynman: explain to a 12-year-old

Imagine a classroom.

  • Observer: the teacher rings a bell and everyone who chose to listen reacts. (broadcast)
  • Strategy: you can solve a maths problem by adding, or by counting on fingers — you pick the method and the problem doesn't care.
  • Command: you write a chore on a sticky note ("take out trash"). Now you can give it to someone later, or tear it up (undo).
  • Iterator: a remote with a "next channel" button — you don't need to know how the TV stores channels.
  • State: a traffic light behaves differently depending on its colour, and it decides when to switch itself.
  • Template Method: a recipe with fixed steps where you choose the filling.
  • Chain of Responsibility: you ask a question; if the first kid doesn't know, they pass it to the next, and so on.
  • Mediator: instead of all kids shouting at each other, they all talk through the teacher.
  • Memento: you take a photo of your Lego build so you can rebuild it exactly if it breaks.
  • Visitor: a guest comes and does one job (counts toys) to every toy, without changing the toys.

Flashcards

What single problem do ALL behavioral patterns address?
How objects communicate and how responsibilities are divided at runtime (flow of control/messaging), via a thin contract that decouples collaborators.
Observer: who owns the subscriber list and what does notify() do?
The subject owns the list; notify() loops over observers calling each observer's update() through an interface — a decoupled fan-out broadcast.
Strategy vs State — same UML, what's the difference?
Intent. Strategy objects are independent and chosen by the client; State objects know each other and self-trigger transitions on the context.
Strategy vs Template Method?
Strategy varies an algorithm via composition (object passed in); Template Method via inheritance (override hook steps in a fixed skeleton).
Command pattern turns a ___ into a ___, enabling what 3 things?
A request into an object; enabling undo/redo, queuing/scheduling, and logging.
What is the key benefit of Iterator?
Traverse a collection without exposing its internal representation; traversal state lives in the iterator so multiple independent traversals are possible.
Chain of Responsibility: what does each handler do with a request it can't handle?
Passes it to its next handler; the chain ends at the first handler that succeeds (or falls off the end → unhandled).
Mediator vs Observer?
Both decouple, but Mediator centralizes specific coordination logic in a hub (many-to-many → star); Observer is generic one-to-many broadcast with no coordination logic.
Memento vs Command-based undo?
Memento stores a full state snapshot (restore by replacing state); Command stores how to reverse a specific action. Use Memento when reversing is hard but snapshotting is cheap.
Visitor's core mechanism and trade-off?
Double dispatch (element.accept(v) → v.visitX(element)). Easy to add new operations, hard to add new element types — the mirror of normal OOP.
What is the universal "trick" behind every behavioral pattern?
Replace a hard-coded direct call or if/switch with a swappable/storable/queuable/notifiable object.
Template Method embodies which principle?
Inversion of control / Hollywood Principle: "Don't call us, we'll call you" — the base method calls subclass hooks in a fixed order.

Connections

  • Design Patterns — Creational — make the objects that behavioral patterns then wire together.
  • Design Patterns — Structural — compose objects; Behavioral defines how composed objects interact.
  • SOLID Principles — Strategy/State/Command enact Open–Closed; Visitor and Observer enact Dependency Inversion via interfaces.
  • Polymorphism — every behavioral pattern leans on dynamic dispatch.
  • Publish-Subscribe & Event-Driven Architecture — Observer scaled across processes/network.
  • Undo-Redo Systems — Command + Memento working together.

Concept Map

motivates

inserts

via

governs

notify

swap algorithm

store or queue

traverse

change behavior

defer steps

pass along

subject notifies many

Behavioral Patterns

Tight coupling problem

Thin agreed contract

Swap object for if/switch

Observer

Strategy

Command

Iterator

State

Template Method

Chain of Responsibility

Communication and control flow

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, **behavioral design patter

Go deeper — visual, from zero

Test yourself — Design Principles

Connections