Click here to Skip to main content
15,891,253 members
Articles / Programming Languages / C#

My Personal and Also Another Approach to Handling the Singleton Design Pattern

Rate me:
Please Sign up or sign in to vote.
1.95/5 (8 votes)
14 Sep 20071 min read 26.8K   68   19  
Quick and simple use of the singleton design pattern
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace UZi.Singleton.TestApp
{
    public partial class Form1 : Form
    {
        private SingletonTestClass _singletonInstanceOfSingletonTestClass = null;
        private SingletonClassConstructorArgsChecker _checker = null;

        public Form1()
        {
            InitializeComponent();
            Text = string.Format("{0}", Application.ProductName);
            _checker = (SingletonClassConstructorArgsChecker)CheckSingletonTestClassConstructorArgs;

            try
            {
                _singletonInstanceOfSingletonTestClass = Singleton<SingletonTestClass, SingletonBuilder<SingletonTestClass>>.GetInstance(
                    _checker, new object[] { "Uwe", "Zimmermann" });
            }
            catch (Exception ex)
            {
                MessageBoxOptions options =
                    CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft ?
                    MessageBoxOptions.RtlReading : 0;
                MessageBox.Show(this, ex.Message, System.Windows.Forms.Application.ProductName,
                    MessageBoxButtons.OK, MessageBoxIcon.Error,
                    MessageBoxDefaultButton.Button1, options);
            }

            button1.Enabled = button2.Enabled = _singletonInstanceOfSingletonTestClass != null;
        }

        // simple check of data of our SingletonTestClass-instance...
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(string.Format("{0} {1}", _singletonInstanceOfSingletonTestClass.FirstName,
                _singletonInstanceOfSingletonTestClass.LastName), "information", MessageBoxButtons.OK,
                MessageBoxIcon.Information);
        }

        /// <summary>
        /// Checks the arguments of our SingletonTestClass. Here are the arguments for the constructor 2 strings.
        /// </summary>
        /// <param name="args">object-Array with 2 elements of type string.</param>
        /// <returns>bool.</returns>
        /// <exception cref="ArgumentNullException">If args is NULL.</exception>
        /// <exception cref="ArgumentException">If args don't have two elements or type of the elements isn't string.</exception>
        private bool CheckSingletonTestClassConstructorArgs(object[] args)
        {
            if (args == null) throw new ArgumentNullException("args");
            if (args.Length != 2) throw new ArgumentException(
                "Parameter 'args' must contain 2 elements!");
            for (int i = 0; i < 2; i++)
            {
                if (args[i].GetType() != typeof(string)) throw new ArgumentException(
                    string.Format("Type of element {0} of parameter 'args' must be {1}!", i, typeof(string).FullName));
            }
            return true;
        }

        // another check of data of our SingletonTestClass-instance...
        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show(string.Format("{0}, {1}", _singletonInstanceOfSingletonTestClass.LastName,
                _singletonInstanceOfSingletonTestClass.FirstName), "information", MessageBoxButtons.OK,
               MessageBoxIcon.Information);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Germany Germany
.NET Professional: 'specialist for application development' (a german job title)

I normally develop applications in C# with the .net-framework 2.0+/3.0 and currently databases for MS Sql Server 2005 and MS Access 2003+.

I'm an employee of an office which is working for insurances and which is watching investigations of damages for incorrect items, plausibility and so on and my job is to develop software to automate these processes. I developed for example an application which can extract values from ocr-texts using regular expressions and save these values into a database for later calculations or other processes.

Comments and Discussions