Design Patterns#

A design pattern is a named, reusable answer to a design problem that keeps coming back. Usually the problem is that one part of the code keeps changing, and you want each change to touch one place instead of twenty. This page covers the ten patterns you will meet most often, and the one idea underneath all of them. Each section starts with the mental model and ends with code. Press Play on any animation to watch the objects move.

Every pattern on this page does the same thing: it finds the part of the code that changes and puts it behind a stable interface, so a change touches one class instead of every caller. Strategy does it for algorithms, Factory for creating objects, Decorator for behaviour added around a call, and Observer for who gets told when something happens. If you know what a pattern isolates, you know when to use it. You also know when not to: if nothing varies there, the pattern is just extra code.

12 Topics • Mental Models, Interactive Animations & Worked Questions
Unit 1

Design Principles#

The two ideas every pattern is built from: keep coupling low, and prefer holding a collaborator to inheriting from one.

Unit 2

Creational Patterns#

Who decides which class gets created, how a complicated object gets assembled, how to copy a ready-made one, and how many instances of something exist.

Unit 3

Structural Patterns#

How objects fit together: wrappers that change an interface so things connect, wrappers that keep it and add behaviour, and shared objects that keep thousands of small ones cheap.

Unit 4

Behavioural Patterns#

How objects split up work and talk to each other: algorithms you can swap at runtime, and events that any number of listeners can react to.

Unit 5

Patterns in Practice#

When a language feature replaces a pattern outright, and how to pick the right one from everything above.