Single responsibility principle (SRP)

Hello friends, In this article we are going to learn, most popular design and pattern SOLID by back to back articles. Today we are going to learn first letter of SOLID that is S. S stands for Single responsibility principle.

Before understanding Single responsibility principle pattern. Lets fast look over SOLID. Because SRP is first principle of SOLID design and pattern.

If you are going to create a good application architecture. You must know about SOLID design and pattern first.

SOLID is an acronym for the five object-oriented design(OOD) principles by Robert C. Martin, popularly known as Uncle Bob.

S stands for SRP (Single responsibility principle)

O stands for OCP (Open closed principle)

L stands for LSP (Liskov substitution principle)

I stands for ISP (Interface segregation principle)

D stands for DIP (Dependency Inversion Principle)

When we combine all these five principles together, They makes developer feel comfortable for software development. Developer can easily maintain software and extend it well when needed. We will learn all these five principles one by one in details and fast track.

Today we are going to learn first principle of SOLID, Single responsibility principle (SRP). In next coming article we will learn second principle i.e. Open closed principle.

SRP says that a class must have only one responsibility and not more than one or multiple.

Suppose we are planing to create a Customer class for Bank Customers performing action to create or add new customer for bank in database by calling BAL(Business Access Layer) DAL(Data Access Layer) methods and if any error occurs control will transfer to catch block and perform logging error as follows.

class Customer { public void AddCustomer() { try { // Business logic and database logic goes here. } catch (Exception ex) { System.IO.File.WriteAllText(@"c:\ErrorLog.log", ex.ToString()); } } }


So we have a class having two responsibilities one Adding new customer account and logging errors.

Moreover, suppose we plan to implement third party logger like elmah so we need to modify customer class code.


If we create two independent classes one for Customer and other only for error log service.

It will make code so simple, understandable and easy to maintain. If one class is modify it will not affect the other class as follows.

Thats only why SRP says a class must have only one responsibility.

//ErrorLogger.cs public static class ErrorLogger { public static void ErrorHandler(string error) { System.IO.File.WriteAllText(@"c:\Error.txt", error); } } //CustomerService.cs class Customer { public void AddCustomer() { try { // Business logic and database logic goes here. } catch (Exception ex) { ErrorLogger.ErrorHandler(ex.ToString()); } } }

In next article we will learn Open closed principle (OCP)

If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.

Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.



Comment By: {{c.Author}}  On:   {{c.CreatedDate|date:'dd/MM/yyyy'}} / Reply