Under certain circumstances
,
Specific type of data requires all other objects in the application. In most cases, this type of data is still unique in the system. How to make object instances available globally, and ensure that only one instance is created only? Singleton provides a unique global instance through the following methods: • Let the class created its own unique instance. 2. 2. Allow other objects to access this instance by returning an instance reference. The class method is global accessible. ?? 3. State the class constructor as a private, so that any other objects cannot create a new instance. Under but c # implementing the code of the Singleton mode:
Public class singleton {?? private singleleton {
??} ?? public static singleleton instance {
????? get {
???????? f (instance == null) instance = new single (); ???????? Return instance; ?????}
??}
?? private static singleleton instance;} // Class Singleton
There are two main points of this implementation: 1. Since the example is created inside the instance property method, the class can use additional functions (for example, instantiate the subclass), even if it may introduce unwanted dependence. 2. Until the object requires an instance to perform instances; this method is called "lazy uniterality". Lazy instantification avoids unnecessary Singleton when the application is started. However, the main disadvantage of this implementation is that it is unsafe in a multi-threaded environment. If the different threads of the execution process are simultaneously entered into the instance property method, multiple Singleton object instances may be created. There are many ways to solve this problem. One method is to use techniques called Double-Check Locking [Lea99]. The C # and the public language running library also provides a "static initialization" method, which does not require developers to explicitly write thread security codes to solve these problems. Static initialization
Public Sealed Class Singleton {??} ?? private static singleleton instance {????? get {return instance;} ???} ?? private static readonly singleton instance = new singleleton (); } // Class Singleton
In this policy, an instance will be created when any member of the first reference class. The public language runtime is responsible for processing variable initialization. This class is marked as Sealed to prevent derivation of derivation, and derived may increase the instance. In addition, the variable is marked as readonly, which means that the variable can only be allocated during static initialization (exemplified here) or in the class constructor. This implementation is similar to the previous example, which is different in that it rely on the public language runtime to initialize the variable. It can still be used to solve two basic problems that SINGLETON mode attempts to solve: global access and instantiation control. Public static properties provide a global access point for access instances. In addition, since the constructor is private, the Singleton class cannot be instantiated outside the class itself; therefore, the variable reference is the only instance that can be present in the system. Since the Singleton instance is referenced by private static member variables, it will not be instantiated before the class is first referenced to the invocation of the instance property. The only potential disadvantage of this method is that you have less control over the instantiation mechanism. In Design Patterns, you can use non-default constructor or perform other tasks before instantiation. Since the .NET Framework is initialized by the .NET Framework in this solution, you don't have these options. In most cases, static initialization is the preferred method of implementing Singleton in .NET. Multi-threaded Singleton static initialization is suitable for most situations. If your application must be delayed, use non-default constructor before instantiation or other tasks, and work in a multi-threaded environment, you need another solution. However, in some cases, you cannot reluctance to ensure the security of the thread like the public language running library as in the Static Initialization example. In this case, a specific language function must be used to ensure only one object instance is created in the presence of a multi-thread. One of the more common solutions is to use Double-Check Locking [Lea99] technology to block different threads simultaneously create new instances of Singleton. The following implementation only allows a thread to enter a critical area in the case where a Singleton instance is not created (the area is identified by the LOCK block). Public Sealed Class Singleton {??} ?? public static singleleton instance {
????? get {
???????? f (instance == null) {??????????? Lock (syncroot) {
?????????????? IF (instance == null) instance = new singleton (); ???????????} ????????}
???????? Return Instance;
??????} ??} ?? private static volatile Singleton Instance; ?? private static object syncroot = new object ();} // Class Singleton
This method ensures that only one instance is created only when an instance is needed. In addition, variables are declared as Volatile to ensure that instance variables can only be accessed only after the instance variable assignment is completed. Finally, this method uses the syncroot instance to lock (instead of the lock type itself, please refer to the insignificant lock type object) to avoid deadlocks. This Double-Check Locking method solves the thread concurrency problems while avoiding exclusive locks in the calls of each instance property method. It also allows you to instantiate delay when you first access objects. In fact, the application rarely requires this type of implementation. In most cases, the static initialization method is already enough. - From MSDN? "Using Microsoft .NET Enterprise Solution Mode" One Singleton Mode