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

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

前言

本篇文章继续探索通过继承实现单例模式的可行方案,这次的方案将采用反射机制隐式创建派生类实例,示例代码为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.派生类的无参构造函数用于初始化;

缺点

1.反射开销;

2.派生类必须具备无参构造函数;

版本改进

......

系列文章

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

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


http://www.mrgr.cn/news/38142.html

相关文章:

  • 前端大模型入门:使用Transformers.js手搓纯网页版RAG(二)- qwen1.5-0.5B - 纯前端不调接口
  • 变量的定义和声明
  • C++入门基础知识90(实例)——实例15【求两数的最大公约数】
  • 初试React前端框架
  • 昇思MindSpore进阶教程--数据处理管道支持Python对象
  • 软件设计模式概述
  • 【JAVA开源】基于Vue和SpringBoot的师生健康管理系统
  • # linux从入门到精通(三)
  • 【幂简集成】手机归属地查询API,精准获取号码所在地,提升数据准确率
  • flatten()
  • Python知识点:如何使用Spark与PySpark进行分布式数据处理
  • 基于SSH的酒店管理系统的设计与实现 (含源码+sql+视频导入教程+文档+PPT)
  • opencv-如何获取图像区域特定像素区域大小
  • 万字长文详解FreeRTOS软件定时器
  • Python in Excel作图分析实战!
  • 一个好用的服务治理组件Sentinel
  • AI大模型:揭秘AI产品经理与传统产品经理的差别与转型攻略
  • 【博弈强化学习】——UAV-BS 的联合功率分配和 3D 部署:基于博弈论的深度强化学习方法
  • 一些硬件知识(二十四)
  • C++入门(1)