设计模式-单例模式
来源:互联网 发布:电信百事通需要网络吗? 编辑:程序博客网 时间:2024/06/09 04:17
1、饿汉式(静态常量)[可用]
public class Singleton { private final static Singleton INSTANCE = new Singleton(); private Singleton(){} public static Singleton getInstance(){ return INSTANCE; }}
2、饿汉式(静态代码块)[可用]
public class Singleton { private static Singleton instance; static { instance = new Singleton(); } private Singleton() {} public Singleton getInstance() { return instance; }}
3、双重检查[推荐用]
public class Singleton { private static volatile Singleton singleton; private Singleton() {} public static Singleton getInstance() { if (singleton == null) { synchronized (Singleton.class) { if (singleton == null) { singleton = new Singleton(); } } } return singleton; }}
4、静态内部类[推荐用]
public class Singleton { private Singleton() {} private static class SingletonInstance { private static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonInstance.INSTANCE; }}
5、枚举[推荐用]
public enum Singleton { INSTANCE; public void whateverMethod() { }}
0 0
- 设计模式------单例模式
- 设计模式------单例模式
- 设计模式-单例模式
- 设计模式 - 单例模式
- 设计模式---单例模式
- 设计模式---单例模式
- 【设计模式】单例模式
- 设计模式-单例模式
- 设计模式----单例模式
- 设计模式--单例模式
- 设计模式-单例模式
- 设计模式-单例模式
- [设计模式] 单例模式
- 设计模式--单例模式
- 设计模式---单例模式
- 设计模式--单例模式
- 设计模式 -----单例模式
- 设计模式:单例模式
- odoo和postgres的安装配置
- NOIP2016提高组总结
- cocos2d 由导出文件.csb反推出cocosUI工程
- 通过winSCP打开putty乱码问题的解决
- Lucene查询语法详解(Lucene query syntax)- 用于Kibana搜索语句
- 设计模式-单例模式
- Tiny210(S5PV210) U-BOOT(一)----启动过程
- 嵌入式开发中http传输二进制数据的注意事项
- Mysql主从复制,读写分离(mysql-proxy),双主结构完整构建过程
- AngularJS(二)_指令ng-*
- Weex开发笔记
- [Android]Android Studio获取开发版和发布版的MD5和SHA1
- 设置EditText只能输入数字和字母
- 影响Linux YUM下载软件以及依赖包的因素