Design Patterns
The canonical Gang-of-Four patterns — creational, structural, behavioral — that show up in every LLD interview.
Design patterns are reusable solutions to recurring object-oriented design problems. The original Gang-of-Four catalogue lists 23; in practice ~12-15 of them carry their weight in interviews. The Design Patterns topic covers a curated subset, organised by the three classical categories — creational (how objects come into being), structural (how objects are composed), and behavioral (how objects collaborate). A fourth category — architectural patterns (MVC, microservices, event-driven, layered, hexagonal) — sits at the boundary between LLD and system design and gets its own overview piece.
Patterns aren't goals. The point is shared vocabulary: when you say 'use a Strategy here', the interviewer hears a specific shape — a context class holding a strategy interface, with concrete strategies plugged in at construction. The communication shortcut is the value.
Key concepts
- Patterns are vocabulary, not endpoints — naming a pattern shortcuts a long explanation
- Creational patterns hide construction; structural patterns compose; behavioral patterns dispatch
- Architectural patterns (MVC, microservices, hexagonal) organise whole systems, not just classes
- Singleton is the most-abused pattern; reach for dependency injection first
- Strategy + Open-Closed Principle = the most-used pattern combo in real codebases
- Overusing patterns is its own anti-pattern — the simplest design that works wins
Reference template
// Picking a pattern
1. What's varying? (the answer often names the pattern)
2. Object construction? → Creational (Factory, Builder, Singleton, Prototype)
3. Object composition? → Structural (Adapter, Decorator, Facade, Composite, Proxy)
4. Object behaviour / dispatch? → Behavioral (Strategy, Observer, State, Command, Template Method)
5. Will I extend it later? → If yes, pattern; if no, plain class
6. Can a plain class do this? → Yes 80% of the time; pattern when the variation actually appears Adapt to your problem; the structure is the load-bearing part.
Common pitfalls
- Picking the wrong category — 'Singleton + Adapter' is rarely the right composition
- Forcing a pattern when a plain class would do
- Implementing Singleton without thread-safety considerations
- Confusing Strategy and State — same shape, different intent (algorithm vs lifecycle phase)
Related topics
Items (15)
- Singleton Pattern
One instance, global access point. The threading caveats, the why-everyone-says-don't, and when it's still right.
Pattern Foundational - Factory Method Pattern
Defer instantiation to a method subclasses override. The seam between knowing what to make and knowing how to make it.
Pattern Foundational - Abstract Factory Pattern
Families of related objects without specifying concrete classes. When one factory isn't enough.
Pattern Intermediate - Builder Pattern
Step-by-step construction of complex objects, fluent API, telescoping-constructor problem solved.
Pattern Foundational - Prototype Pattern
Clone an existing object instead of constructing from scratch. Shallow vs deep copy and when each is correct.
Pattern Intermediate - Adapter Pattern
Make incompatible interfaces work together. Object adapter vs class adapter, and the legacy-integration use case.
Pattern Foundational - Decorator Pattern
Wrap an object to add behaviour without subclassing. The classic IO-stream example and how to avoid wrapper explosion.
Pattern Intermediate - Facade Pattern
A single entry point hiding a complex subsystem. The architectural cost of skipping it.
Pattern Foundational - Composite Pattern
Treat individual objects and compositions uniformly. The recursive tree-of-things pattern that shows up everywhere.
Pattern Intermediate - Strategy Pattern
Encapsulate interchangeable algorithms behind a common interface. The dual of the Open-Closed Principle in practice.
Pattern Foundational - Observer Pattern
Publish-subscribe for objects. Push vs pull, the leaky-listener problem, and modern event-bus alternatives.
Pattern Foundational - State Pattern
An object's behaviour changes with its internal state — represented as a class hierarchy. The state-machine pattern in disguise.
Pattern Intermediate - Command Pattern
Encapsulate a request as an object. Undo, queue, log, and macro — all fall out of this one structure.
Pattern Intermediate - Template Method Pattern
Define the skeleton of an algorithm in a base class and let subclasses fill in the steps. The hollywood principle illustrated.
Pattern Intermediate - Architectural Design Patterns — Overview
MVC, MVVM, layered, microservices, event-driven, hexagonal. The patterns that organise whole systems, not just classes.
Concept Intermediate