<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Singletons: Solving problems you didn’t know you never had since 1995</title>
	<atom:link href="http://jalf.dk/blog/2010/03/singletons-solving-problems-you-didnt-know-you-never-had-since-1995/feed/" rel="self" type="application/rss+xml" />
	<link>http://jalf.dk/blog/2010/03/singletons-solving-problems-you-didnt-know-you-never-had-since-1995/</link>
	<description>Musings and thoughts on programming and other geeky stuff</description>
	<lastBuildDate>Sat, 07 Jan 2012 20:30:11 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Hari Karam Singh</title>
		<link>http://jalf.dk/blog/2010/03/singletons-solving-problems-you-didnt-know-you-never-had-since-1995/comment-page-1/#comment-17597</link>
		<dc:creator>Hari Karam Singh</dc:creator>
		<pubDate>Sat, 19 Nov 2011 16:10:07 +0000</pubDate>
		<guid isPermaLink="false">http://jalf.dk/blog/?p=532#comment-17597</guid>
		<description>&lt;p&gt;Hidden in the sense that the lowest level object which is making the call now has 2 more dependencies in addition to its dependency on the settings class - perhaps not hidden from it&#039;s perspective but from the top level vantage point, doesn&#039;t the dependency on Settings seem quite buried?  Say you changed the structure of the settings object and needed to find all referencing code...&lt;/p&gt;

&lt;p&gt;At first I was inclined toward your stance on using a simple global variable which is init&#039;ed perhaps, with the app startup.  Upon reflection however, I&#039;m not sure I see the difference between this and the singleton pattern, other than that the later packages the global variable (as a static) and has a self contained initialisation.  Some might even argue that this is more in keeping with Single Responsibility Principle.  What do you think?&lt;/p&gt;

&lt;p&gt;Thanks for the thought provoking and detailed discussion by the way.  It&#039;s very pertinent to the code I&#039;m writing as we speak :)&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Hidden in the sense that the lowest level object which is making the call now has 2 more dependencies in addition to its dependency on the settings class — perhaps not hidden from it’s perspective but from the top level vantage point, doesn’t the dependency on Settings seem quite buried?  Say you changed the structure of the settings object and needed to find all referencing code…</p>

<p>At first I was inclined toward your stance on using a simple global variable which is init’ed perhaps, with the app startup.  Upon reflection however, I’m not sure I see the difference between this and the singleton pattern, other than that the later packages the global variable (as a static) and has a self contained initialisation.  Some might even argue that this is more in keeping with Single Responsibility Principle.  What do you think?</p>

<p>Thanks for the thought provoking and detailed discussion by the way.  It’s very pertinent to the code I’m writing as we speak :)</p>]]></content:encoded>
	</item>
	<item>
		<title>By: jalf</title>
		<link>http://jalf.dk/blog/2010/03/singletons-solving-problems-you-didnt-know-you-never-had-since-1995/comment-page-1/#comment-17474</link>
		<dc:creator>jalf</dc:creator>
		<pubDate>Thu, 17 Nov 2011 19:27:32 +0000</pubDate>
		<guid isPermaLink="false">http://jalf.dk/blog/?p=532#comment-17474</guid>
		<description>&lt;p&gt;How is the first one a hidden dependency? It&#039;s clear exactly &lt;em&gt;how&lt;/em&gt; the current object retrieves the settings object. We know that it does this through its reference to the parent controller, which has a reference to the parent application, and so on. We have explicitly said in the code that this code requires a reference to the parent controller, so it is not a hidden dependency. I can look at the object&#039;s constructor and see that it takes a reference to the object it needs to reference.
Or I can follow the code the other way around: Start at wherever the &lt;code&gt;settings&lt;/code&gt; object is created, and then read along to see where it is passed to. If I follow the code from that point, it naturally takes me to every use site.&lt;/p&gt;

&lt;p&gt;On the other hand, in your second snippet, it&#039;s not clear where this settings object &lt;em&gt;comes from&lt;/em&gt;. And I can&#039;t follow the uses of the object. If I start at the construction of the &lt;code&gt;settings&lt;/code&gt; object, I immediately run into &quot;it&#039;s stored into a singleton, which is... just there. At some point, someone might call &lt;code&gt;getInstance&lt;/code&gt; on it, but I don&#039;t know who does that, or when or why.&lt;/p&gt;

