<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ActionScript 3.0 Design Patterns &#187; Singleton</title>
	<atom:link href="http://www.as3dp.com/category/design-patterns/singleton/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.as3dp.com</link>
	<description>OOP Techniques for Flash and Flex Developers</description>
	<lastBuildDate>Sun, 29 Jan 2012 17:00:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Saint Singleton: Have We Martyred a Design Pattern Unfairly?</title>
		<link>http://www.as3dp.com/2009/09/saint-singleton-have-we-martyred-design-pattern-unfairly/</link>
		<comments>http://www.as3dp.com/2009/09/saint-singleton-have-we-martyred-design-pattern-unfairly/#comments</comments>
		<pubDate>Tue, 01 Sep 2009 16:07:02 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Singleton]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1424</guid>
		<description><![CDATA[In our book, I wrote the chapter on the Singleton Design Pattern, and on this blog I penned the post entitled, Singletons Give You Pimples. My comments were based on several articles (including one similarly named!) I had read on why to avoid the Singleton and the 2006 OOPSLA meetings in Portland, Oregon. Of the [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_1425" class="wp-caption alignleft" style="width: 175px"><img src="http://www.as3dp.com/wp-content/uploads/2009/09/saintsingleton.jpg" alt="Saint Singleton" title="saintsingleton" width="165" height="250" class="size-full wp-image-1425" /><p class="wp-caption-text">Saint Singleton</p></div>
<p>In our book, I wrote the chapter on the Singleton Design Pattern, and on this blog I  penned the post entitled, <a href="http://www.as3dp.com/2008/11/26/we-don’t-need-no-stinkin’-singletons-why-to-avoid-the-singleton-pattern-in-actionscript-30-programming/">Singletons Give You Pimples</a>. My comments were based on several articles (including one similarly named!) I had read on why to avoid the Singleton and the 2006 OOPSLA meetings in Portland, Oregon. Of the articles available online that detail the problems with the Singleton Miško Hevery’s blog post, <a href="http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/">Singletons are Pathological Liars</a> is the most detailed, but Scott Densmore&#8217;s article, <a href="http://blogs.msdn.com/scottdensmore/archive/2004/05/25/140827.aspx">Why Singletons are Evil</a> is an equally convincing—and succinct— argument why we should keep our distance from Singletons.</p>
<p>Even Erich Gamma was quoted as lamenting the fact that they included the Singleton in their Design Pattern book. There&#8217;s not a lot more that I can say than I did in my ranting post on Stinkin&#8217; Singletons, and you can find tons more online.</p>
<p><strong>Is There Salvation?</strong></p>
<p>Recently on this <a href="http://www.as3dp.com/2009/08/25/artists-animators-and-actionscript-30/#comments">blog</a>, my old friend aYo Binite (who got me involved in ActionScript 3.0 design patterns in the first place) suggested that the Singleton wasn&#8217;t as bad as I made it out to be.  Naturally, I replied with a reasonable argument comparing them to skin-eating viruses, pedophiles, and Nazis. aYo said that the jury was still out, and I wondered, &#8216;What jury?&#8217; All in all, though, I may have been as unfair to the Singleton as the Roman emperor Diocletian was to Saint Sebastian. (Diocletian first had Sebastian used for archery practice and left him for dead. Then when the emperor found that he had survived—he had Sebastian beaten to death and thrown into a crapper.) Naturally, I don&#8217;t want to be equated with the touchy Diocletian, and so I&#8217;m offering up an opportunity for comments by our readers to either help pull the Singleton out of the crapper or shoot another arrow into the misguided design pattern. The Comment Section is Open!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2009%2F09%2Fsaint-singleton-have-we-martyred-design-pattern-unfairly%2F&amp;title=Saint%20Singleton%3A%20Have%20We%20Martyred%20a%20Design%20Pattern%20Unfairly%3F" id="wpa2a_2"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/09/saint-singleton-have-we-martyred-design-pattern-unfairly/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
		</item>
		<item>
		<title>Singletons Give You Pimples: Why to Avoid the Singleton Pattern in ActionScript 3.0 Programming</title>
		<link>http://www.as3dp.com/2008/11/we-dont-need-no-stinkin-singletons-why-to-avoid-the-singleton-pattern-in-actionscript-30-programming/</link>
		<comments>http://www.as3dp.com/2008/11/we-dont-need-no-stinkin-singletons-why-to-avoid-the-singleton-pattern-in-actionscript-30-programming/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 11:31:09 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Singleton]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=299</guid>
		<description><![CDATA[At the 2006 OOPSLA Conference in Portland, Oregon one of the people who came into our session was pounding on the Singleton Design Pattern. He even brought in a PowerPoint presentation quoting Erich Gamma as saying he wished he’d never included the Singleton in the GoF book. This wasn’t a mild rebuff of the Singleton; [...]]]></description>
			<content:encoded><![CDATA[<p>At the 2006 OOPSLA Conference in Portland, Oregon one of the people who came into our session was pounding on the Singleton Design Pattern. He even brought in a PowerPoint presentation quoting Erich Gamma as saying he wished he’d never included the Singleton in the GoF book. This wasn’t a mild rebuff of the Singleton; it was a wholesale condemnation.</p>
<p><strong>The Singleton in ActionScript 3.0</strong></p>
<p>A quick online search reveals a good deal of discussion about the Singleton in ActionScript 3. However, much of that discussion is based on how to improve it given AS3’s lack of private class constructors. Both <a href="http://www.gskinner.com/blog/archives/2006/07/as3_singletons.html">Grant Skinner</a> and <a href="http://www.tink.ws/blog/stricter-singletons/">Tink</a> have provided brilliant workarounds that seem to solve the problem of multiple instantiations of a Singleton. <a href="http://www.darronschall.com/weblog/2007/11/actionscript-3-singleton-redux.cfm">Darron Schall</a> has written a nice summary of the ActionScript 3.0 Singleton discussions and reached a similar conclusion as I <em>originally</em> did. Namely, most of the problems envisioned by ActionScript 3.0’s lack of private class constructors never caused the problems that AS3’s shortcomings were supposed to have caused. In our book, we provide a brief cautionary tale about using the Singleton, but it was not very strong or substantive. (I wrote the Singleton chapter; so I can say that.)</p>
<p>Any attempt to improve the Singleton is like making the black mamba more deadly. The point <strong><em>is not</em></strong> to improve it but to <strong><em>avoid it</em></strong>. However, in understanding the problems using the Singleton, we can better understand what the focus of OOP programming should be and the reason that using design patterns is important for achieving the dual goals of change and re-usability. A lot of the criticisms of the Singleton have been summarized by <a href="http://blogs.msdn.com/scottdensmore/archive/2004/05/25/140827.aspx"> Scott Densmore</a> and on the <a href="http://flash-focus.blogspot.com/2007/11/end-of-singleton.html">Flash-Focus</a> blog,  and my intention is not to improve on their comments. Rather, I’d like to use the general comments by them and others to focus on good practices and re-focus the discussion of design patterns to the key issues important to OOP and design patterns. That focus, I believe, is the <strong><em>relationship between objects</em></strong>.</p>
<p><span id="more-299"></span></p>
<p><strong>Dependencies Hamper Change</strong></p>
<p>A core principle in design patterns is to reduce dependencies.  A primary culprit in creating dependencies is the <strong>global variable</strong>. Global variables can run through a program like a skin-eating virus. One little change requires unzipping the whole package and changing all of the parts—rewriting the program. That’s why private variables and functions are so important. The little buggers are encapsulated and won’t run amok and change or get changed by unwanted operations in the program. Global variables, while making life simpler in the short run, cause headaches when even little changes have to be made. The extent to which Singletons provide a global access point, they serve many of the same functions and generate the same kind of problems as global variables. However, they often hide their global character and untangling unwanted dependencies because of Singletons is often more troublesome than the same process with global variables. You don’t expect Singletons to create dependencies and so they may be overlooked as the source of the problem. Creating global variables, on the other hand, is an act that programmers actually have to think about and so should be well known when dependency problems arise. As such they are (or should be) the immediate suspects. Not so with Singletons. After all, the Singleton structure <em>is a design pattern</em> and we all expect design patterns to have the opposite effect and automatically reduce dependencies. However, Singletons may have been denizens of the Dark Side all along, and we just never noticed. (Miško Hevery’s blog post, <a href="http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/">Singletons are Pathological Liars</a> is another interesting take on why Singletons are nothing more than global variables.)</p>
<p><strong>The Single Responsibility Rule</strong></p>
<p>A fundamental principle in OOP is that each class (or element) should have a single responsibility. Chandima lays it out nicely in the MVC chapter in our book (p.443),</p>
<blockquote><p>…each element in the MVC adheres to the single responsibility principle. Each element has a well-defined role. The model manages state, the view represents state, and the controller handles user input. This allows each element to be swapped out without affecting other elements. </p>
</blockquote>
<p>Now you may be thinking—<em>That’s exactly what a Singleton does! Its single purpose is to provide a single entry point.</em> Well, not according to Scott Densmore. The Singleton provides both a single entry point, <em> and </em> it also insures only a single instance of the class will be created. Count ‘em. That’s two responsibilities. Well maybe so, and maybe not. According to the Freemans (authors of <em>Head First Design Patterns</em>), a single responsibility refers to a design principle,</p>
<blockquote><p> A class should have only one reason to change.</p>
</blockquote>
<p> Okay, but does the Singleton have <em>any</em> reason to change? A responsibility is a reason to change, and so yes, as a matter of fact, it does. Unfortunately, it has more than one.</p>
<p>The <a href="http://www.as3dp.com/2008/01/14/actionscript-30-chain-of-responsibility-design-pattern-decoupling-request-and-request-handler/#more-56">Chain of Responsibility</a> pattern discussed on this blog is one of the best examples of the rule of Single Responsibility. Each element in the chain has one and only one responsibility. Figure 1 illustrates how the chain works. A request is passed to the first element in the chain, and if can handle the request, it does. If not it passes it on to the next element or until it runs out of options or is handled by a residual class.</p>
<p><a href="http://www.as3dp.com/wp-content/uploads/2008/11/chain.png"><img src="http://www.as3dp.com/wp-content/uploads/2008/11/chain.png" alt="" title="chain" width="479" height="179" class="alignnone size-full wp-image-303" /></a></p>
<p><em><strong>Figure 1</strong>: Each element has a single responsibility. </em> </p>
<p>In some respects the Chain of Responsibility pattern represents a microcosm of the Single Responsibility rule. You can see clearly what each element’s responsibility is and whether it can handle the request or not. Likewise, you can change the elements without having to change the rest of the program. All programs should have the feature of Single Responsibility.</p>
<p><strong>No Tight Coupling Please</strong></p>
<p>The principle of <em>Loose Coupling</em> is so ubiquitous in both design pattern and OOP architecture that to mention it may seem to be an overstatement. However, I’ve seen too many instances where a perfectly good design pattern is ruined because the developer willy-nilly tightly coupled objects without really thinking through the consequences. During testing, especially unit testing, these tightly coupled structures make testing alternative implementations virtually impossible without re-writing all the code. That defeats the purpose of OOP and the whole concept of encapsulation.</p>
<p>Singletons type to the exact type of the Singleton object. Of course with that you have no loose coupling; just the opposite. If you think this is a minor matter, <a href="http://code.google.com/p/google-singleton-detector/">tools exist for detecting Singletons</a> (in Java) so that they don’t mess up the program. In other words, Singletons are treated like viruses. At Google, in their<a href="http://misko.hevery.com/code-reviewers-guide/"> Guide: Writing Testable Code</a>  one of the flaws includes  <strong><em>adding or using singletons</em></strong>.</p>
<p><strong>Why So Many Singletons?</strong></p>
<p>Several years ago I was working on a beta for a Web development tool. Like all betas, we had a diverse international group of developers and designers tweaking and testing the application. In the beta forum one of the designer/developers commented that he once had a client who insisted that he use <em>Front Page</em> to develop a site he wanted. When the designer balked, the client explained that <em>Front Page</em> was the most common tool in Web development and so must be the best. It seems that some have applied the same logic to the Singleton. Not only is it common, it is simple to write—even without private class constructors in ActionScript 3.0. In good faith, many developers seem to have concluded that using the Singleton would guarantee that their code was design pattern based and promoted good OOP. Unfortunately, it seems that the opposite may be true. So before plopping another Singleton into your code, keep in mind that you may be inadvertently adding what amounts to an OOP virus to your project.</p>
<p>The following diagram was added in response to Gavin&#8217;s comment that follow this post.<br />
<a href="http://www.as3dp.com/wp-content/uploads/2008/12/singletonclient.png"><img src="http://www.as3dp.com/wp-content/uploads/2008/12/singletonclient.png" alt="" title="singletonclient" width="229" height="428" class="alignnone size-full wp-image-315" /></a></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2008%2F11%2Fwe-dont-need-no-stinkin-singletons-why-to-avoid-the-singleton-pattern-in-actionscript-30-programming%2F&amp;title=Singletons%20Give%20You%20Pimples%3A%20Why%20to%20Avoid%20the%20Singleton%20Pattern%20in%20ActionScript%203.0%20Programming" id="wpa2a_4"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2008/11/we-dont-need-no-stinkin-singletons-why-to-avoid-the-singleton-pattern-in-actionscript-30-programming/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
	</channel>
</rss>

