Design Mode - Single Responsibility Principle seven principles of (four)

Definition of the principle of single responsibility

  Single Responsibility Principle (Single Responsibility Principle, SRP), also known as single responsibility principle, by Robert · C Martin (Robert C. Martin) in. "Agile Software Development: Principles, Patterns and Practice," a book made. Responsibility here is the reason for the change of class, single responsibility principles laid a class should have one and only one cause why it changes, otherwise the class should be split (There should never be more than one reason for a class to change).

  The principles set forth object should not assume too much responsibility, if an object took on too much responsibility, there are at least two disadvantages:

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

The advantages of single responsibility principle

  The core particle size is controlled type principles single responsibility, decouple objects, to improve the cohesion. If you follow the single responsibility principle will have the following advantages.

    • Reduce the complexity of the class. A class is only responsible for a duty, certainly better than its logic is responsible for a number of duties is much simpler.
    • Improve the readability of the class. Reduce complexity, increase natural readability.
    • Improve the maintainability of the system. Improve readability, it is much easier to maintain.
    • Risk reduction due to change. Change is inevitable, if the single responsibility principle comply well, when modifying a function, you can significantly reduce the impact on other functions.

Implementation of the principle of single responsibility

  Single responsibility principle is the simplest but most difficult to use the principle of the different functions need to design classes found and isolated, and then encapsulated into different classes or modules. The class found various responsibilities require designers to have strong analytical capabilities and design experience related to reconstruction. Below job management program student at the University of example to introduce the application of the principle of single responsibility.

Student management program [Example 1] University.

  Analysis: Student Affairs Student Life University, including student academic counseling and guidance two aspects, which include life coaching class leadership building, attendance statistics, psychological counseling, the cost of calls, classroom management and other work, including professional and academic guidance guide , tutoring, research guidance, learning and summarizing work. If these work to a teacher in charge is clearly unreasonable, the correct way of life counseling is the responsibility of the counselor, academic tutors to guide the responsibility of the school, its class diagram shown in Figure 1.

             The class diagram work management program of university students
                  The class diagram work management program of university students 1


  Note: Single Responsibility also apply to the method. A method should do one thing as far as possible. If too many things a method of treatment, it will become very coarse particle size, it is not conducive to reuse.

Guess you like

Origin www.cnblogs.com/yuexiaoyun/p/11825475.html