当前位置:网站首页>创建型模式 - 单例模式Singleton
创建型模式 - 单例模式Singleton
2022-08-02 21:43:00 【Xiaohei.Wang(Wenhao)】
单例模式的定义与特点
创建型模式:
单例模式的定义:指一个类只有一个实例,且该类能自行创建这个实例的一种模式。
例如,Windows中只能打开一个任务管理器,这样可以避免因打开多个任务管理器窗口而造成内存资源的浪费,或出现各个窗口显示内容的不一致等错误。
在计算机系统中,还有Windows的回收站、操作系统中的文件系统、多线程中的线程池、显卡的驱动程序对象、打印机的后台处理服务、应用程序的日志对象、数据库的连接池、网站的计数器、Web 应用的配置对象、应用程序中的对话框、系统中的缓存等常常被设计成单例。
单例模式有3个特点:
- 单例类只有一个实例对象
- 该单例对象必须由单例类自行创建
- 单例类对外提供一个访问该单例的全局访问点
单例模式的优点和缺点
优点:
- 单例模式可以保证内存中只有一个实例对象,减少了内存的开销
- 避免了对资源的多重占用
- 单例模式的全局访问点,优化了对共享资源的访问
缺点:
学习代码:
namespace StudyDesignMode.Singleton
{
/// <summary>
/// 单例基类,防多线程问题
/// </summary>
/// <typeparam name="T"></typeparam>
public class Singleton<T> where T : class
{
protected Singleton() { }
public static readonly T Instance = new Lazy<T>(() => (T)Activator.CreateInstance(typeof(T), true)).Value;
}
/// <summary>
/// 懒汉模式
/// </summary>
public class SingletonL
{
#region 懒汉模式
SingletonL instance;
public SingletonL GetInstance()
{
if (null == instance)
instance = new SingletonL();
return instance;
}
#endregion
}
/// <summary>
/// 饿汉模式
/// </summary>
public class SingletonE
{
#region 饿汉模式
SingletonE instance = new SingletonE();
public SingletonE GetInstance()
{
return instance;
}
#endregion
}
/// <summary>
/// 内部静态模式
/// </summary>
public class SingletonJ
{
#region 内部静态类 //没啥卵用
private SingletonJ() { }
public static SingletonJ getInstance()
{
return InnerClass.instance;
}
public static class InnerClass
{
public static readonly SingletonJ instance = new SingletonJ();
}
#endregion
}
//========================================多地方吃亏而得.==================================
/// <summary>
/// 多线程安全的单例模式
/// </summary>
public class SingletonA
{
private SingletonA() { }
private volatile static SingletonA m_instance;
public static SingletonA Instance()
{
if (null == m_instance)
{
lock (new object())
{
if (null == m_instance)
{
m_instance = new SingletonA();
}
}
}
return m_instance;
}
}
/// <summary>
/// 系统懒加载式创建
/// </summary>
public class SingletonLazy
{
private SingletonLazy() { }
public static readonly SingletonLazy Instance =
new Lazy<SingletonLazy>(
new Func<SingletonLazy>(() => new SingletonLazy())
).Value;
}
}希望大家:点赞,留言,关注咯~
唠家常
- Xiaohei.Wang(Wenhao)的今日分享结束啦,小伙伴们你们get到了么,你们有没有更好的办法呢,可以评论区留言分享,也可以加我的QQ:841298494 (记得备注),大家一起进步
今日无推荐
- 客官,看完get之后记得点赞哟!
- 小伙伴你还想要别的知识?好的呀,分享给你们
- 小黑的杂货铺,想要什么都有,客官不进来喝杯茶么?
边栏推荐
- 搭建直播平台,使用node生成验证码图片,并进行验证
- 以赛促练-力扣第304场周赛反思(持续更新中)
- H.265视频流媒体播放器EasyPlayer.js集成时出现“SourceBuffer ”报错,该如何解决?
- TDengine 在中天钢铁 GPS、 AIS 调度中的落地
- 我用这一招让团队的开发效率提升了 100%!
- 无线振弦采集仪远程修改参数的方式
- Broadcast platform, the use of the node generated captcha image, and validate
- SSM整合步骤(重点)
- How does Redis easily achieve system instant kill?
- Jmeter二次开发实现rsa加密
猜你喜欢

网络运维系列:健康检查的方式

最近火爆朋友圈的“广告电商”,核心商业模式是什么,广告收入真实靠谱吗?

Do you understand the factory pattern?

Ruoyi integrates minio to realize distributed file storage

How does Redis easily achieve system instant kill?

“百日行动”进行时:700余交通安全隐患被揪出

SSM integration steps (emphasis)

ML之PDP:基于titanic泰坦尼克是否获救二分类预测数据集利用PDP部分依赖图对RF随机森林和LightGBM模型实现可解释性案例

I interviewed a 985 graduate, and I will never forget the expression when answering the "performance tuning" question

kubernetes pod podsecurityPolicies(PSP)
随机推荐
Add and delete all these years, finally planted in MySQL architecture design!
Flink优化的方方面面
RuoYi-App Startup Tutorial
win10桌面图标全部变成白色的怎么办
Word operation: adjust the English font individually
ML之PDP:基于titanic泰坦尼克是否获救二分类预测数据集利用PDP部分依赖图对RF随机森林和LightGBM模型实现可解释性案例
RuoYi-App启动教程
圆锥折射作为偏振计量工具的模拟
用于中文文本分类的中文停用词
手把手教你干掉if else
Intensive reading of the Swin Transformer paper and analysis of its model structure
You and I will meet the needs of: how to export the data in a MySQL simple ~!Practical!
@Transactional 事务调用与生效场景总结
CKA、CKAD、CKS、KCNA、CFCD考试
行业 SaaS 微服务稳定性保障实战
Swin Transformer 论文精读,并解析其模型结构
成功解决TypeError: can‘t multiply sequence by non-int of type ‘float‘
【TypeScript】深入学习TypeScript类(下)
30天啃透这份Framework 源码手册直接面进大厂
你离「TDengine 开发者大会」只差一条 SQL 语句!