2.2.12 · HinglishDesign Principles

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

2,989 words14 min readRead in English

2.2.12 · Coding › Design Principles


Behavioral patterns exist kyun karte hain?

Har pattern mein ek hi repeated trick hai: ek hard-coded if/switch ya direct method call ko ek aisi object se replace karo jise tum swap, store, queue, ya notify kar sako.


Ek nazar mein 10 patterns

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


80/20 — asli mein kya yaad rakhna hai


Recall Feynman: ek 12 saal ke bacche ko samjhao

Ek classroom imagine karo.

  • Observer: teacher ghanti bajata hai aur jo sunna chahte hain woh sab react karte hain. (broadcast)
  • Strategy: tum ek maths problem addition se solve kar sakte ho, ya ungliyon par gin ke — tum method choose karte ho aur problem ko koi fark nahi.
  • Command: tum ek chore sticky note par likhte ho ("kachra bahar karo"). Ab tum ise baad mein kisi ko de sakte ho, ya faad sakte ho (undo).
  • Iterator: ek remote jisme "next channel" button hai — tumhe nahi pata TV channels kaise store karta hai.
  • State: ek traffic light apne colour ke hisaab se alag behave karta hai, aur woh khud decide karta hai kab switch karna hai.
  • Template Method: ek recipe jiske steps fixed hain lekin tum filling choose karte ho.
  • Chain of Responsibility: tum ek sawaal poochte ho; agar pehle bacche ko nahi pata, woh agli ko pass karta hai, aur aise chalta hai.
  • Mediator: sab bacchon ke aapas mein chillane ki jagah, sab teacher se baat karte hain.
  • Memento: tum apni Lego build ki photo lete ho taaki agar toot jaaye toh exactly rebuild kar sako.
  • Visitor: ek guest aata hai aur ek kaam karta hai (khilaune ginta hai) har khilaune ke saath, khilauniyon ko change kiye bina.

Flashcards

Woh ek problem kya hai jo SARE behavioral patterns address karte hain?
Objects kaise communicate karte hain aur runtime par responsibilities kaise divide hoti hain (control/messaging ka flow), ek patli contract ke zariye jo collaborators ko decouple karti hai.
Observer: subscriber list ka maalik kaun hai aur notify() kya karta hai?
Subject list ka maalik hai; notify() observers par loop karta hai har observer ka update() ek interface ke through call karta hai — ek decoupled fan-out broadcast.
Strategy vs State — same UML, difference kya hai?
Intent. Strategy objects independent hote hain aur client dwara choose kiye jaate hain; State objects ek doosre ko jaante hain aur context par khud transitions trigger karte hain.
Strategy vs Template Method?
Strategy ek algorithm ko composition se vary karta hai (object pass in kiya); Template Method inheritance se (fixed skeleton mein hook steps override karo).
Command pattern ek ___ ko ___ mein convert karta hai, kaun se 3 cheezein enable karta hai?
Ek request ko ek object mein; undo/redo, queuing/scheduling, aur logging enable karta hai.
Iterator ka key benefit kya hai?
Collection ki internal representation expose kiye bina traverse karo; traversal state iterator mein hoti hai isliye multiple independent traversals possible hain.
Chain of Responsibility: ek handler jo request handle nahi kar sakta woh kya karta hai?
Use apne next handler ko pass karta hai; chain wahan rukti hai jahan pehla handler succeed karta hai (ya end se gir jaata hai → unhandled).
Mediator vs Observer?
Dono decouple karte hain, lekin Mediator ek hub mein specific coordination logic centralize karta hai (many-to-many → star); Observer generic one-to-many broadcast hai jisme koi coordination logic nahi.
Memento vs Command-based undo?
Memento ek full state snapshot store karta hai (state replace karke restore); Command store karta hai kisi specific action ko reverse kaise karein. Memento tab use karo jab reversing mushkil ho lekin snapshotting sasti ho.
Visitor ka core mechanism aur trade-off kya hai?
Double dispatch (element.accept(v) → v.visitX(element)). Naye operations add karna easy, naye element types add karna mushkil — normal OOP ka mirror.
Har behavioral pattern ke peechhe universal "trick" kya hai?
Ek hard-coded direct call ya if/switch ko ek swappable/storable/queuable/notifiable object se replace karo.
Template Method kaun sa principle embody karta hai?
Inversion of control / Hollywood Principle: "Don't call us, we'll call you" — base method subclass hooks ko ek fixed order mein call karta hai.

Connections

  • Design Patterns — Creational — woh objects banao jo behavioral patterns phir wire karte hain.
  • Design Patterns — Structural — objects compose karo; Behavioral define karta hai ki composed objects kaise interact karte hain.
  • SOLID Principles — Strategy/State/Command Open–Closed enact karte hain; Visitor aur Observer Dependency Inversion interfaces ke zariye enact karte hain.
  • Polymorphism — har behavioral pattern dynamic dispatch par lean karta hai.
  • Publish-Subscribe & Event-Driven Architecture — Observer processes/network par scale hota hai.
  • Undo-Redo Systems — Command + Memento saath kaam karte hain.

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