java Java中的接口优势
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6533147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Interface advantages in Java
提问by Adrian
My question is simple: is there any advantage of using interfaces if they are implemented by a single class ?
I always thought that interfaces are good only when there are multiple implementations of that interface.
我的问题很简单:如果接口由单个类实现,使用接口有什么好处吗?
我一直认为只有当接口有多个实现时,接口才是好的。
Thanks.
谢谢。
采纳答案by SJuan76
In a word: no. The contract that an interface means can be specified directly in your only class.
一句话:没有。接口意味着的契约可以直接在你的唯一类中指定。
If you are clear that you won't need another implementation of the same methods in the future, you can avoid defining an interface.
如果您清楚将来不需要相同方法的另一个实现,则可以避免定义接口。
Of course, the issue here is "in the future" clause. If the project is small, without a long development/upgrade period, and well defined, you can be almost sure about what will be needed in the future.
当然,这里的问题是“将来”条款。如果项目很小,没有很长的开发/升级周期,并且定义明确,您几乎可以确定将来需要什么。
If the project is long as is probably that it will be subjected to changes, then you will have to factor in:
如果项目很长,可能会发生变化,那么您必须考虑:
- probability that you finally need an interface.
- probability that you know now what methods the interface will need in the future.
- cost of doing the interface now vs. refactoring in the future.
- 您最终需要一个接口的可能性。
- 您现在知道接口将来需要哪些方法的可能性。
- 现在做接口的成本与未来重构的成本。
回答by Chris Knight
Consider that there may only be one implementation now, but more implmentation may come along in the future. Also, by using an interface you can avoid creating a dependency on the concrete class by only using the interface. Then, later on, if you decide to change the implementation, you can create a new one which implements the interface without adverse affect to the other code which only relies on your interface.
考虑到现在可能只有一种实现,但将来可能会出现更多实现。此外,通过使用接口,您可以避免仅使用接口创建对具体类的依赖。然后,稍后,如果您决定更改实现,则可以创建一个新的实现接口,而不会对仅依赖于您的接口的其他代码产生不利影响。
Consider:
考虑:
public interface SomeInterface {...}
public class AConsumerOfSomeInterface {
private SomeInterface service;
public AConsumerOfSomeInterface(SomeInterface theImplementation) {
service = theImplementation;
}
//some code which uses the service
}
Now you can swap implementations at a future date without ever changing this class. Because this is easy to do, it makes sense to use interfaces even when you don't think you'll need one.
现在,您可以在将来的某个日期交换实现,而无需更改此类。因为这很容易做到,所以即使您认为不需要接口,也可以使用接口。
回答by Qwerky
Interfaces are a way of decoupling seperate software components. Sure, you may use an Oracle data base to store all your data todayso why not dispense of all your DAO interfaces?
接口是一种解耦独立软件组件的方式。当然,您现在可能会使用 Oracle 数据库来存储所有数据,那么为什么不放弃所有 DAO 接口呢?
The answer is that strongly coupling to anythingmay come back and bite you in the future. What if next year you want to use some cloud service to store data? If you coded to a DAO interface you can simply introduce a new cloud implementation and plug it straight in. Your app is loosely coupled so doesn't need to change.
答案是,与任何事物的强烈耦合在未来都可能会反过来咬你。如果明年你想使用一些云服务来存储数据怎么办?如果您编码到 DAO 接口,您可以简单地引入一个新的云实现并直接插入。您的应用程序是松散耦合的,因此不需要更改。
回答by Tapas Bose
I would like to add another point: Interface helps in Proxy pattern.
我想补充一点:接口有助于代理模式。
In a web application this proxy helps a lot for abstraction. I am new in web application, but usually I design my service layer by interface. In a web application build with WicketI have, say, an interface Afrodite
and a class AfroditeImpl
as service layer initialized from applicationContext as spring bean. Then we can get all the methods defined in Afrodite
and implemented in AfroditeImpl
from all the web pages by
在 Web 应用程序中,此代理对抽象有很大帮助。我是 Web 应用程序的新手,但通常我是按界面设计服务层的。在使用Wicket构建的 Web 应用程序中,我有一个接口Afrodite
和一个类AfroditeImpl
作为服务层,从 applicationContext 初始化为 spring bean。然后我们可以通过以下方式从所有网页中获取定义Afrodite
和实现的所有方法AfroditeImpl
AfroditeApplication application = (AfroditeApplication)getApplication();
Afrodite afrodite = application.getAfrodite();
// Now call the service layer's methods by afrodite
The outline is :
大纲是:
public interface Afrodite {
}
public class AfroditeImpl implements Afrodite {
}
public class AfroditeApplication extends WebApplication {
Afrodite afrodite;
public Afrodite getAfrodite() {
return afrodite;
}
@Override
public void init() {
super.init();
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
afrodite = (Afrodite) applicationContext.getBean("afrodite");
}
}
Hope this helps.
希望这可以帮助。
回答by Heisenbug
is there any advantage of using interfaces if they are implemented by a single class ?
如果接口由单个类实现,使用接口有什么好处吗?
If your interface it's implemented by only one class, and you are 100% sure that you will NEVER feel the needs to add another implementation, then there are no advantages.
如果您的接口仅由一个类实现,并且您 100% 确定您永远不会觉得需要添加另一个实现,那么就没有优势。
Anyway I think there is another situation to keep in mind. Suppose you have a class that do something more than what you want to expose to another client class. Than an interface could be used to limit the methods of your class that a client can call:
无论如何,我认为还有另一种情况需要记住。假设您有一个类做的事情比您想向另一个客户端类公开的要多。可以使用接口来限制客户端可以调用的类的方法:
public interface clientInterface{
public foo();
public bar();
}
public class yourClass implements clientInterface{
public foo(){}
public bar(){}
public doSomethingElse(){}
}
public class clientSideFactory{
public clientInterface getClass(){
return new yourClass();
}
}
public class internalSideFactory{
public clientClass getClass(){
return new yourClass();
}
}
}
In the situation illustrated above, even if you have a single implementation (clientClass)of the interface clientInterface, this is useful to be returned to a client class that should only see a limited part of your class (limiting the visibility of your class complete implementation).
在上面说明的情况下,即使您有接口 clientInterface 的单个实现 (clientClass),这对于返回给仅应该看到您的类的有限部分的客户端类(限制您的类完整实现的可见性)很有用)。
回答by Rostislav Matl
Ask yourself if there is a real possibility there will be more than one implementation in near future.
问问自己,在不久的将来是否有可能实现不止一种。
You can add interface easily when neded, sometimes several interfaces.
您可以在需要时轻松添加界面,有时会添加多个界面。
The only case when I create interfaces even for single class is to distinguish API for common usage and special-purpose API(testing, monitoring e.g.).
我为单个类创建接口的唯一情况是区分通用 API 和专用 API(例如测试、监控)。