I usually SEARCH FOR THE CONTENT ON Baidu. It seems very useful. A convenient use of Singleton is to control whether a form is opened multiple times or repeatedly. The following is its usage.
Generally, Singleton mode has several forms:
Public class Singleton { Private Singleton (){} // Define your own instance internally. Isn't it strange? // Note that this is private for internal calls only Private Static Singleton instance = new Singleton (); // Here is a static method for external access to this class, which can be accessed directly. Public static Singleton getinstance (){ Return instance; } } |
Second form:
Public class Singleton { Private Static Singleton instance = NULL; Public static synchronized Singleton getinstance (){ If (instance = NULL) Instance = new Singleton (); Return instance ;} } |
Use singleton. getinstance () to access the single-State class.
The second form above is lazy initialization. That is to say, the initial Singleton will not be regenerated in the first call.
Note that synchronized in the form of lazy Initialization is important. If synchronized is not available, you may obtain multiple Singleton instances by using getinstance. There are many discussions about Singleton of lazy initialization involving double-checked locking (DCL), which will be further studied by interested parties.
It is generally considered that the first form is more secure.
Considerations for using Singleton:
In some cases, Singleton cannot be used for singleton. If multiple Singleton objects are simultaneously loaded by different class loaders; in distributed systems such as ejbs, you should also pay attention to this situation because ejbs are cross-server and cross-JVM.
Take servicelocator of Pet Store source code (Pet Store 1.3.1) of sun as an example to analyze it a little:
In pet store, servicelocator has two types: one is under the EJB directory and the other is under the web directory. When we check the two servicelocator files, we will find that the content is similar, and they all provide the query and positioning service for EJB, but why should we separate them? After careful research on the two servicelocator methods, we can find the difference: servicelocator in the Web adopts the singleton mode, and servicelocator is a resource location. Naturally, we should use the singleton mode. However, in EJB, the singleton mode has no effect, so servicelocator is divided into two types: Web Services and EJB services.
The Singleton mode looks simple and easy to use, but it is not easy to use well. You need to have a good understanding of Java's class thread memory and other concepts.
In short: If your application is based on containers, the singleton mode is rarely used or not used. You can use alternative technologies.
The following describes the specific and flexible use of the project, hoping to help you.
Problem description: The Singleton mode is used to control whether the form is opened repeatedly or multiple times. The method is initially written in the MDI subform, and others write properties, it is basically the same, you can do it, and then you can think that there are multiple forms in the project. If each form is written in one copy, isn't it too brainless? Therefore, we plan to write a base class. However, Singleton can only be instantiated once, so we have to use generic writing. The base class is finally written, and then debugged. It is found that the number of opened forms can be controlled multiple times, but when the form operation is completed and closed ,, when this form is opened again, an error occurs (the released object cannot be accessed. Here it is related to the garbage collection problem in C. C # The Garbage Collector manages all managed objects. All. NET languages (including C #) that need to host data are restricted by the garbage collector of the Runtime Library. The garbage collector can determine the optimal time for running garbage collection and automatically recycle garbage. However, a product of garbage collection is that the C # object is not necessarily destroyed. Therefore, the subwindow object is destroyed but not null. Therefore, an exception occurs when access occurs (from "look at C # from a small point #. net garbage collection ).
Here, we will not go into details about this issue because it is not covered by the subject. If you are interested, please refer to the relevant materials. Back to the question, what should we do? There is a way. The key to the problem is that the form has been released, but it has not yet been processed by the system, so there is an instance. isdisposed = true, but the instance does not have to be null. What should I do? When the form is closed, it can be set to null artificially; then the problem arises again, And Singleton is read-only, if you assign a value to an instance, the system reports an error. The instance is read-only. What should we do? No way, don't hit the south wall, don't look back, just make up your mind to do it. To enable it to be assigned a value, only one set can be added. After debugging, the problem is solved. First, the problem is solved when the form is opened repeatedly or multiple times. Then, when each form is opened, only one line of code is required, and each form does not need to write an attribute, method (the essence is to use Singleton for reference). In the end, there will be no re-opening of the inaccessible problem, that is, the problem of being unable to access released objects.
The following is the singleton base class C # code:
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Namespace dchtgl
{
// Generic implementation of single-piece instances
Public class Singleton <t> where T: New ()
{
Private Static t instance = default (t );
Private Static readonly object lockhelper = new object ();
Private Singleton (){}
Public static t instance
{
Get
{
If (instance = NULL)
{
Lock (lockhelper)
{
If (instance = NULL)
{
Instance = new T ();
}
}
}
Return instance;
}
Set
{
Instance = value;
}
}
}
}
If you want to open a form in the main form, such as form1, the Code is as follows:
// Form1 is an MDI subform
Private void member management toolstripmenuitem_click (Object sender, eventargs E)
{
Form1 F1 = Singleton <form1>. instance;
F1.mdiparent = this;
F1.show ();
}
However, you must add
Private void form=formclosed (Object sender, formclosedeventargs E)
{
Singleton <form1>. instance = NULL;
}
In this way, no problems will occur. It should be clear this time.
I have recently gained a new understanding of this issue and posted the code to help readers.
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
Namespace dchtgl
{
/// <Summary>
/// Generic implementation of single form instance
/// </Summary>
/// <Typeparam name = "T"> form class </typeparam>
Public static class Singleton <t> where T: form, new ()
{
Private Static t instance = default (t );
Private Static readonly object lockhelper = new object ();
/// <Summary>
/// Obtain the unique instance of the form
/// </Summary>
Public static t instance
{
Get
{
If (instance = NULL)
{
Lock (lockhelper)
{
If (instance = NULL)
{
Instance = new T ();
// When an instance Close event is added, the form will be automatically recycled, that is, instance = NULL;
Instance. formclosed + = new formclosedeventhandler (destroyform );
}
}
}
Return instance;
}
}
/// <Summary>
/// Set the instance to null when the form is closed
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private Static void destroyform (Object sender, formclosedeventargs E)
{
Instance = default (t );
}
}
}
The usage is the same as above, but you do not need to add
Private void form=formclosed (Object sender, formclosedeventargs E)
{
Singleton <form1>. instance = NULL;
}
This sentence.