Should we seal Singletons? Should we try to inherit from Singletons in the first place?

后端 未结 5 1247
夕颜
夕颜 2021-01-06 00:08

Should a Singleton class be allowed to have children? Should we seal it? What are the pro\'s and con\'s?

For being able to inherit from a Singleton class, we would h

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-06 01:05

    A better solution is to use an IoC Container framework to handle the "Singleton" (lifetime) aspect of the class. At that point you can use a POJO and simply inherit from it.

    EDIT: Some links that may help:

    • List of .NET Dependency Injection Containers (IOC)
    • Open Source Inversion of Control Containers (Java)
    • Dependency Injection in .NET (book)
    • Mark Seemann's .NET blog - Dependency Injection category

    Look for ones that handle lifetime management. You want to be able to configure the container (which acts like a generic factory) so that requests for instances of "Singleton" classes always return the same instance (at least for the same container). You generally only have one container instance at the highest level of your application.

提交回复
热议问题