Interface advantages in Java
My qu开发者_如何学JAVAestion 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.
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.
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.
Interfaces are a way of decoupling seperate software components. Sure, you may use an Oracle data base to store all your data today so why not dispense of all your DAO interfaces?
The answer is that strongly coupling to anything may 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.
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.
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).
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 Wicket I 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
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.
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.).
精彩评论