The Single Responsibility Principle
Learn about the single responsibility principle (SRP) in software design.
We'll cover the following...
The single responsibility principle (SRP) states that a software component (in general, a class) must have only one responsibility. This means that it is in charge of doing just one concrete thing, and as a consequence of that, we can conclude that it must only have one reason to change.
We should only have to update the class if one specific thing in the domain problem changes. If we have to make modifications to a class for different reasons, it means the abstraction is incorrect and that the class has too many responsibilities. This might be an indication that there is at least one abstraction missing: more objects need to be created to address the extra responsibility that's overloading the class in question.
This design principle helps us build more cohesive abstractions—objects that do one thing, and just one thing, well, which follows the Unix philosophy. What we want to avoid in all cases is having objects with multiple responsibilities (often called God objects, because they know too much, or more than they should). These objects group different (mostly unrelated) behaviors, thus making them harder to maintain.
Again, the smaller the class, the ...