E

Pattern-to-force quick reference

A completeness ledger: every GoF pattern, SOLID principle, and C++ idiom mapped to the force that justifies reaching for it, each linked to where it is built. Scan it to find the tool for a force in front of you, or to confirm the catalogue is complete.

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.
  • Confirm that a pattern you’re about to apply has a force behind it.
  • Jump to where each one is built against a runnable example.

Why this matters

A pattern answers a force: a pressure in the design that the plain, direct solution handles badly. The chapters go from force to pattern; this ledger runs the other way, from the force back to the tool.

Each row names a pattern, principle or idiom, the force that justifies it, and where it’s built. The middle column is the check. If none of its forces is present, the pattern is the over-engineering Anti-patterns and pattern misuse describes. Match the mechanism to the force you have, not the one a textbook example implies.

The ledger covers the twenty-three Gang of Four patterns, the five SOLID principles, and the C++ idioms treated as patterns in their own right.

Principles (SOLID)

The principles aren’t patterns. They’re the vocabulary the 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 C++ idioms

Every other entry builds on these two.

Idiom Reach for it when Chapter
RAII and deterministic lifetimes A resource must be released exactly once, on every path out, including exceptions RAII
Value semantics and ownership Who owns an object and who merely observes it must be clear in the type, not the comments Value semantics

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
PImpl A class should hide its implementation to cut compile-time coupling across a header 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 (and NVI) A fixed sequence has steps that vary, and the sequence itself must not Template Method and NVI
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 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, Interpreter
Memento An object’s state must be captured and restored without exposing its internals Visitor, Memento, Interpreter
Interpreter A small, recurring grammar is best expressed as types that evaluate themselves Visitor, Memento, Interpreter

Idiomatic C++ expressions

Patterns specific to C++ rather than general object-oriented design.

Idiom Reach for it when Chapter
CRTP and policy-based design Polymorphism is needed with no runtime cost and the type is known at compile time CRTP and policy-based design
Type erasure Unrelated types must share one value-semantic interface with no common base class Type erasure and PImpl
Expression templates A chain of operations should evaluate in one pass with no intermediate objects Expression templates and generic idioms
Self-registering registry Implementations should register themselves with no central list to edit Expression templates and generic idioms
Templated serialization One field description should drive both reading and writing a type Expression templates and generic idioms

Patterns in the wild

Pattern Reach for it when Chapter
Resource pool An expensive-to-create resource must be reused under a bound Resource pools
Monitor object An object’s methods must be serialized against concurrent access Resource pools
Active object A method call must be decoupled from the thread that runs it Resource pools
Dispatch table or map A key selects one short handler and a full class hierarchy would be overkill Shortcut patterns: dispatch tables and maps
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, link, preprocessor) 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 The 24 dependency-breaking techniques
Fowler-then-Feathers workflow Inherited code needs both a safety net and a design cleanup Fowler and Feathers
Strangler fig A legacy component must be replaced incrementally with no big-bang cutover Refactor or rewrite

For deeper coverage