Refactoring to Patterns

Kerievsky's composite refactorings whose destination is a design pattern.

Source: Joshua Kerievsky, Refactoring to Patterns (Addison-Wesley, 2004). Code examples on this sub-site are illustrative adaptations written for the site, not direct quotations from the book.

A single canonical constructor owns all field assignment and validation.

The top of the method reads as a numbered outline of intention-revealing steps at one level of detail; each step's how-it-works lives in a named helper.

A single factory (a method on the shared superclass or a dedicated factory class) owns the knowledge of which subclasses exist.

A fluent builder whose API mirrors the tree's shape.

Each variant lives in its own Adapter class wrapping the external API.

A Composite superclass owns the collection and its management methods; each sibling extends the Composite and declares only its distinctive concerns.

One parameterized method replaces the N variants.

The algorithm lives once in a superclass Template Method that calls named primitive operations.

The class is a regular collaborator that callers receive through their constructors (or another explicit hand-off).

A Null Object implementing the same interface stands in for the missing collaborator.

The base class declares an abstract Factory Method and uses the result polymorphically.

Construction is funnelled through a single static accessor that returns the same instance every time.

One mutable accumulator (the collecting parameter) is threaded through the traversal.

Operations live in Visitor classes; the structure exposes only `accept(visitor)`.

A Factory class owns the assembly recipe.

Each optional behaviour is a Decorator class that wraps the core component.

Each handler is a Command object with an `execute(payload)` method.

Each variant lives in its own Strategy class implementing a shared interface.

One canonical constructor accepts the full, unambiguous parameter set; named static creation methods (`Money.

The publisher emits events; listeners subscribe at the composition root and receive notifications without the publisher knowing they exist.

Each grammatical element is a domain object: `Equals`, `GreaterThan`, `And`, `Or`.

Each node is an object with explicit children.

Single and many implement the same interface (Composite).

Each state is a class implementing the operation surface; the host delegates and transitions are state-to-state assignments.

The type code becomes a value object class with named instances.

An Adapter wraps the off-shape class and exposes the canonical interface.

All variants share one method-name vocabulary for the same operations.