【问题标题】:C# Singleton design pattern basicsC# Singleton 设计模式基础知识
【发布时间】:2019-05-09 02:58:28
【问题描述】:

最近刚开始尝试学习一些设计模式。目前正试图让我的单身人士返回一个新对象。但是它一直抛出错误“无法将方法组'getInstance'转换为非委托类型'MainWdinow.CustomerLoader'。您是否打算调用该方法?

这里是设计模式方法的代码

  public class CustomerLoader
    {
        private static Customer firstInstance = null;
        public static Customer getInstance()
        {
            if(firstInstance ==null)
            {
                firstInstance = new Customer();
            }

            return firstInstance;
        }
    }

这里是我尝试调用该方法的地方,我得到了上面提到的错误

CustomerLoader t = CustomerLoader.getInstance();

我希望我的单身人士完成以下代码的工作并创建客户对象的新实例

Customer T = new Customer;

【问题讨论】:

  • 乍一看,我认为您应该这样称呼它Train t = CustomerLoader.getInstance(); 您还想创建火车或客户吗?那里有继承吗?也许你想要private static Customer firstIntance = null; 然后你可以做Customer t = CustomerLoader.getInstance();
  • @BART 感谢您的修复和帮助新手! :)
  • 另请查看 Lazy<T>... 它是为您在那儿所做的事情而设计的。
  • @BART 是的,是的,整个过程都是客户,而不是火车,还没有喝咖啡,对任何混淆感到抱歉,进行了编辑并更新了代码!再次感谢

标签: c# design-patterns singleton


【解决方案1】:

使用这个例子:

public class Singleton    
{
    private static Singleton instance = null;
    private static readonly object padlock = new object();

Singleton()
{
}

public static Singleton Instance
{
    get
    {
        lock (padlock)
        {
            if (instance == null)
            {
                instance = new Singleton();
            }
            return instance;
        }
    }
}
}
  1. Singleton 应该是一类,而不是三类。
  2. 小心它,因为您的实现不是线程安全的。在此处查看此挂锁变量。它可以防止在多线程应用程序中创建多个实例。

【讨论】:

    【解决方案2】:

    使用这个。与您的版本不同,它也是线程安全的

    private static readonly Lazy<Customer> _instance = new Lazy<Customer>(() => new Customer());
    public static Customer Instance => _instance.Value;
    

    但是你真的应该使用依赖注入而不是单例。

    而且你的命名是关闭的,它看起来像 Java,检查这个https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-type-members

    私人成员不在指南的涵盖范围内。但即使是微软在 corefx https://github.com/dotnet/corefx/blob/master/Documentation/coding-guidelines/coding-style.md 中使用 _camelCase 的私有字段@

    【讨论】:

    • 这就是我做单身课程的方式,如果有人有更好的方法,我将不胜感激。我实际上是用public static Session Instance { get { return lazy.Value; } } 来返回对象,我想=&gt; 也这样做?
    • 顺便说一句,线程安全是的,但前提是您使用指定您想要的其他构造函数之一。
    • @sellotape 这与线程安全的属性有什么关系?
    • @Matt 是的 =&gt; lazy.Value;{ get { return lazy.Value; } } docs.microsoft.com/en-us/dotnet/csharp/programming-guide/… 相同
    • @mjwills 对 OP 的原始代码是真的,如果有异常它会重试。
    【解决方案3】:
    CustomerLoader.getInstance
    

    是一个函数,你不能把它分配给 客户加载器

    您的代码应该看起来更像这样:

    class Train
    {
        protected Train() { }
    
        protected static Train instance;
    
        public static Train GetSingeltonInstance()
        {
            if(instance == null)
            {
                instance = new Train();
            }
    
            return instance;
        }
    }
    
    class TainUser
    {
        private readonly Train train;
        public TainUser()
        {
            train = Train.GetSingeltonInstance();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 2014-04-16
      • 2018-05-25
      • 2014-11-28
      • 2014-02-14
      • 2023-04-10
      相关资源
      最近更新 更多