Skip to main content

Design Patterns Overview

Design patterns are reusable solutions to recurring design problems. They are a vocabulary, not a prescription. Knowing when not to use a pattern is as important as knowing the pattern itself.


The Gang of Four (GoF) Patternsโ€‹

The 23 classic patterns from Design Patterns: Elements of Reusable Object-Oriented Software (Gamma et al., 1994) are organized into three categories:

CategoryPurposePatterns
CreationalControl object creationSingleton, Factory Method, Abstract Factory, Builder, Prototype
StructuralCompose classes/objectsAdapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy
BehavioralObject communicationChain of Responsibility, Command, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method, Visitor

Most Commonly Asked in LLD Interviewsโ€‹

From most to least frequently appearing:

๐Ÿฅ‡ Tier 1 (Must Know โ€” appear in almost every problem)
Strategy, Observer, Factory, Singleton, Builder

๐Ÿฅˆ Tier 2 (High Frequency โ€” know well)
Decorator, Command, State, Composite, Template Method

๐Ÿฅ‰ Tier 3 (Know the concept)
Adapter, Facade, Proxy, Iterator, Chain of Responsibility

How to Recognize Which Pattern to Useโ€‹

When you see...Consider...
Multiple algorithms that are interchangeableStrategy
One event that many objects need to react toObserver
Complex object construction with many optionsBuilder
Adding behavior to objects without subclassingDecorator
A request that should be encapsulated as an objectCommand
An object whose behavior changes based on internal stateState
A tree of objects (part-whole hierarchy)Composite
An incompatible interface you can't changeAdapter
Exactly one instance of a class neededSingleton
Subclasses decide which class to instantiateFactory Method
A fixed algorithm with customizable stepsTemplate Method

Common Mistakes to Avoidโ€‹

โŒ Overusing Singletonโ€‹

Singleton is the most misused pattern. It introduces global state and makes testing hard.

// โŒ Using Singleton just because "there's only one right now"
public class DatabaseConnection {
private static DatabaseConnection instance;
// ... is this really a singleton? What about test isolation?
}

// โœ… Better: use dependency injection; let the DI container manage lifecycle
public class DatabaseConnection {
private final String url;
public DatabaseConnection(String url) { this.url = url; }
}
// Wire once in main(), inject everywhere
Interview Tip ๐ŸŽฏ

When you mention Singleton in an interview, proactively say: "I'd use Singleton here for X, but I'm aware of the downsides โ€” global state makes unit testing harder. In a production codebase I'd prefer to manage this lifecycle through a DI framework." This shows senior thinking.

โŒ Pattern Matchingโ€‹

Don't force a pattern onto every problem. The simplest code that expresses intent clearly is always better.

โŒ Naming Patterns in the Nameโ€‹

StrategyStrategy, FactoryFactory โ€” these suggest you're thinking about patterns instead of the domain.


The Pattern Selection Frameworkโ€‹

When you identify a design decision in an interview, ask yourself:

1. What VARIES in this design?
โ†’ The variation point is where a pattern should live.

2. What STAYS THE SAME?
โ†’ This is the stable core โ€” protect it from change.

3. What RELATIONSHIP exists between objects?
โ†’ is-a โ†’ inheritance/interface
โ†’ has-a โ†’ composition
โ†’ uses-a โ†’ dependency injection

4. What CHANGES OVER TIME?
โ†’ State pattern for behavior changes
โ†’ Observer for data propagation
โ†’ Command for action history

Pattern Combinations in Real Problemsโ€‹

Real systems use multiple patterns together. Here's how they combine in classic LLD problems:

ProblemPatterns Used
Parking LotStrategy (pricing), Factory (spot allocation), Observer (availability events)
Rate LimiterStrategy (algorithm), Singleton (per-resource limiter)
ElevatorState (door/movement), Strategy (scheduling), Observer (floor requests)
Movie BookingObserver (seat hold expiry), Command (book/cancel), Factory (seat types)
File SystemComposite (file/folder tree), Iterator (traversal), Decorator (permissions)

Next โ†’ Creational Patterns