【问题标题】:Generic Abstract Singleton with Custom Constructor in C#C# 中带有自定义构造函数的通用抽象单例
【发布时间】:2010-04-26 11:41:40
【问题描述】:

我想编写一个带有外部构造函数的通用单例。换句话说,可以修改构造函数。我脑子里有两个设计,但我不知道它们是否实用。

  • 第一个是强制派生类的构造函数是非公开的,不知道有没有办法?

  • 第二种是使用委托,在构造函数内部调用?

不一定是构造函数。我选择自定义构造函数的原因是做一些自定义初始化。

任何建议将不胜感激:)

【问题讨论】:

  • 您的意思是,可以修改构造函数以针对不同类型工作?
  • 我的建议是尽量远离单身人士。当您开始测试代码时,它们往往会成为主要的皮塔饼。在使用 Singletons 时,请查看这里的一些陷阱。 misko.hevery.com/2008/08/17/singletons-are-pathological-liars
  • @this。 __curious_geek 是的 :)
  • “带有自定义构造函数的通用抽象单例” - 我认为这句话至少有 3 处错误。

标签: c# .net design-patterns singleton


【解决方案1】:

听起来不太好。我想知道通过 IoC 进行配置是否会更简单、更容易支持。大多数 IoC 容器将支持单例样式的对象重用,以及高度可配置的初始化。没有让你的对象变得丑陋的副作用。

Singleton 很好,但被过度使用了。当然,我和下一个极客一样有罪……

【讨论】:

  • 我对 IoC 不是很熟悉。能举个例子吗?
  • @Heka - 在此处查看“记录器”示例:pnpguidance.net/News/…
  • 单身人士就像快餐;快速方便,但从长远来看绝对不健康。
【解决方案2】:

这是一种方法,如果我能帮你正确的话。

public abstract class MySingletonBase<T>
    where T : class
{
    protected MySingletonBase()
    {
    }

    // All other functions that will be inherited.
}

public class MySingleton<T> : MySingletonBase<T>
    where T : class
{
    private static MySingleton<T> instance;
    protected MySingleton()
    {
    }

    public static MySingleton<T> GetInstance()
    {
        if (instance == null)
        {
            instance = new MySingleton<T>();
        }
        return instance;
    }
}

【讨论】:

  • 是的,这是一个很好的解决方案,但我更喜欢使用更简单的东西。我想我找到了一些东西:)让我发布代码。
【解决方案3】:

这是我的观点,使用 .NET 4

public class Singleton<T> where T : class, new()
    {
        Singleton (){}

        private static readonly Lazy<T> instance = new Lazy<T>(()=> new T());

        public static T Instance { get { return instance.Value; } } 
    }

【讨论】:

    【解决方案4】:

    您可以尝试使用工厂模式来创建一个单例工厂,它将产生具体的单例。

    【讨论】:

      【解决方案5】:

      好的,这是我的解决方案。我使用反射来检查公共构造函数。如果我遗漏了什么,请发表评论。

            public abstract class Singleton<T> where T: class
            {
      
              private static volatile T _instance;
      
              private static object _lock = new object();
      
              protected Singleton()
              {
                ConstructorInfo[] constructorPublic = typeof(T).GetConstructors(BindingFlags.Public | BindingFlags.Instance);
                if (constructorPublic.Length > 0)
                {
                  throw new Exception(String.Format("{0} has one or more public constructors so the property cannot be enforced.",
                                                    typeof(T).FullName));
                }
              }
      
              public static T Instance
              {
                get
                {
                  if (_instance == null)
                  {
                    lock (_lock)
                    {
                      if (_instance == null)
                      {
                        ConstructorInfo constructorNonPublic = null;
      
                        try
                        {
                          constructorNonPublic = typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null,
                                                                  new Type[0], null);
                        }
                        catch (Exception e)
                        {
                          throw e;
                        }
      
                        if (constructorNonPublic == null || constructorNonPublic.IsAssembly)
                        {
                          throw new Exception(String.Format("A private or protected constructor is missing for {0}",
                                                            typeof (T).Name));
                        }
      
                        _instance = constructorNonPublic.Invoke(null) as T;
                      }
                    }
                  }
      
                  return _instance;
                }
              }
            }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-10
        • 2011-08-01
        • 2023-03-04
        相关资源
        最近更新 更多