SIGN UP MEMBER LOGIN:    
ARTICLE

Singleton Pattern

Posted by Amit Dhania Articles | Design & Architecture June 09, 2009
In this article we will implement Singleton pattern to maintain global variables in Winform application.
Reader Level:
 


Singleton Pattern:- A design pattern used in application when we need to restrict instantiation of a class to one object.

In following example, we have a Winform application. We need to maintain global variable. For it we create a singleton class and access it throughout application.

--------------Singleton Class------------


class
cGlobalVariable

{

    private string userName;

    private string passWord;

    private int patientID;

 

    private static cGlobalVariable objcGlobalVariable;

    private cGlobalVariable()

    {

    }

    public string UserName

    {

        get { return userName; }

        set { userName = value; }

    }

    public string Password

    {

        get { return passWord; }

        set { passWord = value; }

    }

    public int PatientID

    {

        get { return patientID; }

        set { patientID = value; }

    }

    public static cGlobalVariable GetInstance()

    {

        if (objcGlobalVariable == null)

        {

            objcGlobalVariable = new cGlobalVariable();

        }

        return objcGlobalVariable;

    }

 

}

-----------Use of Singleton class in Window form--------------

public partial class Form1 : Form

{

    public Form1()

    {

        InitializeComponent();

    }

 

    private void button1_Click(object sender, EventArgs e)

    {

        cGlobalVariable objcGlobalVariable = cGlobalVariable.GetInstance();

        label1.Text = objcGlobalVariable.UserName;

        objcGlobalVariable.PatientID = 572;

    }

 

    private void button2_Click(object sender, EventArgs e)

    {

        cGlobalVariable objcGlobalVariable = cGlobalVariable.GetInstance();

        label2.Text = objcGlobalVariable.PatientID.ToString();

    }

 

    private void Form1_Load(object sender, EventArgs e)

    {

        cGlobalVariable objcGlobalVariable = cGlobalVariable.GetInstance();

        objcGlobalVariable.UserName = "Amit Dhania";

    }

}

Above code give simple way to use singleton pattern in application where we can keep static variable values available throughout application.

Login to add your contents and source code to this article
share this article :
post comment
 

Very nice Article for Begginers like me..

Posted by ashok kumar Nov 29, 2010

its a nice article .please post some topics on oops.

Posted by guru Oct 04, 2010

This article give basic detail to make understanding what actually Singleton pattern is. So this article is created for those who are beginers or wants things to be explained very simple to understand.

Posted by Amit Dhania May 18, 2010

Singletons are best avoided where possible. They make unit testing harder and impose a constraint upon the application that is then difficult to break (try removing a singleton from a legacy application).

The pattern shown here is the most simplistic implementation of the Singleton pattern, is not threadsafe, and doesn't leverage the native .Net support for the pattern.
See Microsoft's discussion on implementations of the singleton pattern here http://msdn.microsoft.com/en-us/library/ms998558.aspx, as a starting point.

John

Posted by John Brett Jul 16, 2009

Thanks  Article and Code is Self Explanatory. :-)

Posted by Ketan Kukade Jul 10, 2009
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor