C# design pattern --- singleton pattern

Singleton

The singleton pattern is a commonly used software design pattern that belongs to the creation type. The class created by the method of singleton mode has only one instance in the current process.

1) Ordinary singleton mode

using System;
namespace SingletonPattern
{
    /// 
    /// 单例模式(非线程安全)
    /// 
    public class Singleton
    {
        private static Singleton singleton;
        private Singleton() { }
        /// 
        /// 获取实例-线程非安全模式
        /// 
        /// 
        public static Singleton GetSingleton()
        {
            if (singleton == null)
                singleton = new Singleton();
            return singleton;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var obj = Singleton.GetSingleton();
            Console.ReadKey();
        }
    }
}

 2) Lazy singleton mode

using System;
namespace ConsoleApplication
{
    /// 
    /// 单例类:一个构造对象很耗时耗资源类型
    /// 
    /// 懒汉式单例模式
    /// 
    public class Singleton// //泛型类型里面的静态字段,是随着不同的类型参数唯一的  泛型单例是不可能的
    {
        /// 
        /// 构造函数耗时耗资源
        /// 
        private Singleton()
        {
        }
        /// 
        /// 全局唯一静态  重用这个变量
        /// 
        private static volatile Singleton _Singleton = null;
        //volatile是C#中用于控制同步的关键字,其意义是针对程序中一些敏感数据,
        //不允许多线程同时访问,保证数据在任何访问时刻,最多有一个线程访问,以保证数据的完整性,volatile是修饰变量的修饰符。
        private static object Singleton_Lock = new object();
        /// 
        /// 2 公开的静态方法提供对象实例
        /// 
        /// 
        public static Singleton CreateInstance()
        {
            if (_Singleton == null)
            {
                lock (Singleton_Lock)//保证只有一个线程进去判断+初始化
                {
                    if (_Singleton == null)
                    {
                        _Singleton = new Singleton();
                    }
                }
            }
            return _Singleton;
        }//懒汉式  调用了方法才去构造
    }
    class Program
    {
        static void Main(string[] args)
        {
            var obj = Singleton.CreateInstance();
            Console.ReadKey();
        }
    }
}

 3) Hungry Chinese style singleton mode

using System;
namespace ConsoleApplication
{
    /// 
    /// 单例类:一个构造对象很耗时耗资源类型
    /// 饿汉式
    /// 
    public class Singleton
    {
        /// 
        /// 构造函数耗时耗资源
        /// 
        private Singleton()
        {
        }
        /// 
        /// 静态字段:在第一次使用这个类之前,由CLR保证,初始化且只初始化一次
        /// 这个比今天构造函数还早
        /// 
        private static Singleton _Singleton = new Singleton();
        public static Singleton CreateInstance()
        {
            return _Singleton;
        }//饿汉式  只要使用类就会被构造
    }
    class Program
    {
        static void Main(string[] args)
        {
            var obj = Singleton.CreateInstance();
            Console.ReadKey();
        }
    }

}

 or

using System;
namespace ConsoleApplication
{
    /// 
    /// 单例类:一个构造对象很耗时耗资源类型
    /// 
    /// 饿汉式
    /// 
    public class Singleton
    {
        /// 
        /// 1 构造函数耗时耗资源
        /// 
        private Singleton()
        {
        }
        /// 
        /// 静态构造函数:由CLR保证,程序第一次使用这个类型前被调用,且只调用一次
        /// 
        /// 
        static Singleton()
        {
            _Singleton = new Singleton();
        }
        private static Singleton _Singleton = null;
        public static Singleton CreateInstance()
        {
            return _Singleton;
        }//饿汉式  只要使用类就会被构造
    }
    class Program
    {
        static void Main(string[] args)
        {
           var obj= Singleton.CreateInstance();
            Console.ReadKey();
        }
    }
}

 

Guess you like

Origin blog.csdn.net/lwf3115841/article/details/132126485