Singleton Inheritance

后端 未结 4 1977
再見小時候
再見小時候 2021-02-20 00:28

How do I inherit from a singleton class into other classes that need the same functionality? Would something like this make any sense?

相关标签:
4条回答
  • 2021-02-20 01:08

    There are several solutions that use inner classes and reflection (Some of them are mentioned in other answers). However I believe all these solutions involve a tight coupling between the clients and the derived classes.

    Here is a way that allows the clients to depend only on the base class and is open for extension.

    The base class is similar to the usual implementation, except we define it as an abstract class (If it is legitimate to instantiate the base class you can also define it like you would a regular singleton)

    public abstract class Animal {
        protected static Animal instance;
        public static Animal theInstance(){
            assert instance != null : "Cannot instantiate an abstract Animal";
            return instance;
        }
        abstract void speak();
    }
    

    Any subtype would have its own theInstance method (you can't override a static method). For Example:

    public class Dog extends Animal{
        protected Dog(){}
    
        public static Animal theInstance(){
            if (instance == null){
                instance = new Dog();
            }
            return instance;
        }
    
        @Override
        void speak() {
            System.out.println("Wouf");
        }
    }
    

    In the main class you would write Dog.theInstance() and any subsequent client would call Animal.theInstance(). This way clients can be totally independent of the derived classes and you can add new subclasses without needing to recompile the clients (Open Closed Principle).

    0 讨论(0)
  • 2021-02-20 01:15

    Jon Skeet wrote about this a while back. It is possible to achieve some of the benefits of inheritance with the Singleton, although using nested inner classes does leave a little to be desired. It doesn't have infinite extensibility, it's only a technique for having a Singleton choose its own implementation at runtime.

    Realistically, inheriting from a Singleton doesn't make all that much sense, because part of the Singleton pattern is instance management, and once you already have a physical instance of a base type then it's too late to override any of this in the derived type. Even if you could, I suspect that it could lead to a design that's difficult to understand and even more difficult to test/maintain.

    0 讨论(0)
  • 2021-02-20 01:15

    You can inherit from singleton and for "reuse" or some fine tuning using templates (C++) or generics (C#.NET).

    I've posted in my blog (www.devartplus.com) a serie of posts in this subject:

    1) Basic singleton inheritance in C#.NET

    2) Thread-safe singleton inheritance in C#.NET

    3) Several singleton implementations in C++

    You are invited to visit those links, and share with all your opinion. Good luck.

    0 讨论(0)
  • 2021-02-20 01:26

    Someone feel free to correct me, but the way I understand it, and had an error with this once:

    public class BaseClass
    {
      protected static List<string> ListOfSomething { get; set; }
    }
    
    public class ChildClass
    {
      protected static List<int> ListOfSomethingElse { get; set; }
    }
    
    public class AnotherChildClass
    {
      protected static List<int> ListOfSomethingElse { get; set; }
    }
    

    Both of the child classes would share the same ListOfSomething, they would not have their own copy. The same static one would be shared amongst all children. This is the molt of singleton behavior and inheritance. As silky said...you just shouldn't do it, you'll probably run into something along these lines.

    If you're not talking about something like this...I'm not sure what singleton you're speaking of, and an example would help greatly, since singleton's have a great deal of niche uses.

    0 讨论(0)
提交回复
热议问题