Singleton pattern

Sometimes we need only one instance of a class in whole system, and “Singleton” was born.

Singleton is the simplest pattern in the design pattern world. It invokes only one class which is responsible to instantite itself, to make sure no more than one instance of it was created. This class is used for centralized management of resources and provides global access point to itself.

I usually use Singleton for Database connector, File system connector, ….

  1. Intent
    • There’s only one instance of class is created.
    • Provide a global access point to the instance.
  2. Implement

singleton_uml_classdiagram

 

The heart of singleton pattern is private constructor with a static Singleton memeber and a public static function which return a reference to the member. As we see it, this function is the gateway to create Singleton and access its function.

Here’s an example show how to implement Singleton pattern.

public class ObjectPool {
private ArrayList<Object> objects = new ArrayList<>();
// static member
private static ObjectPool instance;

// private constructor
private ObjectPool() {
// do something
}

// return reference to instance of private member
public static ObjectPool instance() {
if (instance == null) {
instance = new ObjectPool();
}
return instance;
}

public void push(Object object) {
// do something
objects.add(object);
}

public Object peek() {
return objects.get(0);
}
}

When we need to use ObjectPool, just call instance() function like that :

 ObjectPool.instance().push(object);

In Android, the Application class is an great example for Singleton pattern. At the running time, there’s only one instance of Application was created.

3. Singleton pattern and thread-safe implementation :

Above source work fine incase single thread enviroment. However, when we want to call instance() function in multiple thread enviroment, we will get big issues. For example, if two threads ‘re execute line code inside if loop at the same time, so they has sparate their own instance of singleton class. So we need to change above code for multi-threading enviroment. There’re several method to do this.

  • Using synchronization method
public static synchronized ObjectPool instance() {
if (instance == null) {
instance = new ObjectPool();
}
return instance;
}

This code work fine in multi-threading however it maybe reduce performance associate to synchronized function. We see that this code always check sychorinzation although singleton  was created. To avoid extra overhead, double locking mechanism was borm.

  • Using double locking mechanism
public static ObjectPool instance() {
if (instance == null) {
synchronized (ObjectPool.class) { // take monitor for synchronized method.
if (instance == null) {
instance = new ObjectPool();
}
}

}
return instance;
}

These lines of code will check synchronization incase singleton instance equals to null.

  • Using implement with static field

This is a simpler way to implement Singleton in thread-safe : early instantiation using implementation with static memeber. With it above code is changed like that

// static member
private static ObjectPool instance = new ObjectPool();

// return reference to instance of private member
public static ObjectPool instance() {
return instance;
}

As we see it is really simple to implement. However instance of singleton class ‘re created at time class loading, so it is always created although application don’t call. This implementation is suiltable when singleton doesn’t load more resources.

These common situations i work with Singleton pattern. Beside there’re some hack i don’t mention here, such as : broking Singleton pattern with Reflection, implement Singleton with static inner class helper (Bill Pugh solution), ….

Related links :

– http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.3.6

– http://docs.oracle.com/javase/tutorial/reflect/

Leave a Reply

Your email address will not be published. Required fields are marked *