Blogs
Technology

What is Single Responsibility Principle?

Tuesday, Jan 08, 2019
Mohamed Ghoname

Development Manager

01 Posts

Single Responsibility is one of most important rules in Object Oriented Programming and the first rule in SOLID Principles that we should follow in OOP development. This article clarifies how to comply to the rule that states that a class should have single part of functionality.

SOLID Principles

Single Responsibility Principle: which one of SOLID principles that represents “S”which means every object should be responsible for only one thing.

Let’s take Customer class as an example so that in real life Customer class represent a real human. You expect customer has a first name, last name, date of birth and simply you can ask him/her directly about their age. All of those attribute together can be a part of customer’s class.

But what if you ask your real customer that please write his data into your database? Of course it doesn’t make any sense! So such activity cannot be a part of your customer class.

Let's check the below example to recognize the issue and then work on fixing it.

class Program

    {

        static void Main(string[] args)

        {

            Customer customer = new Customer(){FirstName = "CustFName",LastName = "CustLName",DateOfBirth = new DateTime(2000, 1, 1)};

            customer.Save();           

            Console.ReadLine();

        }

    }

class Customer

    {

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public DateTime DateOfBirth { get; set; }

        public double CalculateAge() => DateTime.Now.Subtract(DateOfBirth).TotalDays / 365;

         public override string ToString() => $"{FirstName} {LastName} is {CalculateAge()}";

         public void Save(string fileName="customer.txt")=> System.IO.File.WriteAllText(fileName, this.ToString());       

    }

The main issue here is that customer shouldn’t have Save method as the real customer doesn’t know how to save data into DB or even text file. This in real life, you (as business owner) should delegate the saving data operation to a different person who is your employee for example. And here you should delegate saving data to a different class who will be mainly responsible for that.

In the below example we will see how to fix this violation.

First: we will remove save method from customer class.

  class Customer

    {

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public DateTime DateOfBirth { get; set; }

        public double CalculateAge() => DateTime.Now.Subtract(DateOfBirth).TotalDays / 365;      

        public override string ToString() => $"{FirstName} {LastName} is {CalculateAge()}";       

    }

Second: Create a DataAccessObject class

    class DataAccessObject

    {

public void SaveCustomer(Customer customer, string fileName = "customer.txt") => System.IO.File.WriteAllText(fileName, customer.ToString());

}

Finally call DataAccessObject.SaveCustomer whenever you want to save customer

static void Main(string[] args)

        {

Customer customer = new Customer(){FirstName = "CustFName",LastName = "CustLName",DateOfBirth = new DateTime(2000, 1, 1)};

        DataAccessObject data = new DataAccessObject();

       data.SaveCustomer(cust);           

       Console.ReadLine();

        }

In this case the customer class is responsible only for holding customer’s data and DataAccessObject is only responsible for saving data into any data source.

Comments

No comments yet

Some of our features will not be working properly on IE. We recommend using this website from our supported browsers ex: Google Chrome, Firefox, Opera, Microsoft Edge