Jav*_*aSa 2 c# singleton design-patterns
考虑一下你有以下代码:
1.为什么我们使用双重检查锁,为什么单锁不够好,请提供详细的例子.
2.这种实施的主要缺点是什么?我该如何证明呢?
谢谢.
public sealed class SomeSingleton5
{
private static SomeSingleton5 s_Instance = null;
private static object s_LockObj = new Object();
private SomeSingleton5() { }
public static SomeSingleton5 Instance
{
get
{
if (s_Instance == null)
{
lock (s_LockObj)
{
if (s_Instance == null)
{
s_Instance = new SomeSingleton5();
}
}
}
return s_Instance;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我认为单例类的最佳实现是由提供的Jon Skeet.
public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();
public static Singleton Instance { get { return instance; } }
static Singleton() {}
private Singleton() {}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1692 次 |
| 最近记录: |