GoF Patterns MOC

GoF Patterns MOC

Navigation note for all 23 Gang of Four design patterns across creational, structural, and behavioral categories.

Gang of Four (Gamma, Helm, Johnson, Vlissides, 1994) codified 23 patterns across three categories. Use this MOC to navigate to any pattern note. See Design-Patterns-MOC (created Phase 16) for the root vault entry point with cross-layer lineage chains.


Creational Patterns

Object construction — how objects are created and composed. Creational patterns decouple client code from the concrete classes it instantiates.

PatternIntentUse When
Factory-Method-PatternDefer instantiation to subclasses via a hook methodClient must not know the concrete class; subclass decides the product
Abstract-Factory-PatternCreate families of related objects through a factory interfaceSystem needs product family consistency; swap families atomically
Builder-PatternAssemble a complex object step-by-step4+ parameters (especially optional ones); same steps, different representations
Prototype-PatternClone an existing instance as the basis for new objectsConstruction is expensive; copy is cheaper; runtime-configured types
Singleton-PatternOne instance, global access (prefer DI-managed scope)Shared resource requiring identity equality — use DI, avoid getInstance()

Structural Patterns

Component shapes and composition — how classes and objects are combined into larger structures.

PatternIntentUse When
Adapter-PatternConvert an interface into one the client expectsIntegrating incompatible third-party or legacy interface
Bridge-PatternSeparate abstraction from implementationBoth hierarchies need independent variation
Composite-PatternCompose objects into tree structuresClient must treat leaf and composite uniformly
Decorator-PatternAttach responsibilities dynamicallyCombinatorial subclassing would otherwise be required
Facade-PatternProvide a simplified subsystem interfaceSubsystem is complex; most clients need a simple path
Flyweight-PatternShare fine-grained objects to reduce memoryThousands of near-identical objects consume too much memory
Proxy-PatternSurrogate to control access to an objectLazy init, access control, remote stub, or caching needed

See also: Decorator-vs-Proxy-vs-Adapter — resolves the most common structural confusion.


Behavioral Patterns

Object interaction and responsibility — how objects communicate and distribute responsibility.

PatternIntentUse When
Chain-of-Responsibility-PatternPass requests along a handler chain until one handles itRequest sender should not know which handler processes it; handler set changes at runtime
Command-PatternEncapsulate a request as an objectUndo/redo, queuing, logging, or parameterizing operations needed
Interpreter-PatternDefine a grammar and interpreter for a languageSimple (<=5 rule), stable grammar; use parser generators for anything larger
Iterator-PatternSequential access to elements without exposing representationTraversal over complex structure without coupling to its internals
Mediator-PatternCentralise complex interactions between peer objectsMany objects interact; reduce mesh coupling to hub-and-spoke
Memento-PatternCapture and restore object state without breaking encapsulationUndo/redo or rollback required; state must not be exposed externally
Observer-PatternNotify dependents automatically when subject state changesOne-to-many event notification; sender should not know its listeners
State-PatternAlter object behaviour when internal state changesObject has distinct states with different behaviours; complex conditional state logic
Strategy-PatternDefine interchangeable algorithm familyAlgorithm choice varies at runtime; eliminate conditional algorithm selection
Template-Method-PatternDefine algorithm skeleton, defer steps to subclassesMultiple classes share same algorithm structure with varying steps
Visitor-PatternSeparate operations from element structureStable element hierarchy, frequently adding new operations

Pattern Selection Guide

Use this guide when two patterns seem interchangeable. Pick the one that matches your primary concern.

Chain of Responsibility vs Strategy

  • Chain of Responsibility: You have multiple potential handlers and want to find the right one dynamically. Request may pass through several handlers or none.
  • Strategy: You have one algorithm slot and want to swap which algorithm fills it. Only one strategy runs.

State vs Strategy

  • State: The object's behaviour changes because its own internal state changed. States control their own transitions (context.setState(...)).
  • Strategy: The algorithm is swapped externally by the client. The strategy never changes the context's strategy itself.
  • See State-Pattern for the detailed comparison callout.

Mediator vs Observer

  • Mediator: All colleagues communicate through a central hub. No colleague knows other colleagues directly.
  • Observer: One Subject broadcasts to many Observers. Observers know the Subject (at registration time); Subject does not know Observer types.
  • See Mediator-Pattern for the detailed comparison callout.

Command vs Strategy

  • Command: Encapsulates a specific action on a specific receiver, with state (the receiver, parameters). Supports undo/redo, queuing, logging.
  • Strategy: Encapsulates a reusable algorithm with no receiver binding. Stateless (or nearly so). Cannot be undone — no state to restore.

Template Method vs Strategy

  • Template Method: Uses inheritance. The abstract class controls the algorithm; subclasses fill in steps. Variation at compile time.
  • Strategy: Uses composition. The context delegates the algorithm to an interface. Variation at runtime.
  • Prefer Strategy when you need runtime swapping or want to avoid subclass proliferation.

Intent Summary — All 23 Patterns

Quick-reference table for recognition and recall.

#CategoryPatternIntent
1CreationalFactory-Method-PatternDefer instantiation to subclasses via a hook method
2CreationalAbstract-Factory-PatternCreate families of related objects through a factory interface
3CreationalBuilder-PatternAssemble a complex object step-by-step
4CreationalPrototype-PatternClone an existing instance as the basis for new objects
5CreationalSingleton-PatternOne instance, global access (prefer DI-managed scope)
6StructuralAdapter-PatternConvert an interface into one the client expects
7StructuralBridge-PatternSeparate abstraction from implementation hierarchies
8StructuralComposite-PatternCompose objects into tree structures; treat leaf and composite uniformly
9StructuralDecorator-PatternAttach responsibilities dynamically without subclassing
10StructuralFacade-PatternProvide a simplified interface to a complex subsystem
11StructuralFlyweight-PatternShare fine-grained objects to reduce memory consumption
12StructuralProxy-PatternSurrogate to control access to an object
13BehavioralChain-of-Responsibility-PatternPass requests along a handler chain until one handles it
14BehavioralCommand-PatternEncapsulate a request as an object
15BehavioralInterpreter-PatternDefine a grammar and interpreter for a language
16BehavioralIterator-PatternSequential access to elements without exposing representation
17BehavioralMediator-PatternCentralise complex interactions between peer objects
18BehavioralMemento-PatternCapture and restore object state without breaking encapsulation
19BehavioralObserver-PatternNotify dependents automatically when subject state changes
20BehavioralState-PatternAlter object behaviour when internal state changes
21BehavioralStrategy-PatternDefine interchangeable algorithm family
22BehavioralTemplate-Method-PatternDefine algorithm skeleton, defer steps to subclasses
23BehavioralVisitor-PatternSeparate operations from element structure

Sources

  • Gamma, Helm, Johnson, Vlissides — Design Patterns: Elements of Reusable Object-Oriented Software, Addison-Wesley, 1994