2019.07.08: The road ahead is difficult, kneeling in adhere to.

One, single responsibility principle
Single responsibility Principle: The Single Responsiblity Principle, or SRP. Definition: There should be one and only one reason for a class to change.
Different classes should have responsibilities, with each class responsible for one responsibility.
Benefits:
  1. Class complexity is reduced, and what responsibilities are implemented are clearly defined;
  2. Readability goes up, complexity goes down, so readability goes up;
  3. Increased maintainability, increased readability, which of course is easier to maintain;
  4. Risk reduction due to change.
The simple summary is readability, scalability is also high.
2. Richter’s substitution principle
The Richter Substitution Principle: Liskov Substitution Principle, LSP. Two definitions:
  • The first definition is the most authentic: If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T ,the Behavior of P is unchanged when o1 is corrupted for O2 then S is a subtype of T. (For every object O1 of type S, there is an object O2 of type T. Such that the behavior of all programs P defined by T does not change when all objects o1 are substituted for O2, then type S is a subtype of type T.
  • The second definition: Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing All references to a base class must be able to transparently use objects from its subclasses.
In layman’s terms, subclasses can appear wherever the parent appears, and replacing them with subclasses does not generate any errors or exceptions, so the user may not need to know whether the parent or subclass is present at all. But the reverse is not true. Where there are subclasses, the parent class may not be able to adapt.
The Richter substitution principle defines a specification for good inheritance, a simple definition that contains four layers of meaning:
  1. A subclass must fully implement the methods of its parent class;
  2. When invoking other classes in a class, the parent class or interface must be used. If the parent class or interface cannot be used, the class design violates the LSP principle. If the subclass cannot completely implement the methods of the parent class, or some methods of the parent class have “distortion” in the subclass, it is recommended to disconnect the parent-child inheritance relationship, and use dependency, aggregation, and composition to replace inheritance. Subclasses can have their own personalities;
  3. The input parameters can be amplified when overriding or implementing methods of the parent class; If the input parameter type of the parent class is larger than the input parameter type of the child class, there will be a place where the parent class exists, but not the child class, because once the child class is passed as a parameter, the caller is likely to enter the method category of the child class. The preconditions for a method in a subclass must be the same or looser than those for the overridden method in the superclass.
  4. The output can be reduced when overwriting or implementing methods of the parent class. If a method of a superclass returns a type T, and the same method (overridden or overridden) of a subclass returns S, then the Richter substitution principle requires that S must be less than or equal to T, that is, either S and T are of the same type, or S is a subclass of T.
Conclusion: Subtypes must be able to replace their parent types. Subtypes can extend their methods without changing the functionality of the parent class.
Benefits:
  1. Code sharing reduces the effort to create classes
  2. Improve code reuse
  3. Improve code extensibility
  4. Improve the openness of production code
Disadvantages:
  1. Inheritance is intrusive. As long as you inherit, you must have all the attributes and methods of the parent class.
  2. Reduced code flexibility. Because when you inherit, the parent class has a constraint on the child class.
  3. Enhanced coupling. When you need to make code changes to the parent class, you must consider the impact on the child class. Sometimes even a small change in code may require refactoring of the interrupt program.