SIGN UP MEMBER LOGIN:    
ARTICLE

Single Responsibility Principle with Example

Posted by Biswa Pujarini Mohapatra Articles | OOP/OOD August 25, 2011
Overview of Single Responsibility Principle with code Example.
Reader Level:

Overview of Single Responsibility Principle (SRP)
 
It states that every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class. There should not be more than one reason to change the class. This means class should be designed for one purpose only. This principle states that if we have 2 reasons to change for a class, we have to split the functionality in two classes. Each class will handle only one responsibility and on future if we need to make one change we are going to make it in the class which handle it. When we need to make a change in a class having more responsibilities the change might affect the other functionality of the classes. 

Intent

A class should have only one reason to change.

Example

In short, it says that a subsystem, module, class, or even a function, should not have more than one reason to change. The classic example is a class that has methods that deal with business rules, reports, and database: Below example shows how it violates the rule of single responsibility Principle. Employee class has designed to save,Modify and GetEmployeeInfo() records from Database but MAPEmployee() method is used for mapping Database columns with the Employee class  attributes which can be altered if there is any changes in DB

Public Class EmployeeService
{
public void SaveEmployeeInfo(Employee e)
{// To do something}
public void UpdateEmployeeInfo(int empId, Employee e)
{//To do Something

}
public Employee GetEmployeeInfo(int empID)
{// To do something
}

public void MAPEmployee(SqlDatareader reader)
{// To do something
}
}

We can segregate the classes into two classes which will implement only MAPEmployee, so that if there are any changes in DB no need to change EmployeeService class

Public Class EmployeeService
{
public void SaveEmployeeInfo(Employee e)
{// To do something}
public void UpdateEmployeeInfo(int empId, Employee e)
{//To do Something

}
public Employee GetEmployeeInfo(int empID)
{// To do something
}
}
Public Class ExtendEmployeeService:EmployeeService
{


public void MAPEmployee(SqlDatareader reader)
{// To do something
}
}

Conclusion

The Single Responsibility Principle represents a good way of identifying classes during the design phase of an application and it reminds you to think of all the ways a class can evolve. A good separation of responsibilities is done only when the full picture of how the application should work is well understand.

Login to add your contents and source code to this article
share this article :
post comment
 

The intent and conclusion section in this article are exactly same and seemed copied from http://www.oodesign.com/single-responsibility-principle.html. I feel that the orginal article at oodesign.com has the better example to understand the concept. Thanks.

Posted by knights Aug 26, 2011

Hi, I cannot agree more with the principle, but I think the example stated here is not a good use case for the principle, It is more applicable when we have classes like Game & Scorer They should be implemented as separate classes, because if scoring method change, it is not changing the game.. and it may happen that the Game changes but the scorer remains the same.. I have taken this example from http://www.objectmentor.com/resources/articles/srp.pdf

Posted by Kamal Rawat Aug 26, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor