Design Patterns

Free guide

The 23 Gang of Four design patterns, explained step by step

A free guide. Each page covers one pattern: the problem it solves, its solution, a diagram animated step by step and the same example written in Python, TypeScript, Java, C#, PHP and Go.

Start with Singleton Browse the catalog

  • Python
  • TypeScript
  • Java
  • C#
  • PHP
  • Go
  • 23patterns
  • 6languages
  • 138code examples
  • 160animated steps

A pattern in action: Observer

Define a subscription mechanism to automatically notify several objects of a state change. Press play or move forward step by step. The Observer page then walks through the code.

  • Client
  • Key role of the pattern
  • Object
  • Data or state
  1. Observers subscribe to the subject.
  2. StockListener โ†’ Order [NEW]: subscribe()
  3. The subject's state changesโ€ฆ
  4. โ€ฆit notifies each subscriber, without knowing its class.
  5. Order [PAID] โ†’ StockListener: update(order)
  6. New subscriber added without modifying Order (Open/Closed).

What is a design pattern?

A design pattern is a proven solution to a design problem that keeps coming up in object-oriented programming. It is not a piece of code to copy: it is a way of organizing classes and objects, with a name the whole team understands.

The 23 patterns in this guide come from the book Design Patterns: Elements of Reusable Object-Oriented Software (1994), written by four authors nicknamed the โ€œGang of Fourโ€ (GoF). They are grouped into three families: creational, structural and behavioral.

  • Creational patterns 5 patterns

    How to create objects flexibly, without coupling your code to concrete classes.

  • Structural patterns 7 patterns

    How to assemble classes and objects into larger structures that stay flexible.

  • Behavioral patterns 11 patterns

    How objects communicate and share responsibilities.

How to use this guide

  1. Read the problem before the solution: a pattern sticks when you remember the difficulty it removes.
  2. Step through the animated diagram to see which objects talk to each other, and in what order.
  3. Compare the example in your usual language, then in a second one.

The catalog

The pages follow one another in order, but each one can also be read on its own.

Creational patterns

How to create objects flexibly, without coupling your code to concrete classes.

  1. 1Singleton

    Ensure a class has only one instance and provide a global access point to it.

  2. 2Factory Method

    Define an interface for creating an object, but let subclasses decide which concrete class to instantiate.

  3. 3Abstract Factory

    Provide an interface for creating families of related objects without specifying their concrete classes.

  4. 4Builder

    Construct a complex object step by step, separating its construction from its representation.

  5. 5Prototype

    Create new objects by cloning an existing instance, without depending on its concrete class.

Structural patterns

How to assemble classes and objects into larger structures that stay flexible.

  1. 6Adapter

    Let objects with incompatible interfaces work together, through a translator.

  2. 7Bridge

    Decouple an abstraction from its implementation so that the two can evolve independently.

  3. 8Composite

    Compose objects into tree structures and treat leaves and groups uniformly.

  4. 9Decorator

    Add behavior to an object dynamically by wrapping it in objects that share its interface.

  5. 10Facade

    Provide a simplified interface to a complex set of classes, libraries or subsystems.

  6. 11Flyweight

    Save memory by sharing the common (intrinsic) state across a large number of objects.

  7. 12Proxy

    Provide a stand-in that controls access to an object: caching, lazy loading, security, loggingโ€ฆ

Behavioral patterns

How objects communicate and share responsibilities.

  1. 13Chain of Responsibility

    Pass a request along a chain of handlers: each one handles it, stops it, or forwards it to the next.

  2. 14Command

    Turn a request into a standalone object: it can then be passed as a parameter, queued, logged or undone.

  3. 15Iterator

    Traverse the elements of a collection without exposing its internal representation (list, tree, paginated APIโ€ฆ).

  4. 16Mediator

    Reduce chaotic dependencies between objects by forcing them to communicate through a central mediator object.

  5. 17Memento

    Capture and later restore an object's internal state without violating its encapsulation.

  6. 18Observer

    Define a subscription mechanism to automatically notify several objects of a state change.

  7. 19State

    Let an object change its behavior when its internal state changes, as if it had changed class.

  8. 20Strategy

    Define a family of interchangeable algorithms and let you choose which one to use at runtime.

  9. 21Template Method

    Define the skeleton of an algorithm in a base class, letting subclasses override certain steps without changing its structure.

  10. 22Visitor

    Separate an algorithm from the object structure it operates on, so you can add operations without modifying those classes.

  11. 23Interpreter

    Define the grammar of a small language and an interpreter that evaluates sentences in that language.