RudolphLiu
Back to bookshelf

Gang of FourDesign Patterns: Elements of Reusable Object-Oriented Software

Design Patterns: Elements of Reusable Object-Oriented Software

by Gang of Four

Read: February 15, 2024⭐⭐⭐⭐programming

The foundational catalog of 23 classic software design patterns

Overview

Published in 1994 by the "Gang of Four" (Gamma, Helm, Johnson, Vlissides), this book introduced the concept of design patterns to software engineering. It's the foundational text that every serious programmer should know.

What Are Design Patterns?

Reusable solutions to commonly occurring problems in software design. Not code you copy-paste, but concepts you adapt to your specific situation.

The 23 Patterns (Organized by Purpose)

Creational Patterns

How objects are created:

  • Singleton: Ensure only one instance exists
  • Factory Method: Create objects without specifying exact class
  • Abstract Factory: Create families of related objects
  • Builder: Construct complex objects step-by-step
  • Prototype: Create new objects by copying existing ones

Structural Patterns

How objects are composed:

  • Adapter: Make incompatible interfaces work together
  • Bridge: Separate abstraction from implementation
  • Composite: Treat individual objects and compositions uniformly
  • Decorator: Add responsibilities to objects dynamically
  • Facade: Provide simplified interface to complex subsystem
  • Flyweight: Share common state to support large numbers of objects
  • Proxy: Control access to an object

Behavioral Patterns

How objects interact and distribute responsibility:

  • Chain of Responsibility: Pass request along chain of handlers
  • Command: Encapsulate request as an object
  • Iterator: Access elements sequentially without exposing structure
  • Mediator: Centralize complex communications
  • Memento: Capture and restore object state
  • Observer: Notify dependents of state changes
  • State: Alter behavior when internal state changes
  • Strategy: Encapsulate algorithms as interchangeable objects
  • Template Method: Define algorithm skeleton, let subclasses fill in steps
  • Visitor: Separate algorithm from object structure
  • Interpreter: Define grammar and interpreter for a language

Patterns I Use Most

Observer

Subject  notifies  Observers

Essential for event-driven systems, UI updates, pub/sub architectures.

Strategy

Context  delegates to  Strategy interface

Swap algorithms at runtime. Perfect for A/B testing, payment methods, sorting algorithms.

Factory Method

Creator  creates  Product

Decouple object creation from usage. Crucial for dependency injection.

Decorator

Component  wrapped by  Decorator  wrapped by  Another Decorator

Add behavior without modifying original class. Used in React HOCs, middleware.

Key Principles

  1. Program to an interface, not an implementation
  2. Favor object composition over class inheritance
  3. Encapsulate what varies
  4. Depend on abstractions, not concrete classes

The Good

  • Established common vocabulary for developers
  • Solutions proven in real-world systems
  • Encourages thinking about design
  • Shows relationships between patterns

The Challenging

  • Examples use C++ and Smalltalk (dated for modern developers)
  • Some patterns are language-specific workarounds
  • Can lead to over-engineering if misapplied
  • Academic tone, not beginner-friendly

Modern Perspective

In 2024, some patterns feel less relevant:

  • Singleton: Often an anti-pattern (global state, testing issues)
  • Visitor: Complex, rarely needed
  • Many creational patterns: Less important with modern DI containers

Others are more essential than ever:

  • Observer: Core to reactive programming (RxJS, React hooks)
  • Strategy: Fundamental in functional programming
  • Decorator: Essential in modern frameworks
  • Factory: Central to dependency injection

My Thoughts

This book is dense and academic, but groundbreaking. It gave developers a shared language—saying "use the Observer pattern" immediately communicates a complex idea.

I don't recommend reading it cover-to-cover. Instead:

  1. Learn the concepts from modern sources
  2. Use this book as a reference
  3. Focus on patterns relevant to your language/framework

The patterns themselves are less important than the mindset they teach: recognizing recurring problems and applying proven solutions.

Practical Application

I've used these patterns in:

  • Observer: React state management, event systems
  • Factory: Creating different user types, payment processors
  • Strategy: Sorting algorithms, validation rules
  • Decorator: Logging, authentication, caching middleware
  • Adapter: Integrating third-party APIs

Rating: 4/5

Essential knowledge, but the book itself is dated. Learn the concepts from modern sources, use this as the authoritative reference. Changed software engineering forever.