当前位置: 首页 > news >正文

继承实现单例模式的探索(一)

前言

之前看到朋友采用继承的方式来实现单例模式,觉得很厉害,随后自己去探索了一番,以前实现单例模式都是把代码内联到具体的类中,这使得工程中每次需要使用单例模式时,都采用拷贝的方式,增加了很多冗余代码,并且难以规范单例的统一标准,使得代码不方便扩展和管理。这次探索找到了一种实现方式,先记录下来,后续如果有其它方式再发表系列文章,示例代码为C#。

代码

v1.0版本

using System;/// <summary>
/// 单例模式基类
/// </summary>
/// <typeparam name="T">单例类型</typeparam>
public abstract class Singleton<T>
where T : class, new()
{public static T instance => _instance.Value;static bool _unlock;static readonly Lazy<T> _instance = new Lazy<T>(() =>{_unlock = true;return new T();});protected Singleton(){if (!_unlock)throw new InvalidOperationException("Singleton:The ctor is proxied by singleton and cannot be called from outside.");_unlock = false;}
}

v1.1 

using System;/// <summary>
/// 单例模式基类
/// </summary>
/// <typeparam name="T">单例类型</typeparam>
public abstract class Singleton<T>
where T : class, new()
{public static T instance => _instance.Value;static bool _unlock;static readonly Lazy<T> _instance = new Lazy<T>(Create);static T Create(){_unlock = true;T item = new T();_unlock = false;return item;}protected Singleton(){if (!_unlock)throw new InvalidOperationException("Singleton:The ctor is proxied by singleton and cannot be called from outside.");}
}

测试

单例基类

