Single Responsibility Principle defines a responsibility as a reason to change, and concludes that a class or module shocould have one, and only one, reason to change.
SRP is an important design principle proposed by Uncle Bob. SRP emphasizes that each class or module has only one reason for change. However, the only reason for this change is not easy to understand. Many people think that a single responsibility means that every class should be designed as only one method to do only one thing. The key to understanding SRP is to understand the hierarchy of responsibilities. A good example of the SRP principle is automobile: as a whole, automobile has a single responsibility, and its responsibility is to meet the driving needs; the automobile is composed of the engine, chassis, tires and other components. Each component has a single responsibility. The engine is responsible for providing power, and the tire is responsible for rotating; further, the engine can be subdivided into lower-level parts. Each part is actually a single responsibility. Therefore, there is a hierarchy of responsibilities, and any system or object is responsible for its responsibilities. The responsibility of the system is to exist as a single and complete whole, rather than simply accumulating responsibilities at the subsystem level. This isResponsibilitiesPrinciple.
The hierarchy of duties is a kind of nature, but it does not explain the nature of duties. So what is the essence of responsibility? In my opinion, the responsibility is to satisfy the needs. Responsibility hierarchy comes fromDemand hierarchy. There are layers of requirements and users. Automobile Demand => engine demand => screw demand => steel demand is the hierarchy of demand; vehicle users => Car manufacturers => engine manufacturers => screw manufacturers =>... Is the user hierarchy. Therefore, the "reason for change" mentioned in SRP is demand change. The "only reason for change" implies the level of demand. If the level of demand corresponds to the level of responsibility, therefore, there is only one reason for the change of each system at its level.
What lies behind the hierarchy of duties is the hierarchy of needs. Where does the hierarchy of requirements come from? The hierarchy of requirements comes from design! For a car user, his needs are at the overall level of the car, and the design of the car parts is not in his concern. The engine, tires, and other requirements are derived from the design of the vehicle structure by the vehicle designers. This means that the design to meet the needs of the High-level system has resulted in the need of subsystems, the sub-system needs further develop the sub-system design, which isDesign hierarchy. Requirement => Design => requirement => Design ..., The requirement and design are as detailed as the egg and egg. We call this design methodResponsibility-driven design.
It should be pointed out that the hierarchy of responsibility-driven design is only one of several levels of system. There are many methods for system hierarchy. The traditional three-layer architecture is also a hierarchy. What are the differences between the levels of responsibility-driven design? For example, a company can be divided by project team and then divided into development and testing. Similarly, A company can also be divided into Development Departments and testing departments, and then departments can draw employees to form a project team. The diversity of the System Hierarchy Method is essentially determined by the diversity of the system. From the perspective of project, A person belongs to project A, A person belongs to test based on the type of work, and A graduate student based on the degree of education, gender: male. The responsibility-driven design uses functions as the first level of hierarchy. Each layer is a function cohesion! That is to say, the subsystems collaborate and Aggregate functions to achieve high-level responsibilities, or the subsystems are a function coupling relationship. In the previous example, the Division by project team is one way.Function cohesionBut not by development and test departments. A good system design should have the characteristics of high cohesion and low coupling. Functional coupling is considered to be the best coupling method. For example, the Socket class has functions such as Connect, Disconnect, Send, and Receive. They are coupled as a complete Socket communication function and are a highly cohesive method that is worth advocating.
The hierarchy of duties determinesTest hierarchy. Testing a car requires a screw test, a tire test, and a whole vehicle test. This makes everyone feel at ease. The role is hierarchical, the class is hierarchical, and the unit test of the class is hierarchical. Therefore, unit tests do not test the "screw" level as many people misunderstand. Although the unit test may not be able to reach the "vehicle" test level, it is possible to reach the "tire" test level at least. In addition to the hierarchy and outward expansion of unit testing, integration testing and acceptance testing form a larger test feedback chain. Each layer of testing will provide feedback to the system at this layer.Feedback hierarchy.
Starting with a single responsibility principle, the above section describes various levels of responsibility-driven design, including: Demand hierarchy, responsibility hierarchy, design hierarchy, test hierarchy, and feedback hierarchy. These different hierarchies have internal consistency because they all originate from the systematic hierarchy. Each hierarchy is the view that the multi-dimensional objects of the system are created from different dimensions. From the perspective of requirement, they are demand hierarchy, and from the perspective of test, they are test hierarchy. Therefore, the multi-dimensional and multi-level understanding of the system is the root cause of analysis and understanding.
The above is my superficial understanding of the System and Design. We welcome to criticize and correct the mistakes and shortcomings in this article!