如何从单例类继承到需要相同功能的其他类? 这样会有什么意义吗?Singleton Inheritance
6
A
回答
0
有人随时纠正我,但我的方式的理解,并与这一次的错误:
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; }
}
两个子类的将共享相同ListOfSomething
,他们不会有他们自己的副本。在所有儿童之间将共享相同的静态。这是单身人士行为和继承的蜕变。丝毫说...你不应该这样做,你可能会碰到沿着这些线。
如果你不是在谈论这样的事情......我不确定你在说什么单身人士,而且一个例子会有很大帮助,因为单身人士有很多利基用途。
11
Jon Skeet wrote about this回来。虽然使用嵌套的内部类没有什么需要,但可以实现与Singleton的继承的好处的一些。它没有无限的可扩展性,它只是一种让Singleton在运行时选择自己的实现的技术。
实际上,从Singleton继承并没有多大意义,因为Singleton模式的一部分是实例管理,并且一旦您已经拥有基本类型的物理实例,那么覆盖任何此类实体就太晚了在派生类型中。即使你可以,我怀疑它可能会导致难以理解,甚至更难以测试/维护的设计。
3
您可以从单例继承并使用模板(C++)或泛型(C#.NET)进行“重用”或一些微调。
我已经张贴在我的博客(www.devartplus.com)在这个问题的帖子的意甲:
1)Basic singleton inheritance在C#.NET
在C#.NET2)线程安全的单继承
3)C++中的几个单例实现
欢迎您访问这些链接,并分享您的所有意见。 祝你好运。
相关问题
- 1. C++ inheritance
- 2. ReboundPanel Inheritance
- 3. Javascript:Protoypal Inheritance
- 4. android java inheritance
- 5. PHP Doctrine Inheritance
- 6. boost :: shared_ptr和Inheritance
- 7. Codeception Cest Inheritance
- 8. Coffeescript Backbone extends/Class Inheritance
- 9. scala value class multiple inheritance
- 10. Javascript Prototypal Inheritance怀疑二
- 11. Objective-C使用#import和inheritance
- 12. Table-per-hierarchy and inheritance implementation issue
- 13. FOSUSER Bundle with many USer + Inheritance
- 14. 匕首2 Singleton vs Real Singleton
- 15. Singleton类
- 16. ThreadLocal Singleton
- 17. Singleton database.php
- 18. SqlConnection Singleton
- 19. Singleton Destructors
- 20. Singleton Properties
- 21. MVP:Presenter Singleton
- 22. 基于Jon Skeet的Singleton的Simpleton Singleton
- 23. @Inheritance TABLE_PER_CLASS标准奇怪的行为
- 24. 使用SuperClass/Inheritance /构造函数问题
- 25. asp.net webforms:BaseClass inheritance/ContentPage控件不可访问
- 26. Static Constructor&Singleton class
- 27. NDB Singleton模式
- 28. Singleton DAL类
- 29. tomcat resteasy singleton
- 30. Singleton与ServiceLocator