C#与辛格尔顿的构造函数接受参数 [英] C# Singleton with constructor that accepts parameters

查看:126
本文介绍了C#与辛格尔顿的构造函数接受参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个接受在其构造另一个对象的引用静态类或单例类。静态类都出来了,但我想我可以创建一个接受在其构造函数的参数是独生子。到目前为止,我还没有任何运气搞清楚或google搜索语法。这可能吗?如果是这样,我怎么办呢?

I want to create a static class or singleton class that accepts a reference to another object in its constructor. Static classes are out, but I figured I could create a singleton that accepts parameters in its constructor. So far I haven't had any luck figuring out or googling the syntax. Is this possible? if so, how do I do it?

对不起,在战后初期没有例子,我在赶时间写的。我有我的答案是已经在答复的感觉,但这里的一些澄清什么,我想做的事:

Sorry for no example in the initial post, I wrote it in a rush. I have the feeling that my answer is already in the replies, but here's some clarification of what I want to do:

我想创建一个特定类型的单个实例(所述单件),但类型的单一实例需要保持一个不同的对象的引用。

I want to create a single instance of a specific type (said Singleton), but that single instance of the type needs to hold a reference to a different object.

例如,我可能希望创建一个Singleton状态类,它拥有一个StringBuilder对象,并且可以调用写说StringBuilder的一个draw()方法屏幕。在Draw()方法需要了解我的GraphcisDevice才能得出。
所以我想这样做的:

For example, I might want to create a Singleton "Status" class, which owns a StringBuilder object and a Draw() method that can be called to write said StringBuilder to the screen. The Draw() method needs to know about my GraphcisDevice in order to draw. So what I want to do it this:

public class Status
{
private static Status _instance;
private StringBuilder _messages;
private GraphicsDevice _gDevice;

private Status(string message, GraphicsDevice device)
{
    _messages.Append(message);
    _gDevice = device;
}

// The following isn't thread-safe

// This constructor part is what I'm trying to figure out
public static Status Instance // (GraphicsDevice device) 
    {
    get
        {
        if (_instance == null)
            {
            _instance = new Status("Test Message!", device); 
            }
        return _instance;
        }
    }

public void UpdateMessage
...

public void Draw()
    {
    // Draw my status to the screen, using _gDevice and _messages
    }
}

各地的代码,我找回我的状态辛格尔顿并调用其UpdateMessage()方法。

All over the code, I retrieve my Status Singleton and call its UpdateMessage() method.

private Status _status = Status.Instance; // + pass reference to GraphicsDevice
_status.UpdateMessage("Foo!");



然后,在我的主类我也找回了单,并把它:

Then, in my main class I also retrieve the singleton, and draw it:

_status.Draw();



是的,这意味着,无论我检索单身,我需要通过传递引用这样做到GraphicsDevice的,如果这是我第一次实例化辛格尔顿。我可以/会用不同的方法来检索在我的Singleton类中的GraphicsDevice为根本的东西,比如在其他地方注册服务,并得到在状态类的服务。这个例子搞得非常做作 - 我试图找出如果的的东西的这样的模式可能是摆在首位。

Yes, this means that wherever I retrieve the singleton, I need to do so by passing in the reference to the GraphicsDevice, in case it's the first time I instantiate the Singleton. And I could/would use different means to retrieve something as fundamental as the GraphicsDevice in my Singleton class, for example register a service elsewhere and get that service in the Status class. This example got pretty contrived - I'm trying to figure out if something like this pattern is possible in the first place.

推荐答案

这通常被认为是一个糟糕的主意,因为如果你要接受,要么你打算在一个单身般的包装包裹,你不能保证你持有的唯一实例对象引用或类型参数该类型的AppDomain中。

This is generally considered a bad idea because if you are going to accept either an object reference or a type argument that you plan on wrapping in a singleton-like wrapper you cannot guarantee that you hold the only instance of that type in the AppDomain.

Singleton模式的整点是控制型的单个实例,以便只有一个该类型的实例可以存在。如果允许一个实例中传递,或者如果你犯了一个通用的单供应商不能保证您的实例是的只有的实例。

The whole point of the singleton pattern is to control a single instance of a type so that only one instance of that type can exist. If you allow an instance to be passed in or if you make a generic singleton provider you cannot guarantee that your instance is the only instance.

比方说,我有一个 SingletonFactory< T> ,让我来创建一个围绕任何类型的我通过工厂单身。这将是相当得心应手,可以让我做这样的事情:

Let's say that I had a SingletonFactory<T> that would allow me to create a singleton around any type that I pass to the factory. That would be quite handy and would allow me to do something like this:

SingletonFactory<Foo>.Instance;



但什么阻止我从也这样做:

But what stops me from also doing this:

Foo foo = new Foo();



哎呀,它看起来像 ISN T A单了,因为我希望我能创建它的多个实例。为了使单件模式来工作,你需要能够完全控制的情况下,您需要限制的类型。这就是为什么你不应该使用像什么我 SingletonFactory< T>

Oops, it looks like Foo isn't a singleton anymore as I can create as many instances of it as I wish. In order for the singleton pattern to work you need to be able to completely control the type whose instances you need to restrict. This is why you ought not to use anything like my SingletonFactory<T>.

注意:这同样适用于非通用单接受一个对象实例。我相信,你可以从我前面的例子很多相似的原因推断为什么接受和一个单包装对象引用也将是一个糟糕的主意。

这篇关于C#与辛格尔顿的构造函数接受参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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