Re: OO Concept: Liskov Substitution Principle
- From: Joshua Cranmer <Pidgeot18@xxxxxxxxxxxxxxx>
- Date: Fri, 30 May 2008 21:22:33 GMT
howa wrote:
Hi,
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?
First things first, I am going to stop talking about a hierarchy whose practical importance is practically nil and bring in a more useful example.
Let us assume we are writing a linear algebra package. Quite naturally, at some point we have a Matrix class. While writing Matrix.determinant(), we realize that it may be to our advantage to refactor said method into a SquareMatrix.determinant() for ease of design. Should we do so, i.e., should we create a SquareMatrix class that descends from Matrix?
I propose the following answer: yes, since Matrix here is assumed to be immutable. The problem with compatibility is that SquareMatrix assumes that its two dimensions are equal; since these dimensions can only be affected at initialization, thereby rendering this contractual incompatibility moot.
However, in the end, we decide that requiring a separate SquareMatrix class is pointless since a 3x2 matrix multiplied by a 2x3 matrix is a square matrix even if its type is merely a banal Matrix, rendering the entire discussion moot.
--
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
.
- Follow-Ups:
- References:
- OO Concept: Liskov Substitution Principle
- From: howa
- OO Concept: Liskov Substitution Principle
- Prev by Date: Binary data garbled
- Next by Date: Re: Capital Punishment
- Previous by thread: Re: OO Concept: Liskov Substitution Principle
- Next by thread: Re: OO Concept: Liskov Substitution Principle
- Index(es):
Relevant Pages
|