skip to content

Behavioral Patterns Overview

One place to compare the behavioral patterns by intent: notification, interchangeable algorithms, algorithm skeletons, traversal, request-as-object, state-driven behavior, request routing, double dispatch, centralized coordination and state capture. Read it to answer which pattern fits before diving into any one leaf.

part ofSoftware design & architectureoverview, primer and where to startread it →
on this pageshow

questions

5

In the Gang of Four design pattern classification, what problem space do behavioral patterns address, and how do they differ from creational and structural patterns?

level: juniorimportance: must knowfreq 72%

answer

  1. Creational = how made; structural = how composed; behavioral = how they collaborate
  2. Behavioral ≈ replacing conditionals with polymorphic delegation
  3. Categories describe intent, not code shape
  4. Decorator vs Chain: all contribute vs one consumes
  5. First-class functions collapse the boilerplate, not the pattern

basics

~10 s

Behavioral patterns describe how objects talk to each other and who is responsible for what. Creational patterns are about how objects get made; structural patterns are about how objects are composed into bigger shapes.

solid answer

~50 s

The Gang of Four book splits its 23 patterns into three families by the kind of problem they solve. Creational patterns (Factory Method, Builder, Singleton, Prototype, Abstract Factory) control object instantiation so callers do not hardcode concrete types. Structural patterns (Adapter, Decorator, Composite, Facade, Proxy, Bridge, Flyweight) compose objects and classes into larger structures, mostly a static shape question. Behavioral patterns (Observer, Strategy, Template Method, Iterator, Command, State, Chain of Responsibility, Visitor, Mediator, Memento, Interpreter) are about runtime collaboration: which object holds which responsibility, how control flows between them, and how they communicate without becoming tightly coupled. Practically, behavioral patterns almost always replace conditional logic or hardwired calls with polymorphic delegation, so behaviour can vary or be extended without editing existing callers. The boundaries are not rigid — Decorator (structural) and Chain of Responsibility (behavioral) look nearly identical in code and differ mainly in intent.

go deeper

for a junior

Name the three GoF categories and give one correct example each; say behavioral is about how objects interact and who does what.

for a middle

List several behavioral patterns with their one-line intent, and explain the shared mechanism of replacing conditionals with polymorphism.

for a senior

Contrast pairs that share code shape but differ in intent (Strategy/State, Decorator/Chain, Strategy/Bridge) and discuss the indirection cost.

for a principal

Frame patterns as a shared vocabulary rather than a mandate; discuss when the taxonomy stops being useful, how first-class functions change the economics, and the risk of pattern-driven over-abstraction in a codebase.

