← All posts

Design Patterns: A Practical Overview

· updated · Architecture · 6 min read · Türkçe ↗

I first wrote this post in Turkish in 2017. This is an English translation, lightly edited; I also added Bridge, which the original list left out.

Design patterns are reusable, proven solutions to problems that come up again and again in software design. In object-oriented programming, they describe good ways to structure the relationships between classes and objects.

Because they are tried and tested, patterns speed up development. Reusable, flexible solutions make it easier for software to grow. Code built on well-known patterns is easier to read for anyone who knows them, and patterns give a team a shared vocabulary: saying “put a decorator around it” or “make that a strategy” carries a lot of meaning in a few words.

The classic catalogue, popularised by the “Gang of Four” book Design Patterns: Elements of Reusable Object-Oriented Software, groups patterns into three families. For each pattern below, the short list says when you might reach for it.

1. Creational patterns

Creational patterns are about how objects get created. They hide the details of creation behind flexible structures, so the code that uses an object does not need to know exactly which class it is or how it is assembled.

Abstract Factory creates families of related objects without naming their concrete classes. A client needs objects from several related class hierarchies; the abstract factory hands that responsibility to factory classes. Consider it when:

  • you want your application to be independent of the platform it runs on;
  • you have families of similar objects;
  • you want to group the creation of related objects behind an interface instead of naming each class.

Builder constructs a complex object step by step, setting its parts one at a time.

  • The object is complex.
  • It has to be built in a particular order.

Factory Method gives a method the job of creating objects. The created object is one of several subclasses in a hierarchy, and the factory method decides which one.

  • You do not want to fix the object’s type in advance.
  • The type to create changes at runtime.

Prototype creates new objects by copying an existing one (for example with clone) instead of using new.

  • You want a new object of the same type with the same properties as an existing one.

Singleton ensures a class has only one instance and provides a single point of access to it.

  • You need exactly one instance.
  • It must be reachable from anywhere in the code.

Use Singleton sparingly: it is global state in disguise and makes testing harder. In modern applications, a dependency injection container managing a single instance is usually a better fit.

2. Structural patterns

Structural patterns are about how classes and objects are composed into larger structures.

Adapter lets objects with incompatible interfaces work together by wrapping one in an interface the other expects.

  • You want to make incompatible classes work together.

Bridge separates an abstraction from its implementation so the two can vary independently, instead of multiplying subclasses for every combination.

  • You have two dimensions that change independently (for example shapes and the renderers that draw them).

Composite arranges objects into a tree and lets you treat single objects and groups of objects the same way, because both share a common interface.

  • You need a structure made of simple objects and complex objects that contain them (files and folders, UI components).

Decorator adds behaviour to an object dynamically by wrapping it, without creating a new subclass for every combination of features.

  • You want to add responsibilities to objects at runtime.

Facade provides a single, simple interface to a complex subsystem and hides its details.

  • You want to give clients easy access to several subsystems through one entry point.

Flyweight reduces memory use when you need a very large number of similar objects, by sharing the parts they have in common instead of storing them in every object.

  • You need many fine-grained objects and creating each one in full would be too expensive.

Proxy provides a stand-in for another object and controls access to it, for example to create an expensive object only when it is first needed, or to add caching or access checks.

  • The real object is expensive to create or use.
  • You do not want to initialise it until it is actually needed.

3. Behavioural patterns

Behavioural patterns are about how objects communicate and share responsibility.

Chain of Responsibility passes a request along a chain of handlers until one of them handles it. Each handler decides whether to process the request or pass it on.

  • Several objects could handle a request, but only one should.
  • Example: an ATM handing out a withdrawal using the largest notes first, each note handler passing the remainder to the next.

Command turns a request into an object that contains everything needed to perform it. The object that triggers an action is decoupled from the object that carries it out; commands can be stored, queued, run in sequence or undone, and new commands are easy to add.

  • You want to keep all the information for a call in one object.
  • You want to run commands later, in a batch or in a specific order.

Interpreter defines a grammar for a small language and an interpreter that evaluates sentences in it.

  • You want to express a set of rules as a grammar.
  • You need something that turns those rules into meaningful operations. Examples: musical notation, SQL expressions, simple rule engines.

Iterator gives sequential access to the elements of a collection without exposing how the collection is stored.

  • You have a group of objects you want to walk through in order.
  • You only care about visiting them, not about how the traversal works.

Mediator reduces the dependencies between many classes by making them communicate through a central mediator object instead of directly with each other.

  • You do not want classes talking to each other directly and creating a web of dependencies.
  • Example: air traffic control coordinating aircraft that never talk to each other directly.

Memento captures an object’s internal state so it can be restored later, without breaking encapsulation.

  • You want checkpoints of an object’s state.
  • Example: returning a game character to the last saved checkpoint.

Observer notifies a set of interested objects automatically when another object’s state changes.

  • Specific objects must be told when something changes.
  • Observers need to be added and removed dynamically.

State lets an object change its behaviour when its internal state changes, as if it changed its class.

  • An object should behave differently depending on its current state.

Strategy defines a family of interchangeable algorithms and lets the client choose one at runtime.

  • A class’s behaviour varies, so it makes sense to pull those behaviours out into separate algorithm classes.
  • You want to switch behaviour at runtime without tangled conditionals.

Template Method defines the skeleton of an algorithm in a base class and lets subclasses override specific steps.

  • Several algorithms share the same structure and differ only in some steps.

Visitor lets you add new operations to a set of classes without changing those classes; the operation lives in a separate visitor object.

  • You want to add a new operation to many classes.
  • You do not want to add it to each class by hand, or to an interface that every class must then implement.
  • The set of classes rarely changes, but new operations are added often.

Resources