Ad

Should You Create A Generic Singleton Class In Java?

- 1 answer

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

EDIT after Stephen C comment

** I am creating the Cache as an in memory cache which will be used by a client as a library for caching particular objects. so one cache could contain only objects of Cache<Integer, Integer> while another might be like Cache<String, Integer>. The client should first register the cache with the CacheManager before being able to use it so that the CacheManager can manage the cache for the client and clean expired items etc. instead of having client do these things. **

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> {

    // Cannot make a static reference to the non-static type K,V
    private static final CacheManager<K,V> singletonInstance = new CacheManager<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?

Ad

Answer

You asked two separate questions - the first being "Should You create a Generic Singleton class..." and "Is there a good way to do this (create a Generic Singleton class)".

To answer the first question, I would say no, do not attempt to create generic singleton classes, as making a class a singleton defeats the purpose of using generics.

By very nature, generics allows you to create a class that supports multiple types of data. However, if it's a singleton, only one instance of the class is ever intended to be used, and therefore, you should already know the data-types you'll be using for the duration of the program.

You cannot reference generic type parameters from a static context, as the static context is shared across all instance members of a class, which could include classes of various differing generic types.

To answer your second question, alas, also no, there is no elegant way of doing this. With additional context we may be able to suggest a better model to use to accomplish what you want.

Update in response to your update

Your CacheManager object shouldn't need to know the generic type of the caches that it's managing, in order to do housekeeping tasks. The CacheManager can simply be initilized as a singleton class without generic type parameters, such as:

public class CacheManager {

// No generic type information should be required.
private static final CacheManager singletonInstance = new CacheManager();
// This will allow a 'Cache' of any type to be registered.
private  Map<String, Cache> map = new HashMap<>();

The housekeeping and cache maintenance tasks shouldn't be concerned with the types of data that the client is keeping, but rather, you may have to create an additional data structure to support the time the client registered certain keys/values, and handle the removal of them.

To accomplish tracking the values that the client places in the Cache objects, perhaps you could simply include a map in the Cache class that maps the Key K to a long where you could store a timestamp for the last value update, then simply have the CacheManager query the cache object's keys for the last updated timestamp - this could even be iterated over with the entry set.

Ad
source: stackoverflow.com
Ad