CMD Guide
HomeOO & Low-Level DesignDesign Patterns Overview

Behavioral Patterns — Quick Reference, When-to-Use & Interview Traps

Behavioral Patterns — Quick Reference, When-to-Use & Interview Traps

Quick reference for behavioral patterns. The table below is a map, not a substitute for deep pages — especially for Observer lifecycle and Command undo.

Pattern NameDistinctive FeatureApplicabilityAn ExampleProsCons
IteratorProvides a way to access elements of a collection sequentially without exposing its underlying representationWhen you need to provide a standard way to traverse through a collection without exposing its implementationBrowsing through a playlistSimplifies client code and hides complexity of the collectionIterator state management can be complex
StrategyEnables an algorithm's behavior to be selected at runtimeWhen there are multiple ways to achieve a task and you want to determine the algorithm at runtimeDifferent compression algorithmsFlexibility to switch algorithms; isolation of algorithm implementationComplexity due to additional classes; clients must be aware of different strategies
Template MethodDefines the skeleton of an algorithm in an operation, deferring some steps to subclassesWhen the core structure of an algorithm should not change, yet some steps are open to being overriddenFrameworks defining application structurePromotes code reuse and enforces a predefined algorithm structureCan lead to less flexibility in subclasses; harder to maintain
ObserverNotifies multiple objects about state changesWhen changes in one object's state require other objects to be notifiedReal-time data feedsLoose coupling between subject and observersRisk of unintended performance issues
CommandEncapsulates a request as an objectFor operations that need to be executed, undone, or loggedUser actions in GUI applicationsDecouples command execution from usageCan lead to numerous concrete command classes
StateAllows objects to alter their behavior when their internal state changesFor objects whose behavior depends on their state and can change at runtimeDifferent modes in a gameLocalizes state-specific behaviorsIncreases the number of classes
Chain of ResponsibilityDelegates commands to a chain of processing objectsFor scenarios where multiple objects can handle a request, but the handler is unknownHelp systems in applicationsDecouples request sender and receiverHandling can become inefficient
MediatorCentralizes complex communications between related objectsTo reduce direct communications between multiple objectsChat applicationSimplifies interactions between groups of objectsMediator can become overly complex
MementoCaptures and externalizes an object's internal stateFor undo functionality or state save-and-restoreUndo feature in editorsPreserves encapsulation boundariesCan be memory-intensive
VisitorSeparates an algorithm from the objects on which it operatesWhen operations need to be performed on a group of similar kinds of objectsProcessing different types of document elements in a word processorSimplifies adding new operations; centralizes related operationsCan lead to a bloated visitor interface; harder to maintain when the object structure changes

Pattern taxonomy recap

FamilyWhat it solvesCanonical patterns
CreationalHow objects are instantiated and who decides the concrete typeSingleton, Factory Method, Abstract Factory, Builder, Prototype
StructuralHow classes/objects are composed into larger structuresAdapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy
BehavioralHow objects communicate and how responsibilities are distributedObserver, Strategy, Command, State, Template Method, Iterator, Mediator, Memento, Chain of Responsibility, Visitor

Behavioral patterns: when to use

PatternUse whenSkip when
ObserverOne object must notify many dependents; subscribe/unsubscribe is explicitForgotten unsubscribe leaks; notify while mutating the list (CME); process-boundary needs an event bus, not in-process Observer
StrategyMultiple interchangeable algorithmsA single algorithm or a simple lambda is enough
CommandRequests must be queued, logged, undone, or replayed (store inverse or memento)The request is simple and synchronous with no undo/history need
StateBehavior changes dramatically based on internal stateOnly one or two states exist; an enum is cleaner
Template MethodAn algorithm skeleton is fixed but steps varyEvery step varies or the superclass becomes fragile
IteratorHide traversal details of a collectionThe language already provides adequate iteration
MediatorMany objects communicate through a central coordinatorThe mediator becomes a bottleneck or hidden global state
Chain of ResponsibilityMore than one handler may process a requestOrder is ambiguous or every request needs every handler
VisitorAdd operations to a stable class hierarchy without modifying itThe hierarchy changes often; double dispatch is hard to follow

Observer: senior failure modes (must know)

Command and undo

Command's value is deferred execution, logging, queuing, and undo. A command that cannot invert itself either stores enough state for reverse application or pairs with a Memento of the receiver. "Encapsulate a method call" alone is not enough to justify the pattern.

Interview trap box

🤖 Don't fully get this? Learn it with Claude

Stuck on Behavioral Patterns — Quick Reference, When-to-Use & Interview Traps? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Behavioral Patterns — Quick Reference, When-to-Use & Interview Traps** (OO & Low-Level Design) and want to truly understand it. Explain Behavioral Patterns — Quick Reference, When-to-Use & Interview Traps from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Behavioral Patterns — Quick Reference, When-to-Use & Interview Traps** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Behavioral Patterns — Quick Reference, When-to-Use & Interview Traps** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Behavioral Patterns — Quick Reference, When-to-Use & Interview Traps** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes