Fun with Singleton Pattern

I got an idea of a generic singleton class 20 minutes ago and wanted to try it out, so here are my results 🙂

For those who don’t know what a singleton pattern is, it’s purpose is to have only one instance of some class. As opposed to static classes, singletons use an instance of the class. You can find more information on Google 🙂

So, how would a generic singleton look like in C#? This is what I came up with:

public abstract class Singleton where T: Singleton
{
	private static T instance = Instantiate();

	public static T Instance
	{
		get
		{
			return instance;
		}
	}

	private static T Instantiate()
	{
		System.Reflection.ConstructorInfo[] constructors = typeof(T).GetConstructors(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly );

		foreach(System.Reflection.ConstructorInfo constructor in constructors)
			if(constructor.IsPrivate && constructor.GetParameters().Length == 0)
				return (T)constructor.Invoke(null);

		return null;
	}
}

Let me explain this a little. The class is abstract, which means you cannot create instances of it, only derive from it. It takes a generic parameter T, which must be a derivative of Singleton class (itself). Next we create a static instance of the class that derived from Singleton and expose it through property called Instance. The interesting method here is Instantiate(), which uses reflection to invoke derived class’ private parameterless constructor. Singleton classes hide their constructors, so that no instance of that class could be created outside the class.

So now we can create singleton classes by inheriting from Singleton class:

public class SingletonClass : Singleton
{
	private SingletonClass()
	{
	}

	public void WriteText()
	{
		Console.WriteLine("SingletonClass in action!");
	}
}

The only thing we need to do is to provide a parameterless constructor in the new class 🙂

What even more interesting, we can use multiple inheritance with Singleton class too, like this:

public abstract class SingletonClass: Singleton where T: SingletonClass
{

}

public class SingletonClassDerivative : SingletonClass
{
	private SingletonClassDerivative()
	{
	}

	public void WriteText()
	{
		Console.WriteLine("SingletonClassDerivative in action!");
	}
}

The private constructor must be moved to the top most class for the sample to compile. We can even use this in two ways:

SingletonClass.Instance.WriteText();
SingletonCLassDerivative.Instance.WriteText();

I don’t think I will use this class extensively, but it sure is intereting way to create singletons! 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s