为什么编码应该是“针对接口”?特别是对于原始数据类型?
我对为什么我们将代码实现到接口的设计方法有疑问。这在原始数据类型中非常常见。就像我不明白这两者之间的区别:
Map<Integer, String> mymap = new HashMap<Integer, String>();
?
HashMap<Integer, String> mymap = new HashMap<Integer, String>();
这两者之间有什么区别吗 我的意思是,在这两种情况下,我们要使用 mymap 的每个地方都将保持不变。
如果这个问题似乎没有用,我很抱歉,但我真的不明白这对以后使用 mymap
的地方有何影响。请帮忙?
谢谢..
注意-我已经看到了这个问题,但它没有给出我想要的。
I have a doubt on the design methodology that why we implement the code to the interface. This is very much observed in primitive data types. Like I am not getting the difference between these two :
Map<Integer, String> mymap = new HashMap<Integer, String>();
And
HashMap<Integer, String> mymap = new HashMap<Integer, String>();
Is there any difference between these two? I mean each and every place where we are going to use mymap
will remain same in both the cases.
I am sorry if the question seems to be of no use but I really not getting that how this is going to make any difference later on where mymap
will be used. Please help?
Thanks..
Note - I have already seen this question on SO but it is not giving what I want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
第二个选项限制您始终使用 HashMap,即使有一天 TreeMap 可能会更有用。
在第一个中,您可以更轻松地更改特定的实现 - 您只需更改一行代码。如果您从方法返回映射,这一点尤其明显 - 方法返回类型不必更改。
对接口进行编码还有助于在测试期间模拟对象,但我认为这里的情况并非如此。
The second option limits you to always use HashMap, even if some day TreeMap could be more useful.
In the first one you can change the particular implementation easier - you only have to change one line of code. It's especially visible if you return your map from methods - the method return type doesn't have to change.
Coding to an interface also helps mocking the object during tests, but I assume that's not the case here.
由于 mymap 可以在其他地方使用
Map
的不同实现进行实例化,因此您不应在使用它的代码中依赖它作为HashMap
的实例。Because mymap can be instantiated somewhere else with a different implementation of
Map
, you should not rely on it being an instance ofHashMap
in the code using it.使用
Map mymap
允许您稍后更改实现。例如,如果在某个时候您需要对mymap
进行排序,只需将初始化更改为LinkedHashMap
即可。Using
Map mymap
allows you to change the implementation later. For example, if at some point you needmymap
to be ordered, you just change the initialization toLinkedHashMap
.如果您仅在类型中使用它(意思是:私有)并自己实例化它,那么它并没有真正的区别。
如果您的类型的公共接口公开 Map 与 HashMap,它就会开始变得有趣。
If you are only using this inside your type (meaning: private) and instantiate it yourself, it doesn't make a real difference.
It starts getting interesting if the public interface of your type exposes a Map vs. a HashMap.
在您建议的特定实例中,这可能不会产生任何影响,但最好让自己习惯于在声明中始终使用该接口,因为在某些合法实例中您需要这种灵活性。
In the specific instance you propose, it may not make a difference, but it's good practice to get yourself used to always use the interface in your declarations because there are legitimate instances where you will need that flexibility.
前者允许您更改为:
例如,而不破坏所有其余代码。
一个更小的优点是,您的 IDE 可以向您显示接口的方法,而不是实现的(可能数量更多)方法。
The former allows you to change to:
for example, without breaking all the rest of your code.
A much more minor advantage is that your IDE can show you the methods for the interface, rather than the (potentially much larger number of) methods for the implementation.