【C#】单例模式的实现方案

namespace ConsoleApp1
{
    //不好的解法一:只适用于单线程环境
    public sealed class Singleton1
    {
        private Singleton1()  //私有构造函数禁止他人创建实例
        {

        }
        private static Singleton1 instance = null;
        public static Singleton1 Instance
        {
            get
            {
                if (instance == null)//多线程情况下如果两个线程同时运行到这里,那么两个线程都会创建一个实例
                    instance = new Singleton1();
                return instance;
            }
        }
    }

    //不好的解法二:多线程中能工作,但效率不高
    public sealed class Singleton2
    {
        private Singleton2()
        {

        }
        private readonly object syncObj = new object();
        private static Singleton2 instance = null;
        public Singleton2 Instance
        {
            get
            {
                lock(syncObj) //加锁耗时,而每次通过Instance得到Singleton2的实例,都会试图加上一个同步锁
                {
                    if (instance == null)
                        instance = new Singleton2();
                }
                return instance;
            }
        }
    }

    //可行的解法三:加同步锁前后两次判断实例是否已存在
    public sealed class Singleton3
    {
        private Singleton3()
        {

        }
        private static Singleton3 instance = null;
        private static object syncObj = new object();
        public Singleton3 Instance
        {
            get
            {
                if(instance==null)
                {
                    lock(syncObj) //只在第一次创建实例时加锁
                    {
                        if (instance == null)
                            instance = new Singleton3();
                    }
                }
                return instance;
            }
        }

    }

    //推荐的解法四:
    public sealed class Singleton4
    {
        private Singleton4()
        {

        }
        private static Singleton4 instance = new Singleton4();//实例不是在第一次调用属性Singleton4.Instance的时候被创建,而是在第一次用到Singleton4时被创建
        //假如在该类中添加一个静态方法,调用此方法不需要创建实例,如果按照Singleton4的方式实现单例模式,会过早地创建实例,从而降低内存的使用效率
        public Singleton4 Instance
        {
            get { return instance; }
        }
    }
    //推荐的解法五:借助私有嵌套类按需创建实例
    public sealed class Singleton5
    {
        private Singleton5()
        {

        }
        class Nested
        {
            static Nested()
            {

            }
            internal static readonly Singleton5 instance = new Singleton5();
        }
        public static Singleton5 Instance
        {
            get { return Nested.instance; }
        }    
    }

    //推荐的解法六:懒汉模式
    public sealed class Singleton6
    {
        private static readonly Lazy<Singleton6> lazy = new Lazy<Singleton6>(() => new Singleton6());
        private Singleton6()
        {

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值