‘S’OLID — Single Responsibility Principle

mayank bansal
4 min readSep 25, 2020

What is Single Responsibility Principle ?

It states that every module, class or function in a computer program should have responsibility over a single part of that program’s functionality.
In other words, “A class should have only one reason to change”.

Let’s take an example:

Once upon a time, there lived a man, a shopkeeper, who opened a small grocery store. He hired two employees to handle the customers. He himself managed the orders and the payment transactions.

For the initial stock, he bought

  • Milk Packets
  • Whole Wheat Bread
  • Chocolates
  • Eggs
  • Rice
  • Oil
  • Beer
  • Cheese
  • Chips
  • Cereals
  • Shampoo/ Soaps/ Body Lotions

The shopkeeper did not have any idea about Single Responsibility. He had a single shelf for the storage of groceries. He told his employees to add the items in the storage area as soon as the stock arrives. The storage looked something like this —

Grocery Storage without partition

Employees used to search for the items one by one. To get the two packets of milk, they had to search twice, i.e., getting each milk packet one by one.

With the increasing number of demands, customers started ordering multiple packets belong to a single item. This led the Shopkeeper to maintain the stock also. He stored the count of every item so his employees wouldn’t spend time searching for the item that is not available.

To maintain the count of each item,

  • We need to change the data structure for the storage. Instead of List, we will use Map now.
  • We need to change the method arguments also.

Problem:

Whenever the customer places an order, both the employees start looking for the items in the storage. To search for an item, it takes a lot of effort. This resulted in the loss of customers. Customers were not satisfied with the shopkeeper because he was taking time in dealing with them.

As there are different size of packets for some items, like for-

  • milk — packet can be of half a litre or 1 litre or 2 litres, etc
  • rice — packet can be 1 kg, 5 kgs, 10 kgs, etc
  • shampoo — 200gm bottle, 500gm bottle, 2kg bottle
  • eggs — either complete tray or count of eggs required, etc

It is difficult to find the exact packet size required. It is also difficult to manage the stock of different types of packet size.

Solution:

We can have single responsibility for each type of item, i.e., a single shelf for each category of items. So it would be easy to search for the required item. And also to store the description of the items.

Each shelf has its own responsibility of adding or searching the corresponding items. Now it is easy to search for an item and easy for the employees. Customers are also happy as they do not have to wait for their orders.

Item Model

Rice Model requires gram also to describe the packet size.

Rice Model

Chocolate Model does not require any extra param instead we can use cost as the factor.

Chocolate Model
Shelf

Different types of chocolates can be fetched basis the cost of the chocolate. Customers usually ask to give four chocolate of 10 bucks.

ChocolateShelf

Different types of rice packets can be identified on basis of the weight of the packed in gram. 5 kg of rice packet is more common.

RiceShelf
Grocery Storage

ChocolateShelf has the responsibility of all the functioning related to the chocolates. RiceShelf has the responsibility of all the functioning related to the rice. Similarly, we can have a different class for each type of item.

Conclusion:

Single Responsibility Principle (SRP) should always be considered in mind while writing the code. Before starting the implementation, we should always consider what scope can be added in the future as we expand. It is difficult to manage everything if we have all the code in a single file. We should divide the responsibility from scratch.

We should divide the class as soon as we encounter multiple reasons to change the Class. A ChocolateShelf should not have the responsibility of adding Rice in it or vice-versa.

Hope you all enjoyed reading it. Thanks for reading folks.!!!

--

--