在Java中,使用复杂模型的接口是否有性能提升?

编程入门 行业动态 更新时间:2024-10-21 11:42:19
本文介绍了在Java中,使用复杂模型的接口是否有性能提升?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

标题很难理解,但我不确定如何以另一种方式概括。欢迎任何编辑澄清。

The title is hardly understandable, but I'm not sure how to summarize that another way. Any edit to clarify is welcome.

我被告知,并建议使用界面来提高性能,即使在不特别要求常规的情况下也是如此界面角色。在这种情况下,对象是大模型(在MVC意义上),有许多方法和字段。

I have been told, and recommended to use interfaces to improve performances, even in a case which doesn't especially call for the regular "interface" role. In this case, the objects are big models (in a MVC meaning), with many methods and fields.

建议我使用的好用是创建一个具有独特实现的接口。当然,不会有任何其他类实现此接口。我被告知这样做更好,因为它暴露更少(或更接近)将使用此类中的方法的其他类,因为这些对象是从其接口引用的对象(所有公共方法)从接口中重现的实现。)

The "good use" that has been recommended to me is to create an interface, with its unique implementation. There won't be any other class implementing this interface, for sure. I have been told that this is better to do so, because it "exposes less" (or something close) to the other classes which will use methods from this class, as these objects are referring to the object from its interface (all public methods from the implementation being reproduced in the interface).

这对我来说似乎很奇怪,因为它看起来像是一个C ++用于我(带有头文件)。在那里,我看到了重点,但在Java?

This seems quite strange to me, as it seems like a C++ use to me (with header files). There I see the point, but in Java?

为这种独特的实现制作接口真的有意义吗?我真的很感激对这个主题的一些澄清,所以我可以证明不遵循这种行为,以及它通过复制所有声明而产生的麻烦。

Is there really a point in making an interface for such unique implementation? I would really appreciate some clarifications on the topic, so I could justify not following such kind of behavior, and the hassle it creates from duplicating all declarations.

编辑:感谢大家的答案,这真的很有帮助,也很有启发性(大部分都是,不仅仅是接受的)。

Thanks everybody for the answers, it was really helpful and instructive (most of them, not only the "accepted" one).

显然没有表现优势,除了通常的OO角色外,我现在可以从中获得更大的兴趣范围(取决于具体情况)接口。

There is clearly no advantage in performance, and I now have a larger scope of the interest that can come from it (depending on the situation), besides the usual OO role of the interface.

推荐答案

使用接口与性能无关(除了可能是开发团队的性能,即发展速度)。它更多的是保持依赖关系,并分离程序的不相关部分。

Using interfaces has not much to do with performance (except maybe the performance of the development team, i.e. the speed of development). It is more about keeping dependencies under control, and separating unrelated parts of the program.

如果你直接依赖代码中的具体类C,那么代码就更难了单元测试等。如果您依赖于某个界面,则可以轻松地在单元测试中创建模拟实现。

If you depend directly on concrete class C in your code, that code is more difficult to unit test, among others. If you instead depend on an interface, it is a snap to create a mock implementation in your unit tests.

当然,您可能不需要拉起来您的类的所有方法进入父接口。实际上,您可能不需要一个单一父接口。分析该类的用法(特别是像你这样的大类)的机会是,你找到两个甚至更多不同的方法组,由不同的客户端使用(例如,一组客户端只查询对象状态,而另一组客户端更新它)。这样就可以创建两个或多个不同的界面,每个界面都更简单,更清晰。

Of course, you may not need to pull up all the methods of your class into the parent interface. In fact, you may not need one single parent interface. Analyze the usage of that class and (especially with a big class like yours) chances are, you find two or even more distinct groups of methods, used by different clients (e.g. one group of clients only queries object state, while the other updates it). This makes it possible to create two or more distinct interfaces, which are much simpler and cleaner each.

这个分析甚至可以得出你的班级试图做的结论太多的事情(而不是单一责任),你最好提取一些它的内容分成一个类!换句话说,一旦你开始考虑 - 并编程到 - 接口,你就会开始在不同的层面上看到设计,这可能会带来更好的设计解决方案。

This analysis may even lead to the conclusion that your class is trying to do too many things (instead of having a single responsibility), and you would be better of extracting some of its contents into a separate class! In other words, once you start thinking about - and programming to - interfaces, you start to see design on a different level, and this may lead to better design solutions.

所有这些都说明了,如果经过上面的分析,你仍然看不到你的模型类的接口,请记下它。再说一遍,比如半年。然后,如果您仍然认为它没有为自己买单,只需将其转储。接口 - 就像程序中的任何其他元素一样 - 应始终有明确的目的和存在的理由(并且比我的同事告诉我创建它更好)。他们不是灵丹妙药。如果巧妙地使用它们,就可以使代码更好。如果你愚蠢地使用它们,你会使你的代码变得更糟。您在此处发布此问题的事实意味着您想要学习如何巧妙地使用它们,这很好: - )

All this told, if after all the analysis above you still see no use of the interface for your model class, make a note about it. Revisit it after, let's say, half a year. Then, if you still feel it hasn't paid for itself, just dump it. Interfaces - like any other element of your program - should always have a clear purpose and a reason to exist (and a better one than "my coworker told me to create it"). They are no panacea. If you use them cleverly, you make your code better. If you use them stupidly, you make your code even worse. The fact that you posted this question here means you want to learn how to use them cleverly, which is good :-)

更多推荐

在Java中,使用复杂模型的接口是否有性能提升?

本文发布于:2023-10-29 03:48:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538636.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   接口   性能   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!