问题
Possible Duplicate:
What does it mean to “program to an interface”?
I noticed that some people like to declare an object as one of the interfaces it implements even though, within the scope of the variable, it is not necessary to look at it as the interface, e.g. there is no external API that expect an interface.
For example:
Map<String, Object> someMap = new HashMap<String, Object>();
Or you can just do
HashMap<String, Object> someMap = new HashMap<String, Object>();
and avoid importing java.util.Map
altogether.
What are the advantages of declaring it through an interface (first above) as opposed to the class itself (second above)?
Thanks
回答1:
If the variable is not used later, there is no advantage/disadvantage. The reason you would use an interface rather than an object is to allow for more flexibility, but if that variable isn't used, there is no difference from a performance persepective.
回答2:
An interface such as Map<A, B>
declares what an object can do. On the other hand, a class such as HashMap<A, B>
defines how an object does what the interface declares it does.
If you declare a variable (or field, or whatever) a Map<A, B>
, you are stating that your code depends only on the contract defined by that interface, it does not depend on the specifics of an implementation.
If you declare it a HashMap<A, B>
, it is to be understood that you need that specific version of a map (for whatever reason), and it cannot be replaced for something else.
I tend to dislike the common answers like "because you can change the implementation" simply because, after years and years of practice, you will see that it won't happen as frequently as that, and the major benefits are really this subtle (but clear) expression of your intents.
回答3:
Since it's not part of API, it is implementation detail. It's better to be specific in implementations, there's no point to be abstract here.
my prev answer
Use interface or type for variable definition in java?
回答4:
If you use Map<String, Object> someMap
, you are designing to an interface rather than implementation.. So you can switch between other implementation easily..
So, your Map
can point to a HashMap
, LinkedHashMap
, or any other object, that is a subclass of Map
.
So, if you have: -
Map<String, Integer> someMap = new HashMap<>();
You can change the implementation later on(If you want) to point to a LinkedHashMap
: -
someMap = new LinkedHashMap<>();
Whereas if you use HashMap
on LHS, you can only make it point to an object of type HashMap
..
But, as such there is no difference in performance.. But it is suggested to always design
to an interface
rather than implementation
..
回答5:
A interface defines, which method a class has to implement. This way - if you want to call a method defined by an interface - you don't need to know the exact class type of an object, you only need to know that it implements a specific interface.
Example:
interface Printer {
public void print(String text);
}
class FilePrinter implements Printer {
public void print(String text) {
//append the text to a file
}
}
class ScreenPrinter implements Printer {
public void print(String text) {
//write the text on the screen
}
}
class SomeClass {
public printSomething(Printer myPrinter) {
myPrinter.print("Hello");
}
}
If you call SomeClass.printSomething(...) it does not matter if you pass an instance of FilePrinter or ScreenPrinter, because the method just does not care. It knows that the object implements the interface Printer and also implements it's methods.
Another important point about interfaces is that a class can implement multiple interfaces.
来源:https://stackoverflow.com/questions/12805713/what-are-the-benefits-of-declaring-an-object-as-interface