C # singleton and multi-instance mode

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.