Click here to Skip to main content
13,005,757 members (69,473 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 12 Sep 2007

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

, 14 Sep 2007
Rate this:
Please Sign up or sign in to vote.
Quick and simple use of the singleton design pattern


I wanted to have my own and simple handling for the singleton design pattern without implementing it many times. Singleton is an object-based creation pattern. You should implement it to ensure that only one instance of a class exists to have a global access point.

I often used the singleton design pattern to hold the settings of a configuration file. However, I had to implement the pattern every time and sometimes I had to write a method for initializing some variables of the instance, as well as implement a check to watch for instance initialization. I needed a quick and simple way to use the singleton design pattern... And here comes my approach to solving this.

Using the Code

I delegated creation and initialization of the singleton class instance to a generic builder class called UZi.Singleton.SingletonBuilder<T>. See more details in my source code. You can (and should) check the arguments for the constructor of your singleton class with the delegate UZi.Singleton.SingletonClassConstructorArgsChecker. Now you can write code like this, if x is a defined class:

x singletonInstanceOfX = UZi.Singleton.Singleton<x>, 
    new object[] { 1, "string_value" });

You can now use singletonInstanceOfX everywhere in your application and you only ever have this one instance of x. Don't forget to make the constructor of your class, which should be a singleton class, private or protected!

Points of Interest

With my approach, you can simply use the singleton design pattern. You'll never have to implement it for a class serving as a singleton instance. We also never have to implement a builder class to build an initialized instance of our singleton class. Use the generic code and save time.


  • 2007-09-12: First version
  • 2007-09-12: Tried to fix some formatting mistakes


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


About the Author

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.

You may also be interested in...

Comments and Discussions

GeneralFurther reading Pin
Peter Ritchie17-Sep-07 9:59
memberPeter Ritchie17-Sep-07 9:59 
GeneralThanks for your contribution Pin
jonnii12-Sep-07 6:31
memberjonnii12-Sep-07 6:31 
AnswerRe: Thanks for your contribution Pin
SaxoniaCoder12-Sep-07 18:47
memberSaxoniaCoder12-Sep-07 18:47 
GeneralRe: Thanks for your contribution Pin
jonnii12-Sep-07 22:48
memberjonnii12-Sep-07 22:48 
GeneralRe: Thanks for your contribution Pin
Peter Ritchie17-Sep-07 9:53
memberPeter Ritchie17-Sep-07 9:53 
jonnii wrote:
If you only want one instance of a class, then the best solution is to only create one instance.

That's a rather simplistic comment. What does "only create one instance" mean? In what context is this instance? You could say the context is within a static class or a static member of that class; but what if you want to decouple that single instance from the class it's contained in and share it amongst other classes with different life spans. The only way you can really do that is to decouple if away from all the other classes and abstract it into it's own through a factory pattern. That way the fact that the instance is a Singleton is completely abstracted way from the implementation detail of the various classes that use it.

A singleton is indeed an optimization. It's rarely "required" but it is useful under certain circumstances, like caching a database connection or wrapping a single-instance concept like a serial port.

Having said that, I also think the Singleton class of this solution is over-engineered.

GeneralRe: Thanks for your contribution Pin
Phil_N12-Sep-07 23:03
memberPhil_N12-Sep-07 23:03 
GeneralRe: Thanks for your contribution Pin
Urs Enzler17-Sep-07 22:39
memberUrs Enzler17-Sep-07 22:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170627.1 | Last Updated 14 Sep 2007
Article Copyright 2007 by SaxoniaCoder
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid