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

Source: Internet
Author: User
Tags inheritance

In object-oriented program design, 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 is: let is a property provable about objects of type. Then should being true for objects The Typewhere 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:

[Java] view plain copy 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.

[Java]View plain copy 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 method public 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.

[Java] view plain copy 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 is obviously 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 the method of the parent class is not good, which reduces the reusability of the entire inheritance system and the probability of error

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.