Singleton pattern:
In software engineering, the singleton design pattern is designed to restrict instantiation of a class to one (or a few) objects. This is useful when exactly one object is needed to coordinate actions across the system. Sometimes it is generalized to systems that operate more efficiently when only one or a few objects exist.
The singleton pattern is implemented by creating a class with a method that creates a new instance of the object if one does not exist. If an instance already exists, it simply returns a reference to that object. To make sure that the object cannot be instantiated any other way, the constructor is made private.
Example implementation
I have created a class called SingleTon. the constructor of this class is made as private to make sure the object cannot be instantiated any other way. In this constructor I created three objects of Job class (which is listed later in this article) add added to a job queue.
The function GetObject returns an instance of SingleTon class. If it exists it returns the existing object reference.
The function GetJOB returns a job object based on the index it receives.
The function Release decrements the count of reference.
public class SingleTon
{
# region declaration
/// <summary>
///
/// </summary>
private static SingleTon instance;
private static int m_nNofReference;
private ArrayList m_ArrJob;
# endregion
/// <summary>
///
/// </summary>
private SingleTon()
{
m_nNofReference = 0;
m_ArrJob = new ArrayList();
///
///Add three job types in the queue for
///the time being
///
m_ArrJob.Add(new Job(1,"INSERT INTO TABLE"));
m_ArrJob.Add(new Job(2,"DELETE FROM TABLE"));
m_ArrJob.Add(new Job(2,"UPDATE TABLE"));
}
/// <summary>
/// Return the object if it is existing
/// OR create new ONE and return
/// </summary>
public static SingleTon GetObject()
{
if(instance == null)
instance = new SingleTon();
++m_nNofReference;
return instance;
}
/// <summary>
///
/// </summary>

Join the conversation! Your thoughts help the community grow.