SOLID Principles in LLD: Writing Code That Doesnât Collapse Under Pressure
Thereâs a moment every developer faces.
The code works.
The feature ships.
Everyoneâs happy.
And then⊠requirements change.
Suddenly your once-beautiful code becomes fragile, tangled, and hard to maintain. Thatâs where SOLID principles step in. Not as theory, but as survival tools.
Or as Harvey Specter might put it: > âYou donât prepare for the easy cases. You prepare for the hard ones.â
SOLID is exactly that preparation.
What is SOLID?
SOLID is an acronym representing five design principles:
- S â Single Responsibility Principle
- O â Open/Closed Principle
- L â Liskov Substitution Principle
- I â Interface Segregation Principle
- D â Dependency Inversion Principle
These principles help you write code that is:
- Maintainable
- Scalable
- Testable
- Flexible
Letâs walk through each â with intuition, real-life examples, and code.
1. Single Responsibility Principle (SRP)
Definition
A class should have only one reason to change.
Meaning: One job. One purpose.
Real-life example
Think of a waiter in a restaurant.
A waiter takes orders and serves food.
He doesnât cook, manage inventory, and do accounting.
Bad Design â
class Invoice {
void calculateTotal() {}
void printInvoice() {}
void saveToDB() {}
}Good Design â
class Invoice {
void calculateTotal() {}
}
class InvoicePrinter {
void printInvoice() {}
}
class InvoiceRepository {
void saveToDB() {}
}âFocus is what separates winners from everyone else.â
2. Open/Closed Principle (OCP)
Definition
Software entities should be open for extension but closed for modification.
Bad Design â
class PaymentService {
void pay(String type) {
if(type.equals("UPI")) {}
else if(type.equals("CARD")) {}
}
}Good Design â
interface Payment {
void pay();
}
class UpiPayment implements Payment {
public void pay() {}
}
class CardPayment implements Payment {
public void pay() {}
}âWhen the rules change, adapt â donât panic.â
3. Liskov Substitution Principle (LSP)
Definition
Objects of a superclass should be replaceable with subclass objects without breaking behavior.
Example â
Rectangle vs Square inheritance issue.
âConsistency builds trust â in people and in code.â
4. Interface Segregation Principle (ISP)
Definition
Clients should not be forced to depend on interfaces they donât use.
Bad Design â
interface Machine {
void print();
void scan();
void fax();
}Good Design â
interface Printer {
void print();
}
interface Scanner {
void scan();
}âDonât carry baggage that isnât yours.â
5. Dependency Inversion Principle (DIP)
Definition
High-level modules should depend on abstractions, not concrete implementations.
Bad Design â
class Notification {
EmailService email = new EmailService();
}Good Design â
interface MessageService {
void send();
}
class EmailService implements MessageService {
public void send() {}
}
class Notification {
MessageService service;
Notification(MessageService service) {
this.service = service;
}
}âPower comes from control. Control comes from abstraction.â
Final Thoughts
SOLID principles are not rules to memorize.
Theyâre lenses to think with.
SOLID helps you build systems that evolve without collapsing.
Build clean. Build scalable. Build with intent.
