C#几种单例模式

    /**
     * 单例模式-饿汉式
     */
    public class Singleton
    {
        // 在定义的时候就初始化_instance,
        private static Singleton _instance = new Singleton();

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

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

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

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

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

    /**
     *  单例模式-懒汉式(二)
     */
    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;
        }
    }

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

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

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

/*
*懒汉式加锁解决多线程安全问题
*/
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;
        }
    }

/*
*使用.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()
    {
    }
}
/*
*完全延迟加载实现(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();
    }
}

/*
* 双重验证的线程安全实现
*/
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;
        }
    } 
}

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值