Back to Home
Learn LLD Concepts

Design Patterns Explained

Master the most important design patterns with theory explanations and practical code examples. Learn when to use each pattern and how they solve real-world problems.

Category
Difficulty

Showing 21 patterns

Singleton Pattern

CreationalBeginner
8 min read

Ensures a class has only one instance and provides a global point of access to it.

Instance Control
Global Access
Lazy Loading

Common Use Cases:

  • Configuration Manager
  • Logger

Factory Method Pattern

CreationalBeginner
10 min read

Defines an interface for creating objects, but lets subclasses decide which class to instantiate.

Object Creation
Subclass Control
Loose Coupling

Common Use Cases:

  • Payment Gateways
  • Notification Systems

Builder Pattern

CreationalIntermediate
12 min read

Separates the construction of a complex object from its representation, allowing step-by-step creation.

Step-by-Step Construction
Fluent Interface
Immutability

Common Use Cases:

  • Complex Object Creation
  • Query Builders

Strategy Pattern

BehavioralIntermediate
15 min read

Defines a family of algorithms, encapsulates each one, and makes them interchangeable at runtime without modifying the client.

Interchangeable Algorithms
Open/Closed Principle
Runtime Switching
Composition over Inheritance

Common Use Cases:

  • Payment Processing (Credit Card, PayPal, Crypto)
  • Navigation/Routing (Driving, Walking, Cycling)

Observer Pattern

BehavioralIntermediate
10 min read

Defines a one-to-many dependency so when one object changes state, all its dependents are notified and updated automatically.

Event-Driven
Pub/Sub
Loose Coupling

Common Use Cases:

  • Real-Time Notifications
  • UI State Management

Decorator Pattern

StructuralIntermediate
10 min read

Attach additional responsibilities to an object dynamically, providing a flexible alternative to subclassing.

Composition
Wrapping
Runtime Behaviour

Common Use Cases:

  • Middleware Pipelines
  • I/O Streams

Adapter Pattern

StructuralBeginner
10 min read

Convert the interface of a class into another interface clients expect, letting incompatible classes work together.

Interface Translation
Integration
Compatibility

Common Use Cases:

  • Third-Party SDKs
  • Legacy Systems

Facade Pattern

StructuralBeginner
9 min read

Provide a simplified interface to a complex subsystem, hiding internal complexity behind a single entry point.

Simplification
Single Entry Point
Loose Coupling

Common Use Cases:

  • API Gateway
  • Library Wrappers

Template Method Pattern

BehavioralIntermediate
10 min read

Define the skeleton of an algorithm in a base class, deferring specific steps to subclasses without changing the overall structure.

Algorithm Skeleton
Hooks
Hollywood Principle

Common Use Cases:

  • Report Generators
  • Data Migration Pipelines

Command Pattern

BehavioralIntermediate
11 min read

Encapsulate a request as an object, enabling undo/redo, request queuing, logging, and decoupling of sender from receiver.

Undo/Redo
Request Object
Decoupling

Common Use Cases:

  • Text Editor Undo/Redo
  • Smart Home Controllers

State Pattern

BehavioralIntermediate
11 min read

Allow an object to alter its behaviour when its internal state changes, appearing to change its class at runtime.

State Machine
Runtime Behaviour
Conditional Elimination

Common Use Cases:

  • Order Lifecycle
  • Traffic Light Systems

Chain of Responsibility

BehavioralIntermediate
11 min read

Pass requests along a chain of handlers, where each handler decides to process the request or pass it to the next handler.

Request Pipeline
Decoupling
Dynamic Chain

Common Use Cases:

  • Middleware Pipelines
  • Event Bubbling

Abstract Factory

CreationalAdvanced
14 min read

Provides an interface for creating families of related or dependent objects without specifying their concrete classes.

Family of Products
Dependency Injection
Platform Independence

Common Use Cases:

  • Cross-platform UI Toolkits (Windows/Mac/Linux)
  • Theming Engines

Prototype Pattern

CreationalIntermediate
10 min read

Allows cloning existing objects without coupling your code to their specific classes, ideal for expensive object creation.

Object Cloning
Performance Optimization
Registry Pattern

Common Use Cases:

  • Game Character Spawning
  • Complex Config Duplication

Proxy Pattern

StructuralIntermediate
15 min read

Provides a surrogate or placeholder for another object to control access to it, often for security or lazy loading.

Access Control
Lazy Loading
Network Middleware

Common Use Cases:

  • API Rate Limiting
  • Database Connection Pooling

Composite Pattern

StructuralIntermediate
12 min read

Composes objects into tree structures to represent part-whole hierarchies, treating individual objects and compositions uniformly.

Tree Structure
Recursive Composition
Unified Interface

Common Use Cases:

  • File Systems (Folders/Files)
  • UI Component Trees (DOM)

Bridge Pattern

StructuralAdvanced
18 min read

Decouples an abstraction from its implementation so that the two can vary independently, avoiding an exponential class explosion.

Decoupling
Implementation vs Abstraction
Extensibility

Common Use Cases:

  • Graphics Rendering Engines
  • Device Drivers

Mediator Pattern

BehavioralIntermediate
13 min read

Reduces chaotic dependencies between objects by forcing them to communicate via a central mediator object.

Loose Coupling
Centralized Communication
Hub-and-Spoke

Common Use Cases:

  • Air Traffic Control Systems
  • Chat Room Hubs

Memento Pattern

BehavioralAdvanced
15 min read

Captures and externalizes an object's internal state so that the object can be restored to this state later without violating encapsulation.

Undo/Redo
State Snapshot
Encapsulation Preservation

Common Use Cases:

  • Text Editor History
  • Game Save Checkpoints

Flyweight Pattern

StructuralAdvanced
20 min read

Supports large numbers of fine-grained objects efficiently by sharing common parts of state across multiple objects.

Memory Optimization
Shared State
Object Pooling

Common Use Cases:

  • Particle Systems in Games
  • Text Editor Character Rendering

Visitor Pattern

BehavioralAdvanced
18 min read

Allows adding new operations to existing object structures without modifying the structures themselves.

Double Dispatch
Operation Separation
Object Structure

Common Use Cases:

  • Compiler Syntax Tree Analysis
  • Complex Export Utilities