Re: OO Concept: Liskov Substitution Principle
- From: Lew <conrad@xxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 30 May 2008 19:15:40 -0400
Wayne wrote:
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.
If the article says that Rectangles must have different or independent values for height and width, then they are mistaken. It is permitted for a Rectangle to insist that the height and width be the same, in which case it is also a Square.
There is nothing wrong with having a Square class with both a setHeight() and a setWidth() method. Thus it remains compliant with Liskov.
--
Lew
.
- References:
- OO Concept: Liskov Substitution Principle
- From: howa
- Re: OO Concept: Liskov Substitution Principle
- From: Wayne
- OO Concept: Liskov Substitution Principle
- Prev by Date: Re: OO Concept: Liskov Substitution Principle
- Next by Date: Re: Using POST to send bitmap image with Java
- Previous by thread: Re: OO Concept: Liskov Substitution Principle
- Next by thread: Re: OO Concept: Liskov Substitution Principle
- Index(es):
Relevant Pages
|