"Onlookers" design mode (2)--The Richter replacement principle (Lsp,liskov Substitution Principle)

Source: Internet
Author: User

In object-oriented programming , the Richter substitution principle (Liskov Substitution principle) is a special definition of a subtype. It was first presented in a speech by Barbara Liskov (Barbara Liskov) in a conference entitled "Abstraction and Hierarchy of data" in 1987.

The content of the Richter substitution principle can be described as: "A derived class (subclass) object can be substituted for its base class (superclass) object to be used." "The above is not the original text of Liskov, but the interpretation of the original from the Robert Martin (Robert Martin)." The original text is:

Let is
a property provable about objects of type. Then should being true for objects the type where is a subtype of.

Barbara Liskov and Zhou Yijien (Jeannette Wing) published their paper in 1994 and proposed the principle of Liskov substitution . ----Wikipedia


Richter replacement principle my personal understanding is that in an inheritance relationship, if the object of the parent class is replaced with the object of the subclass, the behavior of his original execution remains the same, then the program conforms to the Richter scale substitution principle, otherwise it violates the Richter scale substitution principle.


Let's take a look at a case where the Richter replacement principle is violated under what circumstances.


A simple inheritance structure, in subclasses, overrides the method of the parent class of the Calc method.

Parent Class Calc:

Package Cn.design.pattern2016032004liskovsubstitutionprinciple;public class Calc {public void calc (int a, int b) {//A-a = ? System.out.println (A + "-" + B + "=" + (a));}}

Subclass Calcson, which is added by overriding the Calc method in the parent class to two numbers.

Package Cn.design.pattern2016032004liskovsubstitutionprinciple;public class Calcson extends calc{public void Calc (int A, int b) {//A+b =? System.out.println (A + "+" + B + "=" + (A + b));} Other methodpublic void Addthem (int a, int b) {System.out.println (A + b);}}

Test class: If this is true of the Richter substitution principle, then it should be said that the place where the call of the parent class is changed directly to the subclass, then there will be no change in the original behavior. But the procedure below proves that this is a violation of the Richter scale substitution principle. When you replace a parent class call with a subclass, it is replaced by the method of the original parent class: subtraction, which becomes now in the subclass: addition. The results changed, thus violating the Richter scale substitution principle.

Calc cal = New Calc (); Cal.calc (10, 20);/*** based on the Richter substitution principle, when the parent class is replaced with a subclass, the behavior of using the parent class should not change, so the following code obviously has changed, which clearly violates the Richter replacement * principle. */calcson Calcson = new Calcson (); Calcson.calc (10, 20);

After we have inherited the parent class from the subclass, we need to be aware that this is not a good practice, reducing the reusability of the entire inheritance system and increasing the chance of error.


Summing up the experience of many predecessors, the Richter replacement principle is mainly four points:

1. Subclasses do not overwrite non-abstract methods of the parent class. Can implement its abstract methods.

2. Subclasses can implement their own unique methods.

3. When the subclass method overrides the parent class method, the parameter part is larger or equal (loose) than the parameter range of the parent class. Explanation: For example > if the parent class's method is ArrayList, then its subclass overrides this method when the parameter is list.

4. When a subclass overrides a parent class method, the return value requires that the return value of the parent class be less than or equal to the return value of the child class.



In the face of such a situation, generally, the current inheritance structure is removed, to become a dependency or aggregation of the form of a combination. Abstract a higher level abstract class, defined as such an abstract method, at the same time by the original two classes inherited implementation.


Public abstract class Calculator {public abstract void calc (int a, int b);}

public class Calc extends calculator{public void Calc (int a, int b) {//A-a =? System.out.println (A + "-" + B + "=" + (a));}}

public class Calcson extends calculator{public void calc (int a, int b) {//A+b =? System.out.println (A + "+" + B + "=" + (A + b));} Other methodpublic void Addthem (int a, int b) {System.out.println (A + b);}}

In this way, the original inheritance structure is re-deconstructed after the reorganization of the inheritance system, it should be said that the probability of error is greatly reduced.





"Onlookers" design mode (2)--The Richter replacement principle (Lsp,liskov Substitution Principle)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.