SingleTon Pattern C#

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

A singleton is a class that can be instantiated once, and only once.

To achieve this we need to keep the following things in our mind.

1. Create a public Class (name SingleTonSample).

1
2
public class SingleTonSample
{}
public class SingleTonSample
{}

2. Define its constructor as private.

1
2
private SingleTonSample()
{}
private SingleTonSample()
{}

3. Create a private static instance of the class (name singleTonObject).

1
private volatile static SingleTonSample singleTonObject;
private volatile static SingleTonSample singleTonObject;

4. Now write a static method (name InstanceCreation) which will be used to create an instance of this class and return it to the calling method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static SingleTonSample InstanceCreation()
{
    private static object lockingObject = new object();
    if(singleTonObject == null)
    {
         lock (lockingObject)
         {
              if(singleTonObject == null)
              {
                   singleTonObject = new SingleTonSample();
              }
         }
    }
    return singleTonObject;
}
public static SingleTonSample InstanceCreation()
{
    private static object lockingObject = new object();
    if(singleTonObject == null)
    {
         lock (lockingObject)
         {
              if(singleTonObject == null)
              {
                   singleTonObject = new SingleTonSample();
              }
         }
    }
    return singleTonObject;
}

Now we to need to analyze this method in depth. We have created an instance of object named lockingObject, its role is to allow only one thread to access the code nested within the lock block at a time. So once a thread enter the lock area, other threads need to wait until the locking object get released so, even if multiple threads try to access this method and want to create an object simultaneously, it’s not possible. Further only if the static instance of the class is null, a new instance of the class is allowed to be created.

Hence only one thread can create an instance of this Class because once an instance of this class is created the condition of singleTonObject being null is always false and therefore rest all instance will contain value null.

5. Create a public method in this class, for example I am creating a method to display message (name DisplayMessage), you can perform your actual task over here.

1
2
3
4
public void DisplayMessage()
{
     Console.WriteLine("My First SingleTon Program");
}
public void DisplayMessage()
{
     Console.WriteLine("My First SingleTon Program");
}

6. Now we will create an another Class (name Program).
Collapse

1
2
class Program
{}
class Program
{}

7. Create an entry point to the above class by having a method name Main.

1
2
3
4
5
6
static void Main(string[] args)
{
    SingleTonSample singleton = SingleTonSample.InstanceCreation();
    singleton.DisplayMessage();
    Console.ReadLine();
}
static void Main(string[] args)
{
    SingleTonSample singleton = SingleTonSample.InstanceCreation();
    singleton.DisplayMessage();
    Console.ReadLine();
}

Now we to need to analyse this method in depth. As we have created an instance singleton of the class SingleTonSample by calling the static method SingleTonSample.InstanceCreation() a new object gets created and hence further calling the method singleton.DisplayMessage() would give an output “My First SingleTon Program”.


Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.