SOLID Principles: Part 1
SOLID stands for :
Single Responsibility Principle
0pen Closed Principle
Liskov Substitution Principle
Interface Segregation Principle
Dependency Inversion Principle
So, starting from the first one:
Single Responsibility Principle:
The name itself suggests that the class should be having one and only one responsibility. What does it mean? Well, let’s take the class A which does the following operations.
- Open a database connection
- Fetch data from the database
- Write the data in an external file
The issue with this class is that it handles a lot of operations. Suppose any of the following change happens in future.
- New database
- Change in the output structure
So in all the cases, the above class would be changed. Which might affect the implementation of the other two operations as well. So ideally according to SRP, there should be three classes each having the single responsibility.
Open Closed Principle
This principle suggests that classes should be open for extension but closed for modification. For Example, If class A is written by the developer AA, and if the developer BB wants some modification on that then developer BB should be easily doing that by extending class A but not by modifying class A.