CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Hybrid View

  1. #1
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Singleton Pattern problem (inheritance)

    suppose i have a class i want as a singleton, and i write:

    Code:
    class Thing{
      static Thing me;
    
      private Thing(string param){
        //do some stuff
      } 
    
      static getInstance(){
        if(me ==null)
          me = new Thing();
    
        return me;
      }
    }
    how do i now inherit this. the new inherited version needs to have a constructor that does something different.. i cant override getInstance in the child class, to call new ChildThing() because static methods cannot be overridden

    Code:
    class ChildThing : Thing{
      
    
      private ChildThing(string param){
        //do some more stuff
      } 
    
      static override getInstance(){
        if(base.me ==null)
          base.me = new ChildThing();
    
        return base.me;
      }
    }

    so how does one write a singleton that is inheritable?
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Singleton Pattern problem (inheritance)

    I don't think Singleton classes can be inheritted. As you have already mentioned that the constructor is private and the getInstance method is static.

    So having inheritence in this case is impossible.

  3. #3
    Join Date
    Feb 2005
    Location
    Israel
    Posts
    1,475

    Re: Singleton Pattern problem (inheritance)

    Long ago I wanted to do the same thing: I wanted to make the base class a singleton, and force the childs to be singletons as well without coding anything in the child classes to indicate it.

    From OO point of view, this cannot be achieved: static methods can obviousely not be virtual, and the creational pattern of the base class do not suggest anything about the creational pattern of the childs.

    I thought about implementing this in Aspect Oriented Programming - to implement an Attribute called Singleton attribute, which I can place on the base class and then all the hirerchy will be singletons. Unfortunately, this involves very difficult code interception mechanism and I couldn't get it to work. I am almost possible that this can be achieved though.

    So, I would suggest seeking the aid of other design patterns. For example you can have a Repository class which hold instances of classes from the hirerchy tree, and using a FactoryMethod, given a Type return the correct object. This will give you the exact result as singleton would.

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Singleton Pattern problem (inheritance)

    You can do this using templates in C++ : see here. Read my comments in the replies about making the constructor of the derived class private before making any judgements.

    Possibly you could apply the same methodology using generics in C#.

    e.g.

    Code:
        public class Singleton<T> where T : new()
        {
            static T _instance = default(T);
    
            protected Singleton()
            {
            }
    
            static public T Instance
            {
                get
                {
                    if (_instance == null)
                    {
                        _instance = new T();
                    }
    
                    return _instance;
                }
            }
        }
    
        public class MySingleton : Singleton<MySingleton>
        {
            public MySingleton()
            {
            }
        }
    Unfortunately the inherited classes have to have a public constructor, because we don't have friends in C# (no I don't have any friends... ) but it might get close to what you want to do. I think you can probably exert better checking - i.e. throw an exception if the constructor isn't being called by the base class - but you can't hide the constructor as far as I can see.

    Darwen.
    Last edited by darwen; February 22nd, 2006 at 04:53 PM.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  5. #5
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Singleton Pattern problem (inheritance)

    Or you can use Factory Pattern. Create standalone class (it can be singleton itself) which will keep instances of your "singleton" classes and ask that class for instances of your classes. Yes, you have to have public constructors in your classes, but I don't think it is a great complication.

    I imagine it like it (just fragment). I design it parametrizable. There are many variations.
    Code:
    public class ThingFactory
    {
      private static instances = new Hashtable();
      
      public static object GetInstance(Type type)
      {
        Object instance = instances[type];
        if (instance == null)
        {
           instance = Activator.CreateInstance(type);
           instances[type] = instance;
         } 
    
        return instance;
      }
    }
    
    // and use it
    ChildThing childThing = ThingFactory.GetInstance(typeof(ChildThing);

    Maybe, someone could try to solve this with generics. Similar to darwen's solution, but I am not sure how it would work in C# and I cannot try it.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  6. #6
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: Singleton Pattern problem (inheritance)

    thanks for the replies guys! i'll be putting them into practice!

    one thing i wondered; someone said that obviously static methods cannot be virtual or override.. i want to know why?
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured