3 ms·
Actually you kind of miss the point. If squares are a subclass or specialization of rectangle that the consumer does not recognize then the consumer will be li
by zaphar 17y ago
Actually you kind of miss the point.
If squares are a subclass or specialization of rectangle that the consumer does not recognize then the consumer will be lied to.
They are using "rectangles" rectangles can change height apart from width and still be rectangles. But if you are a squre then you can't change height apart from width.
The consumer code has every right to expect all rectangles to work like rectangles. But squares break the rectangle contract.
As far as state goes squares are rectangles. But when it comes to behaviour or valid operations squares aren't rectangles. This is the authors point in fact and is why he says immutability makes it go away. When you bundle both state and operations into an object you make it that much more difficult to model your domain.
Another solution to the articles problem would be to have objects that only contain state. Quadrangle state objects. And other objects that contain only operations. A quadrangle resizer object. resizers know that a square which has it's height changed apart from it's width is now just a rectangle This far more accurately models the problem domain.
Squares shouldn't be responsible for knowing whether they can become just rectangles. That should be your knife operators job.