Please enable Javascript to correctly display the contents on Dot Net Tricks!

Singleton Design Pattern - C#

  Author : Shailendra Chauhan
Posted On : 08 Jun 2013
Total Views : 94,638   
Updated On : 26 Sep 2016
 

Singleton pattern falls under Creational Pattern of Gang of Four (GOF) Design Patterns in .Net. It is pattern is one of the simplest design patterns. This pattern ensures that a class has only one instance. In this article, I would like share what is Singleton pattern and how is it work?

What is Singleton Pattern?

Singleton pattern is one of the simplest design patterns. This pattern ensures that a class has only one instance and provides a global point of access to it.

Singleton Pattern - UML Diagram & Implementation

The UML class diagram for the implementation of the Singleton design pattern is given below:

The classes, and objects in the above UML class diagram are as follows:

  1. Singleton

    This is a class which is responsible for creating and maintaining its own unique instance.

C# - Implementation Code

//eager initialization of singleton
public class Singleton
{
 private static Singleton instance = new Singleton();
 private Singleton() { }

 public static Singleton GetInstance
 {
 get
 {
 return instance;
 }
 }
}

////lazy initialization of singleton
public class Singleton
{
 private static Singleton instance = null;
 private Singleton() { }

 public static Singleton GetInstance
 {
 get
 {
 if (instance == null)
 instance = new Singleton();

 return instance;
 }
 }
}

////Thread-safe (Double-checked Locking) initialization of singleton
public class Singleton
{
 private static Singleton instance = null;
 private Singleton() { }
 private static object lockThis = new object();

 public static Singleton GetInstance
 {
 get
 {
 lock (lockThis)
 {
 if (instance == null)
 instance = new Singleton();

 return instance;
 }
 }
 }
}

Singleton Pattern - Example

Who is what?

The classes and objects in the above class diagram can be identified as follows:

  1. Singleton - Singleton class

C# - Sample Code

/// <summary>
/// The 'Singleton' class
/// </summary>
public class Singleton
{
 // .NET guarantees thread safety for static initialization
 private static Singleton instance = null;
 private string Name{get;set;}
 private string IP{get;set;}
 private Singleton()
 {
 //To DO: Remove below line
 Console.WriteLine("Singleton Intance");

 Name = "Server1";
 IP = "192.168.1.23";
 }
 // Lock synchronization object
 private static object syncLock = new object();

 public static Singleton Instance
 {
 get
 {
 // Support multithreaded applications through
 // 'Double checked locking' pattern which (once
 // the instance exists) avoids locking each
 // time the method is invoked
 lock (syncLock)
 {
 if (Singleton.instance == null)
 Singleton.instance = new Singleton();

 return Singleton.instance;
 }
 }
 }

 public void Show()
 {
 Console.WriteLine("Server Information is : Name={0} & IP={1}", IP, Name);
 }

}

/// <summary>
/// Singleton Pattern Demo
/// </summary>
/// 
class Program
{
 static void Main(string[] args)
 {
 Singleton.Instance.Show();
 Singleton.Instance.Show();

 Console.ReadKey();
 }
}

Singleton Pattern Demo - Output

When to use it?

  1. Exactly one instance of a class is required.

  2. Controlled access to a single object is necessary.

What do you think?

I hope you will enjoy the Singleton Pattern while designing your software. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.



Free Interview Books
 
COMMENTS (1)
Sai Kiran Reply

Nice explanation. I have a question. We can use a static class and work in above scenario. What is specialty of singleton pattern there?

6 JUN
Hadoop Development cum Administration (Online)

7:00 AM-9:00AM IST

Know More
30 MAY
ASP.NET MVC with Angular4 Development (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

Know More
28 MAY
ASP.NET Core with Angular4 Development (Classroom)

10:30 AM-11:30 AM Weekend

Know More
28 MAY
ASP.NET Core with Angular4 Development (Classroom)

10:30 AM-11:30 AM Weekend

27 MAY
ASP.NET MVC with Angular4 Development (Classroom)

4:00 PM-5:30 PM Weekend

20 MAY
Angular2 and Angular4 Development (Online)

08:00 AM-10:00 AM IST(+5.30 GMT) Weekend

20 MAY
NodeJS with Angular4 Development (Classroom)

5:00 PM-6:30 PM Weekend

8 MAY
ASP.NET MVC with Angular4 Development (Online)

07:00 AM-09:00 AM IST(+5:30 GMT) M/W/F

2 MAY
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST( TUS, THR)

29 APR
ASP.NET MVC with Angular2 Development (Classroom)

9:30 AM-11:00 AM

22 APR
MEAN Stack 2 Development (Classroom)

11:00 AM-12:30 PM Weekend

10 APR
Master Class ASP.NET MVC 5 with Angular2 Plus Angular4 Development (Online)

09:00 PM - 11:00 PM IST (M/W/F)

25 MAR
Xamarin Forms : Build Cross-platform Apps (Classroom)

8:00 AM-9:30AM Weekend

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+