&lt;p&gt;So I disagree with your analysis of dependencies.&lt;/p&gt;

&lt;p&gt;On the other hand, it is certainly more &lt;em&gt;convenient&lt;/em&gt; to just make the settings object globally accessible.&lt;/p&gt;

&lt;p&gt;So go ahead and do that. But it doesn&#039;t need to be a &lt;em&gt;singleton&lt;/em&gt; to achieve that goal. Just a single global/static instance will allow everyone to access the settings object if they want to. There&#039;s no need for messing around with singletons and all the &lt;em&gt;other&lt;/em&gt; problems they cause.&lt;/p&gt;

&lt;p&gt;Singletons are global, but globals are not singletons. If you need a global, that doesn&#039;t mean you have to use a singleton.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>How is the first one a hidden dependency? It’s clear exactly <em>how</em> the current object retrieves the settings object. We know that it does this through its reference to the parent controller, which has a reference to the parent application, and so on. We have explicitly said in the code that this code requires a reference to the parent controller, so it is not a hidden dependency. I can look at the object’s constructor and see that it takes a reference to the object it needs to reference.
Or I can follow the code the other way around: Start at wherever the <code>settings</code> object is created, and then read along to see where it is passed to. If I follow the code from that point, it naturally takes me to every use site.</p>

<p>On the other hand, in your second snippet, it’s not clear where this settings object <em>comes from</em>. And I can’t follow the uses of the object. If I start at the construction of the <code>settings</code> object, I immediately run into “it’s stored into a singleton, which is… just there. At some point, someone might call <code>getInstance</code> on it, but I don’t know who does that, or when or why.</p>

<p>So I disagree with your analysis of dependencies.</p>

<p>On the other hand, it is certainly more <em>convenient</em> to just make the settings object globally accessible.</p>

<p>So go ahead and do that. But it doesn’t need to be a <em>singleton</em> to achieve that goal. Just a single global/static instance will allow everyone to access the settings object if they want to. There’s no need for messing around with singletons and all the <em>other</em> problems they cause.</p>

<p>Singletons are global, but globals are not singletons. If you need a global, that doesn’t mean you have to use a singleton.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Hari Karam Singh</title>
		<link>http://jalf.dk/blog/2010/03/singletons-solving-problems-you-didnt-know-you-never-had-since-1995/comment-page-1/#comment-17460</link>
		<dc:creator>Hari Karam Singh</dc:creator>
		<pubDate>Thu, 17 Nov 2011 15:01:17 +0000</pubDate>
		<guid isPermaLink="false">http://jalf.dk/blog/?p=532#comment-17460</guid>
		<description>&lt;p&gt;So say you need access to a Settings class at various hierarchical levels in your code, for instance the Application, the Controller and the View.  You could have a globally accessible Singleton and have code like:&lt;/p&gt;

