S.O.L.I.D: The First 5 Principles of Object Oriented Design
WHAT IS SOLID PRINCIPLES :-
1. SOLID principles is a coding standard through which we have a clear concept for developing software in a proper way to avoid bad design
2. It was introduced by Robert C martin
3. When we apply SOLID principles then it makes our code more easier to read
4. It helps us to achieve better testability of code by creating test classes of the classes.
5. This principle is an acronym of the five principles which is given below :-
A. SINGLE RESPONSIBILITY PRINCIPAL
1. One class should only serve one purpose. All the methods and properties should all work towards the same goal
2. So whatever the responsibility of the class it should handle that responsibility only
3. For example : If a class is creating to retrieve the data from the table so it should do the retrieve operation only. It should not perform the other operations in that class. If they want to perform other operations then they have to create other class for that
B. OPEN CLOSED PRINCIPAL
1. Software entities such as classes, functions etc should be open for extension but closed for modification
2. Any new functionality should be implemented by adding new classes and methods instead of changing the current ones OR existing ones
3. For example : If existing functionality is already written to select the data from a particular table and we have to add one more functionality to delete the data from the table then instead of modifying that in existing class we will create new class to add that new functionality OR just we with the help of inheritance we will inherit the existing Functionality and write our own Functionality in derived class
4. Or we can understand like this like one function is already there in order to add a new functionality we can extend that function but we cannot modify that that is why it said like this open for extension but closed for modification.
C. LISKOV SUBSTITUTION PRINCIPLE
1. Derived types must be completely substitute for their base types
2. This principal is extension of the open close principal
3. For example : two classes are there first is animal and second is dog so we can create the object like below
4. Animal obj = new dog(); - Because dog should have all the fields for animal class
5. But we cannot create object like below
6. Dog obj = new animal();
7. Similarly like another example is there : Two classes are there first is employee and second is temporary employee so we can create the object like this Employee obj = new Temporary employee(); but not like this Temporary employee obj = new Employee();
D. INTERFACE SEGREGATION PRINCIPAL
1. A client should not be forced to implement an interface that it doesn't use
2. This rule means that we should break our interfaces in many smaller ones so they better satisfy the exact needs of our clients
3. For example : one interface class is created in which two abstract functions are declared one is to select the data from database and another is to delete the data from table
4. One class is created in which we have implemented the above interface two methods which is fine
5. But I have created the other class in which I need only to select the data from DB but if I will not implement the second function then it will give error to me because that function is abstract function
6. So the solution is for second class we will create the seperate interface in which I will create abstract function and implement in that class then that will be fine
E. DEPENDENCY INVERSION PRINCIPLE
1. High level modules should not depends on low level modules.Both should depend on abstraction
2. The interaction between high level and low level modules should be thought as an abstract interaction between them
3. For example : Two classes are there first one is car and second is Scorpio, Scorpio class has one function drive which car class is using but suppose that if sometime Scorpio function drive become change then it will create the problem for class car. So the solution for this is to create a interface between them and use that interface in child class so whenever Scorpio function be change then it should change in interface also