Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a configurable singleton?

This question is related to Android but can also be asked in other situations. I need to create a library where singletons are exposed; or I want to ensure only one instance of my classes exists and can be grabbed anywhere in the code without passing references.

But thoses singletons needs some parameters. For instance in Android, a Context object is often needed. I must also precise that since I provide a library, I want things to be easy for the user and I do not have control of the Application class in Android (this class can sometimes be used to manage object instances for a whole app).

One known solution is to do the following:

static MySingleton sInstance;
MySingleton.getInstance(Context c) {
    if (sInstance == null) {
        sInstance = new MySingleton(c.getApplicationContext());
    }
    return sInstance;
}

But it is strange since the parameter of getInstance is actually only used at the first creation of the singleton.

I could provide a setter to the singleton and asks the developer to correctly set the needed parameters, but some strange situations could arise:

// Good usage
MySingleton.getInstance().setContext(context.getApplicationContext());
MySingleton.getInstance().doSomethingThatRequiresContext(); // OK

// Bad usage
MySingleton.getInstance().doSomethingThatRequiresContext(); // Error!
MySingleton.getInstance().setContext(context.getApplicationContext());

I could check at the start of each method if the singleton is correctly configured and launch some exceptions in case of a bad state, but the API is less straightforward to use:

MySingleton.getInstance().setContext(context.getApplicationContext());

try {
    MySingleton.getInstance().doSomethingThatRequiresContext();
}
catch(BadSingletonConfiguration e) { }

Even if I use runtime exceptions, it would be dangerous to use.

Unless I kindly ask the user to manually create the instance and to ensure himself only one instance will exist, I do not see a good solution.

like image 285
Vincent Hiribarren Avatar asked Sep 26 '22 18:09

Vincent Hiribarren


1 Answers

You could have a createInstance method that takes a Context and a getInstance that will return null or throw some meaningful exception if they call getInstance before create instance. Maybe throw a RuntimeException stating that createInstance must be called first.

Also, createInstance will just return the already created instance if it has already been called. Here is a code sample of what I'm thinking:

public class MySingleton
{
    private static MySingleton INSTANCE;
    private final Context context;

    public static MySingleton createInstance(Context context)
    {
        if(INSTANCE == null)
        {
            INSTANCE = new MySingleton(context);
        }
        return INSTANCE;
    }

    public static MySingleton getInstance()
    {
        if(INSTANCE == null)
        {
            throw new RuntimeException("You must call createInstance first");
        }
        return INSTANCE;
    }

    public void doSomethingThatRequiresContext()
    {
        context.doSomething();
    }

    private MySingleton(Context context)
    {
        this.context = context;
    }
}
like image 154
Josh Chappelle Avatar answered Oct 12 '22 12:10

Josh Chappelle