Singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. This is very useful when only there is a need of single object which handles all the actions across the system.This pattern restrict the instantiation to a certain number of objects and this concept is to generalize the systems to operate more efficiently when only one object exists.
Example :-
The Abstract Factory, Builder, and Prototype patterns can use Singletons in their implementation
Implementation of Singleton pattern :-
///
/// Thread-safe singleton example without using locks
///
public sealed class SingletonClass
{
private static readonly SingletonClass singletonInstance = new SingletonClass();
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static SingletonClass()
{
}
private SingletonClass()
{
}
///
/// The public Instance property to use
///
public static SingletonClass SingletonInstance
{
get { return singletonInstance ; }
}
}
Happy to code...
Hiii...One more example to clear singleton approach :-
I have implement progress bar for ma current application so i am giving some reference here:-
#region Private Fields
private static StatusProgressBar _instance = null;
#endregion
#region Constructor
private StatusProgressBar()
{
this.Style = ProgressBarStyle.Blocks;
this.Step = 1;
}
#endregion
#region Properties
///
/// Get Singleton instance of progressBar
///
public static StatusProgressBar Instance
{
get
{
if (_instance == null)
{
_instance = new StatusProgressBar();
}
return _instance;
}
}
#endregion
Made the above class as public and access it suppose class name is StatusProgressBar
StatusProgressBar.Instance.PerformStep();
then if instance is already running then it will return that instance otherwise will return a new instance.
Example :-
The Abstract Factory, Builder, and Prototype patterns can use Singletons in their implementation
Implementation of Singleton pattern :-
///
/// Thread-safe singleton example without using locks
///
public sealed class SingletonClass
{
private static readonly SingletonClass singletonInstance = new SingletonClass();
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static SingletonClass()
{
}
private SingletonClass()
{
}
///
/// The public Instance property to use
///
public static SingletonClass SingletonInstance
{
get { return singletonInstance ; }
}
}
Happy to code...
Hiii...One more example to clear singleton approach :-
I have implement progress bar for ma current application so i am giving some reference here:-
#region Private Fields
private static StatusProgressBar _instance = null;
#endregion
#region Constructor
private StatusProgressBar()
{
this.Style = ProgressBarStyle.Blocks;
this.Step = 1;
}
#endregion
#region Properties
///
/// Get Singleton instance of progressBar
///
public static StatusProgressBar Instance
{
get
{
if (_instance == null)
{
_instance = new StatusProgressBar();
}
return _instance;
}
}
#endregion
Made the above class as public and access it suppose class name is StatusProgressBar
StatusProgressBar.Instance.PerformStep();
then if instance is already running then it will return that instance otherwise will return a new instance.