The SOLID Principles of Object Oriented Design
Learn about SOLID principles and how to follow them in our code.
SOLID principles are the five principles that simplify our work by making code more flexible and maintainable. They have object-oriented programming in mind, but they are helpful with any kind of software engineering.
These principles are often discussed during job interviews, so make sure to remember and understand them.
These principles are:
- S – Single responsibility principle: A class should have only one reason to change.
- O – Open-closed principle: Objects or entities should be open for extension but closed for modification.
- L – Liskov substitution principle: Derived classes must be substitutable for their base classes.
- I – Interface segregation principle: Make fine-grained interfaces that are client-specific.
- D – Dependency inversion principle: Depend on abstractions, not on concretions.
Single responsibility principle
This principle states that there should be only one reason to change a class. It applies when the class is doing only one thing, hence the single responsibility name.
Here are a few practical ways to make sure we follow the single responsibility principle:
- Do not manage different entities in the same class: Imagine we’re writing a concert capacity management application and have a service with methods that manage