单例模式
来源:互联网 发布:hr人力资源软件排名 编辑:程序博客网 时间:2024/04/29 20:20
实现方式
- 懒汉模式
public class Singleton { private static Singleton instance; private Singleton (){} public static synchronized Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }
- 饿汉模式
public class Singleton { ////类加载时实例化对象 private static Singleton instance = new Singleton(); private Singleton (){} public static Singleton getInstance() { return instance; } }
- 双重校验锁(DCL,即 double-checked locking)
public class Singleton { private volatile static Singleton singleton;//延迟加载 private Singleton (){} public static Singleton getSingleton() { if (singleton == null) { synchronized (Singleton.class) {//同步锁 if (singleton == null) { singleton = new Singleton(); } } } return singleton; } }
- 登记式
public class Singleton { private static final class Holder { //静态内部类 static final Singleton instance = new Singleton(); } private Singleton() {} public static Singleton getInstance() { return Holder.instance; } }
- 枚举方式
public enum Singleton { INSTANCE; public void whateverMethod() { } }
0 0
- 单例、单例模式
- 单例模式-多线程单例模式
- 单件模式(单例模式)
- 设计模式------单例模式
- 设计模式------单例模式
- 设计模式-单例模式
- 设计模式 - 单例模式
- 设计模式---单例模式
- 设计模式---单例模式
- PHP模式-单例模式
- 【设计模式】单例模式
- 设计模式-单例模式
- 设计模式----单例模式
- 设计模式--单例模式
- 设计模式-单例模式
- 单例模式(单子模式)
- 设计模式-单例模式
- [设计模式] 单例模式
- 怎样在自己的网站上集成paypal国际支付方式
- Python GUI 教程 25行代码写一个小闹钟
- 泛型反射技巧总结
- Databricks、Intel、BAT齐聚,2015 Spark峰会火花四射
- c++ keywords
- 单例模式
- Python GUI编程(Tkinter)
- 读书的日子
- 如何将exel表中数据存入到数据库
- chown的权限限制--普通用户无法将owner改成其他用户,如root
- ios 真实随机数
- 20150313 N1
- 黑马程序员——OC面向对象编程
- Java工厂模式