SOLID

SOLID stands for:

S - Single-responsiblity Principle
A class should have one and only one reason to change.
A class should have only one Job.

O - Open-closed Principle
Objects or entities should be open for extension but closed for modification.

L - Liskov Substitution Principle
Let q(x) be a property provable about objects of x of type T. Then q(y) should be provable for objects y of type S where S is a subtype of T.
Every subclass or derived class should be substitutable for their base or parent class.

I - Interface Segregation Principle
A client should never be forced to implement an interface that it doesn’t use, or clients shouldn’t be forced to depend on methods they do not use.

D - Dependency Inversion Principle
Entities must depend on abstractions, not on concretions. 
        It states that the high-level module must not depend on the low-level module, but they should depend on abstractions.
This principle allows for decoupling.

SOLID principles of Object-Oriented Programming (OOP)

A foundational set of design guidelines that help developers write cleaner, more maintainable code.

S – Single Responsibility Principle (SRP)
A class should have only one reason to change, meaning it should do just one thing.
For example, a `ReportPrinter` class shouldn’t also handle saving files to disk.

O – Open/Closed Principle (OCP)
Software entities should be open for extension but closed for modification.
You should be able to add new functionality without changing existing code—think plugins or subclassing.

L – Liskov Substitution Principle (LSP)
Objects of a superclass should be replaceable with objects of a subclass without breaking the application.
If `Bird` has a `fly()` method, then `Penguin` probably shouldn’t inherit from `Bird` unless it can fly too.

I – Interface Segregation Principle (ISP)
Clients shouldn’t be forced to depend on interfaces they don’t use.
It’s better to have many small, specific interfaces than one large, general-purpose one.

D – Dependency Inversion Principle (DIP)
High-level modules shouldn’t depend on low-level modules. Both should depend on abstractions.
This helps decouple code and makes it easier to test and maintain.

These principles were popularized by Robert C. Martin (aka Uncle Bob) and are a cornerstone of clean software architecture.

The SOLID principles in Java code examples:

Single Responsibility Principle (SRP)
- Each class should have only one reason to change.
class Invoice {
  private List<Item> items;
    public double calculateTotal() {
      return items.stream().mapToDouble(Item::getPrice).sum();
    }
}
class InvoicePrinter {
  public void print(Invoice invoice) {
    System.out.println("Total: " + invoice.calculateTotal());
  }
}

Open/Closed Principle (OCP)
- Open for extension, closed for modification.
interface Discount {
  double apply(double total);
}
class NoDiscount implements Discount {
  public double apply(double total) {
    return total;
  }
}
class PercentageDiscount implements Discount {
  public double apply(double total) {
    return total * 0.9; // 10% off
  }
}
class Invoice {
  private double total;
  private Discount discount;
  public Invoice(double total, Discount discount) {
    this.total = discount.apply(total);
  }
  public double getTotal() {
    return total;
  }
}

Liskov Substitution Principle (LSP)
- Subtypes must be substitutable for their base types.
abstract class Bird {
  abstract void move();
}
class Sparrow extends Bird {
  public void move() {
    System.out.println("Flying");
  }
}
class Ostrich extends Bird {
  public void move() {
    System.out.println("Running");
  }
}

Interface Segregation Principle (ISP)
- No client should be forced to depend on methods it doesn't use.
interface Printable {
  void print();
}
interface Scannable {
  void scan();
}
class BasicPrinter implements Printable {
  public void print() {
    System.out.println("Printing...");
  }
}

Dependency Inversion Principle (DIP)
- Depend on abstractions, not concrete implementations.
interface Keyboard {
  String input();
}
class MechanicalKeyboard implements Keyboard {
  public String input() {
    return "Mechanical keystrokes";
  }
}
class Computer {
  private Keyboard keyboard;
  public Computer(Keyboard keyboard) {
    this.keyboard = keyboard;
  }
  public void type() {
    System.out.println(keyboard.input());
  }
}

Comments

Popular posts from this blog

Java 25 Features

Java Version Features

Java 8 Programs