Overview
The Singleton mode requires that a class has only one instance and provides a global access point.
This raises a question: how to bypass the conventional constructor and provide a mechanism to ensure that a class has only one instance?
When a customer program calls a class, it does not consider whether the class can only have one instance or other issues,
Therefore, this should be the responsibility of the class designer, not the responsibility of the class user.
From another perspective, the Singleton model is actually a responsibility model. Because we have created an object and this object plays a unique role, in this separate object instance, it concentrates all the power of the class to which it belongs, at the same time, it is also responsible for exercising this kind of power!
Intention
Ensure that a class has only one instance and provides a global access point to it.
Model Diagram
Logical Model diagram: Physical Model diagram: <Design Pattern> Singleton example
Comparison:We first compare the advantages and disadvantages of the four methods: Method 1: public sealed class Singleton {private static readonly Singleton instance = new Singleton (); private Singleton () {} public static Singleton Instance {get {return instance ;}} advantages: simple and clear disadvantages: resource consumption Method 2: public sealed class ClassicSingleton {private static ClassicSingleton instance; private static object syncRoot = new Object (); private ClassicSingleton () {} public static ClassicSingleton Instance {get {if (instance = null) {lock (syncRoot) {if (instance = null ){//... custom code instance = new ClassicSingleton () ;}} return instance ;}}advantages: Resource Saving disadvantages: code lengthy method 3: public sealed class Singleton {static Singleton () {Instance = new Singleton () ;}private Singleton () {}public static Singleton Instance {get; private set ;}} advantages: Both Resource Saving and Simplicity: thread Unsafe Method 4: public class Singleton {private static Singleton instance; // Added a static mutex for synchronising use of instance. private static System. threading. mutex mutex; private Singleton () {} static Singleton () {instance = new Singleton (); mutex = new System. threading. mutex ();} public static Singleton Acquire () {mutex. waitOne (); return instance;} // Each call to Acquire () requires a call to Release () public static void Release () {mutex. releaseMutex () ;}}advantages: not only resource saving, but also simple and clear, and thread safety (one arrow, three carvings) Disadvantages: slightly lengthy
The following is an example:When playing online games, calculate the number of online players. Because there are many regions, we need to use the singleton mode. first create Example. cs: html # viewSource "commandName =" viewSource "highlighterId =" highlighter_901949 "> view sourceprint?
public sealed class Example |
/// Define a static Example |
private static Example SingleExample= new Example (); |
private int SumCount = 0; |