Behavioral Patterns Overview

The 10 Gang of Four behavioral patterns address how objects communicate, distribute responsibility, and encapsulate algorithms — the “who does what, and how do they talk” dimension of object-oriented design.

What Are Behavioral Patterns?

Behavioral patterns are concerned with the assignment of responsibilities between objects and with the algorithms that govern their interactions. They do not focus on how objects are created (creational) or assembled (structural), but on the dynamic, runtime protocols through which objects collaborate.

The patterns range from fine-grained object-level protocols (Iterator, Visitor) to high-level communication frameworks (Mediator, Observer). Most work by introducing a level of indirection — an extra object or interface — that decouples the initiator of a behavior from the implementor.

The Patterns at a Glance

PatternProblem SolvedKey Mechanism
Observer PatternOne object must keep arbitrary dependents in syncSubject maintains subscriber list; broadcasts notify()
Strategy PatternMultiple algorithm variants, avoid conditional proliferationEncapsulate each algorithm in a class; swap at runtime
Command PatternDecouple sender from receiver; enable undo/queueWrap request as an object with execute() / undo()
Chain of Responsibility PatternUnknown handler for a request; dynamic dispatchPass request down a linked handler chain
Template Method PatternShared algorithm skeleton with variable stepsDefine skeleton in base class; subclasses fill in steps
Iterator PatternTraverse collection without exposing structureSeparate traversal cursor into an iterator object
State PatternObject changes behavior as internal state changesDelegate to interchangeable state objects
Mediator PatternComplex many-to-many coupling between componentsCentral hub routes all inter-component communication
Memento PatternUndo/rollback without breaking encapsulationOriginator creates opaque state snapshot; caretaker stores it
Visitor PatternAdd operations to a stable hierarchy without modifying itDouble dispatch: elements accept() visitor; visitor visit() element

Groupings Within Behavioral Patterns

Communication / Coupling Reducers

These three directly address how objects talk to each other and how tightly coupled they are:

Algorithm Encapsulators

These patterns encapsulate an algorithm or part of one:

State and History

  • State Pattern — behavior is entirely governed by current state; transitions are first-class.
  • Memento Pattern — state history is captured for rollback.

Traversal and Structure

  • Iterator Pattern — uniform traversal of any collection.
  • Visitor Pattern — operation applied uniformly across a heterogeneous object structure; leverages traversal provided by Iterator or Composite.

Choosing Between Them

Observer vs Mediator Both decouple objects. Use Observer when the relationship is naturally a one-to-many broadcast (a data source, an event emitter). Use Mediator when interactions are many-to-many and the coupling web is complex — centralising in a mediator avoids a tangled graph of direct references.

Strategy vs Template Method Both vary an algorithm. Use Strategy when you need runtime algorithm swapping or want to avoid inheritance; use Template Method when the algorithm skeleton is fixed and only a few well-defined steps vary, and you are comfortable with inheritance.

Strategy vs State Both delegate to an interchangeable object. The key distinction is who drives the swap: with Strategy the client chooses and replaces the strategy from outside; with State the object transitions itself based on internal conditions. States typically know about each other (for transitions); strategies are usually independent.

Command vs Chain of Responsibility Both decouple sender from handler. Command wraps an action and targets a specific receiver. Chain dispatches along a sequence of potential handlers, and only one (or some) handle the request.

Visitor vs Iterator Often used together: Iterator provides traversal; Visitor provides the operation applied at each element. Use Visitor when you have many distinct operations to perform on a stable hierarchy; use Iterator when you simply need to walk a collection uniformly.

Memento vs Command (for undo) Use Command for undo when the operation is reversible by calling an inverse method (undo()). Use Memento when restoring previous state is necessary — especially when the operation is complex or the inverse is not cleanly computable.