Object-Oriented Design Principle 4-Rishi replacement principle

Source: Internet
Author: User

Motivation

When we designProgramModule, we will create some class hierarchies, and then we will create their subclasses by extending some classes.

We must ensure that the child classes only expand without replacing the parent class, otherwise unexpected results will occur when we use them in existing program modules.

 

The Rys replacement principle indicates that when a program module uses a base class, the reference of the base class can be replaced by the quilt class without affecting the function of the module.

 

Rishi replacement principle

The base class can be completely replaced by the quilt class without affecting the function of the module.

 

Instance

 

For polymorphism, the Li's replacement principle seems to be quite obvious, for example:

Public void drawshape (shape s) {<br/> // code here. <br/>}

For any subclass of shape, the drawshape method should work well. We must be careful to implement subclass so that we do not inadvertently violate the Lee's replacement principle. If a function does not meet the Lee's replacement principle, it may have to explicitly reference the subclass object, such a function also violates the open and closed principle, because it must be modified when a new subclass is added.

Consider the following rectangular class:

// A very nice rectangle class. <br/> public class rectangle {<br/> private double width; <br/> private double height; <br/> Public rectangle (double W, double H) {<br/> width = W; <br/> Height = H; <br/>}< br/> Public double getwidth () {return width ;} <br/> Public double getheight () {return height ;}< br/> Public void setwidth (double W) {width = W ;} <br/> Public void setheight (double H) {Height = H ;}< br/> Public double area () {return (width * Height); <br/>}

What if there is a square now? Obviously, a square is a rectangle, So we should let the square inherit the rectangle class. Is that true? Let's take a look!

 

Note:

    • A square does not need to have both width and height attributes, but it still inherits these attributes from the rectangle. Therefore, every square wastes a little space, but this is not a major concern.
    • The inherited setwidth () and setheight () methods are not suitable for square because the width and height of a square are equal. Therefore, we need to override the setwidth () and setheight () methods, which may imply that inheritance is not suitable here.

The following is the square class:

// A square class. <br/> public class square extends rectangle {<br/> Public Square (double S) {super (S, S) ;}< br/> Public void setwidth (double W) {<br/> super. setwidth (w); <br/> super. setheight (w); <br/>}< br/> Public void setheight (double H) {<br/> super. setheight (h); <br/> super. setwidth (h); <br/>}< br/>}

Everything looks good, but pay attention to the followingCode:

Public class testrectangle {<br/> // define a method that takes a rectangle reference. <br/> Public static void testlsp (rectangle R) {<br/> r. setwidth (4.0); <br/> r. setheight (5.0); <br/> system. out. println ("width is 4.0 and height is 5.0" + <br/> ", so area is" + R. area (); <br/> If (R. area () = 20.0) <br/> system. out. println ("Looking good! /N "); <br/> else <br/> system. Out. println (" huh ?? What kind of rectangle is <br/> This ?? /N "); <br/>}</P> <p> Public static void main (string ARGs []) {<br/> // create a rectangle and a square <br/> rectangle r = new rectangle (1.0, 1.0 ); <br/> Square S = New Square (1.0); <br/> // now call the method above. according to the <br/> // LSP, it shoshould work for either rectangles or <br/> // squares. does it ?? <Br/> testlsp (r); <br/> testlsp (s); <br/>}< br/>}

Test program output:

Width is 4.0 and height is 5.0, so area is 20.0 <br/> looking good! <Br/> width is 4.0 and height is 5.0, so area is 25.0 <br/> huh ?? What kind of rectangle is this ??

It seems that we have violated the Li's replacement principle. Where is the problem? The testlsp () method reasonably assumes that when the width of a rectangle changes, its height remains unchanged. When passing a square object, this method violates the Lee's replacement principle. In mathematics, a square is a rectangle, but a square object is not a rectangle object, because the behavior of a square object is inconsistent with that of a rectangle object. In terms of behavior, a square is not a rectangle! Lee's replacement principle clearly shows that the IS-A relationship is for all behavior, in order to follow the Lee's replacement principle, subclass behavior must be consistent with the client's use of the base class behavior.

Subclass cannot have more constraints than the base class, because subclass must be used wherever the base class can be used. If a subclass has more constraints than the base class, then the base class is available, but it violates the subclass constraints.

 

Summary

The Lee's replacement principle is an extension of the open and closed principle. It indicates that when we create a new subclass of the base class, we should not change the behavior of the base class.

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.