One, a singleton mode definition:
Ensure that a class has only one instance and provides a global access point to access it.
Second, the background:
When an object in our system needs only one instance, for example: there can only be one task manager in the operating system, and when the file is manipulated, only one instance is allowed to operate on it during the same time.
Third, realize the thinking:
1, the privatization of the constructor, so that the outside world can not create the class instance.
2. Declare an instance of a static variable receiving class.
3, define a static common method to provide global access.
Iv. Related code:
1, simple example:
As we all know, we add a button event to show out another form, the general practice is to new a Form object and then go to show, but every click will show more than one new form. How do you do it? Click on a show to show out a form as long as the new form does not close, no matter how much we click will not show a new form? Here is the use of a singleton mode, the relevant code is as follows:
Public Partial classForm2:form {//define a static variable to hold an instance of the class Public StaticForm2 Frmsingle =NULL; //privatization constructor, which makes it impossible for the outside world to create instances of this class PrivateForm2 () {InitializeComponent (); } /// <summary> ///define static public methods to provide global access/// </summary> /// <returns></returns> Public StaticForm2 Getsingle () {//created if an instance of the class does not exist, or is returned directly if(Frmsingle = =NULL) {Frmsingle=NewForm2 (); } returnFrmsingle; } }
Public Partial class Form1:form { public Form1 () { InitializeComponent (); } Private void button1_click (object sender, EventArgs e) { // Call Form 2 static method to implement Singleton mode Form2 frm = form2.getsingle (); frm. Show (); } }
Through the above simple example, I believe you also think that the singleton mode is not difficult, but the implementation of the above singleton mode is single-threaded, but in the case of multi-threaded, there will be multiple Form2 instances, because at the same time two threads running Getsingle method, at this time two thread judgment (Frmsingle = = NULL) This condition returns True, at which point two threads will create an instance of Form2, which violates the original idea of our singleton pattern, and the workaround is simple, so that the Getsingle method runs only one thread at a time.
//define a static variable to hold an instance of the class Public StaticForm2 Frmsingle =NULL; //define an identity to ensure thread synchronization Public Static ReadOnly ObjectLocker =New Object(); //privatization constructor, which makes it impossible for the outside world to create instances of this class PrivateForm2 () {InitializeComponent (); } /// <summary> ///define public methods to provide global access/// </summary> /// <returns></returns> Public StaticForm2 Getsingle () {if(Frmsingle = =NULL) { Lock(locker) {//created if an instance of the class does not exist, or is returned directly if(Frmsingle = =NULL) {Frmsingle=NewForm2 (); } } } returnFrmsingle; } }
Some small partners may be curious, why do you want to add an if condition decision, because there is a problem of competing resources, if two threads running the lock outer if (Frmsingle = = null), all set up, the first line loads lock instantiation of an object, after unlocking, if not added judgment , the second thread instantiates an object directly, which is not a singleton. This can also reduce the multi-threaded situation every time you need to get the lock of the resource, if the object has been instantiated can not be locked, saving system resources.
V. Summary
Singleton mode, this knowledge point is not difficult, in the school also studied, but some deepened applications such as the above multi-threaded situation is also through the sharing of Bo friends know there will be this problem, and know how to solve. The so-called from shallow into deep, understand clearly and consolidate the most basic design patterns to facilitate the future we continue to learn the remaining design mode!
C # Design Pattern--Singleton mode