Singleton Inheritance

后端 未结 4 1903
再見小時候
再見小時候 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: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.

提交回复
热议问题