Design Patterns - Singleton Pattern

SingleTon Pattern:

In Singleton Design pattern we restrict the number of instances of a class to 1. So for a particular class we can have only one object. The singleton class should have a global point of access.

Steps to create Singleton class:

1) Declare the instance of a class as static
2) create a private constructor( so that nobody can create new object )
3) Define a static method to create the instance of a class. If the instance already exists return the same instance and if it doesn't exist create a new one.

Example:
class Sample
{
private static Sample inst = null;
private static readonly object objLock = new object();
private Sample(){}
public static Sample CreateInstance()
{
lock (objLock)
{
if (inst == null)
{
inst = new Sample();
}
}
return inst;
}
}

Comments

Popular posts from this blog

Network Security: LAN manager authentication level

Cisco AnyConnect Secure Mobility Client - VPN service not available. The VPN agent service is not responding