What code breaks the LSP?
In fact, there is another more practical and instructive description of the Li substitution principle, that is, Design By Contract.
Subclasses are designed to follow the behavior conventions (or agreements) of their parent classes. The parent class defines the behavior convention of a function, and the subclass can change the internal implementation logic of a function, but not the original behavior convention of the function. The behavioral conventions here include:
- A function declares a function to implement.
- Conventions for inputs, outputs, and exceptions;
- Any special instructions listed in the notes.
In fact, the relationship between the parent and subclass in the definition can also be replaced by the relationship between the interface and the implementation class.
To better understand this sentence, let me give you some examples that violate the li substitution principle.
A subclass violates what its parent class declares to implement
The sortOrdersByAmount () order sorting function provided in the parent class sorts orders from smallest to largest, while the subclass overrides the sortOrdersByAmount () order sorting function to sort orders by creation date. The design of that subclass violates the substitution rule.
A subclass violates its parent’s convention on inputs, outputs, and exceptions
-
In the parent class, a function convention returns NULL on an error and an empty collection on an empty fetch.
-
When a subclass overloads the function, the implementation changes, returning exception on error or NULL if no data is retrieved. The design of that subclass violates the substitution rule.
-
In a parent class, a function convention allows input data to be any integer, but the implementation of a subclass only allows input data to be a positive integer, negative values are thrown out, that is, the subclass is stricter on the input data than the parent class, then the design of the subclass violates the inside substitution principle.
In a parent class, if a convention throws only ArgumentNullException, the subclass is designed to throw only ArgumentNullException. Throwing any other exception will cause the subclass to violate the li substitution principle.
Subclasses violate any special instructions listed in the parent class comments
-
The comment on the withdraw () withdrawal function defined in the parent class reads: “The withdrawal amount of the user may not exceed the account balance…”
-
However, after rewriting the withdraw () function, the subclass realizes the function of overdraft withdrawal for VIP accounts, that is, the withdrawal amount can be greater than the account balance. Therefore, the design of this subclass also does not conform to the principle of In-substitution.
These are three typical violations of the Li substitution principle.
Another tip to determine if a subclass’s design implementation violates the li substitution rule is to validate the subclass’s code with the parent’s unit tests. If some unit test runs fail, it is possible that the design implementation of the subclass does not fully comply with the conventions of the parent class, and that the subclass may violate the inner substitution principle.
In fact, did you notice that the rule of in-form substitution is very loose. In general, the code we write doesn’t violate that much. So as long as you can understand what I’m talking about today, this principle is not difficult to grasp and not difficult to apply.
Although polymorphism and li substitution are somewhat similar in terms of definition description and code implementation, they focus on different aspects.
-
Polymorphism is a feature of object-oriented programming and a syntax of object-oriented programming languages. It’s an idea of code implementation.
-
In substitution is a design principle used to guide how to design subclasses in inheritance relations. The design of subclasses should ensure that the replacement of the parent class does not change the logic of the original program and does not damage the correctness of the original program.
More original reading: javawu.com