Single Responsibility (SRP) Definition:
There should be no more than one reason for class change. In layman's terms, a class is responsible for only one responsibility.
The origin of the problem:
Class T is responsible for two different responsibilities: Responsibility P1 and Responsibility P2. When class T needs to be modified due to changes in the requirements of responsibilities P1, it may cause the function of responsibilities P2 that was originally running normally.
Solution:
Follow the principle of single responsibility. Two categories T1 and T2 are established separately to enable T1 to complete the functions of responsibilities P1 and T2 to complete the functions of responsibilities P2. In this way, when class T1 is modified, the failure risk of responsibility P2 will not occur; similarly, when class T2 is modified, the failure risk of responsibility P1 will not occur.
ps:
When it comes to the principle of single responsibility, many people will disdain it. Because it's too simple. Even if a programmer with a little experience has never read the design pattern or heard of the single responsibility principle, he will consciously abide by this important principle when designing software, because this is common sense. In software programming, no one wants to cause other functions to fail because of modifying one function. The way to avoid this problem is to follow the single responsibility principle. Although the single responsibility principle is so simple and considered common sense, even programs written by experienced programmers will have code that violates this principle. Why does this phenomenon occur? Because of the spread of responsibilities. The so-called diffusion of responsibilities means that for some reason, responsibilities P are differentiated into smaller-grained responsibilities P1 and P2.
For example: Class T is only responsible for one responsibility P, so the design is in line with the principle of single responsibility. Later, for some reason, perhaps the requirements have changed, or perhaps the level of the program designer has improved, and responsibilities P need to be subdivided into more granular responsibilities P1 and P2. At this time, if the program wants to follow the principle of single responsibilities, class T needs to be decomposed into two categories T1 and T2, responsible for the two responsibilities P1 and P2 respectively. But when the program has been written, it is too time-consuming to do this. Therefore, it is a good choice to simply modify the class T and use it to take care of two responsibilities, although doing so is contrary to the principle of single responsibilities. (The risk of doing this is the uncertainty of the spread of responsibilities, because we would not think of this responsibility P, which may spread to P1, P2, P3, P4…Pn in the future. So remember to refactor the code immediately before responsibilities spread to a level beyond our control.)
C# code examples:
We use a sky class to describe the movement of an animal flying.
class Program { static void Main(string[] args) { Sky SkyExample = new Sky(); ("eagle"); ("sparrow"); (); } } public class Sky { public void fly(string name) { (name + "Flying in the sky"); } }
But if you look closely, you will find a problem. Not all animals will fly in the sky, such as dogs, which run on the ground.
So we need to modify it, but if we want to follow the principle of single responsibility, we need to add a new land class (Land) and provide a part of the running action (run).
class Program { static void Main(string[] args) { Sky SkyExample = new Sky(); ("airplane"); ("eagle"); ("sparrow"); Land LandExample = new Land(); ("pig"); ("dog"); ("horse"); (); } } public class Sky { public void fly(string name) { (name + "Flying in the sky"); } } public class Land { public void run(string name) { (name + "Running on the ground"); } }
It can be seen that this modification method does not change the original class, but adds a new class and adds a method. This means that it complies with the principle of single responsibility at the class level and also complies with the single responsibility in terms of method, because it does not move or affect the original code, but in the actual
During the development process, we often make the class more abstract (sky is changed to no longer the sky, but becomes Animal), while we need to provide multiple methods under the Animal class, which can fly, run, climb, etc., so that we will not rebuild a class to achieve our goal.
Obviously, this approach violates the principle of single duty, but in this wayMethod Level
The above is in line with the principle of single responsibility.
Advantages of following a single responsibility:
·It can reduce the complexity of a class. A class is responsible for only one responsibility, and its logic is definitely much simpler than being responsible for multiple responsibilities;
·Improve the readability of the class and improve the maintainability of the system;
·The risk caused by changes is inevitable. If the single responsibility principle is well followed, when one function is modified, the impact on other functions can be significantly reduced.
ps: One thing to be noted is that the principle of single responsibility is not only unique to object-oriented programming ideas, but also modular programming, the principle of single responsibility is applicable.
This is what this article about C# implements the single responsibility principle of the six major design principles. I hope it will be helpful to everyone's learning and I hope everyone will support me more.