繁体   English   中英

创建具有通用实例变量的Singleton类?

[英]Create a Singleton class with generic instance variable?

我有一个场景,其中有一个带有Future对象的bean(下面的A类)。 我有另一个类(下面的类B),它是一个单例,具有HashMap作为类型的实例变量,并实现了一个接口(下面的TestInterface),该接口又实现了Callable。

根据当前方案,用户只能传递Object类型的Future,但是根据我的新要求,我希望使用Generic Type的Future Object。 我已经修改了似乎有效的代码,但是有很多警告,而且我不确定我的更改是否正确。 我确定在某些情况下代码会失败。 我面临的主要问题是在单例类中初始化GenericType的HashMap。 谁能帮我这个?

下面的代码是现有代码的示例。

接口测试接口:

interface TestInterface extends Callable<Void>{
    void doSomething(Future<Object> future, String id); 
}

A级

class A{
 private Future<Object> future;
 private CustomInteraface a;

 public A(Future<Object> future, CustomInteraface a){
    //do init
 }
 //getters and setters
}

B级

Class B implements TestInterface{

    private HashMap<String, A> map = new HashMap();
    private static B monitor = new B();


    public Void call(){
        HashMap.Entry<String, A> pair = (HashMap.Entry<String, A>) it.next();
        A a = (A) pair.getValue();
        Future<Object> future = a.getFuture();
        // Do something
    }

    public void doSomething(Future<Object> future, String id){
        if(map.contains(id)){
            //Do something
        }
        else{
            A a = new A(future, null);
            map.put();
        }
    }

}

我为泛型者所做的更改

接口测试接口:

interface TestInterface extends Callable<Void>{
    <T> void doSomething(Future<T> future, String id);  
}

A级

class A<T>{
 private Future<T> future;
 private CustomInteraface a;

 public A(Future<T> future, CustomInteraface a){
    //do init
 }
 //getters and setters
}

B级

Class B implements TestInterface{

    private HashMap<String, A> map = new HashMap();
    private static B monitor = new B();


    public Void call(){
        HashMap.Entry<String, A> pair = (HashMap.Entry<String, A>) it.next();
        A a = (A) pair.getValue();
        //Code will definitely fail here as I'm trying to cast a future object of generic type to Object class
        Future<Object> future = a.getFuture();
        // Do something
    }

    public void doSomething(Future<T> future, String id){
        if(map.contains(id)){
            //Do something
        }
        else{
            A<T> a = new A<T>(future, null);
            map.put();
        }
    }

}

如果您可能将A<T>的异构类型放入映射中,则需要使用通配符声明该映射:

private HashMap<String, A<?>> map = new HashMap();

然后,您将从地图中获取值:

    // The cast was only necessary because A by itself is a raw type.
    HashMap.Entry<String, A<?>> pair = it.next();
    A<?> a = pair.getValue();
    Future<?> future = a.getFuture();
    // Note that future.get() yields an Object

并将其放入地图中,如下所示:

public void doSomething(Future<?> future, String id){
    ...
        A<?> a = new A<>(future, null);
        map.put(id, future);
    ...
}

如果在doSomething需要将来的T返回类型,则可以在方法上声明一个类型变量:

public <T> void doSomething(Future<T> future, String id){

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM