As the object creation mode, the singleton mode ensures that a class has only one instance, and the instance is self-instantiated and provided to the entire system. This class is called a singleton class.
The Singleton mode has the following features:
A singleton class can have only one instance.
The Singleton class must create its own unique instance.
The Singleton class must provide this instance to all other objects.
The implementation of a typical Singleton class is as follows: the construction of sub-Private indicates that sub-classes cannot be inherited.
Public Class Singleton
{
Private Static Singleton m_instance = Null ;
PrivateSingleton ()
{
}
Public StaticSingleton getinstance ()
{
If(M_instance=Null)
{
M_instance=NewSingleton ();
}
ReturnM_instance;
}
}
The so-called multi-instance mode is actually a natural promotion of the single-instance mode. In general, a single-instance type can only have one instance, but a single-instance type can also be promoted to allow a limited number of instances, this mode is the multi-sample mode. As the object creation mode, the multi-instance mode has the following features:
Multiple instance types can have multiple instances.
You must create and manage your own instances and provide your own instances to the outside world.
Multiple classes can be divided into upper-limit multi-case classes and no upper-limit multi-case classes.
A multi-instance class with an upper limit has considered the upper limit of the instance as a logical part and built into the interior of the Multi-instance class. As follows:
Public Class Multiton
{
Private Static Multiton instance1 = Null ;
Private Static Multiton instance2 = Null ;
Private Multiton ()
{
}
Public Static Multiton getinstance ( Int Whichone)
{
If (Whichone = 1 )
{
If (Instance1 = Null )
{
Instance1 = New Multiton ();
}
Return Instance1;
}
Else
{
If (Instance2 = Null )
{
Instance2 = New Multiton ();
}
Return Instance2;
}
}
}
You do not need to limit the number of instances of multiple classes. The multi-instance mode with no limit on the number of instances is called the multi-instance mode with no limit. Because there is no upper limit on the number of instances, although this multi-instance mode is a single-instance mode, this multi-instance type may not be able to return to the single-instance type. Generally, all instances are managed by clustering.
Transferred from:Http://blog.csdn.net/21aspnet/archive/2007/03/24/1539852.aspx
For more information about the singleton mode, see http://www.cnblogs.com/sjms/archive/2010/08/30/1812303.html.