设计模式-单例
来源:互联网 发布:linux echo 串口 编辑:程序博客网 时间:2024/05/29 09:42
为了保持对象的一致性,实现对象只被创建一次,单例模式被经常用到,整理了几种常用的写法如下:
普通单例模式
基本满足日常使用的需要
public class Singleton { private static Singleton instance; public static Singleton getInstance() { if (instance == null) { instance = new Singleton (); } return instance; } private Singleton () { }}
双重校验单例模式
应用于高并发情况
public class Singleton { private static Singleton instance; public static Singleton getInstance() { if (instance == null) { synchronized (Singleton .class) { if (instance == null) { instance = new Singleton (); } } } return instance; } private Singleton () { }}
Effective Java中提到的单元素枚举类型实现
无偿提供了序列化机制,可应对序列化及反射攻击
public enum Singleton { INSTANCE; public void test(){ }}
0 0
- 设计模式--单例
- 单例设计模式
- 设计模式----单例
- 单例设计模式
- 单例设计模式
- 单例设计模式
- 单例设计模式
- 单例设计模式
- 单例设计模式
- 单例设计模式
- 设计模式-单例
- 单例设计模式
- 单例设计模式
- 单例设计模式
- 单例设计模式
- 单例设计模式
- 单例设计模式
- 单例设计模式
- Linux Centos下安装与设置Vim编辑器和基本配置
- 页面JS复制功能
- Android关于Dex拆分(MultiDex)技术的解析
- vold挂载管理
- CentOS7桌面版安装中文输入法
- 设计模式-单例
- 页面引导
- mongodb的sharding(分片)横向扩容的方法
- Android MD风格(仿饿了么透明状态栏) app换肤(夜间模式与日间模式主题切换)
- json 的优点
- java创建线程的三种方式及其对比
- 逻辑回归
- %{TIMESTAMP_ISO8601} 匹配2016-08-29 17:40:01,191
- introduce page