单例模式
来源:互联网 发布:php测试工具 编辑:程序博客网 时间:2024/05/01 12:43
单例就是该类只能返回一个实例。
单例所具备的特点:
1.私有化的构造函数
2.私有的静态的全局变量
3.公有的静态的方法
单例分为懒汉式、饿汉式和双层锁式
饿汉式:
public class Singleton1 { private Singleton1() {}; private static Singleton1 single = new Singleton1(); public static Singleton1 getInstance() { return single; } }
懒汉式:
public class Singleton2 { private Singleton2() {} private static Singleton2 single=null; public tatic Singleton2 getInstance() { if (single == null) { single = new Singleton2(); } return single; } }
线程安全:
public class Singleton3 { private Singleton3() {} private static Singleton3 single ; public static Singleton3 getInstance() { if(null == single){ synchronized(Singleton3.class){ if(null == single){ single = new Singleton3(); } } } return single; }}
阅读全文
0 0
- 单例、单例模式
- 单例模式-多线程单例模式
- 单件模式(单例模式)
- 设计模式------单例模式
- 设计模式------单例模式
- 设计模式-单例模式
- 设计模式 - 单例模式
- 设计模式---单例模式
- 设计模式---单例模式
- PHP模式-单例模式
- 【设计模式】单例模式
- 设计模式-单例模式
- 设计模式----单例模式
- 设计模式--单例模式
- 设计模式-单例模式
- 单例模式(单子模式)
- 设计模式-单例模式
- [设计模式] 单例模式
- 安卓联网框架 Retrofit
- 瀑布流和一级购物车
- 机器学习常用python语句总结
- TabLayout
- 路由器LAN口
- 单例模式
- 【设计模式】策略模式
- 基于ArcGIS_10.0的地表高程序贯高斯模拟和地质统计建模
- CodeForces
- mymoni3——具体代码~Activity类 + Fragfment类 的具体逻辑代码(+外部类MAsyncTask)
- Java IO流面试题
- 百万级数据优化
- python自动化之迭代器
- python--join函数