Explain the Richter substitution principle in Java design pattern programming

  • 2020-04-01 04:39:13
  • OfStack

Definition 1: If for every object o1 of type T1, there is an object o2 of type T2, so that the behavior of program P defined by T1 does not change when all objects o1 are replaced by o2, then type T2 is a subtype of type T1.
Definition 2: All references to the base class must transparently use the object of its subclass.
Problem origin: there is A function P1, completed by class A. Now function P1 needs to be extended, and the expanded function is P, where P is composed of the original function P1 and the new function P2. The new function P is completed by subclass B of class A, so subclass B may cause the failure of the original function P1 while completing the new function P2.
Solution: when using inheritance, follow the Richter substitution principle. When class B inherits class A, try not to overwrite the methods of parent class A, and try not to overload the methods of parent class A, except adding new methods to complete the new function P2.
                Inheritance contains a layer of meaning: those who have achieved good method in the parent class (relative to the abstract method), are actually set a series of specifications and contract, although it is not compulsory for all the subclasses must conform to the contract, but if the subclasses for any modification, abstract methods of these products will damage to the entire inheritance system. The Richter substitution principle expresses this.
              Inheritance, as one of the three features of object-oriented, not only brings great convenience to program design, but also brings disadvantages. Using inheritance will bring program invasive, for example, application of portability is reduced, increase the coupling between the objects, if a class is inherited by other classes, is when this class needs to be modified, must consider all the subclasses, and modified the parent class, all involves the function of the subclass may lead to failure.

Example:


public class Rectangle { 
  int width; 
  int height; 
  public Rectangle(int w, int h){ 
    width = w; 
    height = h; 
  } 
  public int getArea(){ 
    return width*height; 
  } 
} 
 
public class Square extends Rectangle { 
  public Square(int w, int h) { 
    super(w, h); 
  } 
   
  public int getArea(){ 
    return width*width; 
  } 
} 
 
public class Test { 
  public static void main(String[] args) { 
    Rectangle rectangle = new Rectangle(10, 20); 
        // Square rectangle = new Square(10, 20); 
    System.out.println(" Area: "+rectangle.getArea()); 
  } 
} 


If we replace the Rectangle class with the Square class, the area is not correct because we overwrote the getArea method of the parent class. This is a violation of the Richter substitution principle.
Of course, this is just an example, and we don't do that on real projects.

Conclusion:
1. Try not to override parent methods, but to add your own.
2. Inheritance not only brings great convenience to program design, but also brings disadvantages. If a class is inherited by another class, all subclasses must be considered when the class needs to be modified, and all functions involving subclasses may cause bugs after the parent class is modified.


Related articles: