Re: OO Concept: Liskov Substitution Principle



howa wrote:
Just read an article talking about the LSP in term of OO design:
http://www.objectmentor.com/resources/articles/lsp.pdf

The article said Rectangle class should not be a superclass of Square
class.

Okay, so how you would design the Rectangle & Square class?

The article did not cover that, so what are your opinion?
Howard

Clearly all Rectangles are not also Squares. Considering LSP,
Squares are not Rectangles either. Rectangles have two
*independent* values, height and width. With a Square the
height and width must be the same. What the article is
saying is that there is no IS-A relationship between
Squares and Rectangles.

Look at the java.lang.Integer and Double classes. These have
the same issues, in that you might think all Integers are
Doubles. But they're not when you consider LSP.

That doesn't mean there is no relationship however. A Rectangle
might be a Square, and any square Rectangle should be convertible
to a Square. Also any Square should be convertible to a Rectangle.
In Java you generally provide xxxValue and isXxx methods for this.
You might try something such as:

public class Rectangle {
private int width, height;
public Rectangle ( int width, int height ) { ... }
public int getWidth () { return width; }
public int getHeight () { return height; }
public void setWidth ( int width ) {
if ( width >= 0 ) this.width = width;
else ...
}
public void setHeight ( int height ) { ... }
public boolean isSquare () { return width == height; }
public Square squareValue () {
if ( isSquare() ) return new Square( height ); }
else ...
}
}

public class Square {
private int side;
public Square ( int side ) { ... }
public int getHeight () { return side; }
public int getWidth () { return side; }
public void setWidth ( int width ) {
if ( width >= 0 )
side = width;
else ...
}
public void setHeight ( int height ) { ... }
public Rectangle rectangleValue () {
return new Rectangle( side, side );
}
}

============================

What do you think of this?

-Wayne
.



Relevant Pages

  • Re: Why is OO Popular?
    ... Ellipses are circles is horribly wrong and squares are rectangles is ... Circles are ellipses breaks LSP ... > rectangle can be changed independently ... square value in a rectangle variable or a square value in a square ...
    (comp.object)
  • Re: Agile Ecosystems Vs (Rational) Unified Process
    ... > conflict between Rectangle and Square, and not one line of code ... and practice on the other are seen as mostly independent and separate ... Google Home - Advertising Programs - Business Solutions - About Google ...
    (comp.object)
  • Re: SubTyping versus SubClassing
    ... small subset of ones used in mathematics. ... >> which you'd better have a square for each rectangle, ... Operations defined on the types are that theorems we have to prove ...
    (comp.object)
  • Re: Frage zu verdecktem Member
    ... definieren und zwar in dem man sagt, die Menge der Quadrate ist die Menge aller Rechtecke für die gilt, dass beide Seiten gleich lang sind. ... In Java ist ein Rectangle auch dann ein Rectangle wenn beide Seiten gleich lang sind und kann nicht ein Square werden, weil das Typsystem es nicht erlaubt. ...
    (de.comp.lang.java)
  • Re: delegation vs. inheritance
    ... the debate centered around deriving Rectangle behaviors from Square behaviors. ... The real problem was that Square was defined with a sideLength knowledge attribute while Rectangle was defined with majorSideLength and minorSideLength -- apples & oranges. ... So if one defined sideLengthN for Square, then Rectangle could be a subclass without any LSP inconsistency in the knowledge attributes. ...
    (comp.object)