Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Singleton Design Pattern Real-Time Example in C#

The Singleton Pattern ensures that a class has only one instance in the across application and the object instance coordinates across the app.

This pattern is the simplest design pattern and is used to restrict the creation of multiple object instances.

This pattern is the simplest design pattern and is used to restrict the creation of multiple object instances.

A class should have the following structure for the Singleton Design Pattern:

1.      Should have a private or protected constructor. No public and parameterized constructors.

2.      Should have a static property to return an instance of a class.

3.      At least have one non-static public method for a singleton operation.

The Singleton pattern is often used for:

1.      Logging

2.      Database connections

3.      Caching

4.      Thread pools

5.      Data Sharing

The main purpose of the singleton design pattern is to ensure that a class only has one instance and provide a global point of access to it throughout the life of an application.


There are several ways to Implement the Singleton Pattern in C#
1.      Non-thread-safe version
2.      Simple Thread Safety via locking
3.      Double-checked locking
4.      Safety through initialization
5.      Safe and Fully Lazy Static initialization
6.      Lazy

Let's see the implementation of the Singleton Pattern in C# in depth –

Example 1 for Non-thread-safe version:

//First version - not thread-safe
//Bad code! Do not use it!

public sealed class Singleton
{
    private static Singleton instance=null;

    private Singleton() {   }

    public static Singleton Instance
    {
        get
        {
            if (instance==null)
            {
                instance = new Singleton();
            }
            return instance;
        }
    }
}

Example 2 for Simple thread safety via locking:

//Second version - simple thread-safety

public sealed class Singleton
{
    private static Singleton instance = null;
    private static readonly object padlock = new object();

    Singleton()  { }

    public static Singleton Instance
    {
        get
        {
            lock (padlock)
            {
                if (instance == null)
                {
                    instance = new Singleton();
                }
                return instance;
            }
        }
    }
}

Example 3 for Double-checked locking:

//Third version - attempted thread safety using double-check locking
// Bad code! Do not use it!

public sealed class Singleton
{
    private static Singleton instance = null;
    private static readonly object padlock = new object();

    Singleton() {  }

    public static Singleton Instance
    {
        get
        {
            if (instance == null)
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new Singleton();
                    }
                }
            }
            return instance;
        }
    }
}

Example 4 for Safety through initialization:

//Fourth version - not quite as lazy, but thread-safe without using locks

public sealed class Singleton
{
    private static readonly Singleton instance = new Singleton();

    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static Singleton() {  }

    private Singleton() {  }

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

Example 5- Safe and fully lazy static initialization: 



This post first appeared on Programming, please read the originial post: here

Share the post

Singleton Design Pattern Real-Time Example in C#

×

Subscribe to Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×