Singleton Pattern

Singleton Pattern: This pattern is used you want to eliminate the option of instantiating more than one object. It means one and only one instance of the class can be instantiated.

In other words, Singleton pattern is used when, A single instance of a class is required in the application and the class itself is able to enforce the single instance on itself. The rest of the system need not worry about managing the single instance.

Suitable Scenarios for Singleton: Following are examples:

  1. Creating a printer spooler: A printer spooler is unlikely to be called from more than one place.
  2. Creating a Logging system: A simple logger is the most obvious example of a possibly-valid Singleton, but this can change with more complex logging schemes.

Different ways of Implementing Singleton: Singleton seems to a simple pattern but there are many advantages and disadvantages, depending upon implementation. Following are different implementation in detail:

  1. Singleton:

 

 

public class Singleton
{
private static Singleton instance;

private Singleton() { }

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

Points to consider:

  • Instantiation is not performed until and object ask for an instance.
  • This implementation is not thread safe. For multi-threaded enviro If separate threads of execution enter the Instance property method at the same time, more than one instance of the Singleton object may be created.
  1. Static Initialization:

public sealed class Singleton1

    {

        private static readonly Singleton1 instance = new Singleton1();

        private Singleton1() { }

        public static Singleton1 Instance

        {

            get

            {

                return instance;

            }

        }

}

Points to consider:

  • It is thread safe but not quite as lazy.
  1. Multithread Singleton:

public sealed class Singleton3

{

private static volatile Singleton3 instance;

private static object _lock;

private Singleton3()

{        }

public static Singleton3 Instance

{

get

{

if (instance == null)

{

lock (_lock)

{

instance = new Singleton3();

}

}

return instance;

}

}

}

Points to consider:

  • Only one instance is needed when only when instance is needed.
  • It used lock object to lock on to avoid deadlocks
  1. Fully Lazy Instantiation:

public sealed class Singleton4

{

private Singleton4()

{

}

public static Singleton4 Instance { get { return Nested.instance; } }

private class Nested

{

// Explicit static constructor to tell C# compiler

// not to mark type as beforefieldinit

static Nested()

{

}

internal static readonly Singleton4 instance = new Singleton4();

}

}

Point to Consider:

  • Instantiation is triggered by the first reference to the static member of the nested class, which only occurs in Instance.
  1. Using .NET Lazy<T> type

public sealed class Singleton5

{

private static readonly Lazy<Singleton5> lazy =

new Lazy<Singleton5>(() => new Singleton5());

public static Singleton5 Instance { get { return lazy.Value; } }

private Singleton5()

{

}

}

Points to consider:

  • It’s simple and performs well

Update: Code is also available on GitHub. Click here to go to git hub repository.

Author: Rupesh

Hi! I'm Rupesh, a funophile and technophile. I'm an Application Developer, Solution Architect and IT-Consultant, and an author in the works. I am a Microsoft certified Professional and Solution Developer (MCP and MCSD).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s