从矩形导出正方形是否违反了 Liskov 的替换原则? [英] Is deriving square from rectangle a violation of Liskov's Substitution Principle?
问题描述
我是设计和学习设计原则的新手.
I am new to design and learning the design principles.
它说从矩形导出正方形是违反 Liskov 替换原则的典型例子.
It says deriving square from rectangle is a classic example of violation of Liskov's Substitution Principle.
如果是这样,正确的设计应该是什么?
If that's the case, what should be the correct design?
推荐答案
我相信推理是这样的:
假设您有一个接受矩形并调整其宽度的方法:
Let's say you have a method that accepts a rectangle and adjusts its width:
public void SetWidth(Rectangle rect, int width)
{
rect.Width = width;
}
考虑到矩形是什么,假设这个测试会通过应该是完全合理的:
It should be perfectly reasonable, given what a rectangle is, to assume that this test would pass:
Rectangle rect = new Rectangle(50, 20); // width, height
SetWidth(rect, 100);
Assert.AreEqual(20, rect.Height);
... 因为改变矩形的宽度不会影响它的高度.
... because changing a rectangle's width does not affect its height.
但是,假设您已经从 Rectangle 派生了一个新的 Square 类.根据定义,正方形的高度和宽度始终相等.让我们再次尝试该测试:
However, let's say you've derived a new Square class from Rectangle. By definition, a square has height and width always equal. Let's try that test again:
Rectangle rect = new Square(20); // both width and height
SetWidth(rect, 100);
Assert.AreEqual(20, rect.Height);
该测试将失败,因为将正方形的宽度设置为 100 也会改变其高度.
That test will fail, because setting a square's width to 100 will also change its height.
因此,从 Rectangle 导出 Square 违反了 Liskov 的替换原则.
Thus, Liskov's substitution principle is violated by deriving Square from Rectangle.
is-a"规则在现实世界"中有意义(正方形绝对是一种矩形),但在软件设计世界中并不总是如此.
The "is-a" rule makes sense in the "real world" (a square is definitely a kind of rectangle), but not always in the world of software design.
编辑
要回答您的问题,正确的设计应该是 Rectangle 和 Square 派生自常见的Polygon"或Shape"类,它们不强制执行任何有关宽度或高度的规则.
To answer your question, the correct design should probably be that both Rectangle and Square derive from a common "Polygon" or "Shape" class, which does not enforce any rules regarding width or height.
这篇关于从矩形导出正方形是否违反了 Liskov 的替换原则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!