2.2.12 · D1Design Principles

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

1,882 words9 min readBack to topic

The parent note throws around words like object, method, interface, delegate, subscribe, runtime, generator, hook, and self. If any of those feel fuzzy, this page builds them from nothing. Read top to bottom — each brick rests on the one above it.


0. What is an object?

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

Why the topic needs it: every behavioral pattern is a story about boxes handing work to other boxes. Observer has subject-boxes and observer-boxes; Command wraps a job in a box. No boxes, no patterns.


1. Fields and self


2. Method and method call

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

Why the topic needs it: patterns like State and Template Method work by making new classes (new cutters) instead of adding if branches. "Adding a status = adding a class" (parent note) only makes sense once class ≠ object is clear.


4. Interface — the agreed-upon shape

This is the single most important brick. Read slowly.

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

Why the topic needs it: every behavioral pattern hangs on this. The subject talks to observers through an interface; the context talks to strategies through one; the invoker calls command.execute() through one. See SOLID Principles (the "D" — depend on abstractions) and Polymorphism for the deeper machinery.


5. Polymorphism — same call, different result

Deep dive lives at Polymorphism. Behavioral patterns are essentially polymorphism organised into recurring shapes.


6. Delegation — "not my job, I'll pass it on"


7. Runtime vs compile/write time


8. First-class functions & generators (Python specifics the parent uses)


Prerequisite map

Object = data plus behavior

Fields and self

Method

Method call x.do a

Class vs object

Interface shape contract

Polymorphism

Delegation

Inheritance

Behavioral Patterns

Runtime choice

First-class functions and generators

Read it as: boxes and their fields make method calls possible; classes make interfaces possible; interfaces unlock polymorphism and delegation; those two, plus inheritance and runtime choice, are the raw material every behavioral pattern rearranges.


Equipment checklist

Cover the right side and answer out loud before revealing.

An object bundles which two things?
Data (fields) + behavior (methods) in one box.
What does self refer to inside a method?
The specific object that method is currently running on.
In s.subscribe(o), what do the three parts mean?
s = who does it, subscribe = the method, o = the argument handed in.
Difference between a class and an object?
Class = blueprint/cutter; object = one instance stamped from it.
Define interface in one line.
A promise about which methods exist, with no promise about how they work.
What does "program to the interface" force you to ignore?
Which concrete class is behind the method — only the method shape is used.
Polymorphism in one sentence?
The same method call gives different results depending on which object receives it.
Delegation vs inheritance?
Delegation = has-a object it forwards to; inheritance = is-a kind of a base class.
Why does "choose at runtime" matter here?
Behavior is picked live from current data, not hard-coded when writing the file.
What does yield remember?
Where it paused — that saved position is the iterator's traversal state.
Which pattern mechanism does Template Method use, and which does Strategy use?
Template Method = inheritance; Strategy = delegation/composition.

See also: Design Patterns — Creational, Design Patterns — Structural, Publish-Subscribe & Event-Driven Architecture (Observer at scale), Undo-Redo Systems (Command in practice), and back to the parent Hinglish version.