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

前言

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

代码

v1.0

cs 复制代码
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 CS8603
    static T Create()
    {
        return Activator.CreateInstance(typeof(T),
        BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
        null, null, null) as T;
    }
#pragma warning restore CS8603
}

测试

单例模式基类

cs 复制代码
/// <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 CS8603
    static T Create()
    {
        return Activator.CreateInstance(typeof(T),
        BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
        null, null, null) as T;
    }
#pragma warning restore CS8603
}

派生类

cs 复制代码
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";
}

测试代码

cs 复制代码
// ********************************* 线程安全测试:通过 *********************************

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.派生类必须具备无参构造函数,且无参构造函数需要向单例基类提供可进行反射访问的权限;

版本改进

......

系列文章

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

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

相关推荐
向宇it1 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎
九鼎科技-Leo2 小时前
什么是 WPF 中的依赖属性?有什么作用?
windows·c#·.net·wpf
Heaphaestus,RC3 小时前
【Unity3D】获取 GameObject 的完整层级结构
unity·c#
baivfhpwxf20233 小时前
C# 5000 转16进制 字节(激光器串口通讯生成指定格式命令)
开发语言·c#
直裾3 小时前
Scala全文单词统计
开发语言·c#·scala
ZwaterZ5 小时前
vue el-table表格点击某行触发事件&&操作栏点击和row-click冲突问题
前端·vue.js·elementui·c#·vue
ZwaterZ7 小时前
el-table-column自动生成序号&&在序号前插入图标
前端·javascript·c#·vue
SRC_BLUE_179 小时前
SQLI LABS | Less-55 GET-Challenge-Union-14 Queries Allowed-Variation 2
oracle·c#·less
yngsqq10 小时前
037集——JoinEntities连接多段线polyline和圆弧arc(CAD—C#二次开发入门)
开发语言·c#·swift
Zԅ(¯ㅂ¯ԅ)10 小时前
C#桌面应用制作计算器进阶版01
开发语言·c#