LLD Hub
Learn

Decorator PatternAdd behaviour at runtime without subclassing

Wrap the same interface to add behaviour without subclass explosion. · Decorators can stack — each delegates inward.

Watch

Watch, then scroll down for code and practice.

In code

Composable beverageTypeScript
interface Beverage {
  cost(): number;
  describe(): string;
}

class Coffee implements Beverage {
  cost() {
    return 40;
  }
  describe() {
    return "Coffee";
  }
}

class Milk implements Beverage {
  constructor(private inner: Beverage) {}
  cost() {
    return this.inner.cost() + 10;
  }
  describe() {
    return this.inner.describe() + " + milk";
  }
}

📘 Key ideas

The pattern

Wrap an object with another object that has the same interface. The wrapper delegates to the inner object and adds behaviour before/after.

Avoids class explosion

Without Decorator, MilkCoffee, SugarCoffee, MilkSugarCoffee, WhipCoffee... → 2^N classes. With Decorator: just add wrappers at runtime.

Key rule

Every decorator must accept ANY object of the base type, including other decorators. This is what enables stacking.

Real-world

Java's BufferedInputStream wraps FileInputStream. HTTP middleware chains. Express/Koa middleware. All are Decorator.

🧠 Practice — Apply What You Learned

Factory Pattern: Notification Creator

A NotificationService creates different notification objects based on type: EMAIL, SMS, PU

FreeL3 · Medium

Builder Pattern: SQL Query Builder

Building a SQL query string by concatenating strings leads to bugs and unreadable code. De

L3 · Medium

Singleton Pattern: Thread-Safe Config Manager

Design an AppConfig singleton that loads configuration from environment/file once and prov

L3 · Medium

Observer Pattern: Stock Price Alerts

Design a StockMarket system where multiple observers (mobile app, email alert, dashboard w

L3 · Medium

Decorator Pattern: Coffee Customisation

Design a coffee ordering system where a base Coffee can be decorated with add-ons (Milk, S

L3 · Medium

Logger / Logging Framework

Design a flexible logging framework that supports multiple log levels, formatters, and out

L2 · Easy

Food Delivery System (Swiggy/Zomato)

Design a food delivery platform where customers can browse restaurants, place orders, and

L5 · Intermediate

Chat Application (WhatsApp-like)

Design a messaging system supporting 1-on-1 chats, group chats, message status, and media

L5 · Intermediate

Notification System

Design a notification service that can send alerts via multiple channels based on user pre

L4 · Medium

LRU Cache System

Design an in-memory cache system with LRU eviction policy, TTL support, and thread safety.

L4 · Medium

Distributed Job Scheduler

Design a job scheduling system that can queue, execute, and monitor background jobs with r

L9 · Expert

Social Media Feed (Twitter/Instagram)

Design a social media platform with posts, follows, and a personalized news feed.

L7 · Hard

Rate Limiter

Design a rate limiting service that restricts request rates per user/IP using multiple alg

L7 · Hard

🚀 Now apply what you learned

Pick a problem above, write your solution, and get AI feedback on your design.

Start Practice →