我可以在这里申请Liskov替代原则 [英] Can I apply here Liskov substitution principle

查看:139
本文介绍了我可以在这里申请Liskov替代原则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据模型由以下类别表示:



1) ImagesSet - 拥有 2DImage的对象,每个 2DImage 有自己的位置(原点( 3DPoint ),x-,y-轴( 3DVector )和尺寸沿x和y轴(以像素为单位)),但相同的像素大小(例如mm),x轴和y轴之间的角度(90度)



对象具有以下方法(伪代码):




  • AddImage(2DImage);

  • RemoveImage(ImageIndex);

  • 数字GetNumberOfImages();

  • 2DImage Get2DImage(ImageIndex) / li>


2) 3DImage - 类似于第一个但具有以下限制的对象:
它可以存储2D图像,只能沿着x轴和y轴以相同的x轴,y轴和尺寸存储。



在这种情况下是否正确导出 3DImage ImagesSet
从我的角度来说, 3DImage ImagesSet (但是限制很小)
我可以在这里申请Liskov替代原则吗? >

在这种情况下,如果我们尝试使用另一个x,y轴 - 方法 AddImage 添加图像,则会抛出异常或返回错误。 / p>

提前感谢
谢谢

解决方案

我同意与maxim1000的LSP将被违反,因为派生类添加基类中不存在的限制。如果你仔细看看你的描述,你会注意到这个问题可以颠倒:ImageSet是从3DImage派生的吗?



你的情况有点类似于Ellipse - 圆问题。哪一个来自另一个?圆圈是一个带有约束的椭圆,还是椭圆形的一个带有半径的圆?关键在于两者都是错误的。如果将椭圆限制为等于半径,则尝试设置不同值的客户端将收到错误。



否则,如果我们说椭圆只是一个较小约束的圆,我们得到一个更微妙的错误。假设形状可能不会破坏屏幕的边界。现在假设用椭圆替换一个圆。根据测试的坐标,形状可能会从屏幕区域中跳出而不改变客户端代码。这就是LSP的确切违规。



结论是 - 圆和椭圆是不同的类; 3DImage和ImageSet是分开的类。


I have two data models which are represented by the following classes:

1) ImagesSet - an object that owns 2DImage's, each 2DImage has its own position (origin(3DPoint), x-,y-axes(3DVector) and dimension along x and y axes(in pixels)), but the same pixel size(in mm for example), angle between x and y axes(90 degrees)

This object has following methods(in pseudo code):

  • AddImage(2DImage);
  • RemoveImage(ImageIndex);
  • number GetNumberOfImages();
  • 2DImage Get2DImage(ImageIndex);

2) 3DImage - an objects that is similar to the first but with following restrictions: it can store 2D images only with the same x-,y-axes and dimensions along x and y axes.

Is it correct in this case to derive 3DImage from ImagesSet? From my point of view 3DImage "is a" ImagesSet (but with small restrictions) Could I apply here Liskov substitution principle?

In this case if we are trying to add an image with another x,y axes - method AddImage either will throw an exception or return an error.

Thanks in advance, Sergey

解决方案

I agree with maxim1000 that LSP will be violated because derived class adds restrictions that are not present in the base class. If you take a close look at your description you will notice that the question can be turned upside-down: Can ImageSet derive from 3DImage?

Your situation is somewhat similar to Ellipse-Circle problem. Which one derives from the other? Is circle an ellipse with a constraint, or is an ellipse a circle with additional radius? The point is that both are wrong. If you constrain ellipse to equal radiuses, then client which attempts to set different values would receive an error.

Otherwise, if we say that ellipse is just a less constrained circle, we get a more subtle mistake. Suppose that shapes may not breach boundaries of the screen. Now suppose that a circle is replaced with an ellipse. Depending on which coordinate was tested, the shape might break out of the screen area without changing the client code. That is the exact violation of LSP.

Conclusion is - circle and ellipse are separate classes; 3DImage and ImageSet are separate classes.

这篇关于我可以在这里申请Liskov替代原则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