👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Single Responsibility Principle in SOLID

Single Responsibility Principle in SOLID

Author - Abdul Rahman (Bhai)

SOLID

6 Articles

Improve

Table of Contents

  1. What we gonna do?
  2. Why we gonna do?
  3. How we gonna do?
  4. Summary

What we gonna do?

In this article, let's learn about Single Responsibility Principle in SOLID Principles in .NET.

Note: If you have not done so already, I recommend you read the article on SOLID Principles Introduction.

The Single Responsibility Principle (SRP) is a fundamental principle of SOLID. The SRP states that a class should have only one responsibility or one specific functionality. The reason behind this principle is to make the code more modular, maintainable, and reusable. When a class has multiple responsibilities, it becomes difficult to modify or test the code. If a class has many responsibilities, it's time to split into smaller ones. Breaking down the code into smaller, focused classes makes it easier to understand and maintain. This principle applies not only to classes but also to components and microservices.

Why we gonna do?

Without the Single Responsibility Principle, classes tend to accumulate multiple concerns over time. A class that manages data, handles business logic, and formats output all at once becomes difficult to understand, test, and modify. A change to one responsibility risks breaking unrelated functionality, leading to fragile code and unexpected bugs.

Following SRP keeps each class focused and purposeful. When a bug appears or a requirement changes, you know exactly where to look and what to change. Teams can work in parallel on separate classes without conflicting, and individual classes are small enough to be unit-tested in isolation with minimal setup.

How we gonna do?

When to apply SRP?

When you're coding, always ask yourself what the responsibility of the class is, and if it has multiple responsibilities with AND, consider breaking it up into smaller ones.

Demo

Let's take an example of a Circle class to understand the Single Responsibility Principle. Suppose we have a Circle class that has two functions, CalculateArea and CalculateCircumference which are used to calculate the area and circumference of the circle. But, if we look a little closer, there's another function called PlotCircleOnCanvas. Doing something on a canvas is a different responsibility and is not directly related to the circle. In essence, this function does not belong to this class.


class Circle
{
    public double Radius { get; set; }

    public Circle(double radius)
    {
        Radius = radius;
    }

    public double CalculateArea()
    {
        return Math.PI * Radius * Radius;
    }

    public double CalculateCircumference()
    {
        return 2 * Math.PI * Radius;
    }
    
    public void PlotCircleOnCanvas(Canvas canvas)
    {
        // This does not belong here !!
    }
}
            

To apply the Single Responsibility Principle, we will create a new class called PlotOnCanvas to contain the functionality of plotting on a canvas. We will cut the PlotCircleOnCanvas function from the Circle class and paste it into the PlotOnCanvas class. Now we will rename to PlotCircle to indicate that it's plotting a circle. We can also add another method PlotSquare to plot a square.


class Circle
{
    public double Radius { get; set; }

    public Circle(double radius)
    {
        Radius = radius;
    }

    public double CalculateArea()
    {
        return Math.PI * Radius * Radius;
    }

    public double CalculateCircumference()
    {
        return 2 * Math.PI * Radius;
    }
}

class PlotOnCanvas
{
    public void PlotCircle(Canvas canvas)
    {
        // Plot circle on canvas
    }
    
    public void PlotSquare(Canvas canvas)
    {
        // Plot Square on canvas
    }
}
            

Advantages

The Single Responsibility Principle has several advantages, including:

  • Modularity: Breaking down code into smaller, focused classes makes it more modular and easier to understand.
  • Maintainability: When a class has only one responsibility, it's easier to modify or test the code without affecting other parts of the system.
  • Reusability: Smaller classes with specific functionality can be reused in other parts of the system.
  • Scalability: By keeping classes focused on one specific functionality, it's easier to scale the system without worrying about affecting other parts of the system.

Summary

In conclusion, the Single Responsibility Principle is an important concept in the SOLID principles of object-oriented programming. It states that every class, module, component, or microservice should have one specific responsibility or functionality. This makes the code more readable, flexible, and maintainable. When you're coding, always ask yourself what the responsibility of the class is, and if it has multiple responsibilities, consider breaking it up into smaller ones.

In the C# example provided, we saw how the Circle class had a function that was not directly related to the circle's functionality, and therefore violated the Single Responsibility Principle. We created a new class called PlotOnCanvas to handle the plotting functionality, and moved the relevant function from the Circle class to the new class. This helped us adhere to the Single Responsibility Principle and improved the overall design of our code.

👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
  • SOLID
  • SRP
  • Single Responsibility