[AYJS] wpf设计模式学习[16]-单例模式-Singleton

最常见,使用最多的一个

为了保证只有1个对象的实例的,避免创建多个。

方式1

 public class Singleton
    {
        private static Singleton instance;
        public Singleton()
        {

        }

        public static Singleton GetInstance()
        {
            if (instance == null)
            {
                instance = new Singleton();
            }
            return instance;
        }
    }

调试

       Singleton sing = Singleton.GetInstance();
            Singleton sing2 = Singleton.GetInstance();
            if (sing.GetHashCode() == sing2.GetHashCode())
            {
                Console.WriteLine("相同实例");
            }

输出:相同实例

上面的 GetInstance可以简写

public static Singleton GetInstance
    {
        get
        {
                return instance ?? (instance = new Singleton());
        }
    }

改良1 :适合多线程

 public class Singleton2
    {
        private static Singleton2 instance;
        public static readonly object sync = new object();
        public Singleton2()
        {

        }

        public static Singleton2 GetInstance()
        {
            lock (sync)
            {
                if (instance == null)
                {
                    instance = new Singleton2();
                }
            }

            return instance;
        }
    }

再次改良,双重锁定

    public class Singleton3
    {
        private static Singleton3 instance;
        public static readonly object sync = new object();
        public Singleton3()
        {

        }

        public static Singleton3 GetInstance()
        {
            if (instance == null)
            {
                lock (sync)
                {
                    if (instance == null)
                    {
                        instance = new Singleton3();
                    }
                }
            }

            return instance;
        }
    }

C#与公共语言运行库也提供了一种‘静态初始化’方法,这种方法不需要开发人员显式地编写线程安全代码,既可解决多线程环境下它是不安全的问题

他是系统在运行时候,就已经创建自己了。上面一种,引用才创建。

   public sealed class Singleton4   //注意必须设置为密封类
    {
        private static readonly Singleton4 instance = new Singleton4();    //注意声明为只读
        private Singleton4()
        {

        }
        public static Singleton4 GetInstance()
        {
            return instance;
        }
    }

继续一种单例模式,延迟初始化方式的单例

实现1

public sealed class Singleton
{
    private Singleton()
    {
    }


    public static Singleton Instance { get { return Nested._instance; } }

    private class Nested
    {
        static Nested()
        {
        }

        internal static readonly Singleton _instance = new Singleton();
    }
}

实现2

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()
    {
    }
}

这种方式的简单和性能良好,而且还提供检查是否已经创建实例的属性IsValueCreated。

总结:

使用Singleton模式有一个必要条件:在一个系统要求一个类只有一个实例时才应当使用单例模式。反之,如果一个类可以有几个实例共存,就不要使用单例模式。

不要使用单例模式存取全局变量。这违背了单例模式的用意,最好放到对应类的静态成员中。

不要将数据库连接做成单例,因为一个系统可能会与数据库有多个连接,并且在有连接池的情况下,应当尽可能及时释放连接。Singleton模式由于使用静态成员存储类实例,所以可能会造成资源无法及时释放,带来问题。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值