In software development one of the most important concepts for writing easily understood, modifiable code, and reusable code is SOLID.
SOLID stands for:
- Single Responsibility Principle
- Open Closed Principle
- Liskov Substitution Principle
- Interface Segregation Principle and
- Dependency Inversion Principle.
Single Responsibility Principle
The Single Responsibility Principle states that each class should only have one responsibility – meaning it should only do one thing.
A responsibility is a reason to change. Each class should only have one reason to change. By grouping together functions that needs to change for the same reason we can separate out the things that need to change.
Open Closed Principle
Software entities should be open for extension but closed for modification.
The idea is that you should be able to add new functionality without changing to existing code. When the Open/Closed Principle was originally created it used inheritance which can cause tight coupling and other problems.
Instead of using inheritance we should use polymorphism to adhere to this principle. Instead of a superclass we should use interfaces because the implementation can then change without needing to change the code that uses it.
Liskov Substitution Principle
Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program. Put another way, a superclass should be able to be replaced without breaking the subclasses.
The Liskov Substitution Principle is really similar to the design by contract concept that Bertrand Meyer defined.
Interface Segregation Principle
Small client specific interfaces are better than one general-purpose interface.
Dependency Inversion Principle
The dependency inversion principle is an approach for producing loosely coupled software modules. Use interfaces instead of concrete implementations.
If followed correctly, it should result in high level modules that are independent of the low level module implementations.
Wrapping It Up
In this post, we covered the SOLID principles that are based on work from Bertrand Meyer and Robert Martin.
SOLID stands for:
- Single Responsibility Principle
- Open Closed Principle
- Liskov Substitution Principle
- Interface Segregation Principle and
- Dependency Inversion Principle.