Six principles of design pattern: single responsibility principle

There are six principles in design mode: Interface isolation

Six principles of design pattern: dependency inversion principle

Six principles of design pattern: Richter’s substitution principle

There are six principles of design patterns: Demeter’s Law

Six principles of design pattern: open and close principle

Richter’s substitution principle:

Subclasses should be able to replace the parent class and appear where the parent class can appear. For example, if the company holds its annual party and any employee can draw a lottery, then both new and old employees, headquarters employees and expatriate employees should be able to participate in the lottery.

Two implications of Richter’s substitution principle:

1. The Richter substitution principle is for inheritance. If inheritance is for code reuse, that is, for shared methods, then shared parent methods should remain unchanged and cannot be redefined by subclasses. Subclass only through new add methods to extend the function of parent and child classes can be instantiated, and a subclass inherits methods and the parent class is the same, the parent class method is called, the subclass can also call the same inherited, logic, and the parent class consistent method, then use a subclass object replace superclass object, of course, logically consistent, smoothly.

2, if the purpose is to inherit polymorphism, and polymorphism is the premise of subclasses override and redefine the superclass method, in order to conform to the LSP, we should be the parent class is defined as an abstract class, and defines the abstract method, let subclasses redefine the method, when the parent class is an abstract class is the parent class cannot be instantiated, so there is no can instantiate the superclass object in the program. There is no possibility of logical inconsistencies when subclasses replace superclass instances (there are no superclass instances at all).

Therefore, the Richter conversion principle should meet the following requirements:

A subclass can implement abstract methods of its parent class, but cannot override non-abstract methods of its parent class

Subclasses can add their own special methods

3. When a subclass’s method overrides a parent class’s method, the method’s parameters are looser than the parent’s input parameters

4. When a subclass’s method implements an abstract method of its parent class, the method’s return value should be stricter than that of the parent class

Advantages:

Can greatly reduce program bugs and enhance the readability of the code

Source: www.cnblogs.com/az4215/p/11…