有7-8类(实现可调用),它们具有一些相似的行为,即它们具有类似的功能和类似的实现。而且所有这些都使用HashMap(仅用于阅读目的),对于所有这些类都是相同的。 所以我决定做一个抽象的超类,它包含所有类似的方法加上这个hashMap作为一个静态成员。 我将为这些7-8个可调用类(因此这些也可以通过继承来调用)进行子类化,以便可以改进应用程序的性能。OO设计和初始化Asbtract超类中的静态成员
现在我有3个疑问:
1)是否有这样的设计缺陷任何可以和我进一步提高了吗?
2.)是否会出现任何并发问题,因为它是一个三层结构,可调用类位于底层的两个层次上?
3.)初始化静态块内部的静态成员(hashmap)是否错误?因为我的老板痴迷于使用静态成员和块。那么如果我在静态块中初始化这个映射会发生什么样的问题?
public abstract class AbSuper {
private static HashMap hmap;
private static CompletionService<String> service;
private static int maxThreads = 10;
static{
initializeMap();
}
public static void initializeMap(){
//load from file
}
public HashMap getmap(){
return hmap;
}
public void commonMethodOne(){
//do something
}
public static CompletionService<String> getService(){
ExecutorService executor = Executors.newFixedThreadPool(maxThreads);
service = new ExecutorCompletionService<String>(executor);
return service;
}
}
public class CallableOne extends AbSuper implements Callable<String>{
private List<String[]> taskList;
protected HashMap resultMap;
public List<String[]> getTaskList(){
return taskList;
}
public String call(){
for(String[] task : getTaskList()){
getService().submit(new SubCallableOne(task));
}
return "done with Callable One";
}
}
public class SubCallableOne extends CallableOne {
String[] task;
public SubCallableOne(String[] task) {
this.task = task;
}
public String call(){
//do what you are suppose to do
//and then access and populate "resultMap" fom superclass
return "done with subCallableOne";
}
}
将有7-8 CallableOne /二/三thier相应SubCallableOne/2/3。
你能否提供一些与你的问题相关的代码? http://stackoverflow.com/help/how-to-ask – user5292387
我已经提供了它 –