Skip to main content

Design Pattern Basics

Gang of Four (GoF) Overview

0:00
LearnStep 1/4

The 23 GoF Patterns

The Complete Gang of Four Pattern Catalog

The Gang of Four identified 23 patterns organized into three categories.

Creational Patterns (5)

PatternPurposeUse When
Abstract Factory Create families of related objects Need to create product families without specifying concrete classes
Builder Construct complex objects step by step Object has many optional parameters or complex construction
Factory Method Define interface for creating objects Don't know exact types of objects needed until runtime
Prototype Create objects by cloning Creating new objects is expensive; better to clone existing ones
Singleton Ensure only one instance exists Need exactly one instance (config, connection pool, logger)

Structural Patterns (7)

PatternPurposeUse When
Adapter Make incompatible interfaces compatible Integrating with legacy code or third-party libraries
Bridge Separate abstraction from implementation Want to avoid permanent binding between abstraction and implementation
Composite Treat individual and composite objects uniformly Working with tree structures or hierarchies
Decorator Add responsibilities dynamically Need to add functionality without subclassing
Facade Simplify complex subsystems Need simple interface to complex system
Flyweight Share objects to save memory Need many similar objects; memory is a concern
Proxy Control access to an object Need lazy initialization, access control, or logging

Behavioral Patterns (11)

PatternPurposeUse When
Chain of Responsibility Pass request along chain of handlers Multiple objects can handle a request
Command Encapsulate request as object Need to parameterize objects with operations, queue requests, or support undo
Interpreter Define grammar and interpret sentences Need to interpret a language or expressions
Iterator Access elements sequentially Need to traverse collection without exposing structure
Mediator Centralize complex communications Objects communicate in complex ways; want to reduce coupling
Memento Capture and restore object state Need to implement undo/redo or snapshots
Observer Notify dependents of state changes Change to one object requires changes to others
State Alter behavior when state changes Object behavior depends on its state
Strategy Encapsulate interchangeable algorithms Need different variants of an algorithm
Template Method Define algorithm skeleton Have algorithm with steps that can vary
Visitor Add operations to object structure Need to perform operations on elements of complex object structure