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

server/2024/10/19 7:30:42/

前言

本篇文章继续探索通过继承实现单例模式的可行方案,这次的方案将采用反射机制隐式创建派生类实例,示例代码为C#。

代码

v1.0

using System.Reflection;/// <summary>
/// 单例模式基类
/// </summary>
/// <typeparam name="T">单例类型</typeparam>
public abstract class Singleton<T>
where T : class
{public static T instance => _instance.Value;static readonly Lazy<T> _instance = new Lazy<T>(Create);#pragma warning disable CS8603static T Create(){return Activator.CreateInstance(typeof(T),BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,null, null, null) as T;}
#pragma warning restore CS8603
}

测试

单例模式基类

/// <summary>
/// 单例模式基类
/// </summary>
/// <typeparam name="T">单例类型</typeparam>
public abstract class SingletonWithTest<T>
where T : class
{public static T instance => _instance.Value;static readonly Lazy<T> _instance = new Lazy<T>(Create);#pragma warning disable CS8603static T Create(){return Activator.CreateInstance(typeof(T),BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,null, null, null) as T;}
#pragma warning restore CS8603
}

派生类

public class TestA : SingletonWithTest<TestA>
{public readonly string key;TestA() { key = "Default A"; }
}public class TestB : SingletonWithTest<TestB>
{public readonly string key;public TestB() { key = "Default B"; }
}public class TestC : SingletonWithTest<TestC>
{public readonly string key;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;TestE() { key = "Default E"; }public TestE(string key) { this.key = key; }
}public class TestF : SingletonWithTest<TestF>
{public readonly string key = "Default F";
}

测试代码

// ********************************* 线程安全测试:通过 *********************************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:" + 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.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.可以通过单例基类规范统一标准;

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

6.将派生类实例创建权限交由派生类本身决定,通常,为遵循单例模式的原则应向外部关闭通过new关键字显式调用构造函数的权限,但这对派生类而言并非硬性要求;

缺点

1.反射开销;

2.派生类必须具备无参构造函数,且无参构造函数需要向单例基类提供可进行反射访问的权限;

版本改进

......

系列文章

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

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


http://www.ppmy.cn/server/124416.html

相关文章

小白投资理财 - 证券开户

小白投资理财 - 证券开户 前言股票交易费用计算示例名词解释&#xff1a;佣金&#xff0c;印花税&#xff0c;过户费佣金印花税过户费 开户开户前准备开户流程开户完成后注意事项 前言 开通证券账户是投资者进入金融市场、进行各种投资活动的前提条件。作为小白&#xff0c;先…

高通平台修改Android 10源码获取root权限

一、修改文件:/build/make/core/main.mk user_variant := $(filter user userdebug,$(TARGET_BUILD_VARIANT)) enable_target_debugging := true tags_to_install := ifneq (,$(user_variant))# Target is secure in user builds.ADDITIONAL_DEFAULT_PROPERTIES += ro.secure…

element-plus中el-table固定列fixed失效问题

问题描述 场景:表格在子组件中&#xff0c;同时该子组件在父组件中引用。 表格具有合并表头的操作&#xff0c;同时对第一列“姓名列”进行了"fixed“固定列的操作&#xff0c;不起效。最初怀疑是合并表头行操作的影响&#xff0c;但经排查&#xff0c;发现是父组件中一句…

大数据新视界 --大数据大厂之大数据实战指南:Apache Flume 数据采集的配置与优化秘籍

&#x1f496;&#x1f496;&#x1f496;亲爱的朋友们&#xff0c;热烈欢迎你们来到 青云交的博客&#xff01;能与你们在此邂逅&#xff0c;我满心欢喜&#xff0c;深感无比荣幸。在这个瞬息万变的时代&#xff0c;我们每个人都在苦苦追寻一处能让心灵安然栖息的港湾。而 我的…

基因功能分析:DAP-seq与H3K4me3 ChIP-seq的协同效应

什么是DAP-Seq&#xff1f; DAP-Seq&#xff0c;即DNA亲和纯化测序技术&#xff0c;是一种创新的基因组分析方法。它通过体外表达转录因子&#xff0c;精确地鉴定转录因子与基因组的结合位点。与传统的ChIP-seq相比&#xff0c;DAP-Seq不受抗体和物种的限制&#xff0c;使得研…

【JavaEE初阶】深入理解wait和notify以及线程饿死的解决

前言&#xff1a; &#x1f308;上期博客&#xff1a;【JavaEE初阶】深入解析死锁的产生和避免以及内存不可见问题-CSDN博客 &#x1f525;感兴趣的小伙伴看一看小编主页&#xff1a;【JavaEE初阶】深入解析死锁的产生和避免以及内存不可见问题-CSDN博客 ⭐️小编会在后端开…

【python】函数介绍

学习目标 快速体验函数的使用了解函数的作用 函数 函数&#xff1a;是组织好的&#xff0c;可重复使用的&#xff0c;用来实现特定功能的代码段。 name "xiaoming" length len(name) print(length)输出结果是&#xff1a; 8 为什么随时都可以使用 len() 统计长…

Windows开发工具使用技巧

在 Windows 上进行开发时&#xff0c;有许多工具和技巧可以提升开发效率和用户体验。以下是一些常用的开发工具和技巧&#xff1a; 常用开发工具 1. Visual Studio Code (VS Code) - 插件管理&#xff1a;利用扩展市场&#xff08;Extension Marketplace&#xff09;安装各种…