skipnothing/Design Patterns
UNIT 01

Behavioral Patterns

Observer, Strategy, State, patterns for runtime behavior

These patterns solve the most immediately recognizable problems, how objects communicate, swap behavior, and manage state at runtime.

Loading…
TOPICS
#1

Observer

Spot the polling-loop smell, implement subscribe/notify/unsubscribe on a class, and trace the lapsed listener leak that makes forgotten detach calls eat memory silently.

setInterval(check, 100) lives in every widget on a dashboard, each one asking the counter if it changed and mostly hearing 'no'. Ten widgets, one slow source, thousands of wakeups a minute for a number that ticks once.

14 min
#2

Strategy

Recognize the 'growing switch on type' smell, extract each branch into a swappable algorithm object or lambda, and spot the same shape inside Array.sort, Python sorted(key=…), and Java Comparator.

A process(type, amount) method has gained a fresh case every release for six months. Card, PayPal, crypto, gift card, store credit, BNPL, each new payment type adds a branch and pushes the file past 200 lines.

12 min
#3

State

Rotate the multi-method switch(mode) matrix into one class per mode, push transition decisions onto the modes themselves, and trace the silent-failure shape where the host re-grows an instanceof ladder before delegating.

An audio player's play() method starts with switch (this.mode), and so does pause(), and so does lock(). Four modes today, one switch per method, three methods, a fifth mode lands and you're touching every method to add the same branch.

14 min
#4

Command

Freeze each button-handler call into an object holding its own execute and undo, watch the same list of objects become an undo stack or a job queue when you swap the consumer, and spot the shape in Java Runnable, Swing AbstractAction, Redux actions, and Celery or BullMQ job queues.

An editor's bold button calls doc.applyFormat(...) directly. Add undo, and every method on doc grows a twin, every handler caches the old value, the document class doubles in size, and redo is still ahead.

14 min
#5

Chain of Responsibility

Pull each if from checkAccess into a one-method class holding one next reference, watch a request walk the chain choosing handle/pass/mutate-pass at every box, and spot the same shape in Express middleware, ASP.NET pipelines, Servlet filters, Python try/except, and DOM event bubbling.

Every new role drops a line into checkAccess. The function has grown past 40 lines of if (user.role === '...'), and every team that adds a role edits the same file.

14 min
#6

Mediator

Collapse N(N-1) peer imports into one central coordinator that holds the rules, watch the same shape inside MVC controllers, MediatR's _sender.Send, and useReducer's dispatch, and recognize when the hub drifts toward a god object.

An email input calls submit.disable() whenever the value changes, add a password field and email grows an if; add remember-me and every older handler grows another. Four fields, twelve edges of coupling, and every new one re-edits the rest.

12 min
UNLOCKS
All units