Low-Level Design · Note 3 of 9
Design Patterns in LLD
Quick answer
Which design patterns are used most in low-level design interviews?
Strategy, Observer, State, Factory, Singleton and Decorator cover most low-level design rounds. Strategy swaps a rule such as pricing, Observer notifies displays of a change, State models a vending machine or an order, Factory creates the right subclass from a type, Singleton guards one shared registry, and Decorator stacks optional add-ons. Use each only where a requirement is likely to change.
On this page
Patterns are named answers to changes you can predict. In an LLD round, reach for one when a requirement says "rules vary", "others must be told" or "behaviour depends on status".
The intent and structure of each are in Design Patterns; this note is where they land in a design.
Strategy: rules that vary
- Use: pricing, discount, spot allocation, payment method.
- A context holds the rule behind an interface and can swap it at run time.
- A new rule is a new class: the open/closed principle from SOLID.
A parking lot must price a stay, and the pricing rules will change. FeeCalculator keeps the rule in a field typed by an interface, never by a class.
Transcript · 4 steps
Observer: tell everyone who cares
- Use: display boards, notifications, stock alerts, auction bids.
- The subject keeps a list of observers and calls
updateon each. - The subject never knows what an observer does with the news.
The board at the entrance and the mobile app both need the free-spot count. They subscribe to the lot, which keeps them in a list typed by an Observer interface.
Transcript · 4 steps
State: behaviour follows status
- Use: vending machine, ATM, elevator, order lifecycle, traffic light.
- One class per state; the machine delegates every call to its current state.
- Each state decides the next state; no
if (status == …)chains.
A vending machine behaves differently in each state. With the State pattern each state is a class implementing the same methods, and the machine delegates to its current state object.
Transcript · 5 steps
Factory: one place to create
- Use: vehicles from a type, payment methods, notification channels.
- Callers ask for a type and get back the interface.
- A new subclass changes the factory only.
The entry gate gets a type name from a sensor or a form and needs a vehicle object. It asks VehicleFactory instead of calling new on a concrete class.
Transcript · 4 steps
Decorator: stackable add-ons
- Use: ticket add-ons, pizza toppings, coffee extras, logging around a service.
- Each decorator is the same interface and wraps one inner object.
- Any combination at run time, without a subclass per combination.
A BasicPass costs 60. Customers can add EV charging, valet, or both, and a subclass per combination would explode.
Transcript · 4 steps
Singleton: exactly one
- Use: an ID generator, a configuration, a registry that must be unique.
- Private constructor and one access point.
- Prefer passing the instance in; a global is hard to test and hides dependencies.
Choosing one
| Signal in the requirement | Pattern |
|---|---|
| "different ways to compute X" | Strategy |
| "notify", "subscribe", "alert" | Observer |
| "in state A it does X, in B it does Y" | State |
| "create a … based on type" | Factory |
| "optional extras in any combination" | Decorator |
| "only one … in the system" | Singleton |
Interview questions
- Strategy or State for an order that moves from placed to shipped? State: the order changes its own status and each status allows different actions.
- How does Observer avoid tight coupling? The subject depends only on the observer interface.
- What does a factory buy you? Callers never name concrete classes, so adding one touches one place.
- When not to use a pattern? When nothing in the requirements is going to change there.
Next: Design an LRU Cache · Before this: UML Class Diagrams for LLD Interviews
Common questions
How do I know which design pattern to use in an LLD round?
Look for the part of the requirement that will change or vary. A rule with several versions points to Strategy, many parties reacting to one event to Observer, behaviour that depends on a status to State, and creating one of several subclasses from input to Factory.
What is the difference between Strategy and State?
Both put behaviour behind an interface. In Strategy the client picks the object and the strategies know nothing of each other. In State the object changes its own state as events happen, and each state usually decides which state comes next.
Should I use Singleton for the parking lot class?
Only if the problem says there is exactly one lot per process, and even then pass the instance to the classes that need it. Singleton is global state, which hides dependencies and makes tests share data, so many interviewers prefer you to justify it.
Is it bad to use too many patterns in an LLD interview?
Yes. A pattern adds classes and indirection, so each one should answer a change you can name from the requirements. Interviewers mark down a design that wraps every class in a factory or interface for no stated reason.
When is Decorator better than inheritance?
When features combine freely. With three optional add-ons, subclasses would need one class per combination, while decorators wrap one object in any number of add-ons in any order at run time.