Tagged “Behavioral”
14 articles with this tag.
- GoF OverviewA map of the 23 Gang of Four design patterns: the three categories, class versus object scope, one-line intents, JDK examples, and how to choose between patterns that look alike.
- 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.
- Observer vs Mediator vs Event BusThree ways for objects to communicate without direct calls: Observer broadcasts from a subject to its listeners, Mediator centralises coordination logic, and an event bus decouples publishers and subscribers completely. Coupling, control flow, failure handling and how each scales to messaging and sagas.
- 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.
- Strategy vs State vs Template MethodThree patterns that all vary behaviour: Strategy swaps a whole algorithm by composition, State swaps behaviour as an object's lifecycle changes, and Template Method varies steps by inheritance. Side-by-side code, the deciding questions and how to choose.
- 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.