Behavioral Patterns
Patterns for assigning responsibilities and organizing communication between objects.
Behavioral patterns deal with algorithms and with how responsibility is shared between objects: who decides, who reacts, and how variation is plugged in. The GoF catalogue contains eleven. All eleven are published, each with a runnable Java 21 example. Strategy vs State vs Template Method and Observer vs Mediator vs Event Bus compare the ones most often confused.
Articles
- Chain of ResponsibilityPass a request along a chain of handlers until one handles it, decoupling the sender from the receiver. A runnable Java 21 expense-approval chain plus a modern list-of-handlers pipeline, servlet filters and Spring Security as real chains, trade-offs and interview questions.
- CommandTurn a request into an object so you can queue it, log it, undo it or replay it. A runnable Java 21 text editor with undo, redo and macro commands, Command versus Strategy, lambdas as commands, and Command in executors, job queues and CQRS.
- InterpreterRepresent a small language's grammar as classes and evaluate sentences by walking the syntax tree. A runnable Java 21 feature-flag rule language with sealed records and a recursive-descent parser, when a DSL is worth it, and Interpreter in SpEL, regex and query languages.
- IteratorTraverse a collection without exposing how it is stored. A runnable Java 21 iterator that hides cursor-based API pagination behind Iterable and Stream, internal versus external iteration, fail-fast iterators and Spliterator, with trade-offs and interview questions.
- MediatorCentralise how a group of objects interact so they no longer refer to each other directly. A runnable Java 21 checkout form where widgets talk only to a mediator, many-to-many versus hub-and-spoke coupling, Mediator versus Observer and Facade, and the god-object risk.
- MementoCapture and restore an object's internal state without breaking encapsulation. A runnable Java 21 drawing canvas with checkpoints, narrow and wide memento interfaces using a private record, bounded history, immutability for cheap snapshots, and Memento versus Command for undo.
- ObserverNotify many dependent objects automatically when one object changes. A runnable Java 21 event publisher with subscribe/unsubscribe, failure isolation and CopyOnWriteArrayList, push versus pull, lapsed-listener leaks, Spring application events, reactive streams and the step up to messaging.
- StateLet an object change its behaviour when its internal state changes, by delegating to state objects instead of switch statements. A runnable Java 21 order lifecycle (awaiting payment, paid, shipped, delivered, cancelled), enum and sealed alternatives, State versus Strategy, and state machines in practice.
- StrategyDefine a family of interchangeable algorithms behind one interface and choose between them at runtime — with a runnable Java 21 discount-calculation example, UML, trade-offs and interview questions.
- Template MethodDefine the skeleton of an algorithm in a base class and let subclasses fill in specific steps. A runnable Java 21 data-import pipeline with abstract steps and hooks, the Hollywood Principle, Spring's JdbcTemplate and callbacks, and when to prefer Strategy and composition.
- VisitorAdd new operations to a stable class hierarchy without changing its classes. Double dispatch explained, a runnable Java 21 shape hierarchy with area and SVG-export visitors, and the modern alternative: sealed interfaces with exhaustive pattern-matching switch. The expression problem and trade-offs.