SOLID Design Principles Explained: The Single Responsibility Principle

Irmak Esin
3 min readJun 6, 2021

Hi everyone, in this article I will talk about the single responsibility from SOLID principles. Then let’s see what single responsibility principle is. As its very name signifies, it is a principle designed that each class has a single responsibility for its method. Simply, our classes should have a single and well-defined responsibility. A class (object) can be changed for only one purpose, that purpose is the responsibility assigned to that class. So, a class has only one responsibility to do.

With this picture, everything will become clearer and visualize in our minds. You can see the class in the first picture has multiple responsibilities, so do you think this is an effective way? Imagine you want to replace one of these responsibilities. For example, we no longer want a driver but an engineer. Guys, this is where the single principle responsibility becomes important.

When our requirements change, we will need to make changes to the entire class, since we assign multiple responsibilities to one class. Why should other three responsibilities be affected although they are irrelevant with the change? If we split the responsibilities in a purposeful way, we should have no more such problems.

Like in this software world; a class changes as much as the responsibility it takes. Thus, while causing many pieces of code to change, in rewriting; changes become difficult to implement. Thus, if we summarize the benefits of single principle responsibility in 3 articles:

1- The class is easier to understand

2- The class is easier to maintain

3- The class is more reusable

Let’s take a look at a simple code snippet to understand better. Starting with a bad example will be more clear :)

The DeployingService class above handles the responsibilites of both deploying and checking if the user is authorized. Do you think it is effective that the control of the authorized person is in the deployingService class? Let’s create a new class for this control.

DeployingService Class
EmployeesService Class

So, we implemented single responsibility principle simply! In this case, both classes have their own responsibilities. Then, it will be easy to update the code and the case of chance for any place in the code to break the other will be reduced.

I hope this article was useful for you :) Don’t forget, Clean code = Happy team

Avoid from spaghetti coding!

Resources:

--

--

Irmak Esin

🖥️ Front-End Developer | Interested in React & JavaScript