using System;/// <summary>
/// 单例模式基类
/// 版本1.0
/// </summary>
/// <typeparam name="T">单例类型</typeparam>
// public abstract class SingletonWithTest<T>
// where T : class, new()
// {
//     public static T instance => _instance.Value;
//     static bool _unlock;
//     static readonly Lazy<T> _instance = new Lazy<T>(() =>
//     {
//         _unlock = true;
//         return new T();
//     });//     protected SingletonWithTest()
//     {
//         if (!_unlock)
//         {
//             // throw new InvalidOperationException("Singleton:The ctor is proxied by singleton and cannot be called from outside.");
//             Console.WriteLine($"Singleton({GetHashCode()}):The ctor is proxied by singleton and cannot be called from outside.");
//             return;
//         }
//         _unlock = false;
//         Console.WriteLine("*************************");
//     }
// }/// <summary>
/// 单例模式基类
/// 版本1.1
/// </summary>
/// <typeparam name="T">单例类型</typeparam>
public abstract class SingletonWithTest<T>
where T : class, new()
{public static T instance => _instance.Value;static bool _unlock;static readonly Lazy<T> _instance = new Lazy<T>(Create);static T Create(){_unlock = true;T item = new T();_unlock = false;return item;}protected SingletonWithTest(){if (!_unlock){// throw new InvalidOperationException("Singleton:The ctor is proxied by singleton and cannot be called from outside.");Console.WriteLine($"Singleton({GetHashCode()}):The ctor is proxied by singleton and cannot be called from outside.");return;}Console.WriteLine("*************************");}
}

单例继承类

public class TestA : SingletonWithTest<TestA>
{public readonly string key;public TestA() { key = "Default A"; }public TestA(string key) { this.key = key; }
}public class TestB : SingletonWithTest<TestB>
{public readonly string key;public TestB() { key = "Default B"; }public TestB(string key) { this.key = key; }
}public class TestC : SingletonWithTest<TestC>
{public readonly string key;public TestC() { key = "Default C"; }public TestC(string key) { this.key = key; }
}public class TestD : SingletonWithTest<TestD>
{public readonly string key;public TestD() { key = "Default D"; }public TestD(string key) { this.key = key; }
}public class TestE : SingletonWithTest<TestE>
{public readonly string key;public TestE() { key = "Default E"; }public TestE(string key) { this.key = key; }
}public class TestF : SingletonWithTest<TestF>
{public readonly string key;public TestF() { key = "Default F"; }public TestF(string key) { this.key = key; }
}

测试代码

// ********************************* 创建实例测试:通过 ********************************* // 直接创建实例测试:通过
// TestA a = new TestA(); // 无法通过 new + 无参ctor 创建实例
// TestA a1 = new TestA("A1"); // 无法通过 new + 有参ctor 创建实例
// TestA a2 = Activator.CreateInstance<TestA>(); // 无法通过 Activator 创建实例
// TestA? a2 = Activator.CreateInstance(typeof(TestA), "A1") as TestA; // 无法通过 Activator 创建实例// ********************************* 线程安全测试:通过 *********************************Thread t1, t2, t3, t4, t5, t6;// 打印同一单例的 HashCode 测试:通过
t1 = new Thread(() => Console.WriteLine("Thread1:" + TestA.instance.GetHashCode()));
t2 = new Thread(() => Console.WriteLine("Thread2:" + TestA.instance.GetHashCode()));
t3 = new Thread(() => Console.WriteLine("Thread3:" + TestA.instance.GetHashCode()));
t4 = new Thread(() => Console.WriteLine("Thread4:" + TestA.instance.GetHashCode()));
t5 = new Thread(() => Console.WriteLine("Thread5:" + TestA.instance.GetHashCode()));
t6 = new Thread(() => Console.WriteLine("Thread6:" + TestA.instance.GetHashCode()));// 使用单例的同时直接创建该单例类型实例测试:通过
// t1 = new Thread(() => Console.WriteLine("Thread1:" + TestA.instance.GetHashCode()));
// t2 = new Thread(() => Console.WriteLine("Thread2:" + new TestA().GetHashCode()));
// t3 = new Thread(() => Console.WriteLine("Thread3:" + new TestA().GetHashCode()));
// t4 = new Thread(() => Console.WriteLine("Thread4:" + new TestA().GetHashCode()));
// t5 = new Thread(() => Console.WriteLine("Thread5:" + new TestA().GetHashCode()));
// t6 = new Thread(() => Console.WriteLine("Thread6:" + new TestA().GetHashCode()));// 同时使用不同单例的测试:通过
// t1 = new Thread(() => Console.WriteLine("Thread1:" + TestA.instance.GetHashCode()));
// t2 = new Thread(() => Console.WriteLine("Thread2:" + TestB.instance.GetHashCode()));
// t3 = new Thread(() => Console.WriteLine("Thread3:" + TestC.instance.GetHashCode()));
// t4 = new Thread(() => Console.WriteLine("Thread4:" + TestD.instance.GetHashCode()));
// t5 = new Thread(() => Console.WriteLine("Thread5:" + TestE.instance.GetHashCode()));
// t6 = new Thread(() => Console.WriteLine("Thread6:" + TestF.instance.GetHashCode()));// 使用单例的同时创建其它单例类型的实例测试:通过
// t1 = new Thread(() => Console.WriteLine("Thread1:" + TestA.instance.GetHashCode()));
// t2 = new Thread(() => Console.WriteLine("Thread2:" + new TestB().GetHashCode()));
// t3 = new Thread(() => Console.WriteLine("Thread3:" + new TestC().GetHashCode()));
// t4 = new Thread(() => Console.WriteLine("Thread4:" + new TestD().GetHashCode()));
// t5 = new Thread(() => Console.WriteLine("Thread5:" + new TestE().GetHashCode()));
// t6 = new Thread(() => Console.WriteLine("Thread6:" + new TestF().GetHashCode()));t1.Start();
t2.Start();
t3.Start();
t4.Start();
t5.Start();
t6.Start();t1.Join();
t2.Join();
t3.Join();
t4.Join();
t5.Join();
t6.Join();

优缺点分析

优点

1.通过继承实现单例;

2.可以通过单例基类规范统一标准;

3.线程安全;

4.无法通过除单例基类提供的静态属性instance以外的其它方式获取其派生类实例,外部通过new关键字显示调用构造函数或反射等其它获取实例的方式创建派生类实例将触发异常;

5.派生类的无参构造函数用于初始化;

6.按需加载,延迟初始化。

缺点

1.要求派生类的无参构造函数公开;

2.派生类对外部始终开放无参构造函数,无法避免new关键字的显式调用所触发的异常; 

版本改进

V1.1

1.将延迟初始化的工厂方法从Lambda表达式替换为本地静态方法,因为_unlock相对于Lazy<T>是外部引用,所以不可避免存在创建闭包的开销,所以改为本地静态方法进行改进;

2._unlock在构造函数中进行重置会存在线程安全的问题,放在作为延迟初始化的工厂方法的本地静态方法中可以保证线程安全。

......

系列文章

继承实现单例模式的探索(二)

如果这篇文章对你有帮助,请给作者点个赞吧!

相关文章:

继承实现单例模式的探索(一)

前言 之前看到朋友采用继承的方式来实现单例模式&#xff0c;觉得很厉害&#xff0c;随后自己去探索了一番&#xff0c;以前实现单例模式都是把代码内联到具体的类中&#xff0c;这使得工程中每次需要使用单例模式时&#xff0c;都采用拷贝的方式&#xff0c;增加了很多冗余代码…...

【代码实现】opencv 高斯模糊和pytorch 高斯模糊

wiki百科 Gaussian Blur&#xff0c;也叫高斯平滑&#xff0c;是在Adobe Photoshop、GIMP以及Paint.NET等图像处理软件中广泛使用的处理效果&#xff0c;通常用它来减少图像噪声以及降低细节层次。 opencv实现 opencv实现高斯滤波有两种方式&#xff0c; 1、是使用自带的cv2…...

python基础语法2

文章目录 1.顺序语句2.条件语句2.1 语法格式 3.缩进与代码块4.空语句 pass5.循环语句5.1 while循环5.2 for循环 5.3 continue与break 1.顺序语句 默认情况下&#xff0c;python的代码都是按照从上到下的顺序依次执行的。 print(hello ) print(world)结果一定是hello world。写…...

linux第一课:下载与安装

这是我的个人复习笔记&#xff0c;草稿箱字太多会卡就发这了&#xff0c;欢迎大家阅读。 Kali Linux&#xff0c;黑客必备神器。跟着我&#xff0c;带你从入门到入狱&#xff01; 第一课&#xff0c;下载与安装。 第一步&#xff1a; 在官网下载Centos镜像&#xff1a;http…...

虚拟机添加共享文件夹后仍无法显示文件

参考: https://blog.csdn.net/Pretender_1205/article/details/134859089 进入/mnt/hgfs目录下执行 sudo mount -t fuse.vmhgfs-fuse .host:/ /mnt/hgfs -o allow_other/mnt/hgfs 是挂载点&#xff0c;也可以修改为其他挂载点-o allow_other表示允许其他用户(普通用户)访问共…...

OSPF协议

基础知识 OSPF:开放式最短路径优先协议 (无类别链路状态IGP动态协议) OSPF的特点&#xff1a; 1.OSPF将自治系统划分为逻辑上的区域&#xff0c;使用LSA来发布路由信息&#xff0c;并通过OSPF报文在区域内路由器之间交互建立链路状态数据库和路由表 2.支持等开销的负载均衡…...

行为设计模式 -观察者模式- JAVA

观察者模式 一.简介二. 案例2.1 抽象主题&#xff08;Subject&#xff09;2.2 具体主题&#xff08;Concrete Subject&#xff09;2.3 抽象观察者&#xff08;Observer&#xff09;2.4 具体观察者&#xff08;Concrete Observer&#xff09;2.5 测试 三. 结论3.1 优缺点3.2 使用…...

在阿里工作是一种什么体验?

很多人都对在阿里工作感到好奇&#xff0c;今天就来给大家分享一下在阿里工作是一种什么体验~ 首先&#xff0c;先来介绍一下阿里的职位等级划分标准。 简单来讲&#xff0c;阿里的职位等级可以认为是 P 序列和 M 序列&#xff0c;但目前 M 序列已经不太对中下层员工开放了&…...

828华为云征文|华为云Flexus云服务器X实例——uniapp功能开发、搭建股票系统选择用什么服务器比较好?

在二次开发、安装搭建股票系统时&#xff0c;选择华为云Flexus X服务器是一个值得考虑的优质选项。以下是一些具体的建议&#xff1a; 测试环境&#xff1a;Linux系统CentOS7.6、宝塔、PHP7.3、MySQL5.7&#xff0c;根目录public&#xff0c;伪静态thinkphp&#xff0c;开启ssl…...

电子电路元件器介绍与选型——晶振

一、晶振 在我们使用嘉立创的时候&#xff0c;经常会看到晶振接到两个电容&#xff0c;这两个电容毫无疑问是滤波的&#xff0c;整个晶振其实就是一个振荡器&#xff0c;但这个振荡器会将其他频率给过滤掉&#xff0c;只保留一个频率也就是晶振的标称频率。当然上面讲的很明显是…...

【IEEE PDF eXpress】格式不对

目录 一、问题二、解决方法 一、问题 word的文档&#xff0c;用IEEE PDF eXpress网站生成pdf后&#xff0c;提交论文出现错误&#xff1a; Document validation failed due to the following errors: Content exceeds IEEE template margins for its format (Page 1:Bottom).…...

OpenAI全新多模态内容审核模型上线:基于 GPT-4o,可检测文本和图像

在数字时代&#xff0c;内容安全问题愈发受到重视。9月26日&#xff0c;OpenAI 正式推出了一款全新的多模态内容审核模型&#xff0c;名为 “omni-moderation-latest”。 该模型基于最新的 GPT-4o 技术&#xff0c;能够准确地识别检测有害文本图像。这一更新将为开发者提供强大…...

Visual Studio 字体与主题推荐

个人推荐&#xff0c;仅供参考&#xff1a; 主题&#xff1a;One Monokai VS Theme 链接&#xff1a;One Monokai VS Theme - Visual Studio Marketplacehttps://marketplace.visualstudio.com/items?itemNameazemoh.onemonokai 效果&#xff1a; 字体&#xff1a;JetBrain…...

信息学奥赛一本通 1416:【17NOIP普及组】棋盘 | 洛谷 P3956 [NOIP2017 普及组] 棋盘

【题目链接】 洛谷 P3956 [NOIP2017 普及组] 棋盘 ybt 1416&#xff1a;【17NOIP普及组】棋盘 【题目考点】 1. 深搜&#xff1a;深搜回溯 2. 深搜剪枝&#xff1a;最优化剪枝 【解题思路】 搜索从左上角到右下角的所有走法中花费金币最少的走法。 需要使用深搜回溯&…...

UE4完整教程 UE4简介 UE4学习攻略及文件格式

开头附上工作招聘面试必备问题噢~~包括综合面试题、无领导小组面试题资源文件免费!全文干货。 UE4简介学习攻略UE4Demo代码面试内容资源-CSDN文库https://download.csdn.net/download/m0_72216164/89825102 工作招聘无领导小组面试全攻略最常见面试题(第一部分)共有17章+可…...

JVM内存回收机制

目录 1.JVM运行时数据区 2.JVM类加载过程 3.双清委派模型 4.垃圾回收机制&#xff08;GC&#xff09; 找出谁是垃圾方案一&#xff1a;引用计数 找出谁是垃圾&#xff1a;方案二&#xff0c;可达性分析 释放垃圾的内存空间 判断垃圾&#xff1a;jvm依据对象的年龄对 对象…...

中国身份证号码校验

题目描述 第二届河南省最美教师评选开始了&#xff0c;每一位同学都可以投票选出你支持的人选&#xff0c;但是为了防止刷票&#xff0c;必须通过身份验证才可投票。负责投票平台后台的老大爷希望你能帮他验证身份证号的合法性&#xff0c;防止那些熊孩子随意刷票&#xff0c;…...

【Kubernetes】常见面试题汇总(五十四)

目录 120.创建 init C 容器后&#xff0c;其状态不正常&#xff1f; 特别说明&#xff1a; 题目 1-68 属于【Kubernetes】的常规概念题&#xff0c;即 “ 汇总&#xff08;一&#xff09;~&#xff08;二十二&#xff09;” 。 题目 69-113 属于【Kubernetes】的生产…...

不懂外语也能无障碍交流?探索4款超好用中英翻译工具

嘿&#xff0c;各位外贸流程的小伙伴们&#xff0c;今儿咱们来聊聊那些翻译神器&#xff0c;看看它们在中英文互译这条路上&#xff0c;是怎么给我们这些天天跟洋文打交道的哥们儿姐们儿减轻负担的。我亲身体验了福昕翻译在线、福昕翻译大师、海鲸AI翻译还有腾讯翻译君&#xf…...

C++ WebDriver扩展

概述 WebDriver协议基于HTTP&#xff0c;使用JSON进行数据传输&#xff0c;定义了client与driver之间的通信标准。无论client的实现语言&#xff08;如Java或C#&#xff09;&#xff0c;都能通过协议中的endpoints准确指示driver执行各种操作&#xff0c;覆盖了Selenium的所有功…...

19c补丁后oracle属主变化,导致不能识别磁盘组

补丁后服务器重启&#xff0c;数据库再次无法启动 ORA01017: invalid username/password; logon denied Oracle 19c 在打上 19.23 或以上补丁版本后&#xff0c;存在与用户组权限相关的问题。具体表现为&#xff0c;Oracle 实例的运行用户&#xff08;oracle&#xff09;和集…...

TDengine 快速体验(Docker 镜像方式)

简介 TDengine 可以通过安装包、Docker 镜像 及云服务快速体验 TDengine 的功能&#xff0c;本节首先介绍如何通过 Docker 快速体验 TDengine&#xff0c;然后介绍如何在 Docker 环境下体验 TDengine 的写入和查询功能。如果你不熟悉 Docker&#xff0c;请使用 安装包的方式快…...

Keil 中设置 STM32 Flash 和 RAM 地址详解

文章目录 Keil 中设置 STM32 Flash 和 RAM 地址详解一、Flash 和 RAM 配置界面(Target 选项卡)1. IROM1(用于配置 Flash)2. IRAM1(用于配置 RAM)二、链接器设置界面(Linker 选项卡)1. 勾选“Use Memory Layout from Target Dialog”2. 查看链接器参数(如果没有勾选上面…...

Spring数据访问模块设计

前面我们已经完成了IoC和web模块的设计&#xff0c;聪明的码友立马就知道了&#xff0c;该到数据访问模块了&#xff0c;要不就这俩玩个6啊&#xff0c;查库势在必行&#xff0c;至此&#xff0c;它来了。 一、核心设计理念 1、痛点在哪 应用离不开数据&#xff08;数据库、No…...

C++.OpenGL (20/64)混合(Blending)

混合(Blending) 透明效果核心原理 #mermaid-svg-SWG0UzVfJms7Sm3e {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-icon{fill:#552222;}#mermaid-svg-SWG0UzVfJms7Sm3e .error-text{fill…...

NPOI操作EXCEL文件 ——CAD C# 二次开发

缺点:dll.版本容易加载错误。CAD加载插件时&#xff0c;没有加载所有类库。插件运行过程中用到某个类库&#xff0c;会从CAD的安装目录找&#xff0c;找不到就报错了。 【方案2】让CAD在加载过程中把类库加载到内存 【方案3】是发现缺少了哪个库&#xff0c;就用插件程序加载进…...

HybridVLA——让单一LLM同时具备扩散和自回归动作预测能力:训练时既扩散也回归,但推理时则扩散

前言 如上一篇文章《dexcap升级版之DexWild》中的前言部分所说&#xff0c;在叠衣服的过程中&#xff0c;我会带着团队对比各种模型、方法、策略&#xff0c;毕竟针对各个场景始终寻找更优的解决方案&#xff0c;是我个人和我司「七月在线」的职责之一 且个人认为&#xff0c…...

Unity中的transform.up

2025年6月8日&#xff0c;周日下午 在Unity中&#xff0c;transform.up是Transform组件的一个属性&#xff0c;表示游戏对象在世界空间中的“上”方向&#xff08;Y轴正方向&#xff09;&#xff0c;且会随对象旋转动态变化。以下是关键点解析&#xff1a; 基本定义 transfor…...

麒麟系统使用-进行.NET开发

文章目录 前言一、搭建dotnet环境1.获取相关资源2.配置dotnet 二、使用dotnet三、其他说明总结 前言 麒麟系统的内核是基于linux的&#xff0c;如果需要进行.NET开发&#xff0c;则需要安装特定的应用。由于NET Framework 是仅适用于 Windows 版本的 .NET&#xff0c;所以要进…...

内窥镜检查中基于提示的息肉分割|文献速递-深度学习医疗AI最新文献

Title 题目 Prompt-based polyp segmentation during endoscopy 内窥镜检查中基于提示的息肉分割 01 文献速递介绍 以下是对这段英文内容的中文翻译&#xff1a; ### 胃肠道癌症的发病率呈上升趋势&#xff0c;且有年轻化倾向&#xff08;Bray等人&#xff0c;2018&#x…...