distancecompsci

The 23 classic design patterns

In 1994 Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, the "Gang of Four", published Design Patterns: Elements of Reusable Object-Oriented Software with Addison-Wesley. It catalogues 23 patterns in three groups.

Creational (5)

PatternIn one line
Abstract FactoryCreate families of related objects without naming concrete classes.
BuilderAssemble a complex object step by step.
Factory MethodLet subclasses decide which class to instantiate.
PrototypeCreate new objects by copying an existing one.
SingletonGuarantee a class has exactly one instance.

Structural (7)

PatternIn one line
AdapterMake an incompatible interface fit the one clients expect.
BridgeSeparate an abstraction from its implementation.
CompositeTreat single objects and trees of objects the same way.
DecoratorAdd behaviour to an object by wrapping it.
FacadeOffer one simple interface to a complex subsystem.
FlyweightShare fine-grained objects to save memory.
ProxyStand in for another object to control access.

Behavioural (11)

PatternIn one line
Chain of ResponsibilityPass a request along handlers until one deals with it.
CommandTurn a request into an object (undo, queues).
InterpreterEvaluate sentences of a small language.
IteratorWalk a collection without exposing its structure.
MediatorCentralise how a group of objects talk to each other.
MementoCapture and restore an object's state.
ObserverNotify dependents when something changes (Qt's signals and slots are a form of this).
StateChange behaviour when internal state changes.
StrategySwap interchangeable algorithms.
Template MethodFix an algorithm's skeleton, let subclasses fill steps.
VisitorAdd operations to a structure without changing its classes.

The book's best-known advice: program to an interface, not an implementation, and prefer composition over inheritance. In 2005 the authors received the ACM SIGPLAN Programming Languages Achievement Award.

  1. Wikipedia: Design Patterns