logo

kamal's Blog

đŸ€Ÿ Welcome to my tech blog. đŸ’»

About Me

Hey there, I’m Kamal! đŸ€˜ A tech enthusiast with a knack for sharing my mostly silly (and occasionally brilliant đŸ€Ż) discoveries in the coding world. This blog is my playground, where I spill the beans on what I’m learning—whether it’s the basics, the latest tech, or random tidbits that might make you go, “Wait, that’s a thing?” 😅

Here, you’ll find a mix of flexing, cheeky jokes, and even the occasional “mokka” humor to keep things real. Coding is fun, and I write these posts with that same energy! So if you’re having a rough day, stop by—you’ll either walk away grinning or at least wondering, “What was that?!” 😜

Stick around, laugh a little, learn a lot, and remember: coding is fun, and so is reading about it here! 🎉

SOLID Principles in LLD: Writing Code That Doesn’t Collapse Under Pressure

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.