C#中几种单例模式

news/2025/2/23 15:25:11

1.静态代码块

/// <summary>
    /// 静态代码块
    /// 仅在第一次调用类的任何成员时自动执行
    /// </summary>
    public class SingletonStatic
    {
        private static readonly SingletonStatic _instance =null;
        static SingletonStatic()
        {
            _instance = new SingletonStatic();
        }
        private SingletonStatic()
        {

        }
        public static SingletonStatic Instance
        {
            get { return _instance; }
        }

    }

2.内部类

   /// <summary>
    /// 内部类
    /// </summary>
    public class SingletonInner
    {
        private SingletonInner()
        {

        }
        public static SingletonInner Instance
        {
            get{ return InnerClass.inner; }
        }
        private class InnerClass
        {
            internal static readonly SingletonInner inner = new SingletonInner();
        }
    }

3.Lazy

    /// <summary>
    /// Lazy单例
    /// </summary>
    public class SingletonLazy
    {
        private static readonly SingletonLazy _instance = new Lazy<SingletonLazy>().Value;
        private SingletonLazy()
        {

        }
        public SingletonLazy Instance
        {
            get { return _instance; }
        }

    }

4.单例模式基类(转自https://www.cnblogs.com/zhouzl/archive/2019/04/11/10687909.html)

/// <summary>
    /// https://www.cnblogs.com/zhouzl/archive/2019/04/11/10687909.html
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class Singleton<T> where T : class
    {
        // 这里采用实现5的方案,实际可采用上述任意一种方案
        class Nested
        {
            // 创建模板类实例,参数2设为true表示支持私有构造函数
            internal static readonly T instance = Activator.CreateInstance(typeof(T), true) as T;
        }
        private static T instance = null;
        public static T Instance { get { return Nested.instance; } }
    }
    class TestSingleton : Singleton<TestSingleton>
    {
        // 将构造函数私有化,防止外部通过new创建
        private TestSingleton() { }
    }

 

转载于:https://www.cnblogs.com/NNYang/p/11013533.html


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

相关文章

MyBatis——封装MyBatis的输出结果(resultType、resultMap)、使用like进行模糊查询的两种方式

文章目录&#xff1a; 1.封装MyBatis的输出结果 1.1 第一种方式——使用resultType 1.2 resultType返回简单类型的数据 1.3 resultType返回对象类型的数据 1.4 resultType返回Map类型的数据 1.5 resultType默认规则&#xff08;同名的列赋值给同名的属性&#xff09; 1…

python 并行计算架构_python并行编程学习之并行计算存储体系结构

基于指令和可被同时处理的存储单元的数目&#xff0c;计算机系统可以分为以下四种类目&#xff1a;单指令&#xff0c;单数据单元(SISD)在该体系结构中&#xff0c;计算机是单处理器机器&#xff0c;一次只能用单一的指令来操作单一的数据流。在SISD中&#xff0c;机器指令按照…

C++PE文件格式解析类(轻松制作自己的PE文件解析器)

PE是Portable Executable File Format&#xff08;可移植的运行体&#xff09;简写&#xff0c;它是眼下Windows平台上的主流可运行文件格式。PE文件里包括的内容非常多&#xff0c;详细我就不在这解释了&#xff0c;有兴趣的能够參看之后列出的參考资料及其它相关内容。 近期我…

win7 linux三系统,苹果电脑装windows7,Linux,mac os三系统,不需要BootBoot | MOS86

苹果已经简化了在您的Mac上双启动Windows的过程&#xff0c;但是当涉及到Linux&#xff0c;BootCamp不是那么好使了。 这里是如何在苹果电脑装Windows 7,Ubuntu 10.04和mac os x。如果你是Mac用户&#xff0c;你可能已经使用苹果电脑的BootCamp在你的系统上获得Windows7的必要程…

INDEX--创建索引和删除索引时的SCH_M锁

最近有一个困惑&#xff0c;生产服务器上有一表索引建得乱七八糟&#xff0c;经过整理后需要新建几个索引&#xff0c;再删除几个索引&#xff0c;建立索引时使用联机&#xff08;ONLINEON&#xff09;创建,查看下服务器负载&#xff08;磁盘和CPU压力均比较低的情况&#xff0…

MyBatis——动态SQL的四个常用标签(<if>、<where>、<foreach>、<sql>)

文章目录&#xff1a; 1.什么是动态SQL&#xff1f; 2.MyBatis中的动态SQL 2.1 动态SQL——if标签 2.1.1 语法格式 2.1.2 应用举例 2.2 动态SQL——where标签 2.2.1 语法格式 2.2.2 应用举例 2.3 动态SQL——foreach标签 2.3.1 语法格式 2.3.2 应用举例1&#x…

如何解决设置maven时Could not read settings.xml

今天初学maven碰到了配置错误 错误信息: 解决方法: 可以看到再次点击Browse选择settings.xml文件后出现了警告&#xff1a;Unable to parse global ...\u7a9d\n<m... 226:3) , 这个226说明第226行出现了错误&#xff0c;导致了文件不能正常读取&#xff0c;仔细检查该行有没…

孝感市小学生机器人编程比赛_在B站教编程的小学生Vita君比赛拿了一等奖,这会让更多人爱上编程吗?...

记者 | 伍洋宇编辑 | 在这个暑假&#xff0c;八岁半的Vita君又给自己攒了个荣誉——在八月举行的编程体验与比赛类活动核桃杯省级阶段中&#xff0c;他拿到了一等奖。Vita所在的上海市图形化编程小学低年级组一共有996人参加&#xff0c;而他的成绩在一等奖获得者中排名第二。对…