经典单例模式的实现:
来源:互联网 发布:淘宝注册网店网址 编辑:程序博客网 时间:2024/05/16 11:21
经典单例模式的实现:
public class Singleton { // Private constructor prevents instantiation from other classes private Singleton() { } /** * SingletonHolder is loaded on the first execution of Singleton.getInstance() * or the first access to SingletonHolder.INSTANCE, not before. */ private static class SingletonHolder { public static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.INSTANCE; }}
WHY? http://en.wikipedia.org/wiki/Singleton_pattern