## The classification A **design pattern** is a named, reusable solution shape for a recurring design problem — not code you copy, but a structure of roles and responsibilities you re-implement in your own context. The 1994 *Design Patterns* book by Gamma, Helm, Johnson and Vlissides (the "Gang of Four", GoF) catalogued 23 patterns and grouped them along two axes: **purpose** (creational / structural / behavioral) and **scope** (class-level, via inheritance fixed at compile time, vs object-level, via composition assembled at runtime). ### Creational — "how do objects come into existence?" They decouple a caller from the concrete class it ends up using. - **Factory Method** — a subclass decides which concrete product to instantiate. - **Abstract Factory** — one object creates a whole family of related products. - **Builder** — assembles a complex object step by step. - **Prototype** — creates new objects by cloning an existing one. - **Singleton** — guarantees exactly one instance. ### Structural — "how are objects put together?" They describe composition: which object holds a reference to which, and what interface the composite presents. - **Adapter** — makes an incompatible interface usable. - **Decorator** — wraps an object to add behaviour transparently. - **Composite** — treats a tree of objects like a single object. - **Facade** — one simplified entry point over a subsystem. - **Proxy** — a stand-in controlling access (lazy, remote, protective). - **Bridge** — separates an abstraction from its implementation so both vary. - **Flyweight** — shares immutable state across many instances. ### Behavioral — "how do objects collaborate, and who is responsible?" This is the family in question. Behavioral patterns concern **control flow and responsibility assignment at runtime**: - **Observer** — one-to-many notification when state changes. - **Strategy** — interchangeable algorithms chosen by the client. - **Template Method** — a fixed algorithm skeleton with pluggable steps. - **Iterator** — sequential access to a collection without exposing its internals. - **Command** — a request packaged as an object (queue, log, undo). - **State** — behaviour changes when internal state changes; looks like class change. - **Chain of Responsibility** — pass a request along handlers until one handles it. - **Visitor** — add new operations over a fixed object structure via double dispatch. - **Mediator** — centralize many-to-many interaction in one coordinator. - **Memento** — capture and restore an object's internal state without breaking encapsulation. - **Interpreter** — represent a grammar and evaluate sentences in it. ## The common mechanism Nearly every behavioral pattern is a way of turning a **conditional or hardwired call into a polymorphic delegation**. A `switch` over payment types becomes Strategy. A `switch` over lifecycle status becomes State. A hardwired call to three downstream systems becomes Observer. An `if/else if` cascade of eligibility rules becomes Chain of Responsibility. The payoff is the Open/Closed Principle: adding a new case means adding a class, not editing an existing one. The cost is indirection — more small types, control flow you cannot read top-to-bottom, and a harder time answering "what actually runs?" from the source alone. ## Where the boundaries blur The categories are about **intent**, not code shape. Decorator (structural) and Chain of Responsibility (behavioral) both build a linked list of wrappers; Decorator's intent is that *every* wrapper contributes, the chain's intent is that *one* handler consumes. Strategy (behavioral) and Bridge (structural) both delegate to a swappable implementation object; Strategy varies an *algorithm* per call, Bridge varies an *implementation dimension* over the object's whole lifetime. State and Strategy are structurally identical — the difference is who chooses the delegate (client for Strategy, the object itself or its states for State). A strong answer acknowledges this rather than pretending the taxonomy is crisp. ## Edge cases and caveats - The GoF catalogue is not exhaustive or authoritative for modern systems; Null Object, Publish/Subscribe with a broker, and Dependency Injection are widely used and not in it. - In languages with first-class functions, several behavioral patterns collapse to a function value: Strategy is a lambda, Command is a closure, Observer is a list of callbacks, Template Method is a higher-order function. The *pattern* still exists — only its boilerplate disappears. - Naming a pattern is a communication tool. Its main value in an interview is that you can say "this is Strategy" and the other person immediately knows the roles, the extension point, and the trade-off.

  • Give an example where the same code shape belongs to two different categories depending on intent.
    A linked list of wrapper objects. If every wrapper adds behaviour and delegates onward, it is Decorator (structural). If each element decides whether to handle the request and stop, it is Chain of Responsibility (behavioral). Identical structure, different intent.
  • Do behavioral patterns disappear in functional languages?
    The boilerplate does, not the pattern. Strategy becomes a function parameter, Command a closure, Observer a list of callbacks. The design decision — where the extension point lives and who chooses the behaviour — is unchanged, and naming it still communicates intent.

Think of a theatre production. Creational patterns are casting — how you get actors. Structural patterns are the set design — how the pieces fit together on stage. Behavioral patterns are the script and blocking — who speaks when, who reacts to whom, and who is responsible for each beat.

saying these in an interview costs you the question

  • Reciting all 23 pattern names without being able to state what problem the behavioral family solves.
  • Claiming the three categories are mutually exclusive and always obvious from the code.
  • Saying behavioral patterns are about object creation or object composition.
  • Treating the GoF catalogue as a complete or mandatory list for modern systems.

context

open as a page

How do the Strategy and Template Method design patterns differ, and what determines which one you should reach for?

level: middleimportance: must knowfreq 68%

basics

~20 s

Both let part of an algorithm vary. Template Method uses inheritance: a base class fixes the steps and subclasses override some of them. Strategy uses composition: the varying algorithm is a separate object you plug in, swappable at runtime.

open as a page

Explain how the State, Strategy, and Command patterns differ, even though all three delegate behaviour to a separate object.

level: middleimportance: should knowfreq 58%

basics

~20 s

Strategy plugs in one interchangeable algorithm chosen by the caller. State makes an object behave differently as its internal state changes, with states deciding the transitions. Command wraps a request — action plus its arguments — into an object you can queue, log, or undo.

open as a page

Observer, Mediator, and Chain of Responsibility all decouple a sender from a receiver. How does each one route a message differently, and when would you choose one over the others?

level: seniorimportance: should knowfreq 54%

basics

~20 s

Observer broadcasts a change to every registered listener. Mediator puts one coordinator in the middle so components talk to it instead of each other. Chain of Responsibility passes a request down a line of handlers until one handles it.

open as a page

Why does the Visitor pattern need double dispatch, and what trade-off does it make that Iterator does not?

level: principalimportance: nice to knowfreq 31%

basics

~20 s

Visitor lets you add new operations over a fixed set of node types without editing them. It needs two dispatches: the node picks accept(visitor), then calls visitor.visitX(this), so the right method is chosen by both the node type and the visitor type. The trade-off: adding an operation is easy, adding a node type breaks every visitor.

open as a page