請讓我知道什麼是最好的方式來實現Singleton設計模式在C#中的性能約束?在性能約束的C#中實現Singleton設計模式的最佳方式是什麼?
6
A
回答
3
其中一個關於Signleton模式的best article作者:jon skeet。
+0
引用文章的較新URL:http://csharpindepth.com/Articles/General/Singleton.aspx – 2013-03-29 18:12:30
4
2
public class Singleton
{
static readonly Singleton _instance = new Singleton();
static Singleton() { }
private Singleton() { }
static public Singleton Instance
{
get { return _instance; }
}
}
1
直到最近,我才知道,辛格爾頓被許多人認爲是一個反模式,應該避免。更清潔的解決方案可能是使用DI或其他功能。即使你單身去剛讀了這個有趣的討論,從C# in Depth轉述 What is so bad about singletons?
2
: 有各種不同的方式實現在C#Singleton模式,從 不是線程安全的,完全懶洋洋地加載,thread-安全,簡單和高性能的版本。
最好的版本 - 使用.NET 4的懶惰類型:
public sealed class Singleton
{
private static readonly Lazy<Singleton> lazy =
new Lazy<Singleton>(() => new Singleton());
public static Singleton Instance { get { return lazy.Value; } }
private Singleton()
{
}
}
它的簡單和表現良好。它還允許您檢查實例是否已使用IsValueCreated屬性創建,如果需要的話。
相關問題
- 1. 什麼是實現Viewholder設計模式的最佳方式。
- 2. 實現Unity Singleton的最佳方式是什麼?
- 3. 在Delphi中實現C#的BackgroundWorker的最佳方式是什麼?
- 4. 在C#中實現Retry Wrapper的最佳方式是什麼?
- 5. 「TryParse/Parse like」模式:實現它的最佳方式是什麼
- 6. 什麼是使用Selenium PageObject設計模式的最佳方式
- 7. 在C#對象中實現動態屬性的最佳方式是什麼?
- 8. 實現「計時器」的最佳方式是什麼?
- 9. 實現計算列的最佳方式是什麼?
- 10. 在PHP中創建Singleton Webservice的最佳方式是什麼?
- 11. 模仿c#中findstr功能的最佳方式是什麼?
- 12. 實現App Transport安全性的最佳方式是什麼?
- 13. 在Actionscript 3中實現單例模式類的最佳方式是什麼?
- 14. 在模式DAL中實現搜索的最佳方式是什麼?
- 15. 實現忘記密碼功能的最佳方式是什麼?
- 16. C#SQL:實現動態表的最佳方式是什麼?
- 17. 什麼是創建CoreData模型設計的最佳方式?
- 18. 實現Singleton設計模式[請建議]
- 19. 在GridView中實現按鈕的最佳方式是什麼?
- 20. 在PHP中實現控制器的最佳方式是什麼?
- 21. 在Python中實現「from。import *」的最佳方式是什麼?
- 22. 在服務器中實現AutoComplete的最佳方式是什麼?
- 23. 什麼是在asp.net中實現調度的最佳方式MVC
- 24. 在java中實現映射器的最佳方式是什麼?
- 25. 在Android中實現搜索的最佳方式是什麼?
- 26. 在matlab中實現樹的最佳方式是什麼?
- 27. 在PHP中實現longpolling的最佳方式是什麼?
- 28. 在Python中實現Web服務的最佳方式是什麼?
- 29. 什麼是在Go中實現信號量的最佳方式
- 30. 在CakePHP 1.3中實現REST的最佳方式是什麼?
你說的「性能約束」是什麼? – Oded 2010-05-25 06:07:50
我想要最快的實施。我聽說過這個網站上的一篇文章,但不記得名字。如果有人知道,請在此發佈。 – 2010-05-25 06:12:26