Singleton :-
Singleton is controversial design pattern of OOPS. It allows you to restrict the number of instances of an object.It's an interface that allows a class to enforce that it is only allocated once.
Sample Code :-
Namespace :-
using system.text;
using System.collections;
class SampleProgram
{
public static void Main()
{
SingletonStructure singleton = SingletonStructure.Instance;
}
}
public sealed class SingletonStructure
{
static readonly SingletonStructure _instance = new SingletonStructure();
public static SingletonStructure Instance
{
get
{
return _instance;
}
}
Constructor which will be initialize and create the instance
SingletonStructure()
{
// Initialize.
}
}
Null check singleton: 435 ms
Optimized singleton : 42 ms (As Above)
This method is fast beacause instance member is created directly in its declaration
Singleton is controversial design pattern of OOPS. It allows you to restrict the number of instances of an object.It's an interface that allows a class to enforce that it is only allocated once.
Sample Code :-
Namespace :-
using system.text;
using System.collections;
class SampleProgram
{
public static void Main()
{
SingletonStructure singleton = SingletonStructure.Instance;
}
}
public sealed class SingletonStructure
{
static readonly SingletonStructure _instance = new SingletonStructure();
public static SingletonStructure Instance
{
get
{
return _instance;
}
}
Constructor which will be initialize and create the instance
SingletonStructure()
{
// Initialize.
}
}
Null check singleton: 435 ms
Optimized singleton : 42 ms (As Above)
This method is fast beacause instance member is created directly in its declaration