Solid Principles
Table of Contents
Solid Principles
SOLID is a mnemonic acronym for five design principles intended to make object-oriented designs more understandable, flexible, and maintainable. These principles were introduced by Robert C. Martin. They are a core part of Object Oriented Programming and software architecture.
Following these principles helps in reducing tight coupling between software components and encourages creating more cohesive, robust, and reusable code.
S - Single Responsibility Principle (SRP)
A class should have only one reason to change.
- Concept: Every class or module should have responsibility over a single part of the software's functionality. All the class's services should be narrowly aligned with that responsibility.
- Goal: This makes the class more robust. If you need to make changes to a specific function, you only need to go to one place. It also makes the code easier to understand and test.
O - Open/Closed Principle (OCP)
Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.
- Concept: You should be able to add new functionality to a class without changing its existing source code. This is often achieved through interfaces, abstract classes, or polymorphism.
- Goal: To extend a system's behavior without introducing bugs into existing, stable code.
L - Liskov Substitution Principle (LSP)
Subtypes must be substitutable for their base types without altering the correctness of the program.
- Concept: If you have a class B that is a subclass of A, you should be able to pass an object of class B to any method that expects an object of class A and the program should not break or behave unexpectedly.
- Goal: To ensure that inheritance hierarchies are well-designed and that polymorphism works as expected, leading to more reliable code.
I - Interface Segregation Principle (ISP)
No client should be forced to depend on methods it does not use.
- Concept: It's better to have many small, specific interfaces than one large, general-purpose one. Classes should only need to implement the methods that are relevant to them.
- Goal: To reduce the side effects of changing code. By splitting large interfaces, you create a more decoupled system where changes to one part are less likely to affect other parts.
- ependency Inversion Principle (IP)
High-level modules should not depend on low-level modules. Both should depend on abstractions (e.g., interfaces).
- Concept: This principle inverts the way you might typically think about dependencies. Instead of a high-level component (e.g., a business logic class) depending directly on a low-level component (e.g., a specific database connection class), both should depend on a shared abstraction (like an interface).
- Goal: To decouple components, making the system more flexible and easier to change. For example, you could swap your database from SQL to NoSQL without changing the high-level business logic. This is the foundation for dependency injection.