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

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

问题描述

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



所以我已经读过线程单身人士:好的设计或拐杖?

而且争论还是愤怒。



我将Singletons视为设计模式(好与坏)。


Singleton的问题不是模式,而是用户(对不起大家)。每个人和他们的父亲认为他们可以正确地实施一个(从我所做的许多访谈中,大多数人都不能)。同样因为每个人都认为他们可以执行一个正确的Singleton,他们滥用模式并在不合适的情况下使用它(用Singletons代替全局变量!)。



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




  • 您应该什么时候使用Singleton

  • 实现一个Singleton正确



我希望这篇文章是我们可以在一起收集在一起(而不是谷歌和搜索多个站点)一个权威的来源,何时(然后如何)正确使用Singleton。还可以列出反用法和常见的不良实施,解释为什么它们无法正常工作,以及实现其弱点。




所以让球滚动:

我会举起手来说这是我使用的,但可能有问题。

我喜欢Scott Myers处理主题他的书有效的C ++


使用单身人士(不是很多)的好情况:




  • 记录框架

  • 线程回收池




  / * 
* C ++ Singleton
*限制:单线程设计
*参见:http://www.aristeia。 com / Papers / DDJ_Jul_Aug_2004_revised.pdf
*与多线程应用程序锁定相关的问题
*
*限制:
*如果您在一个析构函数中使用此Singleton(A)另一个Singleton(B)
*这个Singleton(A)必须完全被约束在(B)
*的构造函数之前导出。
* /
class MySingleton
{
private:
//私有构造函数
MySingleton();
//停止编译生成方法复制对象
MySingleton(MySingleton const& copy); //未实现
MySingleton& operator =(MySingleton const& copy); //未实现

public:
static MySingleton& getInstance()
{
//唯一的实例
//保证被懒惰初始化
//保证它将被正确销毁
static MySingleton instance;
return instance;
}
};

确定。让一些批评和其他实现在一起。

: - )

解决方案

你们都错了。
阅读问题。
答案:



使用单身人士如果:




  • 如果你需要在系统中只有一个对象类型



如果出现以下情况,请勿使用Singleton:




  • 如果你想保存内存

  • 如果你想尝试新的东西$ /
    <如果你想炫耀你知道多少钱

  • 因为其他人都在做(参见
  • 在用户界面小部件中

  • 应该是缓存

  • 在字符串

  • 在会话中

  • 我可以一整天都可以走了



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




      • 越小越好。我是一个极简

      • 确保它是线程安全的

      • 确保它永远不为空

      • 确定它只创建一次

      • 懒惰或系统初始化?

      • 有时,操作系统或JVM为您创建单例(例如,在Java中,每个类的定义都是单例)

      • 提供析构函数或者以某种方式找出如何处理资源

      • 使用少量内存


      Edit: 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.

      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:

      • When should you use a Singleton
      • How do you implement a Singleton correctly

      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.


      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):

      • Logging frameworks
      • Thread recycling pools

      /*
       * 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.
      :-)

      解决方案

      All of you are wrong. Read the question. Answer:

      Use a Singleton if:

      • If you need to have one and only one object of a type in system

      Do not use a Singleton if:

      • If you want to save memory
      • If you want to try something new
      • If you want to show off how much you know
      • Because everyone else is doing it (See cargo cult programmer in wikipedia)
      • In user interface widgets
      • It is supposed to be a cache
      • In strings
      • In Sessions
      • I can go all day long

      How to create the best singleton:

      • The smaller, the better. I am a minimalist
      • Make sure it is thread safe
      • Make sure it is never null
      • Make sure it is created only once
      • Lazy or system initialization? Up to your requirements
      • Sometimes the OS or the JVM creates singletons for you (e.g. in Java every class definition is a singleton)
      • Provide a destructor or somehow figure out how to dispose resources
      • Use little memory

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

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