&lt;blockquote&gt;
SettingsClass.getSetting(&#039;showTooltips&#039;)
&lt;/blockquote&gt;

&lt;p&gt;Or you have it be an instance var on the Application class which is referenced by an instance var on the Controller class which is referenced by an instance var on the view and so on...  You end up with the following:&lt;/p&gt;

&lt;blockquote&gt;
self.parentController.parentApplication.settings.getSetting(&#039;showTooltips&#039;);
&lt;/blockquote&gt;

&lt;p&gt;Which is a (very!) hidden dependency.  Or you do...&lt;/p&gt;

&lt;blockquote&gt;
Application&#039;s init:
self.viewController.showTooltips = settings.getSettings(&#039;showTooltips&#039;)

viewController&#039;s init:
self.view = this.showTooltips

&lt;/blockquote&gt;

&lt;p&gt;...and create tons of code with a multiplicity of by way of property names, making changes such as refactoring a serious sweat.&lt;/p&gt;

&lt;p&gt;What I&#039;m asking really, is what about the case for the singleton (or at least a global variable) where it reduces complexity for application areas which aren&#039;t resource intensive?&lt;/p&gt;

&lt;p&gt;Like many things in programming isn&#039;t it a trade off between simplicity and covering all potential future  cases?&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>So say you need access to a Settings class at various hierarchical levels in your code, for instance the Application, the Controller and the View.  You could have a globally accessible Singleton and have code like:</p>

<blockquote>
SettingsClass.getSetting(‘showTooltips’)
</blockquote>

<p>Or you have it be an instance var on the Application class which is referenced by an instance var on the Controller class which is referenced by an instance var on the view and so on…  You end up with the following:</p>

<blockquote>
self.parentController.parentApplication.settings.getSetting(‘showTooltips’);
</blockquote>

<p>Which is a (very!) hidden dependency.  Or you do…</p>

<blockquote>
Application’s init:
self.viewController.showTooltips = settings.getSettings(‘showTooltips’)

viewController’s init:
self.view = this.showTooltips

</blockquote>

<p>…and create tons of code with a multiplicity of by way of property names, making changes such as refactoring a serious sweat.</p>

<p>What I’m asking really, is what about the case for the singleton (or at least a global variable) where it reduces complexity for application areas which aren’t resource intensive?</p>

<p>Like many things in programming isn’t it a trade off between simplicity and covering all potential future  cases?</p>]]></content:encoded>
	</item>
	<item>
		<title>By: jalf</title>
		<link>http://jalf.dk/blog/2010/03/singletons-solving-problems-you-didnt-know-you-never-had-since-1995/comment-page-1/#comment-14750</link>
		<dc:creator>jalf</dc:creator>
		<pubDate>Sat, 29 Oct 2011 12:18:41 +0000</pubDate>
		<guid isPermaLink="false">http://jalf.dk/blog/?p=532#comment-14750</guid>
		<description>&lt;p&gt;@Adriano: Why do you need to &quot;avoid&quot; having multiple instances of an object?
I don&#039;t need to do anything to &quot;avoid&quot; jumping into the sea. I don&#039;t need to do anything to &quot;avoid&quot; buying a plane ticket. Those things only happen if I &lt;em&gt;decide&lt;/em&gt; to do them. And likewise, instances of an object only exist if I decide to create them. If I only need one instance of an object, why would I create two? Why do you feel that you need to do anything special to &quot;avoid&quot; creating multiple instances?&lt;/p&gt;

&lt;p&gt;The second part, about global access, is somewhat more subjective. Perhaps you really want &lt;em&gt;global&lt;/em&gt; access to this object. In that case, go ahead and make it global. I think global access is nearly always the wrong thing, but it really doesn&#039;t matter. If you want global access, use a global. If you don&#039;t want global access, don&#039;t make it a global. Neither case requires a singleton.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;I also think you can solve the prob­lem get­ting those depen­den­cies vis­i­ble. Don’t you think you can assign a sin­gle­ton instance to a mem­ber vari­able of a class that needs it&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Sure, you can do that. But then (1) why does it need to be a singleton, why does it need to be globally accessible, and (2) you still have no way to ensure that it&#039;s never used in the &quot;other&quot; way. That&#039;s what&#039;s icky about singletons. You don&#039;t &lt;em&gt;know&lt;/em&gt; if these hidden dependencies exist. They &lt;em&gt;might&lt;/em&gt; all be made explicit as you expect, but there might also be a number of&quot;hidden&quot; uses scattered around your code. You don&#039;t know.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>@Adriano: Why do you need to “avoid” having multiple instances of an object?
I don’t need to do anything to “avoid” jumping into the sea. I don’t need to do anything to “avoid” buying a plane ticket. Those things only happen if I <em>decide</em> to do them. And likewise, instances of an object only exist if I decide to create them. If I only need one instance of an object, why would I create two? Why do you feel that you need to do anything special to “avoid” creating multiple instances?</p>

<p>The second part, about global access, is somewhat more subjective. Perhaps you really want <em>global</em> access to this object. In that case, go ahead and make it global. I think global access is nearly always the wrong thing, but it really doesn’t matter. If you want global access, use a global. If you don’t want global access, don’t make it a global. Neither case requires a singleton.</p>

<blockquote>
  <p>I also think you can solve the prob­lem get­ting those depen­den­cies vis­i­ble. Don’t you think you can assign a sin­gle­ton instance to a mem­ber vari­able of a class that needs it</p>
</blockquote>

<p>Sure, you can do that. But then (1) why does it need to be a singleton, why does it need to be globally accessible, and (2) you still have no way to ensure that it’s never used in the “other” way. That’s what’s icky about singletons. You don’t <em>know</em> if these hidden dependencies exist. They <em>might</em> all be made explicit as you expect, but there might also be a number of“hidden” uses scattered around your code. You don’t know.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Adriano Di Giovanni</title>
		<link>http://jalf.dk/blog/2010/03/singletons-solving-problems-you-didnt-know-you-never-had-since-1995/comment-page-1/#comment-12390</link>
		<dc:creator>Adriano Di Giovanni</dc:creator>
		<pubDate>Wed, 05 Oct 2011 11:22:38 +0000</pubDate>
		<guid isPermaLink="false">http://jalf.dk/blog/?p=532#comment-12390</guid>
		<description>&lt;p&gt;I started studying OOP less than a year ago. So, it is not difficult that all of you are more aware of it than me.&lt;/p&gt;

&lt;p&gt;IMHO, I think it is not a bad idea to use Singletons in order to avoid having multiple instances of an object (i.e. the Model) and have a global access to the single instance.&lt;/p&gt;

&lt;p&gt;I agree with you about the fact that Singletons increase hidden dependencies. But I also think you can solve the problem getting those dependencies visible. Don&#039;t you think you can assign a singleton instance to a member variable of a class that needs it?&lt;/p&gt;

&lt;p&gt;Doing so, you make the dependency visible and give yourself the opportunity to remove the single instance constraint if you made a bad assumption about it.&lt;/p&gt;

&lt;p&gt;Does it make sense?
Do I have to apologize for my english? :)&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I started studying OOP less than a year ago. So, it is not difficult that all of you are more aware of it than me.</p>

