S is for the Single Responsibility Principle
The "S" in
SOLID stands for Single Responsibility Principle, and it’s probably the
principle that gets the most bad press.
It states that each
class should have just one responsibility.
My take on this is
that this principle is another way of saying Keep It Simple Stupid (KISS).
To quote Robert Martin
himself:
A class should have
one, and only one, reason to change.
One of Uncle Bob's
favourite examples is "an “Employee” class that calculates the pay and
saves the employee data to a persistence store. If there's a change required in
how pay is calculated, the class will have to be changed. If the DBA makes some
changes to the database schema, the class will probably have to change as
well."
Here the
"Employee" class has two responsibilities which violates the SRP. One
is business logic related and the other data infrastructure related. According
to the Single Responsibility Principle, these concerns shouldn’t reside in the
same object.