Archive for August 2010

ProblemAt most one instance of a class must be created in an application.

SolutionThat class (singleton) is defined including its own instance, and the constructor must be private.

Implementations

1. Lazy initialization, non-thread safe: This is the classical version, bot it's not thread safe. If more than one thread attempts to access instance at the same time, more than one instance may be created.

public class Singleton {

private static Singleton instance = null;

public static Singleton Instance() {

if (instance == null) {

instance = new Singleton();

}

return instance;

}

private Singleton() {}

}

2. Non-lazy initialization, thread safe: This is the simplest thread safe version, but it does not support lazy initialization.

public class Singleton {

private readonly static Singleton instance = new Singleton();

public static Singleton Instance() {

return instance;

}

private Singleton() {}

}

3. Lazy initialization, thread safe: This version supports both properties, but has performance problems. Once a thread uses singleton instance, the others have to wait because of lock.

4. Double-check locking: An improved version of the third solution. Two null controls prevent lock waits for most time, but not always. Also, it does not work properly for Java because of Java memory management approach.

5. Nested initialization: A nested class is used for lazy initialization. This version is also thread safe, but a bit complex. For most situations, solutions 2 or 4 will be suitable according to performance parameters.