带参数的单例 [英] Singleton with parameters

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

问题描述

我需要用一些参数实例化一个单例类.我现在的做法是:

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 的回答和以下代码:

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天全站免登陆