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

news/2024/9/28 7:42:16 标签: 单例模式, c#

前言

之前看到朋友采用继承的方式来实现单例模式,觉得很厉害,随后自己去探索了一番,以前实现单例模式都是把代码内联到具体的类中,这使得工程中每次需要使用单例模式时,都采用拷贝的方式,增加了很多冗余代码,并且难以规范单例的统一标准,使得代码不方便扩展和管理。这次探索找到了一种实现方式,先记录下来,后续如果有其它方式再发表系列文章,示例代码为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在构造函数中进行重置会存在线程安全的问题,放在作为延迟初始化的工厂方法的本地静态方法中可以保证线程安全。

......

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


http://www.niftyadmin.cn/n/5680666.html

相关文章

【算法】堆排之 215.数组中的第K个最大元素(medium)

系列专栏 双指针 模拟算法 分治思想 目录 1、题目链接 2、题目介绍 3、解法 解题思路 排序方法的选择&#xff1a; 构建小堆&#xff1a; 提取第 k 个最大元素&#xff1a; 4、代码 1、题目链接 215. 数组中的第K个最大元素 - 力扣&#xff08;LeetCode&#xff09;…

【hot100-java】【划分字母区间】

R9-贪心算法篇 印象题&#xff1a; 我记得&#xff0c;先用字典记录每个字母出现的下标&#xff0c;取出首个字母的下标j,然后我们for循环遍历一次&#xff0c;如果该下标大于 j&#xff0c;就要变化新的首字母&#xff0c;如果相等就说明一个字符串完成&#xff0c;如果小于就…

鸿蒙-app进入最近任务列表触发的监听

如果在UIAbility中&#xff0c;参考第一个链接&#xff0c;在页面中参考如下&#xff1a;State windowStage: window.WindowStage (getContext(this) as common.UIAbilityContext).windowStagetry {this.windowStage.on(windowStageEvent, (data) > {// 前台应用进入最近任…

Python PyQt5 在frame中生成多个QLabel控件和彻底销毁QLabel控件

文章目录 步骤 1: 创建主窗口和布局步骤 2: 添加QLabel到QFrame步骤 3: 销毁QLabel示例代码 在PyQt5中&#xff0c;在QFrame或任何其他容器控件中生成多个QLabel控件并通过一个标志位或方法来彻底销毁这些QLabel控件是相对直接的操作。以下是一个简单的示例&#xff0c;展示了如…

51单片机系列-串口(UART)通信技术

&#x1f308;个人主页&#xff1a; 羽晨同学 &#x1f4ab;个人格言:“成为自己未来的主人~” 并行通信和串行通信 并行方式 并行方式&#xff1a;数据的各位用多条数据线同时发送或者同时接收 并行通信特点&#xff1a;传送速度快&#xff0c;但因需要多根传输线&#xf…

【linux】不小心禁用了 nvidia 显卡 PCIe 总线扫描怎么办

问题分析: 系统启动时没有自动加载NVIDIA驱动模块。系统启动时没有自动扫描PCI总线来检测GPU设备。需要手动执行命令来重新扫描PCI总线并加载NVIDIA驱动。 检查内核参数和udev规则: 查看内核启动参数,确保没有影响PCI扫描或GPU检测的参数。检查是否存在NVIDIA相关的udev规则。…

DC00020基于springboot新闻网站系统java web项目MySQL新闻管理系统

1、项目功能演示 DC00020基于springboot新闻网站系统java web项目MySQL 2、项目功能描述 基于springbootvue新闻网站包括用户和系统管理员两个角色。 2.1 用户功能 1、用户登录、用户注册 2、新闻信息&#xff1a;点赞、点踩、收藏、查看 3、用户分享&#xff1a;点赞、点踩…

IP和功能變數名稱的基礎知識-okeyproxy

什麼是IP地址&#xff1f; IP地址是分配給每一個連接到互聯網的設備的唯一識別字。IP地址就像是互聯網中的“門牌號”&#xff0c;它使得數據包能夠在網路中找到正確的目的地。 IP地址有兩種主要類型&#xff1a;IPv4和IPv6。 IPv4&#xff1a;IPv4地址由四個十進位數&#…