Concept Explanation
Design patterns are specific solutions. SOLID is the set of principles those solutions embody. Learn these and you will reinvent half the patterns naturally.
S — Single Responsibility
A class should have one reason to change.
// Three responsibilities: persistence, email, PDF generation
class Order {
void save() { }
void sendConfirmationEmail() { }
void generateInvoicePdf() { }
}
Change your email provider and you edit the Order class — which also risks breaking persistence. Split it: Order (data), OrderRepository, EmailService, InvoiceGenerator.
O — Open/Closed
Open for extension, closed for modification.
// Every new shipping method edits this method — and risks the existing ones
double cost(Order o) {
if (o.method() == STANDARD) return 5.0;
else if (o.method() == EXPRESS) return 15.0;
// ...
}
// Extension now means adding a class, not editing one
public interface ShippingStrategy { double cost(Order o); }
public class ExpressShipping implements ShippingStrategy {
public double cost(Order o) { return 15.0; }
}
L — Liskov Substitution
A subclass must be usable anywhere its parent is, without surprises.
The textbook violation:
class Rectangle {
void setWidth(int w) { this.width = w; }
void setHeight(int h) { this.height = h; }
}
class Square extends Rectangle {
void setWidth(int w) { this.width = w; this.height = w; } // surprise!
}
Code that sets width to 5 and height to 4 expecting an area of 20 gets 16. The subclass broke a contract callers relied on. A Square is not a substitutable Rectangle.
I — Interface Segregation
Do not force a class to implement methods it does not need.
// A robot does not eat or sleep
interface Worker { void work(); void eat(); void sleep(); }
interface Workable { void work(); }
interface Feedable { void eat(); }
class Robot implements Workable { } // implements only what applies
class Human implements Workable, Feedable { }
D — Dependency Inversion
Depend on abstractions, not concrete classes.
// Locked to MySQL forever; untestable without a real database
class OrderService {
private final MySqlOrderRepository repo = new MySqlOrderRepository();
}
class OrderService {
private final OrderRepository repo; // an interface
public OrderService(OrderRepository repo) { // injected
this.repo = repo;
}
}
This is precisely what Spring's dependency injection gives you, and it is why constructor injection is the recommended style.
Common Mistakes
- Applying SOLID everywhere at once: A 200-line CRUD app split into 40 interfaces is worse, not better.
- One interface per class reflexively: An interface with exactly one implementation and no test double is ceremony.
- Confusing SRP with "one method per class": The unit is a reason to change, not a line count.
- Inheriting to reuse code: Prefer composition. Inheritance is for genuine substitutability.
Best Practices
- Let tests guide you: Code that is hard to test usually violates D or S.
- Refactor toward SOLID when a change hurts, not preemptively on day one.
- Composition over inheritance as the default.
- Name the reason to change: If you can list two for a class, it likely wants splitting.
Practice Exercises
- The Split: Find a class in your code doing three things and separate it. Note which one now changes independently.
- The Strategy: Replace an if/else chain over a type enum with an interface plus implementations.
- The Inversion: Take a service that instantiates its own dependency and convert it to constructor injection. Then write a unit test with a fake — impossible before, trivial after.