问题描述
单身人士是一种备受争议的设计模式,所以我对堆栈溢出社区对它们的想法感兴趣.
请提供您意见的理由,而不仅仅是"单身人士适合懒惰的程序员!"
这是关于这个问题的一篇相当不错的文章,尽管它反对使用单人: scientificninja.com:scientificninja.com:calterant-singletons .
有人还有其他好文章吗?也许支持单例?
推荐答案
在捍卫单例:
- 它们不如Globals ,因为Globals没有标准强制的初始化顺序,并且由于天真或意外的依赖性顺序,您可以轻松地看到非确定的错误.单例(假设它们分配在堆上)是在所有Globals之后创建的,并且在代码中的一个非常可预测的位置.
- 它们对于资源液化/流程系统非常有用,例如慢速I/O设备的接口.如果您智能地将单身界界面构建到慢速设备,而没有人称呼它,那么您不会浪费任何时间.如果另一个代码从多个位置调用,则您的单例可以同时优化同时的缓存,并避免任何双重查找.您也可以轻松地避免在Singleton控制的资源上进行任何僵局.
反对单例:
- 在C ++中,没有一个很好的方法来自动清洁.有工作障碍,还有一些骇人听闻的方法可以做到这一点,但是没有简单,普遍的方法可以使确保您的单身人士的破坏者总是被称为.这并不是那么可怕的记忆 - 为此,只将其视为更全球的变量.但是,如果您的Singleton分配其他资源(例如锁定某些文件)并且不会发布它们,那可能会很糟糕.
我自己的看法:
我使用单例,但是如果有合理的选择,请避免使用它们.到目前为止,这对我来说效果很好,我发现它们是可以测试的,尽管要测试的工作稍大.
其他推荐答案
Google有一个 singleton detector 我相信Java首先是必须在Google上生成的所有代码上运行的工具.简而言之的理由去除单例:
因为他们可以进行测试 困难并隐藏您的问题 设计
有关更明确的说明,请参见'为什么单身人士有争议'来自Google.
其他推荐答案
单身人士只是一件花式礼服的全球变量.
全局变量和单身人士一样,但是如果您认为自己在做单程的事情而不是使用Youcky Global变量(每个人都知道Globals都是不好的MMKAY),那么您很遗憾,您很遗憾.
问题描述
Singletons are a hotly debated design pattern, so I am interested in what the Stack Overflow community thought about them.
Please provide reasons for your opinions, not just "Singletons are for lazy programmers!"
Here is a fairly good article on the issue, although it is against the use of Singletons: scientificninja.com: performant-singletons.
Does anyone have any other good articles on them? Maybe in support of Singletons?
推荐答案
In defense of singletons:
- They are not as bad as globals because globals have no standard-enforced initialization order, and you could easily see nondeterministic bugs due to naive or unexpected dependency orders. Singletons (assuming they're allocated on the heap) are created after all globals, and in a very predictable place in the code.
- They're very useful for resource-lazy / -caching systems such as an interface to a slow I/O device. If you intelligently build a singleton interface to a slow device, and no one ever calls it, you won't waste any time. If another piece of code calls it from multiple places, your singleton can optimize caching for both simultaneously, and avoid any double look-ups. You can also easily avoid any deadlock condition on the singleton-controlled resource.
Against singletons:
- In C++, there's no nice way to auto-clean-up after singletons. There are work-arounds, and slightly hacky ways to do it, but there's just no simple, universal way to make sure your singleton's destructor is always called. This isn't so terrible memory-wise -- just think of it as more global variables, for this purpose. But it can be bad if your singleton allocates other resources (e.g. locks some files) and doesn't release them.
My own opinion:
I use singletons, but avoid them if there's a reasonable alternative. This has worked well for me so far, and I have found them to be testable, although slightly more work to test.
其他推荐答案
Google has a Singleton Detector for Java that I believe started out as a tool that must be run on all code produced at Google. The nutshell reason to remove Singletons:
because they can make testing difficult and hide problems with your design
For a more explicit explanation see 'Why Singletons Are Controversial' from Google.
其他推荐答案
A singleton is just a bunch of global variables in a fancy dress.
Global variables have their uses, as do singletons, but if you think you're doing something cool and useful with a singleton instead of using a yucky global variable (everyone knows globals are bad mmkay), you're unfortunately misled.