<p>IMHO, I think it is not a bad idea to use Singletons in order to avoid having multiple instances of an object (i.e. the Model) and have a global access to the single instance.</p>

<p>I agree with you about the fact that Singletons increase hidden dependencies. But I also think you can solve the problem getting those dependencies visible. Don’t you think you can assign a singleton instance to a member variable of a class that needs it?</p>

<p>Doing so, you make the dependency visible and give yourself the opportunity to remove the single instance constraint if you made a bad assumption about it.</p>

<p>Does it make sense?
Do I have to apologize for my english? :)</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Dieter Govaerts</title>
		<link>http://jalf.dk/blog/2010/03/singletons-solving-problems-you-didnt-know-you-never-had-since-1995/comment-page-1/#comment-4984</link>
		<dc:creator>Dieter Govaerts</dc:creator>
		<pubDate>Tue, 08 Mar 2011 12:05:44 +0000</pubDate>
		<guid isPermaLink="false">http://jalf.dk/blog/?p=532#comment-4984</guid>
		<description>&lt;p&gt;Thanks for sharing this. You definitely have a point and I will consider never to use a Singleton again.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Thanks for sharing this. You definitely have a point and I will consider never to use a Singleton again.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Scott</title>
		<link>http://jalf.dk/blog/2010/03/singletons-solving-problems-you-didnt-know-you-never-had-since-1995/comment-page-1/#comment-1407</link>
		<dc:creator>Scott</dc:creator>
		<pubDate>Sat, 29 May 2010 21:39:27 +0000</pubDate>
		<guid isPermaLink="false">http://jalf.dk/blog/?p=532#comment-1407</guid>
		<description>&lt;p&gt;What I&#039;ve taken to saying lately is that Singleton should be an &lt;em&gt;Application&lt;/em&gt; design question rather than a &lt;em&gt;Class&lt;/em&gt; design question.  If in your specific application it&#039;s convenient to have one globally-accessible instance of a certain type, that&#039;s great, but there&#039;s no reason to write the class in such a way that the class needs to know about that decision.&lt;/p&gt;

&lt;p&gt;Of course, that doesn&#039;t &lt;em&gt;prevent&lt;/em&gt; someone using another instance, but if the application is architected that way, you can&#039;t get the other code to use the instance you created even if you wanted it to.&lt;/p&gt;

&lt;p&gt;BTW, it might not be a bad idea to write a bit about dependency injection to add to your singleton rants, since that&#039;s in many ways a &quot;do this where your current instinct is a singleton, and you get all these advantages&quot;.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>What I’ve taken to saying lately is that Singleton should be an <em>Application</em> design question rather than a <em>Class</em> design question.  If in your specific application it’s convenient to have one globally-accessible instance of a certain type, that’s great, but there’s no reason to write the class in such a way that the class needs to know about that decision.</p>

<p>Of course, that doesn’t <em>prevent</em> someone using another instance, but if the application is architected that way, you can’t get the other code to use the instance you created even if you wanted it to.</p>

