Explanation: The main feature of the singleton mode is to ensure that a class has only one instance and provides a global access point to access it
This means: In the case of multithreading: a first creates the instance. b You don't need to create it again when you come in.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacepattern.singleton{/// <summary> ///Singleton mode 1/// </summary> Public classSingleton2 {Private StaticSingleton2 singletoninstance; Private Static ReadOnly ObjectLocker =New Object(); /// <summary> ///defines a private constructor that cannot be created by the outside world/// </summary> PrivateSingleton2 () {} Public StaticSingleton2 Createsingleton () {if(Singletoninstance = =NULL) { Lock(locker) {if(Singletoninstance = =NULL) { return NewSingleton2 (); } } } returnsingletoninstance; } }}
. NET development design pattern-Singleton mode