C#几种单例模式

发布时间:2022-07-03 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了C#几种单例模式脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

 

     /**
     *  单例模式-懒汉式(一)
     */
    public class Singleton
    {
        PRivate static Singleton _instance;

        // 够造函数必须是私有的,不能被外部直接调用。
        private Singleton()
        {
        }

        // 暴露给外部,提供实例。
        public static Singleton getInstance()
        {
            if (_instance == null)
            {
                _instance = new Singleton();
            }
            return _instance;
        }
    }

C#几种单例模式

 

    /**
     *  单例模式-懒汉式(二)
     */
    public class Singleton
    {
        private static Singleton _instance;
        private static readonly object synchronized = new object();
        // 够造函数必须是私有的,不能被外部直接调用。
        private Singleton()
        {
        }

        // 暴露给外部,提供实例。
        public static Singleton getInstance()
        {
            if (_instance == null)
            {
                lock (synchronized)  //加锁止多线程
                {
                    if (_instance == null)
                    {
                        _instance = new Singleton();
                    }
                }
            }
            return _instance;
        }
    }

C#几种单例模式

 

     /**
     *  单例模式-懒汉式(四)
     */
    public class Singleton
    {
        // 够造函数必须是私有的,不能被外部直接调用。
        private Singleton()
        {
        }

        // 暴露给外部,提供实例。
        public static Singleton getInstance()
        {
            return SingletonHolder._instance;
        }

        // 静态内部内,实现延时加载
        private static class SingletonHolder
        {
            public static Singleton _instance = new Singleton();
        }
    }

C#几种单例模式

 

/*
*懒汉式加锁解决多线程安全问题
*/
public class Singleton
    {
        private static Singleton _instance;
        private static readonly object syn = new object();
        private Singleton() //构造函数设置private,不能被new,单例模式
        {

        }
        public static Singleton Createinstance()
        {
            if (_instance == null)
            {
                lock (syn)  //加锁防止多线程
                {
                    if (_instance == null)
                    {
                        _instance = new Singleton();
                    }
                }
            }
            return _instance;
        }
    }

C#几种单例模式

 

/*
*使用.NET4的Lazy<T>类型
*/
public sealed class Singleton
{
    private static readonly Lazy<Singleton> lazy = new Lazy<Singleton>(() => new Singleton());

    public static Singleton Instance 
    {
        get 
        {
            return lazy.Value;
        }
    }

    private Singleton()
    {
    }
}

C#几种单例模式

@H_976_304@/* *完全延迟加载实现(fully lazy instantiation) */ public sealed class Singleton { private Singleton() { } public static Singleton Instance { get { return Nested.instance; } } private class Nested { // ExplicIT static constructor to tell C# compiler // not to mark tyPE as beforefieldinit static Nested() { } internal static readonly Singleton instance = new Singleton(); } }

C#几种单例模式

 

/*
* 双重验证的线程安全实现
*/
public sealed calss Singleton
{
    private static Singleton instance = null;
    private static readonly object padlock = new object();

    Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            if (instance == null)
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new Singleton();
                    }
                }
            }
            return instance;
        }
    } 
}

C#几种单例模式

原文地址:https://www.cnblogs.COM/Zhengxue/p/13141618.htML

脚本宝典总结

以上是脚本宝典为你收集整理的C#几种单例模式全部内容,希望文章能够帮你解决C#几种单例模式所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。