The purpose of Singleton mode is to ensure that the class is instantiated only once in the system, and the unique instance provides services for the system. the Singleton mode is mainly used to ensure the unification of services, such as obtaining uniform numbering services and imitating the generation of Oracle sequences. however, the use of Singleton requires caution, especially when load balancing is required, because this program-level Singleton mode can only ensure that it is Singleton in one application. if it is loaded by multiple applications, it will still be instantiated multiple times.
If the lazy style is used, pay attention to thread security.
The multi-instance mode is similar to the single-instance mode, but the implementation is much more complicated. You need to maintain your own instance pool and allocation usage policies, as well as maintenance of the usage status, the multi-instance mode is mainly used to manage limited resources,
For example, the database connection pool.
The following is an example of the singleton mode:
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;
// Singleton mode: ensures that only one instance is instantiated in the system.
Namespace mybasestudy
{
// Mode 1
Class singleton_1
{
Private double _ value = 0;
Public double value
{
Get
{
Return _ value;
}
Set
{
This. _ value = value;
}
}
Private Static singleton_1 _ instance = new singleton_1 ();
// In Singleton mode, you need to set the constructor to protected or private to prevent other classes from being instantiated using the constructor.
Private singleton_1 ()
{
}
Public static singleton_1 getinstance ()
{
Return _ instance;
}
}
// Mode 2 is called lazy mode and requires thread security.
Class singleton_2
{
Private double _ value = 0;
Public double value
{
Get
{
Return _ value;
}
Set
{
This. _ value = value;
}
}
Private Static singleton_2 _ instance = NULL;
Private Static readonly object padlock = new object ();
// In Singleton mode, you need to set the constructor to protected or private to prevent other classes from being instantiated using the constructor.
Private singleton_2 ()
{
}
// This kind of reduction overhead is relatively large, because the lock is required every time you come in.
Public static singleton_2 getinstancea ()
{
Lock (padlock)
{
If (_ instance = NULL)
{
_ Instance = new singleton_2 ();
}
Return _ instance;
}
}
// The lock is applied only when no instance is instantiated.
Public static singleton_2 getinstanceb ()
{
If (_ instance = NULL)
{
Lock (padlock)
{
If (_ instance = NULL)
{
_ Instance = new singleton_2 ();
}
}
}
Return _ instance;
}
}
// General class
Class commclass
{
Private double _ value = 0;
Public double value
{
Get
{
Return _ value;
}
Set
{
This. _ value = value;
}
}
}
// Use generics to construct a singleton class. The advantage is that the classes in singleton mode can be common classes and can be reused.
Public sealed class singleton_3 <t> where T: New ()
{
Public static t getinstance ()
{
Return singletoncreator. instance;
}
Private class singletoncreator
{
Internal static readonly t instance = new T ();
Static singletoncreator ()
{
}
}
}
Public class singletontest
{
Public static void test ()
{
Singleton_1.getinstance (). Value ++;
MessageBox. Show (singleton_1.getinstance (). value. tostring ());
Singleton_1.getinstance (). Value ++;
MessageBox. Show (singleton_1.getinstance (). value. tostring ());
Singleton_2.getinstancea (). Value ++;
MessageBox. Show (singleton_2.getinstancea (). value. tostring ());
Singleton_2.getinstancea (). Value ++;
MessageBox. Show (singleton_2.getinstancea (). value. tostring ());
Singleton_2.getinstanceb (). Value ++;
MessageBox. Show (singleton_2.getinstanceb (). value. tostring ());
Singleton_2.getinstanceb (). Value ++;
MessageBox. Show (singleton_2.getinstanceb (). value. tostring ());
Singleton_3 <commclass>. getinstance (). Value ++;
MessageBox. Show (singleton_3 <commclass>. getinstance (). value. tostring ());
Singleton_3 <commclass>. getinstance (). Value ++;
MessageBox. Show (singleton_3 <commclass>. getinstance (). value. tostring ());
}
}
}