<?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; multiton</title>
	<atom:link href="http://blog.eamonnfaherty.co.uk/tag/multiton/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 Multiton</title>
		<link>http://blog.eamonnfaherty.co.uk/2009/11/10/actionscript3-design-patternsthe-multiton/</link>
		<comments>http://blog.eamonnfaherty.co.uk/2009/11/10/actionscript3-design-patternsthe-multiton/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 22:27:37 +0000</pubDate>
		<dc:creator>eamonn</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[multiton]]></category>

		<guid isPermaLink="false">http://blog.eamonnfaherty.co.uk/?p=238</guid>
		<description><![CDATA[Background Reading Read about the singleton The Problem? You want to have multiple instances of a singleton and want global access to them all. This may be because the objects use groups of resources, such as different groups of sounds, or because the objects need a lot of configuration, such as currency converters, or need [...]


Related posts:<ol><li><a href='http://blog.eamonnfaherty.co.uk/2009/11/09/actionscript3-design-patternsthe-singleton/' rel='bookmark' title='Permanent Link: Actionscript3 Design Patterns:The Singleton'>Actionscript3 Design Patterns:The Singleton</a> <small>Background Reading None The Problem? You want to ensure you...</small></li>
<li><a href='http://blog.eamonnfaherty.co.uk/2009/10/31/design-patterns-for-actionscript-3/' rel='bookmark' title='Permanent Link: Design Patterns for Actionscript 3'>Design Patterns for Actionscript 3</a> <small>I recently did my first public talk! It was at...</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 />
Read about the singleton</p>
<p><strong>The Problem?</strong><br />
You want to have multiple instances of a singleton and want global access to them all.  This may be because the objects use groups of resources, such as different groups of sounds, or because the objects need a lot of configuration, such as currency converters, or need global access, an event dispatcher.</p>
<p><strong>Examples?</strong><br />
Currency converter, sound controller, global event dispatcher</p>
<p><strong>How?</strong><br />
The Multiton:</p>
<pre class="brush:as3">
public class SocketManagerMultiton {
	private static var _instaces : Dictionary;
	public static const CHAT : String = "CHAT";
	public static const WEATHER : String = "WEATHER";

	public static function getInstace(instance:String) : SocketManagerMultiton {
	ensureInstanceExists(instance);
		return _instaces[instance];
	}

private static function ensureInstanceExists(instance:String) : void {
		if (_instaces == null) {
			_instaces = new Dictionary();
		}
		if (_instaces[instance] == null) {
			_instaces[instance] = new SocketManagerMultiton();
		}
	}

	public function send(message : String) : void {
	}
}
</pre>
<p><strong>Example Use:</strong></p>
<pre class="brush:as3">
var chatSocket : SocketManagerMultiton = SocketManagerMultiton.getInstace(SocketManagerMultiton.CHAT);
chatSocket.send("hello world");

var weatherSocket : SocketManagerMultiton = SocketManagerMultiton.getInstace(SocketManagerMultiton.WEATHER);
weatherSocket.send("HOW_IS_THE_WEATHER");
</pre>
<p><strong>Why is it good?</strong><br />
‚Ä¢	It is easy to understand; it is a simple pattern that needs little extra code.  It builds on the singleton pattern, which is well known.<br />
‚Ä¢	See singleton pattern for other good points.</p>
<p><strong>Why is it bad?</strong><br />
‚Ä¢	It tightly couples the classes that use the multiton to the fact that they are using a multiton.<br />
‚Ä¢	See singleton pattern for other bad points.</p>
<p><strong>Further Reading</strong><br />
Read about the factory and inversion of control patterns. </p>
<p><strong>More</strong><br />
Multitons are a way of grouping Singletons.  They share the benefits and carry the problems that Singletons have.  They are good for classes that use a lot of resources such as socket connections or classes that cache things.  They do make code harder to refactor and very difficult to unit test but if follow the suggestions I made in the Singleton post then you can limit your pain, a bit anyway!  If you are going to use a multiton make sure you use String constants as I did above.  It is easy to make typing mistakes when using String literals.  Also the constants can be meaningful, as they are in my example.</p>


<p>Related posts:<ol><li><a href='http://blog.eamonnfaherty.co.uk/2009/11/09/actionscript3-design-patternsthe-singleton/' rel='bookmark' title='Permanent Link: Actionscript3 Design Patterns:The Singleton'>Actionscript3 Design Patterns:The Singleton</a> <small>Background Reading None The Problem? You want to ensure you...</small></li>
<li><a href='http://blog.eamonnfaherty.co.uk/2009/10/31/design-patterns-for-actionscript-3/' rel='bookmark' title='Permanent Link: Design Patterns for Actionscript 3'>Design Patterns for Actionscript 3</a> <small>I recently did my first public talk! It was at...</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/10/actionscript3-design-patternsthe-multiton/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
