Core Idea
Software entities should be open for extension, closed for modification.
The Violation (Closed for Extension)
Every new payment type requires modifying the processPayment switch in PaymentProcessor.
How to Show This in an Interview
- Interviewer says "add NetBanking payment."
- Naive: "I'll add a case in the switch."
- Better: "With OCP, I already have a stable
PaymentHandlerinterface. I add one new classNetBankingPaymentHandler, implementprocess(), and register it in the router." - Say: "Zero existing code modified. Just one new file added."
Where OCP Applies in LLD Problems
| Problem | Extension Point | Pattern to Use |
|---|---|---|
| Parking Lot (#1) | Add new pricing strategy (weekend surcharge, seasonal) | Strategy (or a new PricingStrategy impl) |
| Splitwise (#5) | Add new split type (ratio, adjustment) | Strategy |
| Vending Machine (#7) | Add new payment method (coins, card, UPI) | Strategy |
| Notification Service (#18) | Add new channel (SMS, push, email) | Strategy / Observer |
| Elevator (#3) | Add new scheduling algorithm (FCFS, SCAN, shortest seek) | Strategy |
Interview Checklist
- Locate variation points โ "what new thing could they ask me to add?"
- Model as interface / abstract class with stable contract
- Keep the resolver/registry simple (map lookup)
- Show ONE extension path as proof โ "adding a new type is one new class + one line registration"
The Fix (Plugin-Style Handlers)
Define a stable PaymentHandler interface. Each payment type is a new class implementing it. PaymentRouter resolves by type โ no switch statement.
// โโโ EXAMPLE 1 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// WHAT WE ARE IMPLEMENTING:
// A payment processing system allowing new payment gateways to be added
// without modifying existing checkout code.
//
// WHERE THE PRINCIPLE FITS IN:
// The system defines a PaymentGateway interface that is open for extension
// via StripePayment or PayPalPayment, but closed for modification in the
// client PaymentService.
// โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// --- Stable interface (closed for modification) ---
interface PaymentHandler {
void process(double amount);
}
// --- Extensions (open for extension) ---
class CardPaymentHandler implements PaymentHandler {
public void process(double amount) {
System.out.println(" [Card] Processing " + amount + " via card gateway. Auth: " + (amount > 100 ? "3DS required" : "Normal"));
}
}
class UPIPaymentHandler implements PaymentHandler {
public void process(double amount) {
System.out.println(" [UPI] Processing " + amount + " via UPI. " + (amount > 5000 ? "OTP validation" : "Quick pay"));
}
}
class WalletPaymentHandler implements PaymentHandler {
public void process(double amount) {
System.out.println(" [Wallet] Processing " + amount + " via wallet. Balance check: " + (amount >= 500 ? "OK" : "Low balance fallback"));
}
}
// --- Resolver (no switch) ---
class PaymentRouter {
private final java.util.Map<String, PaymentHandler> handlers = new java.util.HashMap<>();
public PaymentRouter() {
// Register โ one line per handler
handlers.put("CARD", new CardPaymentHandler());
handlers.put("UPI", new UPIPaymentHandler());
handlers.put("WALLET", new WalletPaymentHandler());
}
public PaymentHandler getHandler(String type) {
PaymentHandler handler = handlers.get(type.toUpperCase());
if (handler == null) throw new IllegalArgumentException("Unknown payment type: " + type);
return handler;
}
// To add NetBanking: no existing class modified.
// Add NetBankingPaymentHandler implements PaymentHandler
// Register: handlers.put("NETBANKING", new NetBankingPaymentHandler())
}
// --- Client ---
public class Main {
public static void main(String[] args) {
PaymentRouter router = new PaymentRouter();
for (String type : new String[]{"CARD", "UPI", "WALLET"}) {
System.out.println("Payment via " + type + ":");
router.getHandler(type).process(250.0);
}
}
}Review
Help Us Improve
How helpful was this walkthrough?
Click a star to rate. We actively use this feedback to refine and update our system design content.
Placeholder
Discussion
Share your thoughts, ask questions, or help others.
Loading comments...