I had a problem to understand this for a long long time! You can’t believe how hard is it when you don’t understand the principle and try to adapt it! Mission impossible! Recently I spent some time again to read more about OOP principles and first step of SOLID is ‘S’.. Single Responsibility introduced by Uncle Bob. So let’s break it down:
Responsibility: Reason to change!!!
Rephrase: One Reason to change!
Easy so what is a reason then? ACTOR!
OMG you just add more vocabulary!! What is Actor?
OK so we need to understand little a bit more of UML, Action means: An Actor models a type of role played by an entity that interacts with the subject (e.g., by exchanging signals and data), but which is external to the subject.
I don’t understand give me an example!!
Think below class as an example:
Employee class above has couple of functions. Is this class violating SRP? Yes! Why?
1. Load and Save functions, who will ask you to change them? DBAs
2. CalculatePay, CalculateHoliday functions, who will ask you to change them? Accountants
3. HoursWorked, HourlyRate functions, who will ask you to change them? Auditors
So three different “Actor”s exists in our example!
Now we understand that “Actor” and “Responsibility” is related to context. In some other context may be a class has one responsibility and in other context it has more than one. So for making your architecture and make sure you are not violating SRP you need to understand USE CASE and ACTOR and in REFACTOR process we can make decision about are we violating SRP!
Enjoy REFACTORING! And remember detecting violation SRP you need to understand user’s need and have good logic.