Faq*_*aqa 19 .net c# singleton
我在考虑懒惰单例初始化的经典问题 - 完全无效的问题:
if (instance == null)
{
instance = new Foo();
}
return instance;
Run Code Online (Sandbox Code Playgroud)
任何知道Singleton是什么的人都熟悉这个问题(你只需要if一次).这是微不足道的,但很刺激.
所以,我想到了一个替代解决方案,至少对于.NET来说(尽管它应该可以在任何具有等效功能指针的地方工作:
public class Foo
{
private delegate Foo FooReturner();
private static Foo innerFoo;
private static FooReturner fooReturnHandler = new FooReturner(InitialFooReturner);
public static Foo Instance
{
get
{
return fooReturnHandler();
}
}
private static Foo InitialFooReturner()
{
innerFoo = new Foo();
fooReturnHandler = new FooReturner(NewFooReturner);
return innerFoo;
}
private static Foo NewFooReturner()
{
return innerFoo;
}
}
Run Code Online (Sandbox Code Playgroud)
简而言之 - Instance返回一个委托方法.委托最初设置为初始化实例的方法,然后将委托更改为指向简单的Return方法.
现在,我喜欢认为我的工作并不可怕,但我并不担心自己很棒.我没有在任何地方看到这个代码的例子.
因此,我得出结论,我错过了一些东西.重要的东西.要么整个问题太过微不足道,要么不必考虑太多,或者这会造成毁灭宇宙的可怕事情.或者我在搜索时失败,因此没有看到数百名开发人员使用这种方法.无论如何,还有什么.
我希望Stack Overflow这里的优秀人员可以让我知道什么(除了关于是否应该使用Singleton的争议).
编辑澄清:
这不是性能代码(尽管如果设计主动降低性能超出传统模型,那么知道这将是有趣的).
它纯粹是作为概念证明编写的,我进一步意识到它不是正确的线程安全.有什么理由不能通过它的性质使它成为线程安全的吗?
Ran*_*pho 31
public sealed class Singleton
{
Singleton(){}
public static Singleton Instance
{
get
{
return Nested.instance;
}
}
class Nested
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Nested() {}
internal static readonly Singleton instance = new Singleton();
}
}
Run Code Online (Sandbox Code Playgroud)
oɔɯ*_*ɯǝɹ 10
为了防止必须复制单例代码,您可以使类型通用,如下所示:
public abstract class Singleton<T>
where T: class, new()
{
public static T Instance
{
get { return Nested.instance; }
}
private class Nested
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Nested() { }
internal static readonly T instance = new T();
}
}
public sealed class MyType : Singleton<MyType>
{
}
class Program
{
static void Main()
{
// two usage pattterns are possible:
Console.WriteLine(
ReferenceEquals(
Singleton<MyType>.Instance,
MyType.Instance
)
);
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3424 次 |
最近记录: |