什么时候应该返回接口和什么时候具体类? [英] When should I return the Interface and when the concrete class?

查看:146
本文介绍了什么时候应该返回接口和什么时候具体类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中编程时,我几乎总是,只是出于习惯,写这样的:

when programming in Java I practically always, just out of habit, write something like this:

public List<String> foo() {
    return new ArrayList<String>();
}

大多数时候甚至没有考虑它。现在,问题是:我应该总是指定接口作为返回类型吗?或者是建议使用接口的实际实现,如果是,在什么情况下?

Most of the time without even thinking about it. Now, the question is: should I always specify the interface as the return type? Or is it advisable to use the actual implementation of the interface, and if so, under what circumstances?

很明显,使用接口有很多优点为什么它在那里)。在大多数情况下,库函数使用什么具体实现并不重要。但也许有些情况下,它是重要。例如,如果我知道我将主要访问列表中的数据随机,一个 LinkedList 将是坏的。但是如果我的库函数只返回接口,我根本不知道。为了安全起见,我甚至可能需要将列表显式复制到 ArrayList

It is obvious that using the interface has a lot of advantages (that's why it's there). In most cases it doesn't really matter what concrete implementation is used by a library function. But maybe there are cases where it does matter. For instance, if I know that I will primarily access the data in the list randomly, a LinkedList would be bad. But if my library function only returns the interface, I simply don't know. To be on the safe side I might even need to copy the list explicitly over to an ArrayList:

List bar = foo();
List myList = bar instanceof LinkedList ? new ArrayList(bar) : bar;

但这看起来很可怕,我的同事可能会在自助餐厅里哭泣。正确的。

but that just seems horrible and my coworkers would probably lynch me in the cafeteria. And rightfully so.

你们怎么想?什么是你的指导方针,你什么时候倾向于抽象的解决方案,你什么时候揭示你的实现的细节潜在的性能提高?

What do you guys think? What are your guidelines, when do you tend towards the abstract solution, and when do you reveal details of your implementation for potential performance gains?

推荐答案


例如,如果我知道我会
主要访问列表中的数据
随机,一个LinkedList将是坏的。
但是如果我的库函数只有
返回的接口,我根本就不要
知道。为了安全起见,我可能
甚至需要将列表显式
复制到ArrayList。

For instance, if I know that I will primarily access the data in the list randomly, a LinkedList would be bad. But if my library function only returns the interface, I simply don't know. To be on the safe side I might even need to copy the list explicitly over to an ArrayList.

正如其他人都提到的,你不必关心库如何实现功能,减少耦合和增加库的可维护性。

As everybody else has mentioned, you just mustn't care about how the library has implemented the functionality, to reduce coupling and increasing maintainability of the library.

如果您作为库客户端可以证明实现对您的用例执行不佳,您可以与负责人联系,讨论最佳路径(

If you, as a library client, can demonstrate that the implementation is performing badly for your use case, you can then contact the person in charge and discuss about the best path to follow (a new method for this case or just changing the implementation).

也就是说,你的例子中有过早的优化。

That said, your example reeks of premature optimization.

如果方法是或可以是关键的,它可能会提到文档中的实现细节。

If the method is or can be critical, it might mention the implementation details in the documentation.

这篇关于什么时候应该返回接口和什么时候具体类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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