如何從單例類繼承到需要相同功能的其他類? 這樣會有什麼意義嗎?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