<?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>Eamonn Faherty &#187; singleton</title>
	<atom:link href="http://blog.eamonnfaherty.co.uk/tag/singleton/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.eamonnfaherty.co.uk</link>
	<description>technology enthusiast, public speaker and technical director</description>
	<lastBuildDate>Mon, 26 Jul 2010 23:33:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Actionscript3 Design Patterns:The Singleton</title>
		<link>http://blog.eamonnfaherty.co.uk/2009/11/09/actionscript3-design-patternsthe-singleton/</link>
		<comments>http://blog.eamonnfaherty.co.uk/2009/11/09/actionscript3-design-patternsthe-singleton/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 00:25:33 +0000</pubDate>
		<dc:creator>eamonn</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[actionscript3]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[singleton]]></category>

		<guid isPermaLink="false">http://blog.eamonnfaherty.co.uk/?p=234</guid>
		<description><![CDATA[Background Reading None The Problem? You want to ensure you have only a single instance of an object and want global access to it. This may be because the object uses a resource, such as sounds, or because the object needs a lot of configuration, such as a currency formatter, or needs global access, an [...]


Related posts:<ol><li><a href='http://blog.eamonnfaherty.co.uk/2009/11/10/actionscript3-design-patternsthe-multiton/' rel='bookmark' title='Permanent Link: Actionscript3 Design Patterns:The Multiton'>Actionscript3 Design Patterns:The Multiton</a> <small>Background Reading Read about the singleton The Problem? You want...</small></li>
<li><a href='http://blog.eamonnfaherty.co.uk/2009/11/17/actionscript3-design-patternsfactory-method/' rel='bookmark' title='Permanent Link: Actionscript3 Design Patterns:Factory Method'>Actionscript3 Design Patterns:Factory Method</a> <small>Background Reading The creation method pattern The Problem? You want...</small></li>
<li><a href='http://blog.eamonnfaherty.co.uk/2009/11/11/actionscript3-design-patternscreation-method/' rel='bookmark' title='Permanent Link: Actionscript3 Design Patterns:Creation Method'>Actionscript3 Design Patterns:Creation Method</a> <small>Background Reading The factory method pattern The Problem? You have...</small></li>
</ol>

Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p><strong>Background Reading</strong><br />
None</p>
<p><strong>The Problem?</strong><br />
You want to ensure you have only a single instance of an object and want global access to it.  This may be because the object uses a resource, such as sounds,  or because the object needs a lot of configuration, such as a currency formatter, or needs global access, an event dispatcher.</p>
<p><strong>Examples?</strong><br />
Currency formatter, sound controller, global event dispatcher</p>
<p><strong>How?</strong><br />
The Singleton:</p>
<pre class="brush:as3">package singletonpattern {

	/**
	 * @author eamonn faherty
	 */
	public class SocketManagerSingleton {
		private static var _instace : SocketManagerSingleton;

		public static function getInstace() : SocketManagerSingleton {
			ensureInstanceExists();
			return _instace;
		}

		private static function ensureInstanceExists() : void {
			if (_instace == null) {
				_instace = new SocketManagerSingleton();
			}
		}

		public function send(message : String) : void {
		}
	}
}</pre>
<p>Example Usage:</p>
<pre class="brush:as3">
package singletonpattern {
	import common.Example;		

	/**
	 * @author eamonn faherty
	 */
	public class SingletonPatternExample extends Example {

		public function SingletonPatternExample() {
			var socket : SocketManagerSingleton = SocketManagerSingleton.getInstace();

			socket.send("Hello world");
		}
	}
}</pre>
<p><strong>Why is it good?</strong><br />
‚Ä¢	It works well; if you use an instance protector then you can be sure that only a single instance will exist.<br />
‚Ä¢	It is well understood; it is widely used in the actionscript world.<br />
‚Ä¢	It is easy to understand; it is a simple pattern that needs little extra code.<br />
‚Ä¢	It is easy to implement; actionscript allows us to check the caller of a method and allows us to use private classes.</p>
<p><strong>Why is it bad?</strong><br />
‚Ä¢	It tightly couples the classes that use the singleton to the fact that they are using a singleton.<br />
‚Ä¢	It is not possible to extend the singleton class if you wanted to add functionality.<br />
‚Ä¢	You cannot use your singleton class again; you cannot extend it or reuse it with composition.<br />
‚Ä¢	If you are unit testing a class that uses a singleton you need to add some code to your singleton to make testing possible.</p>
<p><strong>Further Reading</strong><br />
Read about the factory and inversion of control patterns.</p>


<p>Related posts:<ol><li><a href='http://blog.eamonnfaherty.co.uk/2009/11/10/actionscript3-design-patternsthe-multiton/' rel='bookmark' title='Permanent Link: Actionscript3 Design Patterns:The Multiton'>Actionscript3 Design Patterns:The Multiton</a> <small>Background Reading Read about the singleton The Problem? You want...</small></li>
<li><a href='http://blog.eamonnfaherty.co.uk/2009/11/17/actionscript3-design-patternsfactory-method/' rel='bookmark' title='Permanent Link: Actionscript3 Design Patterns:Factory Method'>Actionscript3 Design Patterns:Factory Method</a> <small>Background Reading The creation method pattern The Problem? You want...</small></li>
<li><a href='http://blog.eamonnfaherty.co.uk/2009/11/11/actionscript3-design-patternscreation-method/' rel='bookmark' title='Permanent Link: Actionscript3 Design Patterns:Creation Method'>Actionscript3 Design Patterns:Creation Method</a> <small>Background Reading The factory method pattern The Problem? You have...</small></li>
</ol></p>
<p>Related posts brought to you by <a href='http://mitcho.com/code/yarpp/'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://blog.eamonnfaherty.co.uk/2009/11/09/actionscript3-design-patternsthe-singleton/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
