I am getting this warning on Sonar:
我在声纳上收到这个警告:
Avoid using implementation types like 'HashMap'; use the interface instead
避免使用像'HashMap'这样的实现类型;改为使用界面
What does it mean?
这是什么意思?
The class in which i get this warning is as:
我收到此警告的类如下:
class A {
private HashMap<String, String> map=new HashMap<String, String>();
//getters and setters
}
Please, I want proper solution to avoid warning on Sonar.
请,我想要适当的解决方案,以避免在声纳上发出警告。
3 个解决方案
#1
14
You should always code to an interface. ie. in this case you should declare your field like this:
您应始终编码到接口。即。在这种情况下,您应该声明您的字段:
private Map<String, String> map= new HashMap<String, String>();
This way anything using the map
variable will treat it as type Map
rather than HashMap
.
这样使用map变量的任何东西都会将它视为Map而不是HashMap。
This allows you to swap out the underlying implementation of your map at a later date without having to change any code. You are no longer tied to HashMap
这允许您在以后更换地图的底层实现,而无需更改任何代码。您不再与HashMap绑定
Read through this question: What does it mean to "program to an interface"?
仔细阅读这个问题:“编程到界面”是什么意思?
Also I am not sure what you were doing casting to a Set
there?
另外我不确定你在那里投了什么?
#2
3
I dont use Sonar, but what the warning basically means is
我不使用声纳,但警告基本上意味着什么
Always program to an interface rather than implemnetation class
始终编程到接口而不是实现类
private Map<String, String> map= new HashMap<String, String>();
Interface Implementing class
#3
3
Generally, you should always implement against an interface instead of a concrete type. In this example it means you should write your code like that:
通常,您应始终针对接口而不是具体类型实现。在这个例子中,这意味着你应该编写你的代码:
private Map<String, String> map= new HashMap<String, String>();
The big advantage is that you can then later always change the concrete implementation of your Map without breaking the code.
最大的好处是,您以后可以随时更改Map的具体实现,而不会破坏代码。
To get more details about it, check out this question: What do programmers mean when they say, "Code against an interface, not an object."?
要获得更多有关它的详细信息,请查看以下问题:程序员在说“代码对接口而不是对象”时的含义是什么?
#1
14
You should always code to an interface. ie. in this case you should declare your field like this:
您应始终编码到接口。即。在这种情况下,您应该声明您的字段:
private Map<String, String> map= new HashMap<String, String>();
This way anything using the map
variable will treat it as type Map
rather than HashMap
.
这样使用map变量的任何东西都会将它视为Map而不是HashMap。
This allows you to swap out the underlying implementation of your map at a later date without having to change any code. You are no longer tied to HashMap
这允许您在以后更换地图的底层实现,而无需更改任何代码。您不再与HashMap绑定
Read through this question: What does it mean to "program to an interface"?
仔细阅读这个问题:“编程到界面”是什么意思?
Also I am not sure what you were doing casting to a Set
there?
另外我不确定你在那里投了什么?
#2
3
I dont use Sonar, but what the warning basically means is
我不使用声纳,但警告基本上意味着什么
Always program to an interface rather than implemnetation class
始终编程到接口而不是实现类
private Map<String, String> map= new HashMap<String, String>();
Interface Implementing class
#3
3
Generally, you should always implement against an interface instead of a concrete type. In this example it means you should write your code like that:
通常,您应始终针对接口而不是具体类型实现。在这个例子中,这意味着你应该编写你的代码:
private Map<String, String> map= new HashMap<String, String>();
The big advantage is that you can then later always change the concrete implementation of your Map without breaking the code.
最大的好处是,您以后可以随时更改Map的具体实现,而不会破坏代码。
To get more details about it, check out this question: What do programmers mean when they say, "Code against an interface, not an object."?
要获得更多有关它的详细信息,请查看以下问题:程序员在说“代码对接口而不是对象”时的含义是什么?