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!

 


Tags: C# Principle


comments powered by Disqus