您的位置:澳门402永利com > 编程应用 > 单例模式

单例模式

发布时间:2019-09-26 00:32编辑:编程应用浏览(177)

    单例形式:有限支撑七个类只有贰个实例,并提供三个访谈它的全局访谈点。

    构成:

    1.私局部构造函数

    2.私有静态的实例

    3.回去实例的静态方法

      public class Singleton    {        private static Singleton uniqueInstance = new Singleton();        private Singleton() { Console.WriteLine("this is a new singleton"); }        public static Singleton getInstance()        {            if (uniqueInstance == null)            {                return uniqueInstance;            }            return uniqueInstance;        }    }
    

    这种称为饿汉方式,实例在类加载时就创办了,劣势是一旦实举个例子果消耗大批量的财富而尚未选取就能够导致浪费,另一种懒汉方式,实例在被采纳时才创造,

      public class Singleton    {        private static Singleton uniqueInstance;        private Singleton() { Console.WriteLine("this is a new singleton"); }        public static Singleton getInstance()        {            if (uniqueInstance == null)            {                return uniqueInstance = new Singleton();            }            return uniqueInstance;        }    }
    

    可是那不是线程安全的

    例如

    class Program {             static void Main(string[] args) {            while (true) {                Thread t1 = new Thread;                t1.Start();            }        }        static void Test() {            Singleton s = Singleton.getInstance();        }           }
    

    施行的结果有望是那般

    图片 1

    次第成立了八个实例,那不是我们想要的结果,原因是有个别线程if (uniqueInstance == null)语句实践后让出了使用权,当它再一次获得CPU使用权的时候,大概其他CPU已经创立了实例,而它并不知道,继续施行return uniqueInstance=new Singleton();导致出现四个实例。

    于是,要为方法加锁

      public class Singleton    {        private static Singleton uniqueInstance;        private Singleton() { Console.WriteLine("this is a new singleton"); }        private static readonly object syncRoot = new object();        public static Singleton getInstance()        {            lock             {                if (uniqueInstance == null)                {                     return uniqueInstance = new Singleton();                }            }            return uniqueInstance;        }    }
    

    可是那又带来了三个主题素材,在实例已经成立完结了,但照旧会有多量的线程卡在lock ,它们都还也许会尝试创设实例,那减少了品质

    为此,还要为此办法创制别的叁个表达

     public static Singleton getInstance()        {            if (uniqueInstance == null)            {                lock                 {                    if (uniqueInstance == null)                    {                      return uniqueInstance = new Singleton();                    }                }            }            return uniqueInstance;        }
    

    那时,当实例已经创办完毕之后,各线程不再访谈临界区,升高了质量

    单例格局和静态类的相比

    1.单例形式能够懒加载,静态类推行更加快,即在区别尺度下二者有例外的性质表现

    2.单例能够三回九转和override

    3.单例易于模拟,利于测量检验

    4.单例利于维护状态音信

    update:

    1.静态类试行更加快?

    因为静态类在编写翻译时绑定(什么是编写翻译时绑定?)

    2.stackoverflow上的作答

    Vadluri Sreenu:

    1. Singleton object stores inHeapbut, static object stores instack
    2. We canclonethe object of Singleton but, we can not clone the static class object
    3. Singleton class follow theOOP(object oriented principles) but not static class
    4. we can implementinterfacewith Singleton class but not with Static class.

    Jon Skeet:

    A singleton allows access to a single created instance - that instance (or rather, a reference to that instance) can be passed as a parameter to other methods, and treated as a normal object.

    A static class allows only static methods.

    本文由澳门402永利com发布于编程应用,转载请注明出处:单例模式

    关键词: