Six design principles of design patterns
I. Single Respnsibility Principle SRP the Single responsibility Principle is defined as: there should be only one reason for class changes. Benefits of a single responsibility principle: 1. The complexity of the class is reduced, and all responsibilities are clearly defined. 2. the readability and complexity are reduced, and the readability of the class is improved; 3. Improving maintainability and readability makes maintenance easier. 4. changes are essential to reduce the risk caused by changes. If a single responsibility for an interface is well performed, modifying an interface only affects the corresponding implementation class and does not affect other interfaces. This is of great help to system scalability and maintainability. The single responsibility principle applies to interfaces and classes, and also to methods. What does it mean? Try to do one thing in one way. For example, if a method "Modify user password" is used, do not put this method in the "Modify user information" method. The granularity of this method is very rough. For a single responsibility, the suggestion is: the interface must be single responsibility, and the design of the class should be changed for only one reason. Note: The single responsibility principle puts forward a program writing standard, and uses "Responsibilities" or "reasons for changes" to measure whether interfaces or classes are well designed, however, "responsibilities" and "reasons for changes" are both non-measurable, and vary with projects and environments. II. The "Liskvo Substitution Principle" LSP's replacement Principle defines that all references to the base class must be transparently used to its subclass objects. Benefits of the Rys replacement principle: 1. Code sharing reduces the workload of creating classes. each subclass has the methods and attributes of the parent class; 2. improves code reusability; 3. Child classes can be similar to parent classes, but they are different from parent classes. "Dragon, Phoenix, and mouse are born with holes" means that a child has a parent ", "There are no two identical leaves in the world" indicates the differences between the child and the parent. 4. Improve code scalability and implement the parent class method to "do whatever you want, we can see that many open-source framework extension interfaces are completed by inheriting the parent class; 5. Improve the openness of products or projects; of course, inheritance also has the following Disadvantages: 1. inheritance is immersion. As long as it inherits, it must have all attributes and methods of the parent class; 2. Reduce code flexibility. Subclass must have all attributes and methods of the parent class, so that the child class has more constraints in the free world. 3. Enhanced coupling. When the constants, variables, and methods of the parent class are modified, you must consider the modification of the subclass. In addition, the modification is in a non-standard environment, this change may lead to very bad results-large pieces of code need to be reconstructed. Note: If the subclass cannot fully implement the parent class method, or some methods of the parent class have been "distorted" in the subclass, we recommend that you disconnect the Parent-Child inheritance relationship, inheritance is replaced by dependency, aggregation, and combination. The Lee replacement principle can be used, but cannot be used in turn. Where a subclass appears, the parent class may not be competent. The Rys replacement principle defines a standard for good inheritance: 1. Subclass must fully implement the parent class method; 2. Subclass can have its own personality; 3. When overwriting or implementing a parent class method, the input parameter can be enlarged. (the pre-condition of the method in the subclass (that is, the input parameter type) must be the same or more relaxed as the precondition for the override method in the superclass ). 4. The output result can be reduced when overwriting or implementing the parent class method. 3. the Dependence Inversion Principle DIP Dependency Inversion occurs in java: 1. dependencies between modules are abstracted, there cannot be direct dependencies between implementation classes. The dependencies are generated through interfaces or abstract classes; 2. interfaces or abstract classes do not depend on implementation classes; 3. Implementation class dependency interfaces or abstract classes; more streamlined definition is "interface-oriented programming" ------ one of the essence of OOD. Advantages of dependency inversion: Dependency inversion can reduce inter-class coupling, improve system stability, reduce risks caused by parallel development, and improve code readability and maintainability. Note: whether the design is stable, as long as the appropriate "loose", observe whether the "design blueprint" can thrive, you can draw a conclusion that the design with high stability can be achieved, when the surrounding environment changes frequently, you can still "I am not sure ".