c# 设计模式-单例模式
来源:互联网 发布:labview编程详解书 编辑:程序博客网 时间:2024/06/08 05:40
1、单线程简单实现
public class SingletonObject { private static SingletonObject instance = null; public static SingletonObject Instance { get { if (instance == null) { instance = new SingletonObject(); } return instance; } } private SingletonObject() { } }
2、处理多线程
public class SingletonObject { private static SingletonObject instance = null; private static readonly object lockObj = new object(); public static SingletonObject Instance { get { lock(lockObj) { if (instance == null) { instance = new SingletonObject(); } } return instance; } } private SingletonObject() { } }
public class SingletonObject { private static SingletonObject instance = null; private static readonly object lockObj = new object(); public static SingletonObject Instance { get { if(instance == null) { lock (lockObj) { if (instance == null) { instance = new SingletonObject(); } } } return instance; } } private SingletonObject() { } }
4、静态初始化
(1)直接初始化方式
public class SingletonObject { private static SingletonObject instance = new SingletonObject(); public static SingletonObject Instance { get { return instance; } } private SingletonObject() { } }
(2)使用静态构造函数
public class SingletonObject { private static SingletonObject instance = null; public static SingletonObject Instance { get { return instance; } } static SingletonObject() { instance = new SingletonObject(); } private SingletonObject() { } }
0 0
- C#单例设计模式
- C#单例设计模式
- C#设计模式(单例
- C# 单例设计模式
- C# 单例设计模式
- c# 设计模式:单例模式
- c# 设计模式:单例模式
- C#设计模式之单例模式
- C#设计模式-单例模式
- C#设计模式(1)单例模式
- C#设计模式_单例模式
- C#设计模式04-单例模式
- 【C#设计模式-单例模式】
- c#设计模式-单例模式
- C#设计模式-单例模式
- c#设计模式-单例模式
- c#设计模式之单例模式
- C#设计模式-单例模式
- C#中using用法
- fl2440添加USB驱动,并添加ntfs的写入
- 机器学习笔记(三) 朴素贝叶斯分类
- 拍卖(京东2017实习生招聘真题)
- url中文乱码
- c# 设计模式-单例模式
- python学习笔记(四)模块、面向对象
- 递推求解专题训练
- Pyhon 网络爬虫--简单的爬取功能
- 判断元素在数组中出现的次数
- 高德地图应用试开发--第二版(百度试验号已阵亡)
- 文章标题
- Maven Spring MVC + MyBatis 开发配置
- mysql数据库常用sql的分类整理