Kotlin-重写一堂课,以至于它不违反Liskov替代

发布于 2025-01-17 20:20:45 字数 367 浏览 0 评论 0原文

我有一个UML图,需要重写Classe,与此同时,我需要尊重Liskov替代原则。我读到了这个原则,但我陷入了克拉斯。

我正在考虑重写Classe,以使其实现这3个接口A,B,C,并且在组件中,我将拥有3个A,B和C型的对象。我的方法正确吗?换句话说,我将删除接口D。

我想的另一种方法是将Classe分为3个类,每个类别以实现A,b,c。 你能给我一些提示吗?我使用Kotlin。

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.
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我闹别瞎闹 2025-01-24 20:20:45

没有任何迹象表明 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文