C# · CodeProject · Dotnet

Singleton in non-inheritable vs inheritable classes

Dear Reader,

Today i learned some thing from my architect @work place which i would like to share with you all. In fact i was reviewing some code and there i saw a class having singleton implemented but not in a traditional way in other words we developers more often implement singleton pattern for static (sealed)/non-inheritable classes as shown below:

 public class SingletonClass
    {
        private static SingletonClass _instance = new SingletonClass();

        static SingletonClass() { }
        private SingletonClass() { }

        public static SingletonClass Instance
        {
            get
            {
                return _instance;
            }
        }
    }

As you might be thinking, the above pattern is much of a standard one used by most of us right. But this class what i was reviewing had a protected ctor in it instead of a private ctor. Infact until now i did not know there could be a singleton pattern which can be implemented this way. I had always thought that singleton is implemented mostly for non-inheritable classes (static/sealed) but not for classes which are in inheritance heirarchy. So as you might expect this strange code confused me and i started to discuss the same with my architect.

Lemme show you a sample code:

   public class SomeBase
   {
       private static SomeBase _instance = new SomeBase();

       static SomeBase(){}
       protected SomeBase()
       {
           Console.WriteLine("Base");
       }

       public static SomeBase Instance
       {
           get { return _instance; }
       }
   }

   public class SomeChild : SomeBase
   {
       private static SomeChild _instance = new SomeChild();

       protected SomeChild()
       {
           Console.WriteLine("child");
       }
       static SomeChild(){}

       public new static SomeChild Instance
       {
           get
           {
               return _instance;
           }
       }
   }

The above singleton implementation surprised me actually and its pretty cool to see and learn that singleton pattern is quite different in a non-inheritable vs inheritable classes. We actually do this style when we want singleton in inheritance hierarchy. It’s actually like hitting 2 birds with 1 stone. This is why i guess OOPS is lovable 🙂

Although in the above pattern implementation there is a small catch, the base protected ctor gets called twice, so one must be careful in implementing or having any initialization code in the ctor.

There is also another catch here is that, the base classes singleton pattern is of no use if its sub-classes has not implemented singleton pattern in itself. That way, one can take advantage of actual intentions here in other words even though my intention of the base class was to be a singleton but due to inheritance and due to sub-classes not implementing singleton, i can create many instances of the base class via sub-class.

Now assuming that the inheritance is implemented with complete singleton, i am actually wondering where in our real world design problem such scenarios could get deployed? If you know, kindly leave a comment.

Hope you liked it, your comments are always appreciated.

Happy Coding 🙂

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

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