Intention : For some purpose (such as performance considerations and logic requirements), only N instances of the same class can be created.
Application: Abstract Factory classes and object pools in factory Mode
Namespace Designpattern. Singleton
{
// Single thread
Public Class Singlethread
{
Private Static Singlethread instance;
Private Singlethread ()
{
}
Public Static Singlethread instance
{
Get
{
If (Instance = Null )
{
Instance= NewSinglethread ();
}
Return Instance;
}
}
// The. NET static Object Instantiation mechanism is implemented with the following code line. This method is simple, but cannot be initialized in the constructor. Of course, you can call a custom method for initialization.
// Public static readonly singlethread instance = new singlethread ();
}
// Multithreading
Public Class Multithread
{
Private Static Volatile Multithread instance;
Private Static Object OBJ = New Object ();
Private Multithread ()
{
}
Public Static Multithread instance
{
Get
{
If (Instance = Null )
{
Lock (OBJ)
{
If (Instance = Null )
{
Instance= NewMultithread ();
}
}
}
Return Instance;
}
}
}
}