Writing Modular Class With Single Responsibility Principle

SRP by example

Mufti
3 min readSep 8, 2019
image from google

Quick References

Single Responsibility Principle is one of the SOLID principles, There are five SOLID principles and this part I will tell you about Single Responsibility Principle or commonly programmer call it SRP, The five principles are:

  1. Single Responsibility Principle — A class or module should have one, and only one, reason to be changed.
  2. Open/Closed Principle — Software entities … should be open for extension, but closed for modification
  3. Liskov Substitution Principle — Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program.
  4. Interface Segregation Principle — Many client-specific interfaces are better than one general-purpose interface.
  5. Dependency Inversion Principle — One should “depend upon abstractions, [not] concretions.

Description

Single Responsibility Principle (SRP) is a computer programming principle that states that every module, class, or function, should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class. The term was introduced by Uncle Bob in his book “Principles, Patterns, and Practices of Agile Software Development” he said that class should only have one reason to change.

God Object

SRP is closely related to the separate the concern of class or objects it means every class should have one job. Maybe when you first write your code you will write the “God Object”, God Object is object that knows everything, you write the God Object because you feel that your class is doing just one job.

Let’s see this example :

Above is an example program called “toll-gate” the program can open the gate when your balance is > 3000, the program can works properly, but let’s interrogate the class. is TollGate can read_card?, is TollGate can check_balance?, that is not TollGate responsibility, TollGate jobs is just open and close the gate we can delegate read card, check balance to other class.

Breaking Down Responsibilities

To break down the responsibilities of the class, we can start with interrogate the god object and then delegate to another class, on the above example we can create another class called sensor, and checker.

let’s see this code:

With this code, you make more modular class, and your class just have one responsibility, and when you interrogate your class, you will get the right answer to what the class do.

Benefits

From example code above we get some benefits from using SRP, the benefits are:

  • The class is easier to understand
    When the class only does “one thing”, usually has a small number of methods that are fairly self-explanatory.
  • The class is easier to maintain
    Changes are isolated, reducing the chance of breaking other unrelated areas of the software. As programming errors are inversely proportional to complexity, being easier to understand makes the code less prone to bugs.
  • The class is more reusable
    If a class has multiple responsibilities, and only one of those is needed in another area of the software, then the other unnecessary responsibilities hinder reusability. Having a single responsibility means the class should be reusable without modification.

So there are short of explanation and example of using SRP, I my article can be helpfull for you.

--

--