首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

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

2012-10-14 
Singleton 单列模式的java实现(转)Singleton 单列模式在实际应用的机会很多。最近刚好碰到,所以顺便总结了

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;}}


建议大家使用第三种。

热点排行