单例模式
来源:互联网 发布:百度阅读json文件转换 编辑:程序博客网 时间:2024/05/17 05:19
单例模式:保证一个类只有一个实例,并且提供一个访问该实例的全局访问点。
常见的五种单例模式:
1、懒汉式:线程安全、调用率低、支持延时加载
2、饿汉式:线程安全、调用率高、不延时加载
3、双重检测锁式
4、静态内类式
5、枚举单例
其中懒汉式和饿汉式较常用
(注:反射和反序列化可以破解单利模式)
示例代码:
//饿汉式public class test01{private static test01 s=new test01();private test01(){}//构造器私有化public static test01 getInstance(){return s;}}//懒汉式public class test02{private static test02 s;private test02 (){}//构造器私有化public static synchronized test02 getInstance(){if(s==null){s=new test02();}return s;}}
1 0
- 单例、单例模式
- 单例模式-多线程单例模式
- 单件模式(单例模式)
- 设计模式------单例模式
- 设计模式------单例模式
- 设计模式-单例模式
- 设计模式 - 单例模式
- 设计模式---单例模式
- 设计模式---单例模式
- PHP模式-单例模式
- 【设计模式】单例模式
- 设计模式-单例模式
- 设计模式----单例模式
- 设计模式--单例模式
- 设计模式-单例模式
- 单例模式(单子模式)
- 设计模式-单例模式
- [设计模式] 单例模式
- poj 2082 Terrible Sets
- 背包问题
- ZOJ-3802-Easy 2048 Again【状态压缩dp】【位运算】【好题】
- compoer安装
- 杭电2187
- 单例模式
- Leetcode-136:Single Number
- 杭电1161
- 使用svn:externals建立SVN文件(夹)链接
- erlang timer
- 树莓派3玩耍日志1——开箱、安装、开机试机
- VS2013 / MFC + FFmpeg实现录屏
- SVN回滚版本
- 杭电2163