Click here to Skip to main content
Click here to Skip to main content

Software Architecture Cheat Blog 2: Single Responsibility Principle

By , 2 Oct 2012
Rate this:
Please Sign up or sign in to vote.

Define Principle

Single Responsibility Principle (SRP) states that there should never be more than one reason for class to change.

Violation Example

Lets consider this sample class Customer

public class Customer
    {
        public String Name { get; set; }
        public String Address1 { get; set; }
        public String City { get; set; }
        public String State { get; set; }
        public String Zip { get; set; }
        public String Email { get; set; }

        //Responsibility # 1 : save customer information
        public bool SaveCustomer()
        {
            if (ValidateCustomer())
            {
                //Save customer data
                SendWelcomeEmail();
            }

            return true;
        }

        //Responsibility # 2 : send email
        private void SendWelcomeEmail()
        {
            //send welcome email to customer
        }

        //Responsibility # 3 : validate customer data
        private bool ValidateCustomer()
        {
            //Check if the emaill is already registered

            return true;
        }
    }

While SRP principle states no class should contains more then one responsibility. Customer class itself  carries three responsibility, save data/validate data/email.  If further we want to add additional validation rule, or need to update email function to facilitate admin email we have to change the Customer class, thus violates SRP.

Resolution

To solve this We have to break down each class respect to responsibility.

    public interface ICustomerInfo
    {
        String Name { get; set; }
        String Email { get; set; }
    }

    public class Customer: ICustomerInfo
    {
        public String Address1 { get; set; }
        public String City { get; set; }
        public String State { get; set; }
        public String Zip { get; set; }
        public String Name { get; set; }
        public String Email { get; set; }        
    }

    public class Emailer
    {
        public void SendWelcomeEmail(ICustomerInfo customerInfo)
        {
            //send welcome email to customer
        }
    }

    public class CustomValidator
    {
        public bool ValidateCustomer(ICustomerInfo customerInfo)
        {
            //Check if the emaill is already registered
            return true;
        }
    }

    public class CustomerManager
    {
        ICustomerInfo _custInfo;
        public void SaveCustomer()
        {
            _custInfo = new Customer();
            if (new CustomValidator().ValidateCustomer(_custInfo))
            {
                new Emailer().SendWelcomeEmail(_custInfo);
            }
        }        
    }

This makes ICustomerInfo independent of the Customer class which makes it easier to maintain and extend. Emailer and Validation is now handle single responsibility and do not depend on entire customer object.Further we can modify validation rules, emailer logic’s as needed without effecting core Customer class.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Shahriar Iqbal Chowdhury/Galib
Technical Lead
Bangladesh Bangladesh
I am a Software Engineer and Microsoft .NET technology enthusiast. Professionally I worked on several business domains and on diverse platforms. I love to learn and share new .net technology and my experience I gather in my engineering career. You can find me from here
 
Personal Site
Personal Blog
FB MS enthusiasts group
About Me

Comments and Discussions

 
GeneralNot an article Pinmemberaspnet_regiis2-Oct-12 6:48 
AnswerRe: Not an article PinmemberShahriar Iqbal Chowdhury2-Oct-12 22:23 
GeneralMy vote of 1 Pinmemberaspnet_regiis2-Oct-12 6:47 
AnswerRe: My vote of 1 PinmemberShahriar Iqbal Chowdhury2-Oct-12 22:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 2 Oct 2012
Article Copyright 2012 by Shahriar Iqbal Chowdhury/Galib
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid