Singleton design pattern of

Disclaimer: This article is a blogger original article. Shall not be reproduced without the consent of bloggers. https://blog.csdn.net/u010786678/article/details/37739825
Singleton (Singleton Pattern) to ensure that only one instance of a class, and it provides access to a global access point.
         




Recently been reconstructed with VB.NET room fee system, there is a reconstruction on this issue:
                                                                    
        each click of the top menu, there will be a new top window. I hope it does not appear either, there was only the emergence of a. This single-case model on and we are talking about today have a relationship. We all know that all things in the world is unique, that no two things are completely the same. From this perspective, with respect to each thing itself is a single embodiment. We are talking about today singleton design pattern is different, it is for a certain class of things is, there is only one instance of an object.

       

Terms of our program in terms of a class that there is only one instance of an object .

        A normal class is the ability to create an object instance using the new operator at will, how to design and implementation of a class can only produce an object instance of it?

        Suppose one wants to produce only a class object instance, can not create object using the new operator!

Let the class constructor can be declared private mode. So you can not create an external object using the new operator up! So how should you create an external class object instance of it? We were able to create objects of class work to the interior. Not create the outside, external knowledge gained this object instance of it, it will not only reduce the coupling between objects, but also to achieve a singleton.

private Singleton()
        {

        }

        To achieve a single case, we need a global static instance of an object. Also we need to declare a static method within the class. This method is an example of the global object instance, and then returns.

But only this is not enough. It is only this, it is assumed instantiated every time, or a plurality of object instances generated in a static method, we also need further improvement.

private static Singleton instance;
<pre name="code" class="csharp">public static Singleton GetInstance() 
 

        在这里我们有两种方式:一、全局的对象实例是一个已经实例化好的。在静态方法中返回即可了;二、在静态方法中首先推断全局对象是否为null。假设是null则实例化,然后返回全局实例对象。这两种方式各有优缺点,使用的时候也须要注意一些问题。

一、饿汉式单例类

    public sealed class Singleton       //阻止发生派生,而派生可能会添加实例
    {
        private static readonly Singleton instance = new Singleton();       //在第一次引用类的不论什么成员时创建实例
        private Singleton()
        {

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

    }

二、懒汉式单例类

<pre name="code" class="csharp">class Singleton
    {
        private static Singleton instance;

        private Singleton()     //构造方法让其private,这就堵死了外界利用new创建此类实例的可能
        {

        }
        public static Singleton GetInstance()       //此方法是获得本类实例的卫衣全局訪问点
        {
            if (instance == null)     //若实例不存在,则new一个新实例,否则返回已有的实例
            {
                instance = new Singleton();
            }
            return instance;
        }
    }

                                                   client代码:
    class Program
    {
        static void Main(string[] args)
        {
            Singleton s1 = Singleton.GetInstance();
            Singleton s2 = Singleton.GetInstance();

            if(s1==s2)      //比較两次实例化后对象的结果是同样实例
            {
                Console.WriteLine("两个对象是同样的实例。");
            }

            Console.Read();
        }
    }
}



懒汉式单例类:

      长处:不会产生内存浪费。由于共享实例对象開始没有被初始化。而是在获得共享对象的方法中动态生成实例的;

       缺点:多线程的程序中,多个线程同一时候訪问Singleton类,调用GetInstance()方法,会有可能造成多个实例。

饿汉式单例类:

       缺点:会产生内存浪费,由于在载入Singleton类时就已经初始化共享对象实例;

       长处:多线程的程序中,多个线程同一时候訪问Singleton类也不会造成多个实例。运行效率高。


利用双重锁定解决懒汉式单例类的同步问题:

class Singleton
    {
        private static Singleton instance;
        private static readonly object syncRoot = new object();
        private Singleton()
        {

        }
        public static Singleton GetInstance()
        {
            if (instance==null)     //先推断实例是否存在,不存在再加锁处理
            {
                lock(syncRoot)
                {
                    if (instance==null)
                    {
                        instance = new Singleton();
                    }
                }
            }
            return instance;
        }
    }


使用场合:
       当在系统中某个特定的类对象实例仅仅须要有一个的时候,能够使用单例模式。须要注意的是,仅仅有真正有“单一实例”的需求时才可使用。













Guess you like

Origin www.cnblogs.com/ldxsuanfa/p/10965898.html