OO design principles-the LSP-Richter replacement principle of Liskov substitution principle:oo design

Source: Internet
Author: User
Summary

Functions ' use pointers or references to base classesmust is able to-use objects of derived classes-without knowing it .
All references to base classes must be able to transparently use objects of their subclasses.

That
◇ so use the base class code place, with the derivation class code replaces, can perform the action processing correctly.

In other words, if the derived class replaces the base class and does not perform the action correctly, then their inheritance should be abolished.

In other words, only OO designs that meet the following 2 conditions can be considered to satisfy the LSP principle:

◇ you should not have a condition in your code to judge a subclass type such as If/else. The following code violates the LSP definition.
View plain if (obj typeof Class1) {do something} else if (obj typeof Class2) {do something else}
◇ subclasses should be able to replace the parent class and appear anywhere the parent class can appear.

Or if we replace the base class in the code with its subclasses, the code will work properly.

The Richter replacement principle LSP is an important guarantee for the code to conform to the open and closed principles. At the same time LSP embodies:

◇ Class Inheritance principle: If an object of an inheriting class might run wrong where the base class appears,

The subclass should not inherit from the base class, or it should redesign the relationship between them.

◇ Action Correctness Guarantee: From the other side, the extension of the class that accords with the LSP design principle will not introduce new errors to the existing system.

the inheritance principle of the class:

Robert C. Martin, in introducing Liskov substitution principle (LSP), gave an example of rectangle and square.

This example is used here, but it is rewritten in the Java language and ignores some details and lists only the following essentials

Section to illustrate the rule of the Richter substitution principle on inheritance of classes.

View plain//code:      class rectangle {       double  width;       doubleheight;                     public double getheight ()  {            returnheight;       }        public void setheight (doubleheight)  {            this

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.