Skip to main content

Pattern Categories and When to Apply Them

What This Concept Is

The GoF catalog organizes patterns along two axes:

  • Purpose: Creational / Structural / Behavioral.
  • Scope: Class-level (relationships set at compile time via inheritance) vs Object-level (relationships set at runtime via composition).

This gives a 2D grid that helps you locate the pattern category before you pick a specific pattern.

A useful working definition:

  • Creational -- controls how and when objects get made (Factory Method, Abstract Factory, Builder, Prototype, Singleton).
  • Structural -- controls how objects are wired into larger structures (Adapter, Facade, Bridge, Composite, Decorator, Proxy, Flyweight).
  • Behavioral -- controls how responsibility and control flow are distributed (Strategy, Observer, Command, Iterator, State, Template Method -- studied in Module 3).

Why It Matters Here

Beginners memorize names; practitioners memorize forces. The selection skill is not "which pattern is this?" but "what force is pressing on this code, and which category responds to that force?"

When you know the category, a small set of patterns is in scope, and the right one usually names itself. When you do not, you end up force-fitting the last pattern you read about (cargo-culting -- covered next).

Concrete Example

Three brief scenarios, each tagged with the category that applies:

  1. Two payment providers need to be interchangeable in our checkout. -- Behavioral/Creational. Strategy plus a Factory Method that selects by config.
  2. Our ORM layer is five libraries deep and every caller wires all five. -- Structural. A Facade over the subsystem.
  3. We need a test-only version of the image service that records calls and returns fake data. -- Structural + Creational. A test double injected at the composition root; no new pattern required.

The category narrows the field to two or three candidates. If more patterns than that seem to apply, the design itself is probably under-specified.

Common Confusion / Misconception

  • Patterns are not enforced by the category grid. Real systems combine them (Composite + Iterator + Visitor on the same tree; Abstract Factory + Singleton; Decorator + Proxy).
  • A pattern does not need a class diagram to count. A closure that accepts another function is a Strategy, whatever its shape.
  • Class-scope patterns (Template Method, class Adapter) are not "worse than" object-scope patterns. They are more rigid. In some languages they are unavoidable; in others they are discouraged.
  • Seeing two patterns that both seem to fit usually means the problem is unclear, not that the catalog is broken.

How To Use It

Selection checklist, in order:

  1. Name the force. In one sentence: what change pressure is hurting this code? Examples: "we need to add a third payment provider", "this construction logic is duplicated", "two libraries have incompatible APIs".
  2. Map the force to a category:
    • something varies at construction time -> Creational
    • something needs to be wired differently across parts -> Structural
    • something varies at runtime control flow -> Behavioral
  3. Shortlist two patterns from that category and write the smallest possible code to compare them.
  4. If neither pattern clearly wins, the problem is not yet sharp. Write another example before picking.
  5. Choose the simpler of the two; the more elaborate pattern is a future option, not a default.

Check Yourself

  1. Which category handles "this subsystem has too many public classes"?
  2. Why does picking the category before the pattern matter?
  3. What does it mean when more than one category seems to fit?

Mini Drill or Application

For each of the following scenarios, write one sentence identifying the category and one candidate pattern:

  1. A logger needs to sometimes add timestamps, sometimes add request IDs, and sometimes do neither.
  2. An IDE plugin system needs to load plugins at startup and treat them uniformly at runtime.
  3. A legacy payroll API returns XML, but the new service needs JSON-shaped objects.
  4. We need exactly one database connection pool for the whole process.
  5. A document contains paragraphs, images, and grouped sub-documents; operations must work on all three.

Read This Only If Stuck