On this page

Design Patterns: One Page Each

A design pattern is a named, reusable answer to a design problem that keeps coming back. The name is half the value. Saying "put a decorator around the repository for caching" or "the order's lifecycle is a state machine" tells another engineer, or an interviewer, a whole structure in a sentence. The other half is knowing the trade-off each pattern makes, so you can tell when it helps and when it's just extra classes.

The design patterns section already covers the classic catalogue on three grouped overview pages: creational, structural and behavioral. Those pages are good for a quick survey. This folder goes one level deeper: one page per pattern, long enough to cover the parts that grouped pages skip, such as the problem in code before the pattern exists, the variations you'll meet in real libraries, the costs, and the patterns each one gets confused with.

Every pattern at a glance

CreationalStructuralBehavioralBeyond GoF
SingletonAdapterStrategySpecification
Factory MethodBridgeObserverNull Object
Abstract FactoryCompositeCommandRepository
BuilderDecoratorStatePattern Selection Guide
PrototypeFacadeTemplate Method
Object PoolFlyweightIterator
Dependency InjectionProxyChain of Responsibility
Mediator
Memento
Visitor
Interpreter

Two entries live outside this folder because they already had full pages: Dependency Injection, which replaces much of what the creational patterns used to do by hand, and the Repository pattern, which hides persistence behind a collection-like interface. Object Pool, Specification and Null Object aren't in the original Gang of Four book, but they come up often enough in real code and interviews to earn a page each.

How each page is laid out

Every pattern page uses the same ten sections, in the same order, so you can jump straight to the part you need:

  1. Intent: what the pattern is for, in a sentence or two.
  2. The problem: a realistic piece of "before" code that shows the pain the pattern removes. If you can't see the problem, the pattern looks like ceremony.
  3. Structure: a Mermaid class diagram of the participants and their relationships.
  4. Participants: what each role is responsible for.
  5. Implementation: the same design in Java, Python and TypeScript, side by side in tabs. Every sample is complete and has been compiled and run with assertions, so you can paste it into a file and execute it.
  6. Real-world uses: where the pattern appears in the JDK, Spring, .NET, the Python standard library, Node and React.
  7. When NOT to use it: the costs, and the situations where a simpler design wins.
  8. Related patterns: the neighbours it's confused with and the difference that matters.
  9. The smell that suggests it: the code symptom that should make you think of this pattern. Some pages also list the smell that suggests removing it.
  10. Interview questions: the questions interviewers actually ask, with the shape of a good answer.

Each pattern in one line

Creational patterns decide how objects get made, so that code that uses objects doesn't hard-wire which classes it creates.

  • Singleton: one instance per scope with a global access point. Mostly replaced by an injected single instance.
  • Factory Method: a class does a job and lets a subclass or a supplied function choose one collaborator's concrete type.
  • Abstract Factory: one object creates a whole family of related products, so families can't be mixed.
  • Builder: step-by-step, validated construction of a complex, usually immutable object.
  • Prototype: new objects are made by copying a configured instance, so getting shallow versus deep copying right is the main job.
  • Object Pool: a bounded set of expensive resources that are borrowed and returned.

Structural patterns compose classes and objects into larger structures without tangling them.

  • Adapter: makes an existing class fit the interface a client expects.
  • Bridge: splits an abstraction from its implementation so both can vary independently.
  • Composite: treats individual objects and trees of objects through one interface.
  • Decorator: adds behaviour by wrapping an object in another with the same interface.
  • Facade: puts one simple entry point in front of a complicated subsystem.
  • Flyweight: shares immutable state among many fine-grained objects to save memory.
  • Proxy: stands in for another object to control access, defer creation or add remote, caching or security behaviour.

Behavioral patterns assign responsibilities and organise how objects communicate.

  • Strategy: swappable algorithms behind one interface.
  • Observer: subscribers are notified when a subject changes.
  • Command: a request as an object, which can be queued, logged or undone.
  • State: an object's behaviour changes with its internal state, with one class per state.
  • Template Method: a fixed algorithm skeleton with overridable steps.
  • Iterator: sequential access to a collection without exposing how it's stored.
  • Chain of Responsibility: a request passes along handlers until one deals with it.
  • Mediator: colleagues talk through a central coordinator instead of to each other.
  • Memento: captures and restores an object's state without breaking encapsulation.
  • Visitor: adds operations to a stable class hierarchy without changing its classes.
  • Interpreter: represents a small language's grammar as classes and evaluates sentences in it.

Beyond GoF

  • Specification: business rules as composable predicate objects (and, or, not).
  • Null Object: a do-nothing implementation that replaces null checks.
  • Pattern Selection Guide: a smell-to-pattern table, plus the pairs people confuse, such as Strategy versus State, Decorator versus Proxy and Adapter versus Facade.

How to study them

Start with the problem, not the diagram. Read the "before" code on each page and ask yourself how you'd fix it before reading on. Patterns stick when you've felt the problem they solve.

Learn them in contrasting pairs. Many interview questions are really "what's the difference between X and Y?" Factory Method and Abstract Factory. Strategy and State. Decorator and Proxy. Adapter and Facade. Builder and Prototype. Each page's related patterns section is written for this, and the pattern selection guide collects the pairs in one place.

Run the code in the language you'll interview in, then change something. Add a fourth strategy, a new product family, or a step to the builder, and notice which classes you had to touch. That's the open/closed principle made concrete, and it's what interviewers mean by "extensible".

Pay attention to the "when NOT to use it" sections. Using a pattern where a plain function or a single class would do is one of the most common ways to lose points. Interviewers reward candidates who reach for a pattern because a requirement asks for it and can say what it costs. See over-patterning for how this plays out in a real round.

Notice what the language already gives you. Several patterns shrink or disappear in modern languages. Python's keyword arguments and dataclasses replace most builders. First-class functions turn many strategies, commands and factory methods into plain callables. TypeScript options objects, Java records and module-scoped instances do the same for other patterns. The pages point these out, because "in Python I'd just use a dataclass here" is a strong interview answer when it's true.

Where patterns show up in the rest of the course

The case studies use these patterns where the requirements call for them, and each case study explains which pattern it picked and which alternatives it rejected. For example, the vending machine is a State machine, the file system is a Composite, the elevator system uses Strategy for scheduling, and the chess design uses Command for undo. The parking lot is a good first exercise: try to find the Strategy, Factory and Singleton decisions in it, and decide whether you agree with each one.

If you're short on time before an interview, prioritise Strategy, Observer, State, Factory Method, Builder, Decorator, Singleton (mainly so you can explain why to avoid it), Command and Composite. They cover most of what appears in LLD rounds. Then use the pattern selection guide to practise going from a smell in the requirements to the right pattern.