LSP the liskov substitution principle
The main mechanism behind the OCP principle is abstraction and polymorphism. Inheritance is the key mechanism supporting abstraction and polymorphism.
Definition of LSP
If the object P1 of each type P has a class C object C1 Program In P, after C1 is replaced with P1, the behavior function of program P remains unchanged, and C is the subtype of P.
The LSP principle clearly states that the IS-A relationship in Ood is in terms of behavioral functions. Behavior is not internal, private, but external and public, and is dependent on the customer program. Behavior is a concern of the software! The behavior functions of all derived classes must be consistent with those expected by the client program for their base classes.
For example:
Public class rectangle
{
Private long width;
Private long height;
Public void setwidth (long width ){
This. width = width;
}
Public void setheight (long height ){
This. Height = height;
}
Public long getwidth (){
Return width;
}
Public long getheight (){
Return height;
}
Public void resizewidth (){
Width ++;
}
};
Public class square: rectangle
{
Public void setwidth (long width ){
This. width = width;
This. Height = width;
}
Public void setheight (long height ){
This. width = width;
This. Height = width;
}
Public void resizewidth (){
Width ++;
Height ++;
}
};
It is not a reasonable design. Square inherits the rectangle interface, but from the square itself, the setwidth and setlength interfaces are not reasonably set up, resulting in Code The meaning of the expression is different from that of the base class.
Design by contract defines the relationship between classes and their customers as a formal agreement that clarifies the rights and obligations of all parties. The method declaration of the class required by DBC is precondition and postcondition ). In order for the method to be executed, the prerequisite must be true. After completion, the method ensures that the subsequent conditions are true. DBC requirements for derived classes when the routines in the derived class are redefined, we can only replace them with weaker prerequisites and stronger subsequent conditions.
The LSP principle is an important feature of OCP-compliant applications. Only when the derived class can completely replace the base class can we safely reuse the functions that use the base class and modify the derived type.
The main mechanism behind the LSP the liskov substitution principle OCP principle is abstraction and polymorphism. Inheritance is the key mechanism supporting abstraction and polymorphism. Definition of LSP if there is a class C object C1 for each type P object P1, so that in all program P for C, after C1 is replaced with P1, the behavior function of program P remains unchanged, and C is the subtype of program p. The LSP principle clearly states that the IS-A relationship in Ood is in terms of behavioral functions. Behavior is not internal, private, but external and public, and is dependent on the customer program. Behavior is a concern of the software! The behavior functions of all derived classes must be consistent with those expected by the client program for their base classes. For example, the following design: public class rectangle {private long width; private long height; Public void setwidth (long width) {This. width = width;} public void setheight (long height) {This. height = height;} public long getwidth () {return width;} public long getheight () {return height;} public void resizewidth () {width ++ ;}}; public class square: rectangle {public void setwidth (long width) {This. width = width; this. h Eight = width;} public void setheight (long height) {This. width = width; this. height = width;} public void resizewidth () {width ++; height ++ ;}}; is not a reasonable design. Square inherits the rectangle interface, however, from the perspective of Square itself, the setwidth and setlength interfaces are not reasonably set up, resulting in the difference between the meaning of the Code and the base class. Design by contract defines the relationship between classes and their customers as a formal agreement that clarifies the rights and obligations of all parties. The method declaration of the class required by DBC is precondition and postcondition ). In order for the method to be executed, the prerequisite must be true. After completion, the method ensures that the subsequent conditions are true. DBC requirements for derived classes when the routines in the derived class are redefined, we can only replace them with weaker prerequisites and stronger subsequent conditions. The LSP principle is an important feature of OCP-compliant applications. Only when the derived class can completely replace the base class can we safely reuse the functions that use the base class and modify the derived type.
<>