Blog

Filter posts by Category Or Tag of the Blog section!

Single Responsibility of Solid Principle

Saturday, 07 September 2013

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 Principal

Interface segregation principle

Dependency inversion principle

 

In this post, I'm going to dig into the 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 response is divided over more than one class, all classes part of that responsibility have to change. "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:

 

 public interface IUser
    {
        string FirstName { get; set; }
        string LastName { get; set; }
    }

    public class DisplayUsers
    {
        private IUser _user;

        public DisplayUsers(IUser user)
        {
            _user = user;
        }

        public string Display()
        {
            return String.Format("{1}, {0}", _user.FirstName, _user.LastName);
        }

        public static string Display(IUser user)
        {
            var displayUsers = new DisplayUsers(user);
            return displayUsers.Display();
        }
    }

    public class Customer : IUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
    }

 

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

 

  public interface IBanking 
    {
        void Deposit(Guid accountNo, decimal amount);
        void Withdraw(decimal amount);
    }

 

And implement the Banking class like this

 

 public class Bank
     {
        public static decimal Balance { get; set; }

        public void Deposit(Guid accountNo, decimal amount)
        {
            //Do the operation
            Balance += amount;
            // Save the changes
        }

        public void Withdraw(decimal amount)
        {
            //Do the operation
            Balance += amount;
            // Save the changes
        }
    }
 

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

 

Category: Software

Tags: Principle

comments powered by Disqus