单例模式

本文原创,版权属作者个人所有,如需转载请联系作者本人。Q&微:155122733

--------------------------------------------------------------------------------------------------------

单例模式:只能生成一个实例的类是实现了单例模式的类。

1 c语言实现:

在实例还没有创建之前需要加锁操作,以保证只有一个线程创建出实例

    public sealed class Singleton3
    {   
        private Singleton3()
        {
        }

        private static object syncObj = new object();

        private static Singleton3 instance = null;
        public static Singleton3 Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncObj)
                    {
                        if (instance == null)
                            instance = new Singleton3();
                    }
                }

                return instance;
            }
        }
    }  
原文地址:https://www.cnblogs.com/lcl0421/p/8473504.html