Ehsan Ghanbari

Experience, DotNet, Solutions

Single Responsibility of Solid Principle

SOLID stands for five basic principles of Object Oriented programming to create a system that is easy to maintainable and  extensible. The principles of SOLID are guidelines to create refactored and clean code understandable by other developers. Here are the five principles of OOP:

 

Single responsibility principle

Open-Close Principle

Liskov Substitution Principl

Interface segregation principle

Dependency inversion principle

 

In this post I'm going to dig in Single responsibility principle.

                                                     "there should never be more than one reason for a class to change."

The single responsibility principle states that every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class. Single Responsibility principle says that every object should have a single responsibility, if a class has more than one responsibility these responsibilities become coupled and cannot be executed independently.

When a class has more than one responsibility, there are also more triggers and reasons to change that class. If a class has more than one responsibility, then the responsibilities become coupled. Also when a responsibility is divided over more than one class, all classes part of that responsibility have to changed. "Always try to give every class its own and unique responsibility. It would be a bad design to couple two things that change for different reasons at different times." What's important is that responsibilities are separated by abstraction and therefore can be implemented by different consumers of that interface.

 

Take a look at this sample code:

 

  1.    public interface IUser
  2.     {
  3.         string FirstName { get; set; }
  4.         string LastName { get; set; }
  5.     }
  6.  
  7.     public class DisplayUsers
  8.     {
  9.         private IUser _user;
  10.  
  11.         public DisplayUsers(IUser user)
  12.         {
  13.             _user = user;
  14.         }
  15.  
  16.         public string Display()
  17.         {
  18.             return String.Format("{1}, {0}", _user.FirstName, _user.LastName);
  19.         }
  20.  
  21.         public static string Display(IUser user)
  22.         {
  23.             var displayUsers = new DisplayUsers(user);
  24.             return displayUsers.Display();
  25.         }
  26.     }
  27.  
  28.     public class Customer : IUser
  29.     {
  30.         public string FirstName { get; set; }
  31.         public string LastName { get; set; }
  32.         public string Email { get; set; }
  33.     }

 

You can see that every class and interface just could be changed for one reason,for example DisplayUsers class just Displays the users. Let's assume that you are going to implement banking, based on SRP you have to create another interface

  1.     public interface IBanking 
  2.     {
  3.         void Deposit(Guid accountNo, decimal amount);
  4.         void Withdraw(decimal amount);
  5.     }
  6.    

And implement the Banking class like this

  1.      public class Bank
  2.      {
  3.         public static decimal Balance { get; set; }
  4.  
  5.         public void Deposit(Guid accountNo, decimal amount)
  6.         {
  7.             //Do the operation
  8.             Balance += amount;
  9.             // Save the changes
  10.         }
  11.  
  12.         public void Withdraw(decimal amount)
  13.         {
  14.             //Do the operation
  15.             Balance += amount;
  16.             // Save the changes
  17.         }
  18.     }
 

There is a great example here that you can use it, thanks for reading!

 

About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns. I spend a lot of time on software architecture. Since 2008, I've been as a developer for different companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the past years, Read More

Post Tags
Pending Blog Posts
using Elmah in asp.net MVC4
Using FluentSecurity in MVC
Strategic design
Factory Pattern
time out pattern in ajax
Redis as a cache server
How to use PagedList In asp.net MVC
Multiple submit buttons in asp.net MVC
Domain driven design VS model driven architecture
What's the DDD-lite?
comments powered by Disqus