First, define
Single Responsibility Principle: one class should have only one reason for change
English name: single responsibility principle (SRP)
Next we will explain this single responsibility from the three books
These three books are respectively "in-depth Object-Oriented Analysis and Design", "Design Pattern Analysis", and "Agile development: Principles, patterns, and practices".
Object-Oriented Analysis and Design
First, his interpretation of SRP is slightly different:
Single Responsibility Principle: every object in the system should have a single responsibility, and all object services should focus on implementing this responsibility.
And he thinks
Cohesion is another name of SRP. If your class is highly cohesive, it means you are using SRP correctly.
Why should we emphasize the cohesion he mentioned here? In agile, he believes that if the class interface is not cohesive, it violatesInterface isolation principle
(In Baidu encyclopedia, he believes that a single responsibility is based on cohesion. Figure below)
Finally, he proposed a way to determine whether it is a single responsibility:
Write the following statement:
The Itself, where the first word write class name and the second word write method name. If there is a parameter, keep up with the parameter after the Method
For example, the modem is a light cat, which can be used to send, receive, and send data by dialing.
The method in "deep dive" is in line with a single responsibility model. But it does not match in Agile. Here we will talk about it later.
Next let's take a look at "Design Pattern Analysis"
Analysis of Design Patterns
He regards the objectSomething with responsibility. Each object contains a set of responsibilities. I have already said a lot about how to understand a class-single responsibility principle. I will not repeat it here.
The last one is to write agile books
Agile development: Principles, models, and practices
The book also said that it comes from cohesion, but in his understanding, the above example of a naked cat is against SRP.
He believes that dialing and hanging up belong to connection management, and receiving and sending belong to data communication.
We regard the former as the role a and the latter as the Role B. whether these two roles should be separated depends onAre the two frequently changing at the same time?.
If a's responsibilities change at the same time, the two roles do not need to be separated. Otherwise, they need to be separated.
- Assume that the two are changing at the same time, but they are separated.
The impact is that it is obviously not good for me to review the correct changes to the class where B is responsible when I modify the class where A is responsible.
- Assume that the two do not change at the same time and are not separated.
The impact is: (as mentioned in the book) Other methods that do not need to be changed need to be re-deployed and compiled, which will increase the number of deployments. On the other hand, I think they can change independently.
The final solution is to divide two independent changing responsibilities into the connection management class and data communication class, and then create a new optical cat class combination of these two classes, the external relies on the optical cat class to call.
Another example is MVC.
For example, from a certain point of view, the persistence (DAO layer) and service responsibilities of a person both belong to the person's responsibilities, but obviously they cannot be put together, the reason is that the persistence layer is almost unchanged, while the business layer is complicated and changeable. The persistence layer is highly reusable after isolation.
Finally, the appearance mode and proxy mode are mentioned. These two modes can be used to separate duties.
PSThe synchronous changes mentioned in Agile are not particularly emphasized in the other two aspects. They are relatively loose and there is no independent explanation in many places.Interface isolation principle.
Advanced one principle of responsibility-different insights and interpretations from multiple places