The simplest of singleton design pattern

Singleton pattern is now the most commonly used software design pattern, the design is the simplest mode, single mode, the system can ensure the embodiment, only one instance of the class

If in the following this case, we have a database connection class, in normal times, we instantiate the class and then write connection, the first time I SQLServer database links, links the second time I MySql database, but I At the same time want to use both databases and found not suitable, you need a way to way to solve the problem, which is the most suitable for a singleton

Although I cite the chestnuts may not be appropriate, but probably such a meaning, to ensure that only one instance, avoid multiple instances do not know which or multiple instances result in an error

use

And a method of obtaining the reference example singleton class requires the object to return a static method

There are two main steps to achieve:

①:经该类的构造函数定义为私有的(private)

②:在这个类里提供一个静态方法

Here is to distinguish between single embodiment has two modes: Mode ruffian mode and lazy

Villains mode is up and running project is an example of this class

Lazy mode in this class only when needed instantiate this class

Not much to say first, on the code:

This is the mode villains:

/// <summary>
/// 单例模式的实现 /// </summary> public class SingletonEh { // 定义一个静态变量来保存类的实例 private static SingletonEh uniqueInstance=new SingletonEh(); private SingletonEh() { } /// <summary> /// 定义公有方法提供一个全局访问点,同时你也可以定义公有属性来提供全局访问点 /// </summary> /// <returns></returns> public static SingletonEh GetInstance() { return uniqueInstance; } } 

This is unsafe lazy mode:

/// <summary>
/// 单例模式线程不安全的实现(懒汉模式) /// </summary> public class SingletonThread { // 定义一个静态变量来保存类的实例 private static SingletonThread uniqueInstance; // 定义一个标识确保线程同步 private static readonly object locker = new object(); // 定义私有构造函数,使外界不能创建该类实例 private SingletonThread() { } /// <summary> /// 定义公有方法提供一个全局访问点,同时你也可以定义公有属性来提供全局访问点 /// </summary> /// <returns></returns> public static SingletonThread GetInstance() { // 如果类的实例不存在则创建,否则直接返回 if (uniqueInstance == null) { uniqueInstance = new SingletonThread(); } return uniqueInstance; } } 

You may notice that I say is unsafe lazy mode, do not worry, this is explained, in lazy mode, if single-threaded, then no problem, if it is multi-threaded, while adding two threads need to use this class, you can see by the code, through judgment, are examples of this class, it does not make sense singletons, and since there is a problem there is a solution, the following is thread safe singleton, singleton locked mode

/// <summary>
/// 单例模式线程安全的实现(懒汉模式) /// </summary> public class SingletonThread { // 定义一个静态变量来保存类的实例 private static SingletonThread uniqueInstance; // 定义一个标识确保线程同步 private static readonly object locker = new object(); // 定义私有构造函数,使外界不能创建该类实例 private SingletonThread() { } /// <summary> /// 定义公有方法提供一个全局访问点,同时你也可以定义公有属性来提供全局访问点 /// </summary> /// <returns></returns> public static SingletonThread GetInstance() { // 当第一个线程运行到这里时,此时会对locker对象 "加锁", // 当第二个线程运行该方法时,首先检测到locker对象为"加锁"状态,该线程就会挂起等待第一个线程解锁 // lock语句运行完之后(即线程运行完之后)会对该对象"解锁" lock (locker) { // 如果类的实例不存在则创建,否则直接返回 if (uniqueInstance == null) { uniqueInstance = new SingletonThread(); } } return uniqueInstance; } } 

It's safe, but each time I had to wait on a task to complete before you can continue to unlock, which resulted in the possibility of a long row of the old team, it is not appropriate, then, we continue to optimize it

/// <summary>
/// 优化后的单例模式线程安全的实现(懒汉模式) /// </summary> public class SingletonThreadOptimize { // 定义一个静态变量来保存类的实例 private static SingletonThreadOptimize uniqueInstance; // 定义一个标识确保线程同步 private static readonly object locker = new object(); // 定义私有构造函数,使外界不能创建该类实例 private SingletonThreadOptimize() { } /// <summary> /// 定义公有方法提供一个全局访问点,同时你也可以定义公有属性来提供全局访问点 /// </summary> /// <returns></returns> public static SingletonThreadOptimize GetInstance() { // 当第一个线程运行到这里时,此时会对locker对象 "加锁", // 当第二个线程运行该方法时,首先检测到locker对象为"加锁"状态,该线程就会挂起等待第一个线程解锁 // lock语句运行完之后(即线程运行完之后)会对该对象"解锁" // 双重锁定只需要一句判断就可以了 if (uniqueInstance == null) { lock (locker) { // 如果类的实例不存在则创建,否则直接返回 if (uniqueInstance == null) { uniqueInstance = new SingletonThreadOptimize(); } } } return uniqueInstance; } } 

advantage

System memory, there is only one object class, saving system resources, to create some objects that require frequent destruction, single-use-case model can improve system performance.

Shortcoming

Whenever you are trying to instantiate a singleton class, you must remember to use the appropriate method to get the object, instead of using new, causing distress may give other developers, especially when you do not see the source code

Guess you like

Origin www.cnblogs.com/linjierd/p/11130313.html