单例:应该如何使用 [英] Singleton: How should it be used

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

问题描述

从另一个问题我提供了一个答案,其中包含许多关于单身人士的问题/答案的链接:有关单身人士的更多信息,请访问:

From another question I provided an answer that has links to a lot of questions/answers about singletons: More info about singletons here:

所以我已经阅读了主题单身人士:好的设计还是拐杖?
争论仍在继续.

So I have read the thread Singletons: good design or a crutch?
And the argument still rages.

我认为单例是一种设计模式(好的和坏的).

Singleton 的问题不在于模式,而在于用户(对不起大家).每个人和他们的父亲都认为他们可以正确地实施一个(从我所做的许多采访来看,大多数人不能).也因为每个人都认为他们可以实现正确的单例,所以他们滥用模式并在不合适的情况下使用它(用单例替换全局变量!).

I see Singletons as a Design Pattern (good and bad).

The problem with Singleton is not the Pattern but rather the users (sorry everybody). Everybody and their father thinks they can implement one correctly (and from the many interviews I have done, most people can't). Also because everybody thinks they can implement a correct Singleton they abuse the Pattern and use it in situations that are not appropriate (replacing global variables with Singletons!).

所以需要回答的主要问题是:

So the main questions that need to be answered are:

  • 什么时候应该使用单例
  • 如何正确实现单例

我对这篇文章的希望是,我们可以在一个地方(而不是必须通过谷歌和搜索多个站点)收集有关何时(以及如何)正确使用 Singleton 的权威来源.同样合适的是反用法和常见错误实现的列表,解释为什么它们无法正常工作以及良好的实现它们的弱点.


My hope for this article is that we can collect together in a single place (rather than having to google and search multiple sites) an authoritative source of when (and then how) to use a Singleton correctly. Also appropriate would be a list of Anti-Usages and common bad implementations explaining why they fail to work and for good implementations their weaknesses.


所以开始吧:
我会举起手说这是我使用的,但可能有问题.
我喜欢Scott Myers"在他的书Effective C++"中对这个主题的处理

So get the ball rolling:
I will hold my hand up and say this is what I use but probably has problems.
I like "Scott Myers" handling of the subject in his books "Effective C++"

使用单例的好情况(不多):

Good Situations to use Singletons (not many):

  • 日志框架
  • 线程回收池

/*
 * C++ Singleton
 * Limitation: Single Threaded Design
 * See: http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf
 *      For problems associated with locking in multi threaded applications
 *
 * Limitation:
 * If you use this Singleton (A) within a destructor of another Singleton (B)
 * This Singleton (A) must be fully constructed before the constructor of (B)
 * is called.
 */
class MySingleton
{
    private:
        // Private Constructor
        MySingleton();
        // Stop the compiler generating methods of copy the object
        MySingleton(MySingleton const& copy);            // Not Implemented
        MySingleton& operator=(MySingleton const& copy); // Not Implemented

    public:
        static MySingleton& getInstance()
        {
            // The only instance
            // Guaranteed to be lazy initialized
            // Guaranteed that it will be destroyed correctly
            static MySingleton instance;
            return instance;
        }
};

好的.让我们一起讨论一些批评和其他实现.
:-)

OK. Lets get some criticism and other implementations together.
:-)

推荐答案

答案:

在以下情况下使用单例:

Use a Singleton if:

  • 您需要在系统中拥有一个且只有一个类型的对象

在以下情况下不要使用单例:

Do not use a Singleton if:

  • 你想节省内存
  • 你想尝试新事物
  • 你想炫耀你知道多少
  • 因为其他人都在这样做(请参阅维基百科中的cargo cult程序员)
  • 在用户界面小部件中
  • 它应该是一个缓存
  • 在字符串中
  • 在会话中
  • 我可以去一整天

如何创建最好的单身人士:

How to create the best singleton:

  • 越小越好.我是极简主义者
  • 确保它是线程安全的
  • 确保它永远不会为空
  • 确保它只创建一次
  • 延迟初始化还是系统初始化?满足您的要求
  • 有时操作系统或 JVM 会为您创建单例(例如,在 Java 中,每个类定义都是单例)
  • 提供析构函数或以某种方式弄清楚如何处理资源
  • 占用很少的内存

这篇关于单例:应该如何使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