Search this site:

2008-01-25

Implmentation of Singleton Pattern in C#


When you are talking about programming, a Singleton is a design pattern that address the problem of restricting only one single instance of a class. You can easily find implementations of the pattern from the web. Today when I was working on a programming tasks using C# .NET, I suddenly come up with an idea of having a generic purpose singleton. If you are using .NET Framework 2.0 or above, this can be easily achieved by using generics in C#. However, my program is still using .NET Framework 1.1 so I have to find other ways.

Thanks Chau for working it out with me, here is the solution we got:

public class Singleton
{
// hash table for storing the instances
private static System.Collections.Hashtable _instances = null;

// for thread safety
private static object MUTEX = new object();

// private to prevent instantiation of the class
private Singleton() { }

// overloaded method for convenience
public static object getInstance(System.Type type)
{
return getInstance(type, new object[0]);
}

// get an instance of specified type, the object array is the parameters to constructor
public static object getInstance(System.Type type, object[] parameters)
{
lock(MUTEX)
{
// initialize the internal hash table during the very first call
if (_instances == null)
_instances
= new Hashtable();

// just like ordinary implmentation of singleton, only creates new instance when
// there isn't one yet
if (!_instances.Contains(type))
{
// we need a list of type to get the ConstructorInfo object
System.Type[] types = new Type[parameters.Length];
for (int i=0; i<parameters.Length; i++)
types[i]
= parameters[i].GetType();

// the new instance is created by the following 3 lines of code
System.Reflection.ConstructorInfo constructor = type.GetConstructor(types);
object instance = Activator.CreateInstance(type);
constructor.Invoke(instance, parameters);

// store it for future use
_instances[type] = instance;
}

return _instances[type];
}
}
}


You can then use it like this:
SomeClass c = (SomeClass) Singleton.getInstance(typeof(SomeClass));


Someone may argue that this is not really a singleton, because one may still instantiate the class directly using its constructors. Strictly speaking yes. So this should only be a workaround, that to those classes can only be initiated once through this object. Maybe I really shouldn't name it a singleton... Anyway, please let it be until I got some other ideas.

Comments are welcome, thanks! Since the codes are simple and the main purpose is only to illustrate the idea, I would like to put it into the public domain, just use it freely if you find it useful. :)

Codes are highlighted by Actipro CodeHighlighter.


No comments:

Post a Comment

HTML Tags allowed (e.g. <b>, <i>, <a>)

Disclaimer

ALL CONTENTS AND INFORMATION IN THIS WEB SITE ARE PROVIDED "AS IT" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED. THE ACCURACY AND AVAILABILITY OF THE CONTENTS, INFORMATION AND THE WEB SITE ITSELF ARE NOT GUARANTEED. THE AUTHOR TAKES NO RESPONSIBILITIES ON ANY COSTS OR DAMAGES (DIRECT OR INDIRECT) ARISING OUT OF OR IN CONNECTION WITH THE ACCESS, USAGE OR INABILITY OF USAGE OF THIS WEB SITE.