Lesson 1 / 25

What Design Patterns Are

The Gang of Four catalogue and its three categories.

A shared vocabulary, not a library

A design pattern describes a recurring design problem, the core of a solution, and its trade-offs. The best-known catalogue is Design Patterns: Elements of Reusable Object-Oriented Software (1994) by Gamma, Helm, Johnson and Vlissides, the "Gang of Four" (GoF), which documents 23 patterns in three groups. Creational patterns deal with how objects are created (Factory Method, Abstract Factory, Builder, Prototype, Singleton). Structural patterns deal with how objects are composed (Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy). Behavioural patterns deal with how objects communicate and share responsibility (Chain of Responsibility, Command, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method, Visitor, and Interpreter). A pattern is not code you install: it is a template you adapt, and its biggest value is a name your team can use to discuss a design quickly.

Named solutions to recurring problems

A design pattern is a reusable, named shape for solving a design problem that keeps coming back.

Three ideas: the pattern catalogue, the principles underneath it, and when patterns help or hurt.
Figure 1.1 — From recurring problem to pattern to judgement.

The catalogue at a glance

The GoF categories with one-line intents.

CREATIONAL  (how objects are made)
  Factory Method    let a method decide which class to instantiate
  Abstract Factory  create families of related objects together
  Builder           assemble a complex object step by step
  Prototype         create new objects by copying an existing one
  Singleton         ensure a class has exactly one instance

STRUCTURAL  (how objects are composed)
  Adapter     make an incompatible interface fit the one you expect
  Decorator   add behaviour by wrapping an object
  Facade      give a simple front door to a complex subsystem
  Proxy       stand in for an object to control access to it
  Composite   treat individual items and trees of items uniformly
  Bridge      separate an abstraction from its implementation
  Flyweight   share common state across many small objects

BEHAVIOURAL (how objects collaborate)
  Strategy, Observer, Command, State, Template Method,
  Chain of Responsibility, Iterator, Visitor, Mediator, Memento, Interpreter

Chess openings

Chess players do not invent every opening from scratch; they learn named openings and know when each one suits the position. Patterns are the named openings of software design.

Quick check: Which category does the Builder pattern belong to?

  • Behavioural
  • Structural
  • Creational
  • Architectural
Answer

Creational — Builder is about constructing objects step by step.