<p>BTW, it might not be a bad idea to write a bit about dependency injection to add to your singleton rants, since that’s in many ways a “do this where your current instinct is a singleton, and you get all these advantages”.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Plunket</title>
		<link>http://jalf.dk/blog/2010/03/singletons-solving-problems-you-didnt-know-you-never-had-since-1995/comment-page-1/#comment-916</link>
		<dc:creator>Tom Plunket</dc:creator>
		<pubDate>Wed, 07 Apr 2010 18:26:07 +0000</pubDate>
		<guid isPermaLink="false">http://jalf.dk/blog/?p=532#comment-916</guid>
		<description>&lt;p&gt;Good points made, especially the ones relating to cout vs. ostream.  I personally feel the &quot;what if another instance of the thing can&#039;t be instantiated&quot; is a weak argument; the construction of an object should require enough context to get a useful object, so if someone wants to create their own Logger instance that&#039;s fine- but they&#039;ll need to configure it properly if it&#039;s going to do anything useful and it should be obvious to anyone who spends ten minutes in the code that that&#039;s not how everyone else gets their messages out to the world.&lt;/p&gt;

&lt;p&gt;Anyway- I see Singletons as additional complexity on top of the overwhelming complexity that mutable global state itself brings.  I don&#039;t want that mutable global state in the first place, so Singletons bring no value to me, personally.&lt;/p&gt;

&lt;p&gt;I don&#039;t think ignorance of the codebase is a fruitful argument, one way or the other; people will do whatever they want to do, and sometimes people screw up.  The best way to code (IMO) is such that screw ups are shown quickly and obviously.  Globals tend to make this more difficult, as well.&lt;/p&gt;

&lt;p&gt;The most useful statement made, I must say, is that of artificially constraining your objects.  What&#039;s the problem if someone instantiates another instance of this class?  (and if they do it wrong, report the error promptly and unambiguously!)&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Good points made, especially the ones relating to cout vs. ostream.  I personally feel the “what if another instance of the thing can’t be instantiated” is a weak argument; the construction of an object should require enough context to get a useful object, so if someone wants to create their own Logger instance that’s fine– but they’ll need to configure it properly if it’s going to do anything useful and it should be obvious to anyone who spends ten minutes in the code that that’s not how everyone else gets their messages out to the world.</p>

<p>Anyway– I see Singletons as additional complexity on top of the overwhelming complexity that mutable global state itself brings.  I don’t want that mutable global state in the first place, so Singletons bring no value to me, personally.</p>

<p>I don’t think ignorance of the codebase is a fruitful argument, one way or the other; people will do whatever they want to do, and sometimes people screw up.  The best way to code (IMO) is such that screw ups are shown quickly and obviously.  Globals tend to make this more difficult, as well.</p>

<p>The most useful statement made, I must say, is that of artificially constraining your objects.  What’s the problem if someone instantiates another instance of this class?  (and if they do it wrong, report the error promptly and unambiguously!)</p>]]></content:encoded>
	</item>
	<item>
		<title>By: gf</title>
		<link>http://jalf.dk/blog/2010/03/singletons-solving-problems-you-didnt-know-you-never-had-since-1995/comment-page-1/#comment-843</link>
		<dc:creator>gf</dc:creator>
		<pubDate>Mon, 29 Mar 2010 19:40:33 +0000</pubDate>
		<guid isPermaLink="false">http://jalf.dk/blog/?p=532#comment-843</guid>
		<description>&lt;p&gt;Ah, you did write about it - nice to see your arguing collected in one article.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Ah, you did write about it — nice to see your arguing collected in one article.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: jalf</title>
		<link>http://jalf.dk/blog/2010/03/singletons-solving-problems-you-didnt-know-you-never-had-since-1995/comment-page-1/#comment-822</link>
		<dc:creator>jalf</dc:creator>
		<pubDate>Thu, 25 Mar 2010 18:32:38 +0000</pubDate>
		<guid isPermaLink="false">http://jalf.dk/blog/?p=532#comment-822</guid>
		<description>&lt;p&gt;Perhaps I can. It depends on what kind of assumptions you&#039;re willing to make about the people who have access to the code.&lt;/p&gt;

&lt;p&gt;I like to assume that people using my code are competent. I&#039;m not naive enough to actually believe it to be the case, but incompetent developers will manage to break the code no matter what I do, so why bother with them?&lt;/p&gt;

