C#Singleton接受参数的构造函数 [英] C# Singleton with constructor that accepts parameters

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

问题描述

我想创建一个静态类或singleton类,它在其构造函数中接受对另一个对象的引用。静态类已经出来了,但我想我可以创建一个单例,在其构造函数中接受参数。到目前为止,我没有任何运气找出或googling的语法。这可能吗?如果是,我该怎么办呢?

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:

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

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.

例如,我可能想创建一个SingletonStatus类,它拥有一个StringBuilder对象和一个Draw()方法,可以调用该方法将StringBuilder屏幕。 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
    }
}  

所有代码,我检索我的状态Singleton并调用其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!");



< b
$ b

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

_status.Draw();

是的,这意味着无论我在何处检索单例,我需要这样做,到GraphicsDevice,以防它是我第一次实例化Singleton。我可以/将使用不同的手段来检索像我的Singleton类中的GraphicsDevice一样基本的东西,例如在其他地方注册一个服务,并在Status类中获取该服务。这个例子很漂亮 - 我想知道如果像这样的模式是可能的第一个地方。

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.

推荐答案

这通常被认为是一个坏主意,因为如果你要接受一个对象引用或一个类型参数,你计划在一个单例包装器包装,你不能保证你持有唯一的实例

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.

单例模式的整体点是控制一个类型的单个实例,使得只有该类型的一个实例可以存在。如果您允许传入实例,或者如果您制作通用单例提供程序,则无法保证您的实例是实例。

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

糟糕,看起来像 Foo isn'一个单身人士,我可以创建尽可能多的实例,因为我希望。为了使单例模式工作,您需要能够完全控制您需要限制的实例的类型。这就是为什么你不应该使用任何像 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>.

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

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

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