introduce

The singleton pattern, as its name implies, has only one instance and is responsible for creating its own objects. This class provides a way to access its unique objects directly, without instantiating the objects of that class. Let’s look at some of the ways we can do this.

implementation

1. Use lock to ensure that only one thread can enter the method.

// </summary> public class Singleton {private Singleton() {long lResult = 0; for (int i = 0; i < 100 - 000 - 000; i++) { lResult += i; } Thread.Sleep(2000); Console. WriteLine (${" enclosing GetType (). The Name} completed construction..." ); } private static Singleton Instance = null; private static readonly object Singleton_Lock = new object(); Public static Singleton CreateInstance() {if (Instance == null) {lock (Singleton_Lock) { Console.WriteLine(" Enter lock queue...." ); Thread.Sleep(1000); if (Instance == null) Instance = new Singleton(); } } return Instance; } public static void DoNothing() { Console.WriteLine("DoNothing"); } public void Show() { Console.WriteLine($"{this.GetType().Name} Show...." ); }}Copy the code

2. Use static constructors, called by the CLR before the type is used for the first time, and only once.

// </summary> public class SingletonSecond {private SingletonSecond() {long lResult = 0; for (int i = 0; i < 100 - 000 - 000; i++) { lResult += i; } Thread.Sleep(2000); Console. WriteLine (${" enclosing GetType (). The Name} completed construction..." ); } private static SingletonSecond Instance = null; /// <summary> /// static constructor, called by CLR, before the type is used for the first time, and only once! /// </summary> static SingletonSecond() { Instance = new SingletonSecond(); } public static SingletonSecond CreateInstance() { return Instance; } public static void DoNothing() { Console.WriteLine("DoNothing"); } public void Show() { Console.WriteLine($"{this.GetType().Name} Show...." ); }}Copy the code

3. Use static fields, called by the CLR, that are initialized before the type is used for the first time and only once.

// </summary> public class SingletonThird {private SingletonThird() {long lResult = 0; for (int i = 0; i < 100 - 000 - 000; i++) { lResult += i; } Thread.Sleep(2000); Console. WriteLine (${" enclosing GetType (). The Name} completed construction..." ); } /// <summary> /// static field, called by CLR, initialized before the type is first used, and only initialized once! /// </summary> private static SingletonThird Instance = new SingletonThird(); public static SingletonThird CreateInstance() { return Instance; } public static void DoNothing() { Console.WriteLine("DoNothing"); } public int iNum = 0; public void Show() { Console.WriteLine($"{this.GetType().Name} Show.. {iNum++}.." ); } public void Add() { this.iNum++; }}Copy the code

Usage scenarios

1. An environment where a unique sequence needs to be generated.

Objects that need to be frequently instantiated and then destroyed.

3. Objects that take too much time or resources to create, but are often used.

4. An environment that facilitates communication between resources.