
Single Responsibility Principle in SOLID
solid
6 Articles
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.
Table of Contents
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.
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.
Code Sample - SRP - Single Responsibility Principle - Before
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.
Code Sample - SRP - Single Responsibility Principle - After
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 this article we learn't what is SOLID
principles.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.