Saar Carmi

A .NET Blog

<September 2008>
SuMoTuWeThFrSa
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011


Navigation

Subscriptions

Post Categories



Singletons and inheritance

Jon Skeet posted an example of Singletons and inheritance.

What do you think about this generic singleton solution? This way you dont have to update the factory method with each subclass creation. It would be neater if the new generics' constraint would support parameters, but you can overcome it with abstract method CreateInstance which returns T.

abstract class DataProvider<T> where T: new()
   {
        private static T mInstance = new T();

        public T Instance
        {
            get
            {
                return mInstance;
            }
        }

        public abstract void Connect();
    }

    class Oracle : DataProvider<Oracle>
    {
        public override void Connect()
        {
            Console.WriteLine ("Connecting to Oracle");
        }
    }

    class SqlServer : DataProvider<Oracle>
    {
        public override void Connect()
        {
            Console.WriteLine ("Connecting to Sql Server");
        }
    }

posted on Monday, February 06, 2006 12:31 PM by saarc





Powered by Dot Net Junkies, by Telligent Systems