读书人

Singleton 单列方式的java实现(转)

发布时间: 2012-10-14 14:55:07 作者: rapoo

Singleton 单列模式的java实现(转)
Singleton 单列模式在实际应用的机会很多。

最近刚好碰到,所以顺便总结了下。

一、最常见的实现如下:(不支持多线程)

public class Singleton {// Private constructor prevents instantiation from other classesprivate Singleton() {}/*** SingletonHolder is loaded on the first execution of Singleton.getInstance()* or the first access to SingletonHolder.INSTANCE, not before.*/private static class SingletonHolder {private static final Singleton INSTANCE = new Singleton();}public static Singleton getInstance() {return SingletonHolder.INSTANCE;}}


建议大家使用第三种。

读书人网 >软件架构设计

热点推荐