Kotlin-重写一堂课,以至于它不违反Liskov替代
我有一个UML图,需要重写Classe,与此同时,我需要尊重Liskov替代原则。我读到了这个原则,但我陷入了克拉斯。
我正在考虑重写Classe,以使其实现这3个接口A,B,C,并且在组件中,我将拥有3个A,B和C型的对象。我的方法正确吗?换句话说,我将删除接口D。
I have an UML diagram and I need to rewrite ClassE and in the same time I need to respect Liskov substitution principle. I read about this principle but I got stuck at the ClassE.
I'm thinking to rewrite ClassE such that it implements those 3 interfaces A,B,C and in the Component I would have 3 objects of type A,B and C. Is my approach correct? In other words, I would remove interface D.
Another approach I'm thinking is to split ClassE in 3 classes, each of these classes to implement A,B,C.
Can you give me some hints? I use Kotlin.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有任何迹象表明
classE
可能与里氏替换原则存在任何问题。事实上,LSP 是关于保证履行的合约(前置条件、后置条件、不变量和历史)。
在这个设计中,
D
可以提供一个不仅仅是 A、B 和 C 单独签订的合同的合同。因此删除 D 并不能解决任何 LSP 问题。备注:向您的问题添加代码不会有任何帮助,除非它包含前置条件、后置条件和不变量的显式断言。添加代码和它应该履行的合同会有更多帮助
Nothing suggest that
classE
may have any issue with Liskov’s substituion principle as it is.In fact LSP is about contracts that are guaranteed to be fulfilled (pre-conditions, post-conditions, invariants and history).
In this design,
D
could offer a contract that is more than just the contracts of A,B and C taken separately. Therefore removing D would not solve any LSP issue.Remark: adding code to your question would not help, unless it would contain explicit assertions of preconditions, postconditions and invariants. Adding the code and the contract it is supposed to fulfill would help more