lundi 19 mars 2018

Can a Singleton class be generic?

I want to have a rest client for my application. As Singletons are used for centralized management of internal or external resources and they provide a global point of access to themselves. I thing the rest client should be implemented as a Singleton class.

Can I implement the class in a generic way so that I can somehow control the type of the object I want the methods of the class to return.

I am looking for something like:

public class JersyRestClient <T> implements RestClient
{
    private static JersyRestClient instance = null;

    private JersyRestClient()
    {

    }

    public static JersyRestClient getInstance()
    {
        if (instance == null)
        {
            synchronized (JersyRestClient.class)
            {
                if (instance == null)
                {
                    instance = new JersyRestClient();
                }
            }
        }
        return instance;
    }

    public T getContent(final String resourceUrl)
    {
        //get content
        //return T
    }

}

Aucun commentaire:

Enregistrer un commentaire