Singleton Design Pattern

one of the good explanation found in ASP.NET 3.5 Application Architecture Design



public sealed class EmailManager

{

private static EmailManager _manager;

//Private constructor so that objects cannot be created

private EmailManager()

{

}

public static EmailManager GetInstance()
{

// Use 'Lazy initialization'

if (_manager == null)

{

//ensure thread safety using locks

lock(typeof(EmailManager)

{

_manager = new EmailManager();

}

}

return _manager;

}

}

let us understand the code step-by-step:

1. public sealed class EmailManager: We have used the sealed keyword to make our EmailManager class uninheritable. This is not necessary, but there is no use having derived classes as there can be only one instance of this class in memory. Having derived class objects will let us create two or more instances which will be against the singleton's design objective.
2. private static EmailManager _manager:Next, we create a variable named _manager, which holds a reference to the single instance of our EmailManager class. We have used a static modifier because we will be accessing this variable from a static method—GetEmailManager(), and static methods can use only static variables.
private EmailManager()
{
}
We need to create a private constructor to make sure that we don't accidentally initialize an object of the GetEmailManager class. By only initializing via the static GetInstance() method, we should get a single instance of this class.
3. public static EmailManager GetInstance()
{
// Use 'Lazy initialization'
if (_manager == null)
{
//ensure thread safety using locks
lock(typeof(EmailManager)
{
_manager = new EmailManager();
}
}
return _manager;
}
GetInstance() is the static method that we will use from outside this code to get the current reference of the EmailManager class. In this method, we are using the lazy loading technique to load the instance on demand. We first check if the current instance is null or not. If it is null, then we create a new one; otherwise we return the existing
static instance.
lock(typeof(EmailManager)



4. The private _manager object, marked static, is used inside a critical section (using lock) to make sure it is thread safe. Thread safety is very important here. Otherwise, two threads might simultaneously call GetInstance() and, on finding the EmailManager instance (_manager) null, will both try to create an instance, thereby creating two instances of the class. The lock keyword helps us make sure that once a thread enters the region, no other thread can do so until the first thread exits, making our code thread safe. We pass the EmailManager's type in order to lock the statement using the typeof operator to define the scope of the lock statement.
An important point to note is that in the above code we have to make sure that the
type used in the typeof() command is not publicly accessible, otherwise the scope
would be affected. It is better to create a private object within our class to use as a
reference object in the lock statement, as in:
private static object forLock = new object();
public static EmailManager GetInstance()
{
// Use 'Lazy initialization'
if (_manager == null)
{
//ensure thread safety using locks
lock(typeof(forLock)
{
_manager = new EmailManager();
}
}
return _manager;
}
So the above code can be used for implementing a Singleton design pattern in

ASP.NET effectively and safely.

Comments

Popular posts from this blog

How to get motherboard serial number, Processor ID, VolumeSerialNumber using C#?

Fiserv Interview Questions.

AngularJs - Simple Example