SOLID
Single Responsibility, Open–Closed, Liskov Substitution, Interface Segregation, Dependency Inversion
In short
SOLID is a set of five object-oriented design principles that help developers write code that is easier to understand, extend, test, and maintain.
What are the SOLID principles?
SOLID is an acronym for five guidelines for designing classes and modules, popularized by software engineer Robert C. Martin in the early 2000s. Each letter stands for one principle, and together they aim to keep code flexible, so new features can be added without breaking existing ones. They are most often discussed in object-oriented programming, but the ideas apply to many kinds of code.
S is the Single Responsibility Principle: a class should have only one reason to change. O is the Open–Closed Principle: code should be open for extension but closed for modification, meaning you add new behavior by writing new code rather than editing working code. L is the Liskov Substitution Principle: a subclass should work anywhere its parent class is expected.
I is the Interface Segregation Principle: several small, focused interfaces are better than one large interface that forces classes to implement methods they don't need. D is the Dependency Inversion Principle: high-level code should depend on abstractions, such as interfaces, rather than on concrete details like a specific database class.
An analogy is a well-organized toolbox: each tool does one job, new tools can be added without modifying the old ones, and any screwdriver of the right size fits the same screw. SOLID principles are guidelines, not strict laws; applying them too rigidly can produce many tiny classes and unnecessary layers of abstraction.
Key takeaways
- S: Single Responsibility, one reason to change.
- O: Open–Closed, extend behavior without editing existing code.
- L: Liskov Substitution, subclasses must be safely interchangeable with their parents.
- I: Interface Segregation, prefer small, focused interfaces.
- D: Dependency Inversion, depend on abstractions, not concrete classes.
Example
// Depend on an interface, not on a concrete class
interface Notifier {
send(message: string): void;
}
class EmailNotifier implements Notifier {
send(message: string) { console.log("Email:", message); }
}
class OrderService {
constructor(private notifier: Notifier) {} // any Notifier works here
placeOrder() { this.notifier.send("Order placed"); }
}
new OrderService(new EmailNotifier()).placeOrder();Readers ask
What does SOLID stand for?
SOLID stands for the Single Responsibility, Open–Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion principles.
What is the Single Responsibility Principle?
The Single Responsibility Principle says a class or module should have only one reason to change, meaning it is responsible for one part of the system's behavior. For example, a class that both calculates invoices and sends emails should usually be split in two.
Do SOLID principles apply outside object-oriented programming?
Yes. The ideas behind them, such as small focused units and depending on abstractions, are useful in functional and modular code too, even though the wording comes from object-oriented design.
See also
- OOPProgramming Fundamentals, p. 40OOP, or object-oriented programming, is a way of structuring code around objects that bundle related data together with the functions that act on that data.
- Design PatternSoftware Architecture, p. 13A design pattern is a proven, reusable solution to a common problem in software design, described as a general template rather than as finished code.
- RefactoringSoftware Architecture, p. 33Refactoring is the process of restructuring existing code to make it cleaner and easier to maintain without changing what the code does from the outside.
- Technical DebtSoftware Architecture, p. 45Technical debt is the future cost of extra work created when developers choose a quick or limited solution now instead of a better approach that takes longer.
- Dependency InjectionSoftware Architecture, p. 12Dependency injection is a design technique in which an object receives the other objects it needs from the outside instead of creating them itself.
- DRYSoftware Architecture, p. 16DRY is a software design principle stating that every piece of knowledge or logic should have one authoritative representation instead of being duplicated.
- CohesionSoftware Architecture, p. 7Cohesion is a measure of how closely the responsibilities inside a module, class, or service belong together, and high cohesion is a sign of good design.
Spotted a mistake or something missing on this page?Suggest an edit