&lt;p&gt;But what situations can you imagine where the library developer might know that only one instance makes sense, but where the library &lt;em&gt;user&lt;/em&gt; would nevertheless instantiate multiple objects?&lt;/p&gt;

&lt;p&gt;Usually, the &quot;you only need one instance&quot; information is conveyed easily through simple naming and/or documentation. If I know that an object is responsible for, say, rendering all objects in my scene graph, then it is pretty intuitive that &lt;em&gt;another instance&lt;/em&gt; won&#039;t have access to the same scene graph, and so won&#039;t be very useful. As a library user, I know intuitively that this one object is all I need, because it is associated with the data I want it to be associated with.&lt;/p&gt;

&lt;p&gt;Or take the &lt;code&gt;std::cout&lt;/code&gt; example from my post above. You&#039;re right, a sufficiently dumb library user &lt;em&gt;could&lt;/em&gt; instantiate another &lt;code&gt;std::ostream&lt;/code&gt; when he intended to write to the standard output, but in reality, it just doesn&#039;t happen, because real-world users of the standard library know that &lt;code&gt;std::cout&lt;/code&gt; is the standard output stream, and they know they only &lt;em&gt;need&lt;/em&gt; that one instance.&lt;/p&gt;

&lt;p&gt;Can you think of a situation where the library user might mistakenly believe that creating multiple instances is warranted? I can&#039;t. I won&#039;t rule out that such situations exist, but in the common cases (see &lt;code&gt;std::cout&lt;/code&gt;) it isn&#039;t an issue.&lt;/p&gt;

&lt;p&gt;It&#039;s not that I assume programmers in general are smart enough to figure out how to use my library. It&#039;s just that those who aren&#039;t will manage to break it no matter what I do. Sure, I could sprinkle singletons all over it, but that still wouldn&#039;t save them from themselves. There are still hundreds of ways in which they could break my library: passing invalid parameters to functions, freeing resources allocated by the library and so on.&lt;/p&gt;

&lt;p&gt;Of course, a final point is that you&#039;re not arguing for a singleton. You&#039;re arguing for a &quot;class-that-enforces-that-exactly-one-instance-will-exist&quot;. That&#039;s only &lt;em&gt;half&lt;/em&gt; of the singleton. The singleton also provides global access. If you really think that enforcing the 1-instance-limit is best, then do that, but how does that excuse making the same object globally accessible?&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Perhaps I can. It depends on what kind of assumptions you’re willing to make about the people who have access to the code.</p>

<p>I like to assume that people using my code are competent. I’m not naive enough to actually believe it to be the case, but incompetent developers will manage to break the code no matter what I do, so why bother with them?</p>

<p>But what situations can you imagine where the library developer might know that only one instance makes sense, but where the library <em>user</em> would nevertheless instantiate multiple objects?</p>

<p>Usually, the “you only need one instance” information is conveyed easily through simple naming and/or documentation. If I know that an object is responsible for, say, rendering all objects in my scene graph, then it is pretty intuitive that <em>another instance</em> won’t have access to the same scene graph, and so won’t be very useful. As a library user, I know intuitively that this one object is all I need, because it is associated with the data I want it to be associated with.</p>

<p>Or take the <code>std::cout</code> example from my post above. You’re right, a sufficiently dumb library user <em>could</em> instantiate another <code>std::ostream</code> when he intended to write to the standard output, but in reality, it just doesn’t happen, because real-world users of the standard library know that <code>std::cout</code> is the standard output stream, and they know they only <em>need</em> that one instance.</p>

<p>Can you think of a situation where the library user might mistakenly believe that creating multiple instances is warranted? I can’t. I won’t rule out that such situations exist, but in the common cases (see <code>std::cout</code>) it isn’t an issue.</p>

<p>It’s not that I assume programmers in general are smart enough to figure out how to use my library. It’s just that those who aren’t will manage to break it no matter what I do. Sure, I could sprinkle singletons all over it, but that still wouldn’t save them from themselves. There are still hundreds of ways in which they could break my library: passing invalid parameters to functions, freeing resources allocated by the library and so on.</p>

<p>Of course, a final point is that you’re not arguing for a singleton. You’re arguing for a “class-that-enforces-that-exactly-one-instance-will-exist”. That’s only <em>half</em> of the singleton. The singleton also provides global access. If you really think that enforcing the 1-instance-limit is best, then do that, but how does that excuse making the same object globally accessible?</p>]]></content:encoded>
	</item>
</channel>
</rss>

