System Design ( Day - 36 )

SOLID Principles | SRP, OCP
Problems without SOLID Principles
1. Maintainability
2. Readability
3. Bugs
SOLID
S → Single Responsibility Principle ( SRP )
O → Open Close Principle ( OCP )
L → Liskov Substitution Principle ( LSP )
I → Interface Segregation Principle ( ISP )
D → Dependency Inversion Principle ( DIP )
S: Single Responsibility Principle
Definition: A class should do only one thing or A class should have only one reason to change.
For Example: A TV remote, it should control only the TV with its button, not ac not fan or not something else
If your class is doing some other functions also then there is too much dependency to that class, if you want to change anything in that method or upgrade that method then you have to come to that single class for everything, it’s tightly coupled with that class, that not good , it will not follow the first rule of SOLID principles.
like a TV Remote, it should only control the TV, if you integrate that remote into the AC, fan and refrigerator then it would be harder for the developer to handle everything in one class because if he wants to change some functionality in the AC then he has to go to the TV remote class to change, and that is tightly coupled with it, it should be like if we want to change something in the AC or fan then we have to go their class like AC class and Fan class to change or upgrade to avoid clutter in our code.
O: Open Close Principle
Definition: A class should be open for Extension closed for modification.
This means the class should open for adding new features, closed like it should not allow to modify the existing methods in that class.
For Example: Let’s say we have a class which is used to save the information to the Sql DB (SaveToSQL( )), we want to add a new feature like we want to add that same content to the MongoDB and in files also, for that if we add new methods to the same class then it will break the Open close Principle. we don’t have to add directly in to the same class, instead of doing that it in a same class we can use Abstraction, Inheritance and polymorphism to do.
For example if we want to add new methods or new features then, we have to create a Abstract class with method save( ) in it, then i have to create 3 classes which is SaveToSql, SaveToMongo, SaveToFile, these three classes will implement the abstract class, if these classes are implementing then it should implement the save( ) method in it, so we have to OverRide the Save ( ) method as per the feature of the class like inside SaveToSql class the Save ( ) method will save the data to the SQL Db, and inside the SaveToMongo class the save method will OverRide the method Save ( ) and implement its own feature in it, and so on.
by doing like this we can add as many features as we want, and it will follow the Open Close Principle.
and this code is easily testable and easy to debug and if we want to add another features to this then its also easily implemented.
Subscribe to my newsletter
Read articles from Manoj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
