Skip to content

Metadata Card

  • Prerequisites: Chapter 7 (Design Principles); Chapters 8-9 (Creational & Structural Patterns)
  • Estimated Time: 60 minutes
  • Core Difficulty: Advanced
  • Completion Milestone: Recognize 11 behavioral pattern use cases and apply them correctly

Your Progress

You can forge and assemble parts. But when the machine runs, parts need to coordinate — which gear moves first, which spring releases when, which lever triggers under what conditions.

You opened an old machine with 5 nested levers and 3 interlinked triggers. You stare at it, afraid to touch: "Who designed this?" You open the workshop manual — you drew it three years ago. Your Task

Behavioral patterns focus on communication between objects. 11 patterns each for a common coordination scenario: Chain of Responsibility chains handlers, Command encapsulates requests, Observer lets objects listen to others, Strategy makes algorithms interchangeable, State changes behavior with state, Template Method defines algorithm skeletons, Iterator unifies collection access, Mediator removes direct communication, Memento saves and restores state, Visitor adds operations without modifying classes, Interpreter parses grammars.

Chapter tiers

  • Required reading: Observer, Strategy, Template Method, Chain of Responsibility
  • Selective: Command, State, Iterator
  • Advanced: Mediator, Memento, Visitor, Interpreter

First Layer: Observer — One-to-Many Notification

Subject notifies all observers when its state changes.

java
public interface MatchObserver { void onMatchFinished(MatchFinishedEvent event); }

public class MatchManager {
    private final List<MatchObserver> observers = new ArrayList<>();
    public void addObserver(MatchObserver o) { observers.add(o); }
    public void finishMatch(Match match, String winner) {
        MatchFinishedEvent event = new MatchFinishedEvent(match.getId(), winner, match.getScore());
        for (MatchObserver observer : observers) { observer.onMatchFinished(event); }
    }
}

Second Layer: Strategy — Interchangeable Algorithm Family

Define a family of algorithms, encapsulate each one, and make them interchangeable.

Third Layer: Template Method — Algorithm Skeleton

Define the skeleton of an algorithm in a method, deferring some steps to subclasses.

Fourth Layer: Chain of Responsibility

Pass a request along a chain of handlers until one handles it.

Fifth through Eleventh Layers: Command (encapsulate request as object), State (state-dependent behavior), Iterator (sequential access without exposing representation), Mediator (centralized coordination), Memento (capture and restore state), Visitor (add operations without modifying classes), Interpreter (grammar evaluation).


Common Pitfalls

Observer event leaks (forget to unregister), Strategy over-segmentation (use lambdas where trivial), Chain too long, State class explosion, Visitor breaks encapsulation.


Passing Challenges: Implement Observer for match notifications, refactor if-else state to State pattern, implement Command for undo functionality.


Traveler's Notes

Behavioral patterns answer "how objects talk": Observer decouples event from response, Strategy makes algorithms swappable, Template Method fixes the skeleton, Chain serializes handlers, Command encapsulates operations, State isolates state behavior. End goal: clearer collaboration, minimal ripple effects when something changes.


Next: Layered Architecture & MVC (Chapter 11).

Built with VitePress | Software Systems Atlas