辛格尔顿与参数 [英] Singleton with parameters

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

问题描述

我需要一个单独的类与一些参数进行实例化。我现在做的方式是:

I need a singleton class to be instantiated with some arguments. The way I'm doing it now is:

class SingletonExample
{
     private SingletonExample mInstance;
     //other members... 
     private SingletonExample()
     {

     } 
     public SingletonExample Instance
     {
         get
         {
              if (mInstance == null)
              {
                  throw new Exception("Object not created");
              }
              return mInstance;
         }
     }

     public void Create(string arg1, string arg2)
     {
         mInstance = new SingletonExample();
         mInstance.Arg1 = arg1;
         mInstance.ObjectCaller = new ObjectCaller(arg2);
         //etc... basically, create object...
     } 
}

实例被创建晚,这意味着我没有所有的需要的参数上的应用程序启动。

The instance is created 'late', meaning I don't have all of the needed arguments on app startup.

在一般我不喜欢强迫方法调用的顺序,但我没有看到这里的另一种方式。 IOC将于不能解决,要么,因为在那里我可以在容器中进行注册,我也可以调用Create()...

In general I don't like forcing an ordering of method calls, but I don't see another way here. The IoC wouldn't resolve it either, since where I can register it in the container, I can also call Create()...

你认为这是一个好方案?你有一些其他的想法?

Do you consider this an OK scenario? Do you have some other idea?

修改:我的知道我写的,因为它不是线程安全的一个例子,线程安全的是的不是问题的一部分。

edit: I know that what I wrote as an example it's not thread safe, thread-safe isn't part of the question

推荐答案

带有参数的辛格尔顿腥的气味给我。

A Singleton with parameters smells fishy to me.

考虑whateva的答案,以下code:

Consider whateva's answer and the following code:

Singleton x = Singleton.getInstance("hello", "world");
Singleton y = Singleton.getInstance("foo", "bar");

显然,X == y和y的作品与X的创建参数,而在Y的创建参数被忽略。结果可能是......混乱最少。

Obviously, x==y and y works with x's creation parameters, while y's creation parameters are simply ignored. Results are probably... confusing at least.

如果你真的喜欢下跌,你必须这样做,这样做是这样的:

If you really, really fell like you have to do it, do it like this:

class SingletonExample
{
     private static SingletonExample mInstance;
     //other members... 
     private SingletonExample()
     {  // never used
        throw new Exception("WTF, who called this constructor?!?");
     }
     private SingletonExample(string arg1, string arg2)
     {
         mInstance.Arg1 = arg1;
         mInstance.ObjectCaller = new ObjectCaller(arg2);
         //etc... basically, create object...    
     } 
     public static SingletonExample Instance
     {
         get
         {
              if (mInstance == null)
              {
                  throw new Exception("Object not created");
              }
              return mInstance;
         }
     }

     public static void Create(string arg1, string arg2)
     {
         if (mInstance != null)
         {
             throw new Exception("Object already created");
         }
         mInstance = new SingletonExample(arg1, arg2);             
     } 
}

在多线程环境中,添加同步,以避免竞争条件。

In a multithreading environment, add synchronisation to avoid race conditions.

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

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