当前位置:网站首页>创建型模式 - 单例模式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之后记得点赞哟!
- 小伙伴你还想要别的知识?好的呀,分享给你们
- 小黑的杂货铺,想要什么都有,客官不进来喝杯茶么?
边栏推荐
猜你喜欢
【Unity】Unity开发进阶(六)UnityEvent使用与源码解析
Flink优化的方方面面
手把手教你干掉if else
矩阵白化原理及推导
Sentinel vs Hystrix 限流对比,到底怎么选?
What is the core business model of the "advertising e-commerce" that has recently become popular in the circle of friends, and is the advertising revenue really reliable?
圆锥折射作为偏振计量工具的模拟
终于明白:有了线程,为什么还要有协程?
In-depth study TypeScript TypeScript 】 【 class (under)
工厂模式理解了没有?
随机推荐
Redis是如何轻松实现系统秒杀的?
Do you understand the factory pattern?
Add and delete all these years, finally planted in MySQL architecture design!
js function anti-shake and function throttling and other usage scenarios
YAML文件格式
CS5213芯片|HDMI to VGA转换头芯片资料分享
You and I will meet the needs of: how to export the data in a MySQL simple ~!Practical!
Summary of @Transactional transaction invocation and effective scenarios
[Dry goods] Best practice of sub-library and sub-table
源码构建LAMP环境-2
增删改查这么多年,最后栽在MySQL的架构设计上!
Auto.js实现朋友圈自动点赞
LeetCode 2360. 图中的最长环 基环树找环+时间戳
go os 包
golang刷leetcode:统计区间中的整数数目
Auto.js脚本程序打包
无线振弦采集仪远程修改参数的方式
【c】操作符详解(一)
word操作:单独调整英文字体
我用这一招让团队的开发效率提升了 100%!