GoF Design Patterns Reference
A comprehensive reference for the Gang of Four design patterns. This skill provides language-agnostic guidance with pseudocode examples that can be translated to any programming language.
When This Skill Activates
This skill automatically activates when you:
- Ask about or need to implement a design pattern
- Need to choose between patterns for a design problem
- Are refactoring code and considering structural improvements
- Discuss architecture, decoupling, or extensibility
- Mention specific patterns: factory, singleton, observer, decorator, etc.
Quick Pattern Reference
Creational Patterns (Object Creation)
| Pattern |
Intent |
Use When |
| Abstract Factory |
Create families of related objects |
Need platform/theme independence |
| Builder |
Construct complex objects step-by-step |
Object has many optional parts |
| Factory Method |
Let subclasses decide which class to instantiate |
Don't know concrete types ahead of time |
| Prototype |
Clone existing objects |
Object creation is expensive |
| Singleton |
Ensure single instance |
Need exactly one shared instance |
Structural Patterns (Composition)
| Pattern |
Intent |
Use When |
| Adapter |
Convert interface to expected interface |
Integrating incompatible interfaces |
| Bridge |
Separate abstraction from implementation |
Need to vary both independently |
| Composite |
Treat individual and groups uniformly |
Have tree structures |
| Decorator |
Add responsibilities dynamically |
Need flexible extension |
| Facade |
Simplified interface to subsystem |
Complex subsystem needs simple API |
| Flyweight |
Share common state efficiently |
Many similar objects needed |
| Proxy |
Control access to object |
Need lazy loading, access control, logging |
Behavioral Patterns (Communication)
| Pattern |
Intent |
Use When |
| Chain of Responsibility |
Pass request along handler chain |
Multiple handlers, unknown which handles |
| Command |
Encapsulate request as object |
Need undo, queue, or log operations |
| Interpreter |
Define grammar and interpret sentences |
Have a simple language to parse |
| Iterator |
Sequential access without exposing internals |
Need to traverse collections |
| Mediator |
Centralize complex communications |
Many objects communicate in complex ways |
| Memento |
Capture and restore object state |
Need undo/snapshot functionality |
| Observer |
Notify dependents of state changes |
One-to-many dependency |
| State |
Alter behavior when state changes |
Object behavior depends on state |
| Strategy |
Encapsulate interchangeable algorithms |
Need to swap algorithms at runtime |
| Template Method |
Define skeleton, let subclasses fill in |
Algorithm structure fixed, steps vary |
| Visitor |
Add operations without changing classes |
Need to add many operations to stable structure |
Decision Guide
See Pattern Selection Guide for help choosing the right pattern.
How to Use This Reference
- Choosing a pattern: Start with the decision guide or tables above
- Learning a pattern: Read the full documentation with examples
- Quick reminder: Use the tables above for at-a-glance reference
- Implementation: Follow the pseudocode, adapt to your language
Language Translation Notes
The pseudocode in this reference uses these conventions:
class for type definitions
function for methods/functions
-> for method calls on objects
// for comments
- Type hints shown as
name: Type
Translate to your language:
- PHP:
class, function, ->, //, type hints in docblocks or PHP 8+
- JavaScript/TypeScript:
class, function/arrow, ., //, TS types
- Python:
class, def, ., #, type hints
- Java/C#: Direct mapping with
new, generics
Based on concepts from "Design Patterns: Elements of Reusable Object-Oriented Software" by Gamma, Helm, Johnson, and Vlissides (Gang of Four), 1994.
1---2name: gof-patterns3description: Gang of Four design patterns reference. Use this skill when implementing, discussing, or choosing object-oriented design patterns. Auto-activates for architecture decisions, refactoring, and decoupling concerns. Comprehensive coverage of all 23 GoF patterns with pseudocode examples.4---5
6# GoF Design Patterns Reference
7
8A comprehensive reference for the Gang of Four design patterns. This skill provides language-agnostic guidance with pseudocode examples that can be translated to any programming language.
9
10## When This Skill Activates
11
12This skill automatically activates when you:
13- Ask about or need to implement a design pattern
14- Need to choose between patterns for a design problem
15- Are refactoring code and considering structural improvements
16- Discuss architecture, decoupling, or extensibility
17- Mention specific patterns: factory, singleton, observer, decorator, etc.
18
19## Quick Pattern Reference
20
21### Creational Patterns (Object Creation)
22| Pattern | Intent | Use When |
23|---------|--------|----------|
24| [Abstract Factory](gof-creational/abstract-factory.md) | Create families of related objects | Need platform/theme independence |
25| [Builder](gof-creational/builder.md) | Construct complex objects step-by-step | Object has many optional parts |
26| [Factory Method](gof-creational/factory-method.md) | Let subclasses decide which class to instantiate | Don't know concrete types ahead of time |
27| [Prototype](gof-creational/prototype.md) | Clone existing objects | Object creation is expensive |
28| [Singleton](gof-creational/singleton.md) | Ensure single instance | Need exactly one shared instance |
29
30### Structural Patterns (Composition)
31| Pattern | Intent | Use When |
32|---------|--------|----------|
33| [Adapter](gof-structural/adapter.md) | Convert interface to expected interface | Integrating incompatible interfaces |
34| [Bridge](gof-structural/bridge.md) | Separate abstraction from implementation | Need to vary both independently |
35| [Composite](gof-structural/composite.md) | Treat individual and groups uniformly | Have tree structures |
36| [Decorator](gof-structural/decorator.md) | Add responsibilities dynamically | Need flexible extension |
37| [Facade](gof-structural/facade.md) | Simplified interface to subsystem | Complex subsystem needs simple API |
38| [Flyweight](gof-structural/flyweight.md) | Share common state efficiently | Many similar objects needed |
39| [Proxy](gof-structural/proxy.md) | Control access to object | Need lazy loading, access control, logging |
40
41### Behavioral Patterns (Communication)
42| Pattern | Intent | Use When |
43|---------|--------|----------|
44| [Chain of Responsibility](gof-behavioral/chain-of-responsibility.md) | Pass request along handler chain | Multiple handlers, unknown which handles |
45| [Command](gof-behavioral/command.md) | Encapsulate request as object | Need undo, queue, or log operations |
46| [Interpreter](gof-behavioral/interpreter.md) | Define grammar and interpret sentences | Have a simple language to parse |
47| [Iterator](gof-behavioral/iterator.md) | Sequential access without exposing internals | Need to traverse collections |
48| [Mediator](gof-behavioral/mediator.md) | Centralize complex communications | Many objects communicate in complex ways |
49| [Memento](gof-behavioral/memento.md) | Capture and restore object state | Need undo/snapshot functionality |
50| [Observer](gof-behavioral/observer.md) | Notify dependents of state changes | One-to-many dependency |
51| [State](gof-behavioral/state.md) | Alter behavior when state changes | Object behavior depends on state |
52| [Strategy](gof-behavioral/strategy.md) | Encapsulate interchangeable algorithms | Need to swap algorithms at runtime |
53| [Template Method](gof-behavioral/template-method.md) | Define skeleton, let subclasses fill in | Algorithm structure fixed, steps vary |
54| [Visitor](gof-behavioral/visitor.md) | Add operations without changing classes | Need to add many operations to stable structure |
55
56## Decision Guide
57
58See [Pattern Selection Guide](pattern-selection.md) for help choosing the right pattern.
59
60## How to Use This Reference
61
621. **Choosing a pattern**: Start with the decision guide or tables above
632. **Learning a pattern**: Read the full documentation with examples
643. **Quick reminder**: Use the tables above for at-a-glance reference
654. **Implementation**: Follow the pseudocode, adapt to your language
66
67## Language Translation Notes
68
69The pseudocode in this reference uses these conventions:
70- `class` for type definitions
71- `function` for methods/functions
72- `->` for method calls on objects
73- `//` for comments
74- Type hints shown as `name: Type`
75
76Translate to your language:
77- **PHP**: `class`, `function`, `->`, `//`, type hints in docblocks or PHP 8+
78- **JavaScript/TypeScript**: `class`, `function`/arrow, `.`, `//`, TS types
79- **Python**: `class`, `def`, `.`, `#`, type hints
80- **Java/C#**: Direct mapping with `new`, generics
81
82---
83
84*Based on concepts from "Design Patterns: Elements of Reusable Object-Oriented Software" by Gamma, Helm, Johnson, and Vlissides (Gang of Four), 1994.*