Design Patterns six principles: the principle of replacing Richter

Richter substitution principle:

  Subclasses should be able to replace the parent class and appear where the parent can appear. For example: a company engaged in the annual party, has employees can draw, so whether it is new or old staff employees, regardless of the headquarters employees or expatriates, all should be able to participate in the lottery.

  Richter replace it includes at least two meanings:

    1, replacing Leeb principle is for the purposes of the inheritance, if the inheritance is to achieve code reuse, that is, in order to share methods, then the share of the parent class method should remain the same, can not be redefined by subclasses. Subclass can extend the functionality by adding new methods, the parent class and subclass can be instantiated, and the child class inherits the parent class and method is the same, the parent calls the method where subclasses can also call the same inheritance come, logical and consistent method parent class, then the parent class object off when Alternatively, of course, consistent with the logic subclass object, no problem.

    2. If the inherited purpose is to polymorphic, and the polymorphic premise is a subclass of coverage and redefine the parent class method, in order to meet the LSP, we should be the parent class is defined as an abstract class, and define abstract methods, but let subclasses redefine these methods, when the parent class is abstract, the parent class is not instantiated, it can not exist in the parent class object instantiated in the program. Does not exist when replacing a subclass of the parent class instance (instance of the parent class did not exist) logical inconsistencies possible. 

Case:

 1 internal class Program
 2 {
 3     private static void Main(string[] args)
 4     {
 5         A a = new A();
 6         Console.WriteLine($"100-50={(a.func1(100, 50))}");
 7 
 8         B b = new B();
 9         Console.WriteLine($"100-50={(b.func1(100, 50))}");
10         Console.WriteLine($"100-50={(b.func2(100, 50))}");
11 
12         Console.ReadKey();
13     }
14 }
15 
16 internal class A
17 {
18     public int func1(int num1, int num2)
19     {
20         return num1 - num2;
21     }
22 }
23 
24 internal class B : A
25 {
26     //public int func1(int num1, int num2)
27     //{
28     //    return num1 + num2;
29     //}
30 
31     public int func2(int num1, int num2)
32     {
33         return func1(num1, num2) + 100;
34     }
35 }
view code

  As it can be seen by the code, if class B inadvertently when the derived class A, override a superclass method func1 will lead to inconsistent results expected, changing the function of the original parent class. Hometown's conversion principle should meet the following requirements:

  1, subclass can abstract class that implements the method of the parent, but not non-abstract parent class covering

  2, subclasses can add their own unique way

  3, when the parent class subclass overloads parameter input parameters than the method of the parent class method looser

  4, when the method of implementation of the abstract subclasses of the parent class, method return value should be more stringent than the parent

advantage:

  Can significantly reduce the bug procedures and enhance code readability

Guess you like

Origin www.cnblogs.com/az4215/p/11462828.html