E

Pattern-to-force quick reference

Every GoF pattern, SOLID principle, and Java idiom mapped to the force that justifies it, each linked to the chapter that builds it. Scan it to find the tool for the force in front of you.

Appendix E

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

Pattern Reach for it when Chapter
Factory Method Which concrete type to build is a decision resolved at runtime, not at the call site Factory Method and Abstract Factory
Abstract Factory A whole family of related objects must be built in a consistent variant together Factory Method and Abstract Factory
Builder An object has many optional parts and construction should read as a sequence of choices Builder and Prototype
Prototype A new object is best made by copying a configured exemplar rather than rebuilding it Builder and Prototype
Singleton Exactly one instance must exist, it is effectively immutable, and nothing needs to substitute another Singleton and the DI alternative

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

Pattern Reach for it when Chapter
Resource pool An expensive-to-create resource must be reused under a bound Resource and concurrency patterns
Monitor object An object’s methods must be serialized against concurrent access Resource and concurrency patterns
Active object A method call must be decoupled from the thread that runs it Resource and concurrency patterns
CDI managed bean with constructor injection A single, container-managed instance must receive its collaborators rather than construct them Patterns in a real framework: Quarkus and CDI
CDI producer method A construction decision belongs to the container’s composition root, not to the class that needs the result Patterns in a real framework: Quarkus and CDI
CDI interceptor binding Behavior such as logging must wrap a method call without editing the method Patterns in a real framework: Quarkus and CDI
CDI event One bean’s change must reach other beans that it has no reference to Patterns in a real framework: Quarkus and CDI
Typed configuration (@ConfigMapping) A caller-swappable algorithm should be selected from external configuration, not from a conditional in code Patterns in a real framework: Quarkus and CDI
Recognizing an anti-pattern A pattern is present with no force that calls for it Anti-patterns and pattern misuse

Changing inherited code

Technique Reach for it when Chapter
Characterization test Inherited code must be pinned before it is safe to change Seams and the test safety net
Seam (object, extract-interface, subclass-and-override) Behavior must be altered without editing the logic in place Seams and the test safety net
Dependency-breaking techniques A hard-wired dependency blocks a class from being tested Dependency-breaking techniques for Java
Fowler-then-Feathers workflow Inherited code needs both a safety net and a design cleanup Characterization tests; Fowler vs Feathers
Strangler fig A legacy component must be replaced incrementally with no big-bang cutover Refactor, rewrite, or strangler fig

For deeper coverage