Overview
The singleton design pattern is focused on
having one—and only one—instance of an object. It instills encapsulated control
of the object from a common place. Most commonly, singletons don't allow
any parameters to be specified when creating the instance - as otherwise a
second request for an instance but with a different parameter could be
problematic! The class implemented using this pattern is responsible for
keeping track of its sole instance rather than relying on global variables to
single instances of objects.
This article deals only with the situation where no
parameters are required. The following sample application depicts how to
implement the singleton design pattern in Windows Application using C#.
Software Requirements
Microsoft Visual C# 2005 Express Edition (or better)
Create the Child form
Add a new Windows Application project (with name as SingletonDesignPattern)
and to it add a form as frmchild and paste the following code in it as shown
below:
Listing 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace SingletonDesignPattern
{
public partial class frmChild: Form
{
private static int iCount = 0;
public frmChild()
{
InitializeComponent();
iCount = iCount + 1;
}
public static int Count
{
get
{
return iCount;
}
}
}
}
In listing 1, firstly a static variable iCount is declared
which keeps the count of the number of instances of the child form. Then in the
form constructor, the iCount variable is incremented every time an instance of the
form is created.
Next, through the Count Public property, the count of the number
of instances of the form is returned to the calling form.
Finally, in the frmChild_FormClosing event, the iCount
variable is set to zero when the frmchild dialog is closed. This is done to
allow the user to open the frmchild dialog again from the Main form if the frmchild
dialog is not open.
Create the Parent form
Add one more form called frmParent to the above windows
application project. To this form, add a Menustrip control with the name menuToolStripMenuItem.
Add a menuitem with name of ChildForm to the Menustrip control. The screen
should look as below:
Figure 1
Paste the following code in the click event of the ChildForm
MenuItem.
Listing 2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace SingletonDesignPattern
{
public partial class frmParent: Form
{
public frmParent()
{
InitializeComponent();
}
private void childFormToolStripMenuItem_Click(objectsender, EventArgs e)
{
frmChild objform = new frmChild();
if (frmChild.Count == 1)
objform.Show();
}
}
}
In Listing 2, first an instance of frmChild is created and
then it is checked whether this is the first instance of frmChild. If yes, it displays
the frmChild dialog. If there is already an instance of thefrmchild dialog
open, other instances of frmChild dialog will not be opened.
Now run the Main form and click the childform menuitem upon
which frmChild dialog is displayed as shown below.
Figure 2
Again click the childform menuitem many times, but this time
additional instances of the frmChild dialog will be not opened. Notice that at
any one time, we are able to open Childform dialog only once from the Main form.
Downloads
[Download
Sample]
Conclusion
The Singleton design pattern has proven to be a useful
pattern in many programs. Now, the code in this code snippet shows how to
implement that same pattern in a .NET Windows Application. There are various
different ways of implementing the singleton pattern in C#.
Resources
Session Based Singleton
Singleton Design
Pattern at dofactory.com