我有7-8个可调用的类,它们使用了一些相似的功能,并且还读了一些相同的列表和HashMap。 因此,我创建了一个静态类,其中包含静态函数和静态列表以及哈希映射的所有功能。面向对象设计vs静态类在java和并发性开发
现在,我有一些疑问:
1)它是一种错误的做法,以在Java静态类作为我的前辈骂我在使用静态类,而让我把它转换成一个单例类。
但静态类比单例更快。不是吗?
2)它是一个不好的设计有一个参考的静态/非静态方法来获得像调用方法内部列表一些数据(线程)或执行某些任务吗?
这是否违反线程安全性或并行性,正如我的老人所说的?并要求我将线程类中的列表作为私有成员。
但不是不好用的内存为其他线程6-7班利用了相同的列表为只读。
3)如何提高OO设计与性能改善以及。
一个示例代码应该是这样的:
public class StaticClass {
private static List<String> ListOne;
private static List<String> listTwo;
private static HashMap<String, String> hMap;
static{
//initialize list and maps by reading from file
}
public static List<String> getListOne() {
return ListOne;
}
public static List<String> getListTwo() {
return listTwo;
}
public static HashMap<String, String> gethMap() {
return hMap;
}
public static void commonMethodOne(){
}
public static String commonMethodTwo(){
}
public static String[] commonMethodThree(){
}
}
public class CallableThread implements Callable<String>{
public String call(){
HashMap<String, String> mapTask = StaticClass.gethMap();
List<String> taskOne =StaticClass.getListOne();
for(String temp : taskOne){
//do what you are suppose to do
}
for(String key : mapTask.keySet()){
//do what you are supposed to do
}
return "Done with CallableThread";
}
}
没有“资深”的人会推荐一个Singleton。我认为没有什么好处,使这个静态或单身。如果你有可变的共享数据,你最好注意正确的同步它。在我看来,你担心错误的事情。 – duffymo