简体   繁体   English

这是单例模式吗

[英]Is this a Singleton Pattern

It looks like this overloaded constructor has itself as an interface, to create a Singleton Pattern - is that what is happening? 看起来这个重载的构造函数本身具有作为创建Singleton Pattern的接口-这是怎么回事?

Is this a common constructor idiom in C# ? 这是C#常见的构造方法吗?

class clDBaccess
{

  // private field
  private readonly string conn;


  public clDBaccess()
    : this(ConfigurationManager.ConnectionStrings["foo"].ConnectionString)
  {
  }
  public clDBaccess(string connectionString)
  {
    this.conn = connectionString;
  }
  ...
  ...

Singleton pattern has the constructor being called once ever . Singleton模式的构造函数曾经被调用过一次 This is just method overloading to provide good defaults to a parameterless constructor. 这只是方法重载,可为无参数构造函数提供良好的默认值。

You can call the constructor and create as many instances as you'd like, so it's not a singleton. 您可以调用构造函数并创建任意数量的实例,因此它不是单例。

No. The default constructor here is simply a convenience for using the connection string foo from app.config . 否。这里的默认构造函数只是使用app.config的连接字符串foo一种便捷方式。 There's nothing preventing multiple instances of this class from being created, but if you create them using the default constructor they'll connect to the same database (but won't necessarily be sharing the same connection). 没有什么可以阻止创建此类的多个实例,但是如果使用默认构造函数创建它们,它们将连接到同一数据库(但不一定共享同一连接)。

One reason to do this is that the conn property is readonly - it must be initialized by the constructor (and cannot be changed after the constructor is done), and so the default constructor attempts to initialize it from a meanigful setting rather than setting it to null or string.Empty . 这样做的一个原因是conn属性是readonly -它必须由构造函数初始化(并且在构造函数完成后不能更改),因此默认构造函数会尝试通过有意义的设置来初始化它,而不是将其设置为null或string.Empty

No, and any proper singleton should have a protected constructor. 不,任何适当的单例都应具有受保护的构造函数。

using System;

public class Singleton
{
   private static Singleton instance;

   private Singleton() {}

   public static Singleton Instance
   {
      get 
      {
         if (instance == null)
         {
            instance = new Singleton();
         }
         return instance;
      }
   }
}

"Singleton" means that there can only ever be one instance of a class. “单身”是指一个类只能有一个实例。 Typically you will find a public static property that returns the single instance of that class. 通常,您会发现一个public static属性,该属性返回该类的单个实例。

To ensure that the class can not be instantiated more than once, the constructor(s) will typically be private. 为了确保不能多次实例化该类,构造函数通常是私有的。

This is not the case here. 这里不是这种情况。 It's a normal class with two constructors, where the parameterless constructor "redirects" to the preferred constructor with a useful default value. 这是具有两个构造函数的普通类,其中无参数构造函数将“重定向”到具有有用默认值的首选构造函数。

No, it does not fit the singleton pattern. 不,它不适合单例模式。 The constructors would need to be private so that object instantiation is controlled by the class itself. 构造函数必须是私有的,以便对象实例化由类本身控制。

For example: 例如:

public class Singleton  
{
    private Singleton()
    {
    }
    private static Singleton instance = new Singleton();
    public static Singleton Instance
    {
        get
        {
            return instance;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM