Single Responsibility Principle (SRP) is the first amongst the five design principles stipulated by SOLID.
Single Responsibility Principle
A class should have one and only one reason to change, meaning that a class should only have one job.
Let’s understand better with an example
Lets take an example of a journal. We could write code to implement a personal journal as follows…
As shown in the code, our class Journal has methods to add and remove entries from it. So far our Journal class is behaving as required - with a single responsibility.
Now, consider the case where we want to extend this by being able to save the journal entries in a file (persist data). We’ll add the following 2 methods to the Journal class.
Now these 2 methods definitely help adding a persistence layer, however they violate the SRP principle. Here in addition to saving and removing entries from the Journal, the Journal has to also manage persistence. This means that there are more than one reasons for a change to this class which violates the SRP principle (as per which there should be only one reason for change in a class).
Hence, to adhere to SRP we can tackle the above in a different way.
We will create a new class Persistence as follows…
This class can now be instantiated and called from the main method as follows…
As is now evident, henceforth if any changes are required in the persistence layer it will not require the Journal class to be modified and hence SRP will not be violated.