Click here to Skip to main content
11,640,910 members (61,356 online)
Click here to Skip to main content

Tagged as

Singleton in non-inheritable vs inheritable classes

, 20 Nov 2011 LGPL3 9.2K 2
Rate this:
Please Sign up or sign in to vote.
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:

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:

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)

Share

About the Author

zenwalker1985
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/

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150731.1 | Last Updated 21 Nov 2011
Article Copyright 2011 by zenwalker1985
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid