Richter seven principles of design patterns substitution principle

Richter substitution principle : all references to place the base class, must be transparent using objects in its child class.

      Popular understanding: subclass can extend the functionality of the parent class, but can not change the parent class of the original function.

Code Example:

1      // PC 
2      public  abstract  class Computer
 . 3      {
 . 4          public  abstract  void the Use ();
 . 5      }
 . 6  
. 7      ///  <Summary> 
. 8      /// laptop
 . 9      ///  </ Summary> 
10      class LaptopCommputer: Computer
 . 11      {
 12 is          public  the override  void the use ()
 13 is          {
 14              Console.WriteLine ( " are in the laptop " );
 15         }
 16      }
 . 17  
18 is      ///  <Summary> 
. 19      /// desktop computer
 20 is      ///  </ Summary> 
21 is      class DesktopComputer: Computer
 22 is      {
 23 is          public  the override  void the Use ()
 24          {
 25              Console.WriteLine ( " currently in use desktop " );
 26 is          }
 27      }
 28      class Program
 29      {
 30          static  void the Main ( String[] Args)
 31 is          {
 32              // notebook computer 
33 is              Computer Laptop = new new LaptopCommputer ();
 34 is              laptop.Use ();
 35              // desktop computers 
36              Computer desktopComputer = new new DesktopComputer ();
 37 [              desktopComputer.Use ();
 38 is  
39              Console.ReadLine ();
 40          }
 41 is      }

           Richter substitution principle shows that the software will be replaced in a base class object to its sub-class object, the program will not have any errors and exceptions, not vice-versa, if a software entity is a subclass object, then, then it may not be able to use over-the base class object.

For example: I like animals, then I must like dogs, that dog is a subclass of animals, but I like dogs, I can not infer like any animal, including cats, mice, although they are also animals.

           Alternatively Richter principle is one important way to achieve the opening and closing principle, since the local use of the base class can be used subclass object, the use of a base type in a program to define the target as much as possible, and then determine at runtime the type of its subclasses, subclasses of the parent class object to replace the object .

            In the run inside when the substitution principle's, should be the parent class is designed to abstract class or interface, so that subclasses inherit the superclass or parent implementation, and implement methods declared in the parent class, runtime instance of a subclass replace the parent class instance , can easily extend the functionality of the system, without having to modify the code of the original sub-class, adding new features can be achieved by adding a new sub-class.

 

Guess you like

Origin www.cnblogs.com/yangda/p/11924691.html