lundi 12 février 2018

Should you create a Generic Singleton class in java?

I am trying to create a CacheManager class for my cache in which I want to register an object of type 'Cache'. My Cache can be generic and accepts two Key of type K and value of Type V

Cache.java

public interface Cache<K,V> {
    public V get(K key);
    public void put(K key, V value);
    public K remove(K Key);
    public void clear();
}

Cache Manager.java

public class CacheManager<K,V> {

private static final CacheManager<K,V> singletonInstance = new CacheManager<K,V>();//   Cannot make a static reference to the non-static type K,V
    private  Map<String, Cache<K,V>> map = new HashMap<>();

    private CacheManager() {}

    public static CacheManager<K,V> getSingletonInstance() {
        return singletonInstance;
    }


    public void addCache(Cache<K,V> cache) {
       //code goes here
    }
}

I want to create only a single instance of the CacheManager hence the idea for Singleton class and quite obviously it is giving the error of non static type K,V . Is there a good way to do this?

Aucun commentaire:

Enregistrer un commentaire