一个明显的单实现.NET? [英] An obvious singleton implementation for .NET?

查看:172
本文介绍了一个明显的单实现.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想偷懒单初始化的经典问题 - 效率低下的整个问题:

I was thinking about the classic issue of lazy singleton initialization - the whole matter of the inefficiency of:

if (instance == null)
{
    instance = new Foo();
}
return instance;

任何人谁知道什么是辛格尔顿是很熟悉的问题(你只需要一次的话)。这是微不足道的,但刺激性。

Anyone who knows what a Singleton is is familiar with the issue(you only need the if once). It's trivial but irritating.

于是,我想到了一个替代的解决方案,至少对.NET(尽管它应该在任何地方工作 有一些等价的函数指针:

So, I thought of an alternate solution, at least for .NET(although it should work anywhere that has some equivalent to function pointers:

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;
    }

}

在短期 - 实例返回委托方法。委托初始设置为一个方法 初始化您的实例,然后更改委托指向一个简单的回归方法。

In short - the Instance returns a delegate method. The delegate is initially set to a method that initializes your instance, then changes the delegate to point at a simple Return method.

现在,我这样想,我并不可怕,在我的工作,但我有关于是真棒没有pretensions。我还没有看到这个code的任意位置的一个例子。

Now, I like to think I'm not terrible at my job, but I have no pretensions about being awesome. I have not seen an example of this code anywhere.

人机工程学,我来,我失去了一些东西的结论。主要的东西。无论是整个问题太琐碎麻烦以为很了解或本做了一些可怕的是会破坏宇宙。或者,我不行了,搜索,因此没见过 数以百计的使用这种方法的开发。东西,反正。

Ergo, I come to the conclusion that I am missing something. Something major. Either that the whole problem is too trivial to bother thinking that much about or this does something horrible that will destroy the universe. Or I fail at searching and therefore haven't seen the hundreds of developers using this method. Something, anyway.

我在这里希望的好乡亲在堆栈溢出还可以给我,什么(姑且不论是否应该使用单在各方的争论)。

I was hoping the good folks here at Stack Overflow could clue me in as to what(leaving aside the controversy on whether one should use a Singleton at all).

编辑澄清:

这是不是性能code(虽然如果设计积极降低超越传统模式的性能,这将是有趣的就知道了)。

This is not performance code(although if the design actively degrades performance beyond the traditional model, that would be interesting to know).

这是纯粹的写作证明的概念,而且我还知道它是不是线程安全的,因为它正确的应该是。是否有任何理由为什么它不能被它的根本性质,是线程安全的?

It was written purely as proof-of-concept, and I am further aware that it is not thread-safe as it properly should be. Is there any reason why it could NOT be made thread-safe by it's very nature?

推荐答案

这是规范的,螺纹在C#中安全,懒Singleton模式:

This is the canonical, thread safe, lazy Singleton pattern in C#:

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();
    }
}

这篇关于一个明显的单实现.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