Introduction
The Single Responsibility Principle is a SOLID principle defined by Robert C. Martin. In principle, it says that an implementation (class/function) should perform only one task or implementation and it (class/function) should be changed for only one reason.
So in simple words, it says that whatever the developer implements (class/function) in code during development should perform only one task and the developer should only have one reason to change the implementation (class/function).
Wrong interpretation of the Principle
Most developers interpret this to mean that a class should perform only one task. But it's not only classes, functions you implement in code during development should also perform only one task. So one should interpret it as meaning that an implementation should perform only one task.
Real Life Example of not following Single Responsibility Principle
What happens when one can do more than one task? The following is an image of an example of it.

One can perform multiple tasks, there is no question of that, but it's not going to provide quality / better output.
So to get good quality/better output of work, one should do one task at a time.
Example of not following Principles in Application Development
In programming, in other words, when developing code as in the following, the Order class does not follow the principle.
Public class OrderManager
{
Public List < string > ValidateOrder()
{
//Code for validation
}
Public bool SaveOrder(OrderInfo order)
{
//Code for saving order
}
Public void NotifyCustomer()
{
//Code for notification
}
}
The preceding order class has the following responsibilities:
- ValidateOrder: Validating an order placed by the customer and return an error message if any
- SaveOrder: Saving an order placed by the customer and returns true/false
- NotifyCustomer: Notifies the customer order has been placed
Method not following the principle.
public int SumOfAllCustomerOrder(int customerId)
{
int sum = 0;
var query = “Select * from order where customerid = ” + customerid;;
//query orders
foreach(Order in OrderCollection)
{
If(Order.Items.Count > 5)
Sum += Order.Price;
}
return sum;
}
The preceding method has the following responsibilities.
- The method first all the orders
- It went through all orders in the collection and does some of that
Single Responsibility Principle
To make a class or function follow the Single Responsibility Principle, divide the responsibility by creating new classes or functions as in the following.

Karthik Muthu KaruppanPosted Apr 23, 2015, 11:22 AM
nice
Santhakumar MunuswamyPosted Apr 21, 2015, 9:41 AM
Good work
hicham Abu SoulaymanePosted Apr 21, 2015, 5:13 AM
Thank you ++
Saroj Kumar SahuPosted Apr 21, 2015, 2:49 AM
Nice one..
Prashant KoliPosted Apr 21, 2015, 2:23 AM
very nice article Pranay. 5/5 from me.
NitinPosted Apr 21, 2015, 2:00 AM
Nice
Gowtham RajamanickamPosted Apr 21, 2015, 1:06 AM
good on
Sam HobbsPosted Apr 20, 2015, 11:26 PM
Is the Single Responsibility Principle also Object Oriented Programming? Many objects do more than one thing, correct?