Learning objectives
By the end of this appendix you can:
- Find the pattern, principle or idiom that answers a force in the code in front of you.
- Tell when no listed force is present, and the pattern would be over-engineering.
- Jump to the chapter that builds each one against a runnable example.
How to read this ledger
A force is a pressure in the design that the plain, direct solution handles badly. A pattern is a response to one. Each row names a pattern, principle or idiom, the force that justifies it, and the chapter that builds it. Read from the middle column: if none of its forces is present in your code, applying the pattern isn’t neutral, it’s the over-engineering described in Anti-patterns and pattern misuse. Match the mechanism to the force you have, not to the one a textbook example implies.
The ledger covers all 23 Gang of Four patterns, the five SOLID principles, and the Java idioms that work as patterns in their own right.
Principles (SOLID)
The principles aren’t patterns. They’re the vocabulary of forces and trade-offs that patterns are judged against.
| Principle |
Reach for it when |
Chapter |
| Single Responsibility |
A class has more than one reason to change, so one concern’s edit risks another |
SOLID I |
| Open/Closed |
New behavior should arrive as new code, not edits to working code |
SOLID I |
| Liskov Substitution |
A subtype must be usable anywhere its base is, without surprising a caller |
SOLID I |
| Interface Segregation |
A client is forced to depend on methods it never calls |
SOLID II |
| Dependency Inversion |
High-level policy should not depend on low-level detail; both depend on an abstraction |
SOLID II |
Foundational Java idioms
The rest of the catalog builds on these.
| Idiom |
Reach for it when |
Chapter |
Try-with-resources and AutoCloseable |
A resource must be released exactly once, on every path out, including an exception |
Resource management |
| Execute-Around Method |
A resource’s open/use/close sequence belongs to a library, not to every call site that uses it |
Resource management |
| Interface with a package-private implementation reached through a static factory |
A caller must depend on what a type does, not on the concrete class behind it |
Encapsulation and API design |
| Record with a validating compact constructor |
A value type’s invariants must hold on every instance, with no setter left to break them later |
Encapsulation and API design |
Creational patterns
Structural patterns
| Pattern |
Reach for it when |
Chapter |
| Adapter |
An existing type has the right behavior behind the wrong interface |
Adapter, Facade, Bridge |
| Facade |
A subsystem’s many moving parts should present one small, intentional surface |
Adapter, Facade, Bridge |
| Bridge |
An abstraction and its implementation must vary independently |
Adapter, Facade, Bridge |
| Decorator |
Behavior should be added to one object at runtime without subclassing every combination |
Decorator and Proxy |
| Proxy |
Access to an object needs a stand-in for lazy creation, caching, or control |
Decorator and Proxy |
| Composite |
Clients should treat a single object and a tree of objects the same way |
Composite, Flyweight, Null Object |
| Flyweight |
Many objects share most of their state and the duplication is too expensive to keep |
Composite, Flyweight, Null Object |
| Null Object |
A do-nothing case should remove a null check from every call site |
Composite, Flyweight, Null Object |
Behavioral patterns
| Pattern |
Reach for it when |
Chapter |
| Strategy |
An algorithm chosen by a caller must be swappable without touching the caller |
Strategy |
| Template Method |
A fixed sequence has steps that vary, and the sequence itself must not |
Template Method |
| State |
Behavior depends on a current status, and illegal transitions must be refused |
State and finite state machines |
| Command |
A request must become an object so it can be queued, logged, or undone |
Command, undo, and command queues |
| Observer |
Subscribers must react to a change without the subject knowing who they are |
Observer and Mediator |
| Mediator |
Many objects’ direct references to each other have become a tangle |
Observer and Mediator |
| Chain of Responsibility |
A request should pass along a line of handlers until one takes it |
Chain of Responsibility and Iterator |
| Iterator |
A collection’s elements must be traversed without exposing its structure |
Chain of Responsibility and Iterator |
| Visitor |
A new operation must be added across a fixed set of types without editing each |
Visitor, Memento, and Interpreter |
| Memento |
An object’s state must be captured and restored without exposing its internals |
Visitor, Memento, and Interpreter |
| Interpreter |
A small, recurring grammar is best expressed as types that evaluate themselves |
Visitor, Memento, and Interpreter |
Idiomatic modern Java
These are specific to modern Java rather than to object-oriented design in general. Several retire a hand-written GoF implementation.
| Idiom |
Reach for it when |
Chapter |
Sealed interface with exhaustive pattern-matching switch |
A closed, known set of variants should be dispatched with the compiler refusing an unhandled case, with no visitor interface and no accept() method |
Records, sealed interfaces, and pattern matching |
| Enum singleton |
A single instance must be serialization-safe and reflection-proof with no extra code |
Singleton and the DI alternative |
| Initialization-on-demand holder idiom |
Lazy, thread-safe construction is needed with no synchronized block on the hot path |
Singleton and the DI alternative |
| Functional interface satisfied by a lambda or method reference |
A one-method contract does not need a named implementing class |
Enums, functional interfaces, streams, and Optional |
| Stream pipeline |
A collection transformation reads better as filter/map/collect than as a hand-written accumulator loop |
Enums, functional interfaces, streams, and Optional |
Optional as an explicit answer |
A method does not always have an answer, and a caller must be forced to handle the absence |
Enums, functional interfaces, streams, and Optional |
| Virtual threads |
A workload blocks on I/O far more than it computes, and a platform thread per task would not scale |
Virtual threads |
EnumMap or Map dispatch table |
A key selects one short handler and a full class hierarchy would be overkill |
Shortcut patterns: dispatch tables and maps |
Patterns in the wild
Changing inherited code
For deeper coverage
- Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley): the catalog this ledger indexes, where each pattern’s intent, structure, and consequences are set out in full.
- Robert C. Martin, Agile Software Development: Principles, Patterns, and Practices (Prentice Hall): the SOLID principles the ledger’s principle rows condense.
- Joshua Bloch, Effective Java (Addison-Wesley): the source for the static-factory, enum-singleton, and holder-idiom techniques the foundational and idiomatic-Java rows apply.