相关疑难解决方法(0)

单身人士有什么不好的?

单例模式是一个缴足成员四人帮模式书,但最近似乎而是由开发者世界孤立.我仍然使用相当多的单例,特别是对于工厂类,虽然你必须对多线程问题(实际上是任何类)有点小心,但我不明白为什么它们如此可怕.

Stack Overflow特别假设每个人都同意Singletons是邪恶的.为什么?

请用" 事实,参考或特定专业知识 " 支持您的答案

singleton design-patterns

1931
推荐指数
30
解决办法
48万
查看次数

在 C# 中使用 Singleton 类的正确方法是什么

我正在学习 C# 中的单例设计模式,我用两种方式编写了下面的代码,我想知道哪一种是创建单例类的正确方法:

public sealed class TranslationHelper
{
    // first way
    private static readonly TranslationHelper translationHelper = new TranslationHelper();
    // second way
    public static readonly TranslationHelper translationHelpers = new TranslationHelper(); // Direct call

    public static TranslationHelper GetTranslationHelper()
    {
        return translationHelper;
    }

    private TranslationHelper()
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

称呼:

TranslationHelper instance = TranslationHelper.GetTranslationHelper();
TranslationHelper ins = TranslationHelper.translationHelpers;
Run Code Online (Sandbox Code Playgroud)

我是初学者,所以我不确定这两种方法是否相同。请指导我。

.net c# design-patterns

-1
推荐指数
1
解决办法
2943
查看次数

标签 统计

design-patterns ×2

.net ×1

c# ×1

singleton ×1