Thursday, December 24, 2009

Singleton Pattern with Lazy Initialization On Demand Holder

This is one of the better ways to implementing a singleton class (class which lets user create one and only one instance).

It uses Initialization on demand holder pattern.



public class Singleton {
private static class LazyHolder {
private static final Singleton instance = new Singleton();
}

public static Singleton getInstance() {
return LazyHolder.instance;
}

private Singleton() {
//initialize singleton instance
}
}

No comments: