<?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>dndigital.net &#187; Flash</title>
	<atom:link href="http://dndigital.net/category/flash/feed/" rel="self" type="application/rss+xml" />
	<link>http://dndigital.net</link>
	<description>Notes on Adobe Flash, Flex, AIR Development</description>
	<lastBuildDate>Mon, 07 May 2012 15:12:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Advanced Actionscript Tutorial &#8211; The Abstract Factory Design Pattern</title>
		<link>http://dndigital.net/2010/07/advanced-actionscript-tutorial-the-abstract-factory-design-pattern/</link>
		<comments>http://dndigital.net/2010/07/advanced-actionscript-tutorial-the-abstract-factory-design-pattern/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 11:17:58 +0000</pubDate>
		<dc:creator>Nils</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Free Tutorials]]></category>

		<guid isPermaLink="false">http://dndigital.net/?p=180</guid>
		<description><![CDATA[Although it wasn&#8217;t always the case, these days a good understanding of object oriented programming principles and the use of common design patterns are essential in order to become a successful Actionscript developer. When we&#8217;re working on projects, the Head First Design Patterns book is never far away &#8211; and one of the patterns that [...]]]></description>
			<content:encoded><![CDATA[<p>Although it wasn&#8217;t always the case, these days a good understanding of object oriented programming principles and the use of common design patterns are essential in order to become a successful Actionscript developer. When we&#8217;re working on projects, the <a href="http://www.amazon.co.uk/Head-First-Design-Patterns-Freeman/dp/0596007124/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1278608410&amp;sr=8-1" target="_blank">Head First Design Patterns</a> book is never far away &#8211; and one of the patterns that has been incredibly useful time and time again is the Abstract Factory and variations thereof, so I thought we&#8217;d dedicate a YouTube tutorial to it.</p>
<p>I&#8217;d love to hear about your own experiences in regards to object oriented programming in AS3 &#8211; please leave a comment!</p>
<p>The material in this blog post and in the video is an excerpt from our <a href="http://dndigital.net/training/advanced-as3-oop-patterns/">Advanced Actionscript 3 (Object Oriented Programming and Design Patterns) training course</a>.</p>
<p>You can watch the video and read up on the details after the break.</p>
<p><span id="more-180"></span></p>
<h2>Making Choices</h2>
<p>The Abstract Factory Design Pattern is all about choice. Not any kind of choice mind you &#8211; but very specifically choosing which class to instantiate given certain conditions. Whether you realise it or not, you are probably making these choices numerous times in your own programming code. Here are some examples:</p>
<ul>
<li>Which type of menu to display based on the admin privileges of the current user</li>
<li>Which type of skin to apply to your game characters, based on the selections made at the beginning of the game &#8211; and subsequently which types of weapons are available for that character</li>
<li>Which type of button graphic to use based on the size of a banner</li>
<li>Which type of XML parser to use based on the type of feed the user has loaded in</li>
</ul>
<p>and so on. Once you&#8217;re dealing with object oriented programming, most of those choices are going to result in a class being instantiated and the trick is to choose the correct class based on a set of circumstances.</p>
<p>No problem, right? You just whack an if-statement in there:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> user.<span style="color: #006600;">isAdmin</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">menu</span> = <span style="color: #000000; font-weight: bold;">new</span> AdminMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">menu</span> = <span style="color: #000000; font-weight: bold;">new</span> NormalMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h2>Weeds Everywhere</h2>
<p>Of course, using if-statements will work fine, however it spreads the choices throughout your application, like a weed taking a strong-hold in your garden, then your front lawn, then your walls&#8230;you get the picture. Suppose you introduce a third user privilege level? Time to revisit all those many if-statements (of course you&#8217;ll remember where they are!). The more complex your project, the more difficult, time-consuming and error prone such a change becomes.</p>
<h2>Factory Pattern to the Rescue</h2>
<p>It is in these cases that the Factory Design Pattern comes to your rescue, by moving the choice into a single, isolated piece of code. The resulting class that is instantiated &#8211; the factory &#8211; will then provide the application with the correct class instances. As a result, you only need to change the single point-of-choice and create a new factory class in order to make wide-ranging changes to your entire application.</p>
<p>In practice, it looks something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// isolated choice code (eg. after user logs in)</span>
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> user.<span style="color: #006600;">isAdmin</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    factory = <span style="color: #000000; font-weight: bold;">new</span> AdminUserFactory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
    factory = <span style="color: #000000; font-weight: bold;">new</span> NormalUserFactory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// and in the AdminUserFactory class:</span>
<span style="color: #000000; font-weight: bold;">function</span> getMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Menu</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> AdminMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// and in the NormalUserFactory class:</span>
<span style="color: #000000; font-weight: bold;">function</span> getMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Menu</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> NormalMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// much later, in a remote location in your code-tree</span>
<span style="color: #0066CC;">menu</span> = factory.<span style="color: #006600;">getMenu</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Can you see how you would add a third user level &#8211; and how much more manageable it has become compared to the previous example?</p>
<p>Let&#8217;s follow the example code through to its logical conclusion. Say we have 10 user levels. Then we&#8217;ll end up with 10 different factory classes (AdminUserFactory, NormalUserFactory, SuperUserFactory, etc). So far so good. But how does the code that is actually using the factory to retrieve the menu know how to deal with all those 10 different classes without using another if-statement? Surely that will lead to type-errors if we don&#8217;t know the exact data-type?</p>
<h2>Interfacing with the Factory</h2>
<p>In order to allow code throughout your application to use different factory classes, we&#8217;re going to use an interface to define what methods a factory class provides. This interface will be used as the data-type when passing the factory around to other pieces of code. So in practice it doesn&#8217;t matter which factory class is used behind the scenes and dependencies within your code are reduced to a single interface class, which is what we like to see!</p>
<p>Our user-menu example seems to use factory to create its user-interface elements, so let&#8217;s create an interface called IUIFactory. We start with just a single function &#8211; we can always add to it later on:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// in the IUIFactory.as file</span>
<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IUIFactory <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">function</span> getMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Menu</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>As a rule, you&#8217;d start with your interface first, then create your first factory class and then create your choice code &#8211; otherwise you&#8217;ll end up in a bit of a pickle.</p>
<p>Here is the sample code from earlier, with the interface thrown in.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// we start with an interface</span>
<span style="color: #000000; font-weight: bold;">var</span> factory:IUIFactory;
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> user.<span style="color: #006600;">isAdmin</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
    factory = <span style="color: #000000; font-weight: bold;">new</span> AdminUserFactory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
    factory = <span style="color: #000000; font-weight: bold;">new</span> NormalUserFactory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// and in the AdminUserFactory class - which implements IUIFactory</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Menu</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> AdminMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// and in the NormalUserFactory class - which also implements IUIFactory</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Menu</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> NormalMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// much later, in a remote location in your code-tree</span>
<span style="color: #000000; font-weight: bold;">var</span> factory:IUIFactory;
<span style="color: #0066CC;">menu</span> = factory.<span style="color: #006600;">getMenu</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Splendid.</p>
<p>Another beneficial side-effect of using an interface in this way comes into play when adding new features to your factory. Any new feature requires a new method in your factory interface. That new method will immediately result in compiler errors because none of your factory classes has implemented it yet. You have no option but to implement the new method in every single factory class &#8211; hence automatically making sure that your code is properly maintained.</p>
<p>Doesn&#8217;t that sound better than finding a long-lost if-statement deep down in the murky depths of your code-tree that your colleague forgot to change when they added a new user-group?</p>
<h2>YouTube Tutorial and Source Code</h2>
<p>The YouTube tutorial steps through a different example of coding a user-interface that can change depending on the age-group of the viewer.</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="640" height="385" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/spfienbiqqs&amp;hl=en_GB&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="640" height="385" src="http://www.youtube.com/v/spfienbiqqs&amp;hl=en_GB&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p>You can <a title="Factory Design Pattern Source Code" href="http://dndigital.net/wp/wp-content/uploads/2010/07/dndigital-abstract-factory.zip">download the source-code as a FlashBuilder 4 project</a> so that you can follow the tutorial and experiment further. There are a couple of extra images in the assets folder so you can produce a third factory class. Copyright / licensing info is included in each file, all images used are publicly available via <a href="http://sxc.hu" target="_blank">sxc.hu</a>.</p>
<h2>Further Thoughts &#8211; Factory Pattern and Dependency Injection</h2>
<p>The Factory Design Pattern&#8217;s strengths come from isolating choices in a single location. Furthermore, each factory class also encapsulates a very simple set of actions &#8211; it knows how to create the correct instances but doesn&#8217;t require knowledge of the surrounding application to do so &#8211; so we are adhering to the  &#8217;one class &#8211; one responsibility&#8217; concept.</p>
<p>Of course you still need to pass the actual factory instance around to the classes that require them and there are various ways of managing these dependencies. In the YouTube tutorial, I simply pass the factory instance to the view classes via their constructor and this is a perfectly valid way of handling it.</p>
<p>There are of course other options &#8211; and Dependency Injection (DI) is especially helpful when working with the factory pattern.</p>
<p>Since DI hinges in principal around matching interface data-types to concrete classes, switching factory classes becomes a doddle. You simply specify the appropriate factory class to be used when the DI container encounters the factory interface and your project can be dramatically changed by essentially touching only a single line of code. This is especially true when there is a fixed, pre-determined choice of factory class, for example when repurposing an entire project for a different market or target audience.</p>
<p>I&#8217;d love to hear your own experiences of the Factory Pattern and OOP in general &#8211; leave a note in the comments below.</p>
]]></content:encoded>
			<wfw:commentRss>http://dndigital.net/2010/07/advanced-actionscript-tutorial-the-abstract-factory-design-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using XML with Actionscript 3 &#8211; Tutorial now on YouTube</title>
		<link>http://dndigital.net/2010/07/using-xml-with-actionscript-3-tutorial-now-on-youtube/</link>
		<comments>http://dndigital.net/2010/07/using-xml-with-actionscript-3-tutorial-now-on-youtube/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 18:24:20 +0000</pubDate>
		<dc:creator>Nils</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Free Tutorials]]></category>

		<guid isPermaLink="false">http://dndigital.net/?p=189</guid>
		<description><![CDATA[Are you uncertain of how to use XML data in Actionscript 3 and Flash? If so, you&#8217;re in luck because we&#8217;ve just uploaded a brand new Flash tutorial to YouTube. It comes in two parts and guides you through how to load and use XML data in Actionscript 3. In particular, the tutorial works with [...]]]></description>
			<content:encoded><![CDATA[<p>Are you uncertain of how to use XML data in Actionscript 3 and Flash? If so, you&#8217;re in luck because we&#8217;ve just uploaded a brand new Flash tutorial to YouTube. It comes in two parts and guides you through how to load and use XML data in Actionscript 3. In particular, the tutorial works with an RSS feed &#8211; but of course the techniques apply to all types of XML data.</p>
<p>Here are some of the points covered:</p>
<ul>
<li>loading an XML file in Actionscript 3</li>
<li>extracting particular data from the XML file</li>
<li>extracting lists of tags from the XML file</li>
<li>working with namespaces</li>
<li>using filtering expressions</li>
</ul>
<p>It&#8217;s all pretty straightforward once you&#8217;ve mastered the technique &#8211; come and take a look.</p>
<p>View the videos after the break &#8211; or simply <a title="DN Digital YouTube Channel" href="http://youtube.com/dndigital" target="_blank">visit our YouTube channel</a>.</p>
<p><span id="more-189"></span>The tutorial comes in 3 parts, it&#8217;s probably best to watch them back to back. Total viewing time comes to around 14 minutes.</p>
<p>The videos are available in full 720p HD format on YouTube &#8211; link underneath each embedded video.</p>
<p>Enjoy!</p>
<p><object width="579" height="351"><param name="movie" value="http://www.youtube.com/v/t49NRfwpxl8&amp;hl=en_US&amp;fs=1?rel=0&amp;hd=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/t49NRfwpxl8&amp;hl=en_US&amp;fs=1?rel=0&amp;hd=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="579" height="351"></embed></object></p>
<p><a href="http://www.youtube.com/watch?v=t49NRfwpxl8">Part 1 &#8211; Available in HD on YouTube</a></p>
<p><object width="580" height="351"><param name="movie" value="http://www.youtube.com/v/R-Ub5ZqGpzk&amp;hl=en_US&amp;fs=1?rel=0&amp;hd=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/R-Ub5ZqGpzk&amp;hl=en_US&amp;fs=1?rel=0&amp;hd=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="580" height="351"></embed></object></p>
<p><a href="http://www.youtube.com/watch?v=R-Ub5ZqGpzk">Part 2 &#8211; Available in HD on YouTube</a></p>
<p>Enjoy &#8211; and please let us know in the comments what you&#8217;d like to learn next!</p>
]]></content:encoded>
			<wfw:commentRss>http://dndigital.net/2010/07/using-xml-with-actionscript-3-tutorial-now-on-youtube/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Intel GameOn video/image gallery now live</title>
		<link>http://dndigital.net/2008/08/intel-gameon-videoimage-gallery-now-live/</link>
		<comments>http://dndigital.net/2008/08/intel-gameon-videoimage-gallery-now-live/#comments</comments>
		<pubDate>Fri, 15 Aug 2008 15:21:23 +0000</pubDate>
		<dc:creator>Nils</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Portfolio]]></category>

		<guid isPermaLink="false">http://dndigital.net/blog/?p=21</guid>
		<description><![CDATA[The newly developed Intel GameOn site has now gone live &#8211; and with it the two Flash components that I developed for it. Both components are used extensively throughout the site to show pictures and videos related to particular games, articles and other content. Read further details on the updated portfolio page.]]></description>
			<content:encoded><![CDATA[<p>The newly developed Intel GameOn site has now gone live &#8211; and with it the two Flash components that I developed for it.</p>
<p>Both components are used extensively throughout the site to show pictures and videos related to particular games, articles and other content.</p>
<p>Read further details on the updated <a title="Nils Millahn - Portfolio" href="http://portfolio.dndigital.net/">portfolio page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://dndigital.net/2008/08/intel-gameon-videoimage-gallery-now-live/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Crossdomain Flex and Flash SOAP Webservice access</title>
		<link>http://dndigital.net/2008/08/crossdomain-flex-and-flash-soap-webservice-access/</link>
		<comments>http://dndigital.net/2008/08/crossdomain-flex-and-flash-soap-webservice-access/#comments</comments>
		<pubDate>Mon, 11 Aug 2008 12:00:06 +0000</pubDate>
		<dc:creator>Nils</dc:creator>
				<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://dndigital.net/blog/?p=9</guid>
		<description><![CDATA[Here&#8217;s a quick guide on how to get crossdomain access working for SOAP Webservices from Flash and Flex. Obviously you need to allow access to the domain, however it&#8217;s more difficult to realise that you also need to allow the SOAP headers to be sent to the foreign server. Here is an example crossdomain file [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a quick guide on how to get crossdomain access working for SOAP Webservices from Flash and Flex. Obviously you need to allow access to the domain, however it&#8217;s more difficult to realise that you also need to allow the SOAP headers to be sent to the foreign server.</p>
<p>Here is an example crossdomain file that does just that &#8211; amend to suit your own requirements, the * wildcard is probably not what you need for your production server.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">&nbsp;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://dndigital.net/2008/08/crossdomain-flex-and-flash-soap-webservice-access/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

