<p>Singletons are a hotly debated design pattern, so I am interested in what the Stack Overflow community thought about them.</p>

<p>Please provide reasons for your opinions, not just "Singletons are for lazy programmers!"</p>

<p>Here is a fairly good article on the issue, although it is against the use of Singletons:
<a href="http://web.archive.org/web/20090303174418/http://scientificninja.com/advice/performant-singletons" rel="nofollow noreferrer">scientificninja.com: performant-singletons</a>.</p>

<p>Does anyone have any other good articles on them? Maybe in support of Singletons?</p>

[{'answer_id': 11839, 'author': 'denis phillips', 'author_id': 748, 'author_profile': 'https://Stackoverflow.com/users/748', 'pm_score': 5, 'selected': False, 'text': '<p>The purpose of a Singleton is to ensure a class has only one instance, and provide a global point of access to it. Most of the time the focus is on the single instance point. Imagine if it were called a Globalton. It would sound less appealing as this emphasizes the (usually) negative connotations of a global variable.</p>\n\n<p>Most of the good arguments against singletons have to do with the difficulty they present in testing as creating test doubles for them is not easy.</p>\n'}, {'answer_id': 11877, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 1, 'selected': False, 'text': '<p>Singletons have their uses, but one must be careful in using and exposing them, because they are <a href="http://dotnet.kapenilattex.com/?p=35" rel="nofollow noreferrer">way too easy to abuse</a>, difficult to truly unit test, and it is easy to create circular dependencies based on two singletons that accesses each other.</p>\n\n<p>It is helpful however, for when you want to be sure that all your data is synchronized across multiple instances, e.g., configurations for a distributed application, for instance, may rely on singletons to make sure that all connections use the same up-to-date set of data.</p>\n'}, {'answer_id': 11895, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 5, 'selected': False, 'text': "<p>A singleton is just a bunch of global variables in a fancy dress.</p>\n\n<p>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.</p>\n"}, {'answer_id': 11905, 'author': 'Brian', 'author_id': 700, 'author_profile': 'https://Stackoverflow.com/users/700', 'pm_score': 5, 'selected': False, 'text': '<p>Google has a <a href="http://code.google.com/p/google-singleton-detector/" rel="noreferrer">Singleton Detector</a> 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:  </p>\n\n<blockquote>\n  <p>because they can make testing\n  difficult and hide problems with your\n  design</p>\n</blockquote>\n\n<p>For a more explicit explanation see \'<a href="http://code.google.com/p/google-singleton-detector/wiki/WhySingletonsAreControversial" rel="noreferrer">Why Singletons Are Controversial</a>\' from Google.</p>\n'}, {'answer_id': 11911, 'author': 'Dan Herbert', 'author_id': 392, 'author_profile': 'https://Stackoverflow.com/users/392', 'pm_score': 1, 'selected': False, 'text': "<p>I find you have to be very careful about <strong>why</strong> you're deciding to use a singleton. As others have mentioned, it's essentially the same issue as using global variables. You must be very cautious and consider what you could be doing by using one.</p>\n\n<p>It's very rare to use them and usually there is a better way to do things. I've run into situations where I've done something with a singleton and then had to sift through my code to take it out after I discovered how much worse it made things (or after I came up with a much better, more sane solution)</p>\n"}, {'answer_id': 11914, 'author': 'Dan Blair', 'author_id': 1327, 'author_profile': 'https://Stackoverflow.com/users/1327', 'pm_score': 3, 'selected': False, 'text': '<p>I\'ve been trying to think of a way to come to the poor singelton\'s rescue here, but I must admit it\'s hard.  I\'ve seen very few legitimate uses of them and with the current drive to do dependency injection andd unit testing they are just hard to use.  They definetly are the "cargo cult" manifestation of programming with design patterns I have worked with many programmers that have never cracked the "GoF" book but they know \'Singelton\' and thus they know \'Patterns\'.</p>\n\n<p>I do have to disagree with Orion though, most of the time I\'ve seen singeltons oversused it\'s not global variables in a dress, but more like global services(methods) in a dress.  It\'s interesting to note that if you try to use Singeltons in the SQL Server 2005 in safe mode through the CLR interface the system will flag the code.  The problem is that you have persistent data beyond any given transaction that may run, of course if you make the instance variable read only you can get around the issue.  </p>\n\n<p>That issue lead to a lot of rework for me one year.</p>\n'}, {'answer_id': 11934, 'author': 'Mario Marinato', 'author_id': 431, 'author_profile': 'https://Stackoverflow.com/users/431', 'pm_score': 0, 'selected': False, 'text': '<p>I really disagree on the <em>bunch of global variables in a fancy dress</em> idea.  Singletons are really useful when used to solve the right problem.  Let me give you a real example.</p>\n\n<p>I once developed a small piece of software to a place I worked, and some forms had to use some info about the company, its employees, services and prices.  At its first version, the system kept loading that data from the database every time a form was opened.  Of course, I soon realized this approach was not the best one.</p>\n\n<p>Then I created a singleton class, named <em>company</em>, which encapsulated everything about the place, and it was completely filled with data by the time the system was opened.</p>\n\n<p>It was not just a bunch of variables in a fancy dress because this was had dozens of responsibilities, like communicating with persistence layer to save/retrieve data about the company, deal with employees and prices collections, etc.</p>\n\n<p>Plus, it was a fixed, system-wide, easily accessible point to have the company data.</p>\n'}, {'answer_id': 11985, 'author': 'Dan Blair', 'author_id': 1327, 'author_profile': 'https://Stackoverflow.com/users/1327', 'pm_score': 2, 'selected': False, 'text': "<blockquote>\n  <p>It was not just a bunch of variables in a fancy dress because this was had dozens of responsibilities, like communicating with persistence layer to save/retrieve data about the company, deal with employees and prices collections, etc.</p>\n</blockquote>\n\n<p>I must say you're not really describing somthing that should be a single object and it's debatable that any of them, other than Data Serialization should have been a singelton.</p>\n\n<p>I can see at least 3 sets of classes that I would normally design in, but I tend to favor smaller simpler objects that do a narrow set of tasks very well.  I know that this is not the nature of most programmers.  (Yes I work on 5000 line class monstrosities every day, and I have a special love for the 1200 line methods some people write.)   </p>\n\n<p>I think the point is that in most cases you don't need a singelton and often your just making your life harder.</p>\n"}, {'answer_id': 24406, 'author': 'Gishu', 'author_id': 1695, 'author_profile': 'https://Stackoverflow.com/users/1695', 'pm_score': 3, 'selected': False, 'text': "<p>Holy wars! Ok let me see.. Last time I checked the design police said..</p>\n\n<p>Singletons are bad because they hinder auto testing - instances cannot be created afresh for each test case.\nInstead the logic should be in a class (A) that can be easily instantiated and tested. Another class (B) should be responsible for constraining creation. Single Responsibility Principle to the fore! It should be team-knowledge that you're supposed to go via B to access A - sort of a team convention.</p>\n\n<p>I concur mostly..</p>\n"}, {'answer_id': 29307, 'author': 'Cem Catikkas', 'author_id': 3087, 'author_profile': 'https://Stackoverflow.com/users/3087', 'pm_score': 3, 'selected': False, 'text': '<p>I think there is a great misunderstanding about the use of the Singleton pattern. Most of the comments here refer to it as a place to access global data. We need to be careful here - Singleton as a pattern is <strong>not</strong> for accessing globals. </p>\n\n<p>Singleton should be used to have <strong>only one instance</strong> of the given class. <a href="http://www.netobjectivesrepository.com/" rel="nofollow noreferrer">Pattern Repository</a> has great information on <a href="http://www.netobjectivesrepository.com/TheSingletonPattern" rel="nofollow noreferrer">Singleton</a>.</p>\n'}, {'answer_id': 30769, 'author': 'Vinnie', 'author_id': 2890, 'author_profile': 'https://Stackoverflow.com/users/2890', 'pm_score': 1, 'selected': False, 'text': '<p>I\'ve used singletons a bunch of times in conjunction with <a href="http://www.springframework.org/" rel="nofollow noreferrer">Spring</a> and didn\'t consider it a crutch or lazy.</p>\n\n<p>What this pattern allowed me to do was create a single class for a bunch of configuration-type values and then share the single (non-mutable) instance of that specific configuration instance between several users of my web application.  </p>\n\n<p>In my case, the singleton contained client configuration criteria - css file location, db connection criteria, feature sets, etc. - specific for that client.  These classes were instantiated and accessed through Spring and shared by users with the same configuration (i.e. 2 users from the same company). * **I know there\'s a name for this type of application but it\'s escaping me*</p>\n\n<p>I feel it would\'ve been wasteful to create (then garbage collect) new instances of these "constant" objects for each user of the app.</p>\n'}, {'answer_id': 64887, 'author': 'Nate Kohari', 'author_id': 1282, 'author_profile': 'https://Stackoverflow.com/users/1282', 'pm_score': 0, 'selected': False, 'text': '<p>Singletons are very useful, and using them is not in and of itself an anti-pattern. However, they\'ve gotten a bad reputation largely because they force any consuming code to acknowledge that they are a singleton in order to interact with them. That means if you ever need to "un-Singletonize" them, the impact on your codebase can be very significant.</p>\n\n<p>Instead, I\'d suggest either hiding the Singleton behind a factory. That way, if you need to alter the service\'s instantiation behavior in the future, you can just change the factory rather than all types that consume the Singleton.</p>\n\n<p>Even better, use an inversion of control container! Most of them allow you to separate instantiation behavior from the implementation of your classes.</p>\n'}, {'answer_id': 64999, 'author': 'Tyler', 'author_id': 3561, 'author_profile': 'https://Stackoverflow.com/users/3561', 'pm_score': 6, 'selected': False, 'text': "<p>In defense of singletons:</p>\n\n<ul>\n<li><strong>They are not as bad as globals</strong> 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.</li>\n<li><strong>They're very useful for resource-lazy / -caching systems</strong> 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.</li>\n</ul>\n\n<p>Against singletons:</p>\n\n<ul>\n<li><strong>In C++, there's no nice way to auto-clean-up after singletons.</strong>  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.</li>\n</ul>\n\n<p>My own opinion:</p>\n\n<p>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.</p>\n"}, {'answer_id': 65108, 'author': 'Bleadof', 'author_id': 7159, 'author_profile': 'https://Stackoverflow.com/users/7159', 'pm_score': 5, 'selected': False, 'text': '<p>There\'s three pretty good blog posts about Singletons by <a href="http://misko.hevery.com/about/" rel="noreferrer">Miško Hevery</a> in the Google Testing blog.</p>\n\n<ol>\n  <li><a href="http://googletesting.blogspot.com/2008/08/by-miko-hevery-so-you-join-new-project.html" rel="noreferrer">Singletons are Pathological Liars</a></li>\n  <li><a href="http://googletesting.blogspot.com/2008/08/where-have-all-singletons-gone.html" rel="noreferrer">Where Have All the Singletons Gone?</a></li>\n  <li><a href="http://googletesting.blogspot.com/2008/08/root-cause-of-singletons.html" rel="noreferrer">Root Cause of Singletons</a></li>\n</ol>\n'}, {'answer_id': 71189, 'author': 'Bill Michell', 'author_id': 7938, 'author_profile': 'https://Stackoverflow.com/users/7938', 'pm_score': 2, 'selected': False, 'text': '<p>The biggest problem with singletons is that they make unit testing <em>hard</em>, particularly when you want to run your tests in parallel but independently.</p>\n\n<p>The second is that people often believe that lazy initialisation with double-checked locking is a good way to implement them.</p>\n\n<p>Finally, unless your singletons are immutable, then they can easily become a performance problem when you try and scale your application up to run in multiple threads on multiple processors. Contended synchronization is expensive in most environments.</p>\n'}, {'answer_id': 87053, 'author': 'Asgeir S. Nilsen', 'author_id': 16023, 'author_profile': 'https://Stackoverflow.com/users/16023', 'pm_score': 0, 'selected': False, 'text': "<p>One scary thing on singletons in for instance Java is that you <em>can</em> end up with multiple instances of the same singleton in some cases.  The JVM uniquely identifies based on <em>two</em> elements:  A class' fully qualified name, and the classloader responsible for loading it.</p>\n\n<p>That means the same class can be loaded by two classloaders unaware of each other, and different parts of your application would have different instances of this singleton that they interact with.</p>\n"}, {'answer_id': 114400, 'author': 'andreas buykx', 'author_id': 19863, 'author_profile': 'https://Stackoverflow.com/users/19863', 'pm_score': 3, 'selected': False, 'text': "<p>One of the colleagues I have worked with was very Singleton-minded. Whenever there was something that was kind of a manager or boss like object he would make that into a singleton, because he figured that there should be only one boss. And each time the system took up some new requirements, it turned out there were perfectly valid reasons to allow multiple instances. </p>\n\n<p>I would say that singleton should be used if the domain model dictates (not 'suggests') that there is one. All other cases are just accendentally single instances of a class.</p>\n"}, {'answer_id': 562473, 'author': 'Ben Hardy', 'author_id': 59441, 'author_profile': 'https://Stackoverflow.com/users/59441', 'pm_score': 0, 'selected': False, 'text': "<p>Write normal, testable, injectable objects and let Guice/Spring/whatever handle the instantiation. Seriously. </p>\n\n<p>This applies even in the case of caches or whatever the natural use cases for singletons are. <b>There's no need to repeat the horror of writing code to try to enforce one instance.</b> Let your dependency injection framework handle it. (I recommend Guice for a lightweight DI container if you're not already using one).</p>\n"}, {'answer_id': 562544, 'author': 'MSN', 'author_id': 6210, 'author_profile': 'https://Stackoverflow.com/users/6210', 'pm_score': 2, 'selected': False, 'text': "<p>Singleton as an implementation detail is fine. Singleton as an interface or as an access mechanism is a giant PITA.</p>\n\n<p>A static method that takes no parameters returning an instance of an object is only slightly different from just using a global variable. If instead an object has a reference to the singleton object passed in, either via constructor or other method, then it doesn't matter how the singleton is actually created and the whole pattern turns out not to matter.</p>\n"}, {'answer_id': 562550, 'author': 'Esko Luontola', 'author_id': 62130, 'author_profile': 'https://Stackoverflow.com/users/62130', 'pm_score': 2, 'selected': False, 'text': '<p>Many applications require that there is only one instance of some class, so the pattern of having only one instance of a class is useful. But there are variations to how the pattern is <em>implemented</em>.</p>\n\n<p>There is the <strong>static singleton</strong>, in which the class forces that there can only be one instance of the class per process (in Java actually one per ClassLoader). Another option is to <strong>create only one instance</strong>.</p>\n\n<p>Static singletons are <em>evil</em> - one sort of global variables. They make testing harder, because it\'s not possible to execute the tests in full isolation. You need complicated setup and tear down code for cleaning the system between every test, and it\'s very easy to forget to clean some global state properly, which in turn may result in unspecified behaviour in tests.</p>\n\n<p>Creating only one instance is <em>good</em>. You just create one instance when the programs starts, and then pass the pointer to that instance to all other objects which need it. Dependency injection frameworks make this easy - you just configure the scope of the object, and the DI framework will take care of creating the instance and passing it to all who need it. For example in Guice you would annotate the class with @Singleton, and the DI framework will create only one instance of the class (per application - you can have multiple applications running in the same JVM). This makes testing easy, because you can create a new instance of the class for each test, and let the garbage collector destroy the instance when it is no more used. No global state will leak from one test to another.</p>\n\n<p>For more information: \n<a href="http://www.youtube.com/watch?v=-FRm3VPhseI" rel="nofollow noreferrer">The Clean Code Talks - "Global State and Singletons"</a></p>\n'}, {'answer_id': 567544, 'author': 'Chap', 'author_id': 2207, 'author_profile': 'https://Stackoverflow.com/users/2207', 'pm_score': 4, 'selected': False, 'text': '<p>Singleton is <strong>not</strong> a horrible pattern, although it is <strong>misused</strong> a lot.  I think this misuse is because it is one of the easier patterns and most new to the singleton are attracted to the global side effect.</p>\n\n<p><a href="http://www.se-radio.net/podcast/2007-12/episode-81-interview-erich-gamma" rel="noreferrer">Erich Gamma</a> had said the singleton is a pattern he wishes wasn\'t included in the GOF book and it\'s a bad design. I tend to disagree.</p>\n\n<p>If the pattern is used in order to create a single instance of an object at any given time  then the pattern is being used correctly.  If the singleton is used in order to give a global effect, it is being used incorrectly.</p>\n\n<p>Disadvantages:</p>\n\n<ul>\n<li>You are coupling to one class throughout the code that calls the singleton\n\n<ul>\n<li>Creates a hassle with unit testing because it is difficult to replace the instance with a mock object</li>\n<li>If the code needs to be refactored later on because of the need for more than one instance, it is more painful than if the singleton class were passed into the object (using an interface) that uses it</li>\n</ul></li>\n</ul>\n\n<p>Advantages:</p>\n\n<ul>\n<li>One instance of a class is represented at any given point in time. \n\n<ul>\n<li>By design you are enforcing this</li>\n</ul></li>\n<li>Instance is created when it is needed</li>\n<li>Global access is a side effect</li>\n</ul>\n'}, {'answer_id': 1400915, 'author': 'Jacobs Data Solutions', 'author_id': 140126, 'author_profile': 'https://Stackoverflow.com/users/140126', 'pm_score': 4, 'selected': False, 'text': '<p>Chicks dig me because I rarely use singleton and when I do it\'s typically something unusual. No, seriously, I love the singleton pattern. You know why? Because:</p>\n\n<ol>\n<li>I\'m lazy.</li>\n<li>Nothing can go wrong.</li>\n</ol>\n\n<p>Sure, the "experts" will throw around a bunch of talk about "unit testing" and "dependency injection" but that\'s all a load of dingo\'s kidneys. You say the singleton is hard to unit test? No problem! Just declare everything public and turn your class into a fun house of global goodness. You remember the show Highlander from the 1990\'s? The singleton is kind of like that because: A. It can never die; and B. There can be only one. So stop listening to all those DI weenies and implement your singleton with abandon. Here are some more good reasons...</p>\n\n<ul>\n<li>Everybody is doing it.</li>\n<li>The singleton pattern makes you invincible.</li>\n<li>Singleton rhymes with "win" (or "fun" depending on your accent).</li>\n</ul>\n'}, {'answer_id': 1605151, 'author': 'bloparod', 'author_id': 134559, 'author_profile': 'https://Stackoverflow.com/users/134559', 'pm_score': 1, 'selected': False, 'text': '<p>I\'m reading a lot about "Singleton", its problems, when to use it, etc., and these are my conclusions until now:</p>\n\n<ul>\n<li><p>Confusion between the classic implementation of Singleton and the real requirement: TO HAVE JUST ONE INSTANCE OF a CLASS!</p></li>\n<li><p>It\'s generally bad implemented. If you want a unique instance, don\'t use the (anti)pattern of using a static GetInstance() method returning a static object. This makes a class to be responsible for instantiating a single instance of itself and also perform logic. This breaks the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle" rel="nofollow noreferrer">Single Responsibility Principle</a>. Instead, this should be implemented by a factory class with the responsibility of ensuring that only one instance exists.</p></li>\n<li><p>It\'s used in constructors, because it\'s easy to use and must not be passed as a parameter. This should be resolved using <a href="http://en.wikipedia.org/wiki/Dependency_injection" rel="nofollow noreferrer">dependency injection</a>, that is a great pattern to achieve a good and testable object model.</p></li>\n<li><p>Not <a href="http://en.wikipedia.org/wiki/Test-driven_development" rel="nofollow noreferrer">TDD</a>. If you do TDD, dependencies are extracted from the implementation because you want your tests to be easy to write. This makes your object model to be better. If you use TDD, you won\'t write a static GetInstance =). BTW, if you think in objects with clear responsibilities instead classes, you\'ll get the same effect =).</p></li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/11831', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1366/']