zoukankan      html  css  js  c++  java
  • C# 单例模式

    静态变量

    public sealed class Singleton
        {
            private Singleton() { }
    
            private static readonly Singleton singleInstance = new Singleton();
    
            public static Singleton GetInstance
            {
                get
                {
                    return singleInstance;
                }
            }
        }

    静态构造函数

    public class SingletonSecond
        {
            private static SingletonSecond _SingletonSecond = null;
    
            static SingletonSecond()
            {
                
                _SingletonSecond = new SingletonSecond();
            }
            
            public static SingletonSecond CreateInstance()
            {
                return _SingletonSecond;
            }
        }

    延迟加载

    public sealed class Singleton
        {
            private Singleton()
            {}
    
            private static readonly Lazy<Singleton> Instancelock =
                        new Lazy<Singleton>(() => new Singleton());
    
            public static Singleton GetInstance
            {
                get
                {
                    return Instancelock.Value;
                }
            }
        }

     对应的泛型方式

        public abstract class SingletonStatic<T> where T:class,new()
        {
            private static T instance = null;
            static SingletonStatic()
            {
                instance = new T();
            }
            public static T GetInstance()
            {
                return instance;
            }
        }
    
        public class SingletonSafe<T> where T : class, new()
        {
            private static T _instance;
            private static readonly object syslock = new object();
            public static T GetInstance()
            {    //线程安全锁
                if (_instance == null)
                {
                    lock (syslock)
                    {
                        if (_instance == null)
                        {
                            _instance = new T();
                        }
                    }
                }
                return _instance;
            }
        }
    
    
        public abstract class Singleton<T> where T:class,new()
        {
            private static T instance;
            public static T GetInstance()
            {
                if (instance == null)
                    instance = new T();
                return instance;
            }
    
        }

    使用:

    public class GlobalConfiguration : SingletonStatic<GlobalConfiguration>
    {....}
  • 相关阅读:
    【IdentityServer4文档】- 整体情况
    【IdentityServer4文档】- 欢迎来到 IdentityServer4 (ASP.NET Core 3.x)
    证券相关基础概念
    [基于NetCore的简单博客系统]-登录
    JAVA mysql数据库 配置
    简单验证码识别及登陆并下载页面
    Java 无法初始化Connection的问题
    MUI scroll 定位问题
    CentOS7 安装 MySql
    ASP.NET CORE 2.0 文档中文正式版已经出来了
  • 原文地址:https://www.cnblogs.com/noigel/p/14372135.html
Copyright © 2011-2022 走看看