TOP7. Single responsibility principle-object-oriented design principle

         Definition of the single responsibility principle

The advantages of the single responsibility principle

The realization method of single responsibility principle


The single responsibility principle is also one of the object-oriented design principles, and we will introduce it in detail below.

Definition of the single responsibility principle


The Single Responsibility Principle (SRP), also known as the Single Function Principle, was proposed by Robert C. Martin in "Agile Software Development: Principles, Patterns, and Practices." The responsibility here refers to the reason for the class change. The single responsibility principle stipulates that a class should have and only one reason for its change, otherwise the class should never be more than one reason for a class to change.

The principle states that objects should not take on too many responsibilities. If an object takes on too many responsibilities, there are at least the following two shortcomings:

  1. A change of responsibility may weaken or inhibit the ability of this class to fulfill other responsibilities;
  2. When the client needs a certain responsibility of the object, it has to include all other unnecessary responsibilities, resulting in redundant code or code waste.

The advantages of the single responsibility principle


The core of the single responsibility principle is to control the granularity of the class, decouple the objects, and improve their cohesion. If you follow the single responsibility principle, there will be the following advantages.

  • Reduce the complexity of the class. A class is only responsible for one responsibility, its logic is definitely much simpler than responsible for multiple responsibilities.
  • Improve the readability of the class. The complexity is reduced, and its readability will naturally increase.
  • Improve the maintainability of the system. With improved readability, it is naturally easier to maintain.
  • The risk caused by the change is reduced. Changes are inevitable. If the single responsibility principle is followed well, when one function is modified, the impact on other functions can be significantly reduced.

The realization method of single responsibility principle


The single responsibility principle is the simplest but the most difficult to apply. It requires designers to discover the different responsibilities of a class, separate them, and then encapsulate them into different classes or modules. The multiple responsibilities of the discovery category require designers to have strong analysis and design capabilities and relevant refactoring experience. The following uses the university student work management program as an example to introduce the application of the single responsibility principle.

[Example 1] University student work management procedures.

Analysis: University student work mainly includes two aspects of student life guidance and student academic guidance. Life guidance mainly includes class committee construction, attendance statistics, psychological guidance, fee collection, class management, etc. Academic guidance mainly includes professional guidance , Study guidance, scientific research guidance, study summary, etc. It is obviously unreasonable to entrust these tasks to a teacher. The correct approach is that the counselor is responsible for the life counseling and the academic counselor is responsible for the academic guidance. The class diagram is shown in Figure 1.
 

Class diagram of university student work management program


                                                                           Figure 1 Class diagram of university student work management program


Note: Single responsibility also applies to methods. A method should do one thing as best as possible. If a method handles too many things, its granularity will become very coarse, which is not conducive to reuse.

<Depending on the principle of inversion and                                                                                                                                                             interface isolation principle>

Guess you like

Origin blog.csdn.net/m0_38023584/article/details/106344428