Factory Pattern — Centralise and hide object creation
Centralise creation so callers ask for a product by kind, not new Concrete(). · Registries beat giant switch statements as types grow.
Watch
Watch, then scroll down for code and practice.
In code
type Kind = "email" | "sms";
interface Notification {
send(msg: string): void;
}
function createNotification(kind: Kind): Notification {
if (kind === "email") return { send: (m) => console.log("email", m) };
return { send: (m) => console.log("sms", m) };
}📘 Key ideas
Problem it solves
NotificationService has a giant switch-case to create Email/SMS/Push objects. Adding a new type means opening the service.
Solution
Extract a NotificationFactory. It maps type strings to constructors. NotificationService calls factory.create(type) and uses the interface.
Registry variant
Store a Map<string, Constructor> in the factory. Registering a new type = one line. No switch case ever.
When to use
When you need to create objects whose type is determined at runtime, or when creation logic is complex enough to deserve its own class.
🧠 Practice — Apply What You Learned
Factory Pattern: Notification Creator
A NotificationService creates different notification objects based on type: EMAIL, SMS, PU…
Builder Pattern: SQL Query Builder
Building a SQL query string by concatenating strings leads to bugs and unreadable code. De…
Singleton Pattern: Thread-Safe Config Manager
Design an AppConfig singleton that loads configuration from environment/file once and prov…
Observer Pattern: Stock Price Alerts
Design a StockMarket system where multiple observers (mobile app, email alert, dashboard w…
Decorator Pattern: Coffee Customisation
Design a coffee ordering system where a base Coffee can be decorated with add-ons (Milk, S…
Logger / Logging Framework
Design a flexible logging framework that supports multiple log levels, formatters, and out…
Food Delivery System (Swiggy/Zomato)
Design a food delivery platform where customers can browse restaurants, place orders, and …
Chat Application (WhatsApp-like)
Design a messaging system supporting 1-on-1 chats, group chats, message status, and media …
Notification System
Design a notification service that can send alerts via multiple channels based on user pre…
Ride Sharing System (Uber/Ola)
Design a ride sharing platform where riders request rides and drivers accept them.…
LRU Cache System
Design an in-memory cache system with LRU eviction policy, TTL support, and thread safety.…
Distributed Job Scheduler
Design a job scheduling system that can queue, execute, and monitor background jobs with r…
Hotel Booking System
Design a hotel room booking platform with availability search, pricing, and reservation ma…
Social Media Feed (Twitter/Instagram)
Design a social media platform with posts, follows, and a personalized news feed.…
Rate Limiter
Design a rate limiting service that restricts request rates per user/IP using multiple alg…
Event Ticketing System
Design a large-scale event ticketing platform (like Ticketmaster) with high concurrency su…
🚀 Now apply what you learned
Pick a problem above, write your solution, and get AI feedback on your design.
Start Practice →