interviews.dotnetthread.com

↑ Grab this Headline Animator

Wednesday, November 12, 2008

Single Responsibility Principle

Generally Developers will follow the object oriented concepts for rich the application performance like reusability,polymerphism,encapsulation etc. features implemented using classes.In the object oriented for the riching design principle we have SIGNLE RESPONSIBILITY PRINCIPLE

The concept of SIGNLE RESPONSIBILITY PRINCIPLE is
choosing a single, clearly defined goal for any particular bit of code. Do One Thing. That much is clear. But in choosing one thing, you are ruling out an infinite universe of other possible things you could have done. Curly's Law also means consciously choosing what your code won't do. This is much more difficult than choosing what to do, because it runs counter to all the natural generalist tendencies of software developers. It could mean breaking code apart, violating traditional OOP rules, or introducing duplicate code. It's taking one step backward to go two steps forward.Each variable, each line of code, each function, each class, each project should Do One Thing.

More clearly about SIGNLE RESPONSIBILITY PRINCIPLE is:
a responsibility is considered to be one reason to change. 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.

The Single Responsibility Principle is a simple and intuitive principle, but in practice it is sometimes hard to get it right.

Intent
A class should have only one reason to change.


Let us Explain the above things with Examples.

EXAMPLE:1
Let's assume we need an object to keep an email message. We are going to use the IEmail interface from the below sample. At the first sight everything looks just fine. At a closer look we can see that our IEmail interface and Email class have 2 responsibilities (reasons to change). One would be the use of the class in some email protocols such as pop3 or imap. If other protocols must be supported the objects should be serialized in another manner and code should be added to support new protocols. Another one would be for the Content field. Even if content is a string maybe we want in the future to support HTML or other formats.

If we keep only one class, each change for a responsibility might affect the other one:

Adding a new protocol will create the need to add code for parsing and serializing the content for each type of field.
Adding a new content type (like html) make us to add code for each protocol implemented.


// single responsibility principle - bad example
interface IEmail
{
public void setSender(String sender);
public void setReceiver(String receiver);
public void setContent(String content);
}

class Email implements IEmail
{
public void setSender(String sender) {// set sender; }
public void setReceiver(String receiver) {// set receiver; }
public void setContent(String content) {// set content; }
}



We can create a new interface and class called IContent and Content to split the responsibilities. Having only one responsibility for each class give us a more flexible design:

adding a new protocol causes changes only in the Email class.
adding a new type of content supported causes changes only in Content class.


// single responsibility principle - good example
interface IEmail
{
public void setSender(String sender);
public void setReceiver(String receiver);
public void setContent(IContent content);
}
interface Content
{
public String getAsString(); // used for serialization
}
class Email implements IEmail
{ public void setSender(String sender) {// set sender; }
public void setReceiver(String receiver) {// set receiver; }
public void setContent(IContent content) {// set content; }
}


EXAMPLE:2:

Suppose there is an employee class have three methods like

Class Employee
{
public Money calculatepay()
public void save()
public string reportHours()
}

This class violates the SRP because it has three reasons to change:

1.The business rules having to do with calculating pay.
2.The database schema.
3.The format of the string that reports hours.

We don't want a single class to be impacted by these three completely different forces. We don't want to modify the Employee class every time the accounts decide to change the format of the hourly report, or every time the DBAs make a change to the database schema, as well as every time the managers change the payroll calculation. Rather, we want to separate these functions out into different classes so that they can change independently of each other.

Submit this story to DotNetKicks

No comments:

Post a Comment

Post your comments/questions/feedback for this Article.

 

Latest Articles