Tuesday, August 18, 2009

Singleton pattern in C#



Intent: The Singleton pattern ensures that a class has only one instance, and provides a global point of reference to that instance.

Non-Software Example:
The United States (In that matter any country’s President’s Office) can be example of Singleton Pattern. At the most at any point of time there can be only one President to hold the position, which means that there is only one instance at any point of time.



Benefits:

The president title controlled access to the sole instance. Since the president office encapsulates the president there is much control over the single instance.

Software Example & Explanation of Code:

Let us assume that we are building an ASP.Net application for a school, where we have to display information about the school in most of the pages of the application. Of course I understand this may not be a very good example in the real time scenario, but from understanding a concept perspective this gives a good understanding.
In the example we have two pages “Default.aspx” and “Page2.aspx” which needs to display the school information. Although both the pages will make the request to the “SchoolMaster” class for the school information, but both the requested will be served from the same instance once it is instantiated.

This approach ensures that only one instance is created and only when the instance is needed. Also, the variable “objSingleTon” in “SchoolMaster” Class is declared to be volatile to ensure that assignment to the instance variable completes before the instance variable can be accessed. Lastly, this approach uses an “objRoot” instance to lock on, rather than locking on the type itself, to avoid deadlocks.

The double-check locking approach solves the thread concurrency problems while avoiding an exclusive lock in every call to the Instance property method. It also allows you to delay instantiation until the object is first accessed. In practice, an application rarely requires this type of implementation. In most cases, the static initialization approach is sufficient. Below is the code snippet

SchoolMaster.cs

public class SchoolMaster
{
private static volatile SchoolMaster objSingleTon;
private static object objRoot = new Object();

private string _SchoolName;

public string SchoolName
{
get { return _SchoolName; }
set { _SchoolName = value; }
}
private string _SchoolStandards;

public string SchoolStandards
{
get { return _SchoolStandards; }
set { _SchoolStandards = value; }
}

//Declare a private constructor so that no one can create the object from outside
private SchoolMaster()
{
_SchoolName = "ABC SCHOOL LTD";
_SchoolStandards = "High Standard School";
}

public static SchoolMaster GetSchoolDetails()
{
if (objSingleTon == null)
{
lock (objRoot)
{
//Double check again
if (objSingleTon == null)
{
objSingleTon = new SchoolMaster();
}
}
}
return objSingleTon;
}
}

Default.aspx.cs

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strName = SchoolMaster.GetSchoolDetails().SchoolName;
string strStandards = SchoolMaster.GetSchoolDetails().SchoolStandards;

Response.Write("Welcome To : " + strName +"--");
Response.Write("Standards are : " + strStandards);
}

protected void btnMove_Click(object sender, EventArgs e)
{
Response.Redirect("Page2.aspx");
}
}

Page2.aspx.cs

public partial class Page2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strName = SchoolMaster.GetSchoolDetails().SchoolName;
string strStandards = SchoolMaster.GetSchoolDetails().SchoolStandards;

Response.Write("Welcome To : " + strName + "--");
Response.Write("Standards are : " + strStandards);

}
}


The entire code for download is available at http://www.csharppatterns.com/


No comments: