Consider Actors for the Single Responsibility Principle (SRP)
Tags: architecture, design, solid, oop
Published: 2022-05-22 18:34:09

#Introduction
The Single Responsibility Principle is the S in [SOLID](http://www.butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod), it is defined as:

> Single Responsibility Principle(SRP) - A class should have one, and only one, reason to change.

The SRP is at many times misinterpreted as a method should  do one thing and one thing only, this is a wrong definition of the SRP.
When we think about methods or modules having single responsibility we can consider it is part of a [clean code](https://dev.to/caiocesar/clean-code-in-c-part-2-methods-58mb) concept and not how the SRP should be addressed.

Instead software developers needs to think about the users or group of users that will make use of a certain classes as seen in the following sections.

##Problem Definition

When applying the SRP software developers need to take into consideration the actors that will make use of the developed module. Consider Figure 1 and its actors.

| ![Student class diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wbiiowrh185w71k5lg30.png)|
|:--:|
|<b>Figure 1 - Student class diagram and its actors</b>|



There are three public methods contained in the student class:  

- The CalculateGradePointAverage is mostly used by the students.
- The GetAbsence is mostly used by the parent to check the status of the student absence.
-  The GetRanking is typically used by the school to print the student ranking compared to his peers in his report card.

>At this point all of these methods are coupled into a single Student class that is used by different groups of users.

| ![Shared private method](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4wz4a2cq759j553sa17u.png)|
|:--:|
| <b>Figure 2 - Shared private method</b>|


Figure 2 displays that the calculateGradeAverage private method is used by the  CalculateGradePointAverage and GetRanking. The developer considered creating this private method to prevent code duplication.

Lets now consider business changes in the school policies that will affect the ranking system logic. In this case changes to the calculateGradeAverage method is necessary to comply with the rules, however the changes to the calculateGradeAverage method also introduced changes to the CalculateGradePointAverage that should not have changed.

##Problem Resolution
This is where architectural decisions can come into place, since there may be different solutions to the same problem. Creating a separate class for each user is one solution as displayed in Figure 3. Also a common model between these classes could reduce code duplication.

| ![classes isolated](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hx40do3x30eu8iczzc8a.png)|
|:--:|
| <b>Figure 3 - Different classes isolated from each other</b>|


##Going Deeper
At this point we now have three classes that should be instantiated, if these classes are used in the same scope a facade could be introduced as displayed in Figure 4.

| ![Facade](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f8h3etmb44ywzyqvwcyc.png)|
|:--:|
| <b>Figure 4 - Facade pattern</b>|

The Student Facade class does not implement much code since it redirects to the classes responsible for each operation.
 

#Conclusion
The SRP is connected with methods and classes, when designing classes for different users or groups of users it is crucial to take into account the context and classes that are created to solve the users need and how these changes might impact code maintenance and business growth.



#References
1. Martin, Robert C. Clean Architecture