Click here to Skip to main content
15,884,472 members
Articles / Programming Languages / C#

Singleton in non-inheritable vs inheritable classes

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
20 Nov 2011LGPL32 min read 11.7K   2  
Singleton in non-inheritable vs inheritable classes

Today, I learned something from my architect at my 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:

C#
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 that I was reviewing had a protected ctor in it instead of a private ctor. In fact, 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 hierarchy. So as you might expect, this strange code confused me and I started to discuss the same with my architect.

Let me show you a sample code:

C#
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 it's 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 in 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. :)


Filed under: C#, CodeProject, Dotnet Tagged: .NET, blog, C#, codeproject, Dotnet, tips

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) Siemens
India India
A .net developer since 4+ years, wild, curious and adventurous nerd.

Loves Trekking/Hiking, animals and nature.

A FOSS/Linux maniac by default Wink | ;)

An MVP aspirant and loves blogging -> https://adventurouszen.wordpress.com/

Comments and Discussions

 
-- There are no messages in this forum --