Press "Enter" to skip to content

Single Responsibility Principle in C# – SRP

single responsibility principle in c#

Hi, my friends! we are going on SOLID principle as we promise! Todays topic is “single responsibility principle”. This principle is also first character of SOLID!

Now, move on to article!

Lets make a definition: Single Responsibility principle says that “There must be one reason to change an object!” Of course, we will examine this sentence with a little more examples. But before that, by reversing our key phrase, let’s find out how to identify the object that does not comply with this principle: I have an object with many methods for different purposes.

The main purpose of these principles is to ensure the flexibility of your code. So if you are writing a project that is likely to grow continuously, your long-term code maintenance and easy-to-develop architecture application will make your work easier.

This principle, which we call SRP in short, says that every object should be responsible for only one job. Now, please bring a restaurant’s kitchen in front of you. The employees in this kitchen were asked to prepare a tuna salad. There are people working side by side on a long bench. In the first row, he cuts the tomatoes, then the other slices the cucumber. After the third person has washed the greens beautifully, the fourth person prepares pickles. The fifth person adds the tuna to the salad, and the sixth conveys the salad to the waiter.

So each of the chefs here has only one responsibility, right? Now let’s take a look at our key sentence: There must be only one reason to change an object . How many people will you say when you want to change the chopping shape of tomatoes? Aha! That’s it!

The task of a developer is to solve problems. These problems can be very complex sometimes. At this point, with the SRP approach, you can break the big problem into small pieces. Thus, by focusing on these mini problems, you can solve the big problem much more comfortably.

So, the application you developed would be like a clock’s working mechanism. Every module has one responsibility!

First, take a look not SRP principle code:

	
public class Pizza
    {
        public string Name { get; set; }
        public string Size { get; set; }
        public double Price { get; set; }
 
        
        public void AddMaterial(string material)
        {
            //do stuff
        }
        public List<string> ShowMaterials()
        {
            //do stuff
        }
        public void RemoveMaterial(string material)
        {
            //do stuff
        }
        public void Cook(int minutes)
        {
            //cook...
        }
        public void Dough(int size)
        {
            //do stuff
        }
 
 
    }

This code doing theese items:

  • Keeps certain values of the pizza object.
  • doing stuff with materials
  • cooking pizzas

What we said, each class should have only one responsibility. So, I have to separate these three responsibilities into other types.

Lets code:

public class Pizza
{
   public string Name { get; set; }

   public string Size { get; set; }

   public double Price { get; set; }
}
public class MaterialFunction
    {
        public void AddMaterial(string material)
        {
            //adding materials
        }

        public void RemoveMaterial(string material)
        {
            //removing material
        }

        public List<string> ShowMaterials()
        {
            //showing materials
        }
    }
public class PizzaFunctions
    {
        public void Cook(int minutes)
        {
            //Cooking pizzas
        }
        public void Dough(int size)
        {
            //do stuff
        }

        public MaterialFunction Materials { get; set; }


    }

Here! Now each class is only responsible for one job! The pizza class is completely independent of the other classes and only features the object!

Now think about it: to develop & maintain this class is easier, right? But more important thing is, it is easy reading!

You did see how SRP implemented to C# with an example code! Share this article with your developer friends!

Thanks for reading! Go home to see what new programmers memes!

Comments are closed.