Singleton Pattern


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>

      public static void Release()

      {

           --m_nNofReference;

      }

 

      /// <summary>

      ///

      /// </summary>

      /// <param name="nJob"></param>

      /// <returns></returns>

      public object GetJOB(int nJob)

      {

          if(nJob < m_ArrJob.Count)

          return m_ArrJob[nJob];

          else

          return null;

      }

}

 

The following is the implementation of class Job

public class Job

{

 

    # region declaration

    private int m_nJobID;

    private string m_strJobName;

    # endregion

 

    /// <summary>

    ///

    /// </summary>

    /// <param name="nJID"></param>

    /// <param name="strJName"></param>

    public Job(int nJID,string strJName)

    {

        m_nJobID = nJID;

        m_strJobName = strJName;

    }

 

    /// <summary>

    ///

    /// </summary>

    public int JOBID

    {

        get

        {

            return m_nJobID;

        }

     }

 

    /// <summary>

    ///

    /// </summary>

    public string JOBNAME

    {

        get

        {

            return m_strJobName;

        }

    }
}

The following statement can be used to check functionality of the above code

  1. SingleTon objSingleTon = SingleTon.GetObjesct();
    Job objJob = (Job)objSingleTon.GetJOB(0);

  2. SingleTon objSingleTon2 = SingleTon.GetObject();
    objJob = (
    Job)objSingleTon.GetJOB(1);

The first line retrieves an object of SingleTon class. In the second line we are requesting for the SingleTon object once again. But at this time it retrieves the existing SingleTon object reference only.


Similar Articles