<?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; Factory Method</title>
	<atom:link href="http://www.as3dp.com/category/design-patterns/factory-method/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>ActionScript 3.0 Design Patterns and iOS:  A New Home for Apps</title>
		<link>http://www.as3dp.com/2011/08/actionscript-3-0-design-patterns-and-ios-a-new-home-for-apps/</link>
		<comments>http://www.as3dp.com/2011/08/actionscript-3-0-design-patterns-and-ios-a-new-home-for-apps/#comments</comments>
		<pubDate>Mon, 15 Aug 2011 13:42:26 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Decorator]]></category>
		<category><![CDATA[Factory Method]]></category>
		<category><![CDATA[Mobile]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6123</guid>
		<description><![CDATA[It&#8217;s Easy Once You Get Your Certificate and Provisioning Profile Having finally got my magical combination of permissions set up just so, the process began coming fairly easily. The problem is not Adobe, but rather getting the files from Apple just right. Once you get the files, you can use them over an over. One [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6124" class="wp-caption alignleft" style="width: 330px"><img src="http://www.as3dp.com/wp-content/uploads/2011/08/apps.PNG" alt="Make Your Own Apps with Design Patterns" title="apps" width="320" height="480" class="size-full wp-image-6124" /><p class="wp-caption-text">Make Your Own Apps with Design Patterns</p></div><strong>It&#8217;s Easy Once You Get Your  Certificate and Provisioning Profile</strong></p>
<p>Having finally got my magical combination of permissions set up just so, the process began coming fairly easily. The problem is not Adobe, but rather getting the files from Apple just right. Once you get the files, you can use them over an over. One is a certificate file with a .p12 extension, and the other is a provisioning file. It looks like a lot of people are having the same problem because when I finally found a <a href="http://stackoverflow.com/questions/999313/iphone-app-signing-a-valid-signing-identity-matching-this-profile-could-not-be-f">solution on the Web at StackOverflow</a> several other developers indicated (with heart-felt thanks) that they were encountering the same problem. (The solution that worked for me was to start over and delete all provisioning profiles and login keychain.)  So if you got your Apple iOS Developer ticket ($99 or free to University students/faculty), that&#8217;s just the first part. The next part is to hook up your iOS device (mine is a 3S iPhone) and using the Xcode development tools, go through a process that generates the files you need. The following image shows what these little gems look like. If you put those two files in with your AIR iOS files, it&#8217;s a piece of cake. (Go ahead and download the files as well.) <div id="attachment_6127" class="wp-caption alignleft" style="width: 259px"><img src="http://www.as3dp.com/wp-content/uploads/2011/08/FigA.png" alt="Same files can be used with different apps" title="FigA" width="249" height="102" class="size-full wp-image-6127" /><p class="wp-caption-text">Same files can be used with different apps</p></div><br />
<a href="http://www.mwd.hartford.edu/mwd11/downloads/MWDdownloads.php" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a></p>
<p><a href="http://developer.apple.com/devcenter/ios/index.action">Apple provides videos and a whole slew of tools and aids</a>, and if you follow the instructions, you should be able to develop the right files. (Sad experience had led me to suggest that you take your time and follow instructions carefully.) Anyway, whether you get the certificate and provisioning files on the first time or whether you struggle like I did, once you&#8217;ve got them; they&#8217;re good for about four months. You can develop on either a Mac or Windows PC and re-use the same files on different apps. (I haven&#8217;t gone through the process that Apple has for getting an app in the App Store; so you&#8217;re on your own in that department.)  The image on the right is a screenshot taken with an iPhone:<div id="attachment_6134" class="wp-caption alignright" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/08/cartoonVG.png" alt="Animated tween, text and graphic" title="cartoonVG" width="250" height="375" class="size-full wp-image-6134" /><p class="wp-caption-text">Animated tween, text and graphic</p></div></p>
<p><strong>Step-By-Step</strong></p>
<p>This section walks you through the process of working with a Flash Pro project set up for use with iOS. In the previous post on iOS, the sample project was to create banners that can be used in a Factory Method context by loading external .SWF files. On the iPhone, that didn&#8217;t work.  The problem seemed to be the mobile device&#8217;s inability to grab the external .SWF files even though, the .SWF files were included in the General settings for the Included files window. So, using the same tweens, I created MovieClips and put them in the Flash Pro library and then set them up as classes. Then in the concrete product files, I just instantiated classes with the tweens instead of loading external .SWF files, and the rest of the program was unchanged. (That is where design patterns really shine—easy to make changes.)</p>
<p>Okay, now to step through the process. This was all done using AIR 3.0, Flash Player 11 public betas on a iMac running the new Lion OS.  After you open a new AIR for iOS and write all of your code and add any graphics and text, the first step is to select the FLA file, make sure that nothing is selected on the stage and then click the little wrench icon next to the Player selection where it should indicate &#8216;AIR for iOS.&#8217; That will open the AIR for iOS Settings window as shown in Figure 1:<br />
<div id="attachment_6142" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/08/Fig1.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; General Settings of AIR for iOS&lt;/em&gt;" title="Fig1" width="500" height="694" class="size-full wp-image-6142" /><p class="wp-caption-text"><em><strong>Figure 1:</strong> General Settings of AIR for iOS</em></p></div></p>
<p>For getting started, choose Full screen instead of Auto orientation. Use the CPU for the Rendering processor, specify your device and choose the Standard resolution. The included files will be an .SWF and .XML file. Leave those alone, and for this app, you do not need to add any others.</p>
<p>For this next step, you&#8217;ll need both the certificate and provisioning files in the same folder with the other class files.  So if you haven&#8217;t created those files and linked them to the device you&#8217;re testing it on; do so now. The click the Deployment button next to the General button at the top of the AIR for iOS Settings window. Figure 2 shows the deployment settings:<br />
<div id="attachment_6155" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/08/Fig2.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2:&lt;/strong&gt; Deployment window&lt;/em&gt;" title="Fig2" width="500" height="694" class="size-full wp-image-6155" /><p class="wp-caption-text"><em><strong>Figure 2:</strong> Deployment window</em></p></div></p>
<p>The password for the certificate is the one you entered when you created the certificate—not one associated with your computer. Use the search folder to find your Provisioning profile file.  The App ID is the name you gave your initial FLA file—I&#8217;m in the habit of calling all of mine &#8220;Client,&#8221; but now I&#8217;ve got to think about what users will see on their mobile device..</p>
<p>This post does not get into the iOS deployment beyond the local development stage. So, select the &#8220;Quick publishing for device testing&#8221; or &#8220;Quick publishing for device debugging.&#8221; I selected the former because I chose to do the debugging using the standard Flash tools. After all I went through to get the certificate and mobile provision files to work right, I wasn&#8217;t about to tempt fate just yet and do more than the minimum.</p>
<p>This final step is fun. You can easily create your own iOS icon. Here&#8217;s what you need to do to get started:</p>
<ol>
<li>Create a square image with your logo or some symbol  you want to use to identify you application. Save it as a .PNG file. (In the image at the top of this post, you can see the two application icons using the Sandlight logo.) Be sure that the image has equal width and height.</li>
<li>Save three images in sizes 29 pixel square, 57 pixel square and 512 pixel square. Depending on the mobile device, you may want to create other sizes as well.  I started off with the Adobe Illustrator version and saved each size as a PNG file.</li>
<li>Place the three image files in a folder named &#8220;icons&#8221; and place the icons folder in the directory where you are creating your mobile app.</li>
</ol>
<p>In Figure 3, you can see the preview window showing the image used for the application described in this post.</p>
<div id="attachment_6158" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/08/Fig3.png" alt="&lt;em&gt;&lt;strong&gt;Figure 3:&lt;/strong&gt; Assigning icons for app image</em>&#8221; title=&#8221;Fig3&#8243; width=&#8221;500&#8243; height=&#8221;694&#8243; class=&#8221;size-full wp-image-6158&#8243; /><p class="wp-caption-text"><em><strong>Figure 3:</strong> Assigning icons for app image</strong></p></div>
<p></em>Be sure to click each icon size for each of the three sizes and load the URL (icons/iconXX.png) before you click OK. Note also that two of the icons are settings for iPads, and so if you have an iPad, create image files for those as well.</p>
<p><strong>Installing your iOS on your Mobile Device</strong></p>
<p>Now you&#8217;re set to Publish your files required for an iOS mobile app. When you click &#8220;Publish&#8221; you will find that it takes a while to process everything. Once you&#8217;re finished, you will see all the files generated for your iOS device. Figure 4 shows the files you will see—including the all-important .IPA file.<br />
<div id="attachment_6165" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/08/folder.png" alt="&lt;em&gt;&lt;strong&gt;Figure 4:&lt;/strong&gt; Files used and generated&lt;/em&gt;" title="folder" width="500" height="305" class="size-full wp-image-6165" /><p class="wp-caption-text"><em><strong>Figure 4:</strong> Files used and generated</em></p></div></p>
<p>As you can see in Figure 4, one of the files is named <strong>BannerFactory.ipa</strong>. That is the file that will install your app onto your iOS mobile device. Connect your iPhone or iPad or iWhatever that runs on iOS, and double-click the IPA file and it will upload your app into the iTunes Apps folder.  (On your iTunes, click <strong>Apps</strong> in the LIBRARY folder, and you should see your new application as shown in Figure 5.)</p>
<div id="attachment_6167" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/08/Fig5.png" alt="&lt;em&gt;&lt;strong&gt;Figure 5:&lt;/strong&gt; iTunes Apps folder&lt;/em&gt;" title="Fig5" width="500" height="252" class="size-full wp-image-6167" /><p class="wp-caption-text"><em><strong>Figure 5:</strong> iTunes Apps folder</em></p></div>
<p>If you see your application icon and file, you&#8217;ve been successful—up to this point. The last step is to install your app onto your mobile device. The final step is to select your device and then check your application Sync. Figure 6 shows  iTunes with your application selected (checkbox) and ready for Sync.</p>
<div id="attachment_6169" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/08/Fig6.png" alt="&lt;em&gt;&lt;strong&gt;Figure 6&lt;/strong&gt;App set for installing in iPhone&lt;/em&gt;" title="Fig6" width="500" height="252" class="size-full wp-image-6169" /><p class="wp-caption-text"><em><strong>Figure 6:</strong> App set for installing in iPhone</em></p></div>
<p>Note in Figure 6 that the iOS device is selected under DEVICES in the left panel and that the new application (BannerFactory) has a check next to it. Click the Sync button (or Apply and then Sync) and if you have your device properly identified, you will see your icon and app on your mobile device.</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%2F2011%2F08%2Factionscript-3-0-design-patterns-and-ios-a-new-home-for-apps%2F&amp;title=ActionScript%203.0%20Design%20Patterns%20and%20iOS%3A%20%20A%20New%20Home%20for%20Apps" 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/2011/08/actionscript-3-0-design-patterns-and-ios-a-new-home-for-apps/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Mobile Banner Factory for iOS</title>
		<link>http://www.as3dp.com/2011/08/mobile-banner-factory-for-ios/</link>
		<comments>http://www.as3dp.com/2011/08/mobile-banner-factory-for-ios/#comments</comments>
		<pubDate>Mon, 08 Aug 2011 01:00:25 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Factory Method]]></category>
		<category><![CDATA[Mobile]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6100</guid>
		<description><![CDATA[Surprise your Users! I&#8217;ve never been a big animated banner fan. They are contrary to everything I know about good Information Design. However, done right, they can add pizazz to a page. The trick is to make sure that after the banner has performed its show once to stop it. Like a twice told joke; [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6099" class="wp-caption alignleft" style="width: 259px"><img src="http://www.as3dp.com/wp-content/uploads/2011/08/iphone1.png" alt="Animated Banners are easy with Flash" title="iphone1" width="249" height="475" class="size-full wp-image-6099" /><p class="wp-caption-text">Animated Banners are easy with Flash</p></div><strong>Surprise your Users!</strong></p>
<p>I&#8217;ve never been a big animated banner fan. They are contrary to everything I know about good <strong>Information Design</strong>. However, done right, they can add pizazz to a page. The trick is to make sure that after the banner has performed its show <em>once</em> to stop it. Like a twice told joke; the animation is only interesting the first time. After that, the banner needs to serve as a branding decoration. Further, you can make your opening page a bit more interesting by having multiple banners randomly appear. Every time the visitor opens the mobile app, he/she can look forward to seeing a different banner appear. Using a <strong>Factory Method</strong> design pattern, that can be easy to create and (more importantly) update. In this post I want to get set up with a simple (but handy) <strong>Factory Method</strong> design pattern for calling up different banners; so it can be regarded as a beginner&#8217;s introduction to the <strong>Factory Method</strong> pattern.</p>
<p>The plan is to walk through an iOS implementation of ActionScript 3.0 in both Flash Pro and Flash Builder. However, first I want to get the design pattern squared away, and then we can provide total focus on the iOS issues. Further, if you would rather create an app using Android or Blackberry, you can just use the code and design pattern is this post and forget about the subsequent iOS posts that use the design pattern shown in this post. I did not provide the downloads for this post because I used a generic ActionScript 3.0 implementation, and so you can just cut and paste the code in the listings that are part of this post. (The downloads for the iOS implementations will be provided in subsequent posts.) To get an idea of what this does, click the Play button and refresh the HTML page to see how one of two animated banners appear.<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/dp/bannerFactory/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignnone size-full wp-image-1257" /></a></p>
<p><strong>The Factory Method in an iPhone</strong></p>
<p>Here, I want to use the Factory Method, but I&#8217;m not going to explain it (again.) That&#8217;s been done <a href="http://www.as3dp.com/2011/05/31/beginning-actionscript-3-0-abstraction-the-factory-method-at-work/" target="_blank">here</a> in our blog and in Chapter 2 in our book. Here I just want to explain how it is used to generate random banners for an iPhone (or whatever other mobile or non-mobile device you want to use it with.)  The factory (Creator) instantiates any number of concrete Product instances. As implemented in this example, each concrete product requires a concrete factory instance.  I added a helper class, <strong>RandomSelector</strong> to generate random numbers in a range.  The Client pulls in a random value (between 0 and 1) and then using a <strong>switch</strong> statement calls a method to request a SWF file to play an animated banner. (Those among you who want to create your own SWF banner and pull it up instead of the ones you see in the example can do so easily.) Figure 1 shows the File Diagram of the Factory Method as used in this implementation to provide an overview.<br />
<div id="attachment_6112" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/08/banFac.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; File Diagram of Factory Method Implementation&lt;/em&gt;" title="banFac" width="500" height="419" class="size-full wp-image-6112" /><p class="wp-caption-text"><em><strong>Figure 1:</strong> File Diagram of Factory Method Implementation</em></p></div> As you can see in Figure 1, the Client makes the requests through the ICreator (factory) interface, the concrete factories instantiate the concrete products, which load the SWF files for display. The RandomSelector class sits off by the side providing a random value.<br />
<span id="more-6100"></span><br />
Given that arrangement, it&#8217;s easy to add as many new concrete products with different animated banners as you want. Each concrete product in this design requires a concrete creator (factory), but that&#8217;s pretty simple, and you don&#8217;t have to worry about the program crashing down around your ears because you made changes. Beginning with the Client code, the request is through the ICreator interface, picking up the random number through the helper class:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6100code5'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p61005"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
</pre></td><td class="code" id="p6100code5"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> banner:ICreator;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> rs:RandomSelector=<span style="color: #000000; font-weight: bold;">new</span> RandomSelector<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> choose:uint;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> tucker:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">/*Add more banners
			Change the first argument of randomChoose()
			to the total number of banners minus 1.  */</span>
&nbsp;
			choose=uint<span style="color: #66cc66;">&#40;</span>rs.<span style="color: #006600;">randomChoose</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span>choose<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">0</span>:
				doCartoon<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #b1b100;">break</span>;
&nbsp;
				<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span>:
				doLogo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #b1b100;">break</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> doCartoon<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			banner=<span style="color: #000000; font-weight: bold;">new</span> MakeCartoonBanner<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			tucker.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>banner.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			tucker.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">25</span>; tucker.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">113</span>;
			addChild<span style="color: #66cc66;">&#40;</span>tucker<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> doLogo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			banner=<span style="color: #000000; font-weight: bold;">new</span> MakeLogoBanner<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			tucker.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>banner.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			tucker.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">25</span>; tucker.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">113</span>;
			addChild<span style="color: #66cc66;">&#40;</span>tucker<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The one change made to the Client that will not be in the final version is the addition of the <strong>tucker</strong> Sprite object. The <strong>tucker</strong> object was added to position the display in the middle of the graphic iPhone that you saw when you clicked the Play button. In the iPhone implementation, it will use the 0,0 position and not need a positioning sprite.</p>
<p>The class for the random selections is a standard algorithm for generating ranged values. It could have easily gone into the Client class, but it has value for re-use and so is cast into a class.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6100code6'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p61006"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p6100code6"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RandomSelector
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> randomChoose<span style="color: #66cc66;">&#40;</span>top:<span style="color: #0066CC;">Number</span>, bottom:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">floor</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">Math</span>.<span style="color: #0066CC;">random</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>+top-bottom<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>+bottom;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The ICreator (Factory) interface and concrete factories instantiate their respective products. If you look at the Client, you will see that the banner object is typed as an instance of ICreator, and so it can be used with any of the concrete factories. For beginners, this is why <em>programming to the interface instead of the implementation</em> is important—it only commits to the interface allowing for loose binding and more flexibility.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6100code7'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p61007"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
</pre></td><td class="code" id="p6100code7"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Factory interface</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> ICreator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Concrete factory for Cartoon Banner</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MakeCartoonBanner <span style="color: #0066CC;">implements</span> ICreator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> logoNow:IProduct=<span style="color: #000000; font-weight: bold;">new</span> CartoonBanner<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> logoNow.<span style="color: #006600;">moBanner</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Concrete factory for Logo Banner</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MakeLogoBanner <span style="color: #0066CC;">implements</span> ICreator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> logoNow:IProduct=<span style="color: #000000; font-weight: bold;">new</span> LogoBanner<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> logoNow.<span style="color: #006600;">moBanner</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Notice in the code for the concrete factories that the product objects are programmed to the IProduct interface rather than the concrete ones. So, both the Client class and concrete factory classes are typing variables to the interface instead of the implementations that they instantiate. As a fundamental principle to promote loose coupling, it is the first and most important design pattern principle to get into the habit of using.</p>
<p>Finally, the Product Interface and classes are pretty standard load and return objects. Each concrete product loads up an SWF file and sends the results to the Client. Instead of a SWF file, the same code would work with a graphic file as well. The IProduct interface has a single method, <strong>moBanner()</strong> that returns a Sprite. (&#8216;moBanner&#8217; refers to&#8217; Mobile Banner&#8217;, but sounds more interesting.)</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6100code8'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p61008"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
</pre></td><td class="code" id="p6100code8"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Product Interface</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">interface</span> IProduct
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> moBanner<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Concrete Product--Cartoon Banner</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CartoonBanner <span style="color: #0066CC;">implements</span> IProduct
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loader:Loader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> req:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> transporter:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> moBanner<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			req = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;CartoonFace.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
			loader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>req<span style="color: #66cc66;">&#41;</span>;
			transporter.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>loader<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> transporter;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Concrete Product--Logo Banner</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LogoBanner <span style="color: #0066CC;">implements</span> IProduct
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loader:Loader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> req:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> transporter:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> moBanner<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			req = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;LogoGrow.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
			loader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>req<span style="color: #66cc66;">&#41;</span>;
			transporter.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>loader<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> transporter;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In looking at the code, there seems to be a lot of code for some pretty simple operations. I considered adding a  string parameter to the factory method to call the required SWF file through a product. I could conceivably create a program with one concrete factory and one concrete product with all of the differences handled through the parameter. However, this design is a nice simple tool to use in working with an iOS design and having multiple concrete factories and products seems to be a better test of using a design pattern with iOS.</p>
<p><strong>Preparation for the iOS Building</strong></p>
<p>This post is designed to give you some time to familiarize yourself with the design pattern prior to putting it into an iOS application. To prepare for the iOS application, you have two routes:</p>
<ol>
<li>Go to http://developer.apple.com/programs/ios/ and cough up $99/year to enroll in the iOS developer program.</li>
<li>Go to http://developer.apple.com/programs/ios/university/ and sign up for the iOS Developer University Program for <strong>free</strong>. This requires someone in your major/department (usually the dept chair) to sign off on it. It can be handled easily via email and constant whining.</li>
</ol>
<p>If you don&#8217;t have an iOS Developer ticket, there&#8217;s not much point in even trying to get an Apple mobile app working.  However, if you are considering the program but not want to fork over $99 just yet, you can wait until the next post to see how it&#8217;s done and whether it&#8217;s worth it.</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%2F2011%2F08%2Fmobile-banner-factory-for-ios%2F&amp;title=Mobile%20Banner%20Factory%20for%20iOS" 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/2011/08/mobile-banner-factory-for-ios/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Beginning ActionScript 3.0 Abstraction : The Factory Method at Work</title>
		<link>http://www.as3dp.com/2011/05/beginning-actionscript-3-0-abstraction-the-factory-method-at-work/</link>
		<comments>http://www.as3dp.com/2011/05/beginning-actionscript-3-0-abstraction-the-factory-method-at-work/#comments</comments>
		<pubDate>Tue, 31 May 2011 10:35:42 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Abstract Classes]]></category>
		<category><![CDATA[Beginner's Guide]]></category>
		<category><![CDATA[Factory Method]]></category>
		<category><![CDATA[Learning Design Patterns]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=5638</guid>
		<description><![CDATA[Join the Campaign to Stamp Out Ugly! Gentle Readers: When you click the Play button in this post, the UI that appears isn&#8217;t very pretty. That&#8217;s because I have no graphic design skills. By changing the requests in the Client class, you could easily make a much nicer UI. Why not give it a shot [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.as3dp.com/wp-content/uploads/2011/05/abstraction1.png" alt="abstraction" title="abstraction" width="250" height="251" class="alignleft size-full wp-image-5639" /><em><strong>Join the Campaign to Stamp Out Ugly!</strong></em></p>
<p><em><strong>Gentle Readers:</strong> When you click the Play button in this post, the UI that appears isn&#8217;t very pretty. That&#8217;s because I have no graphic design skills. By changing the requests in the Client class, you could easily make a much nicer UI. Why not give it a shot and send us your revised Client request set? You can even add new Concrete Creators and Products. Send in your refinements to our comment section. Either that or click Play and see Ugly!</em></p>
<p><strong>The Concreteness of Abstraction</strong></p>
<p>The first time I went to London, I picked up a Tube Map (Map of the London Underground) and was able to get where I wanted to go. The map of London&#8217;s tube is a masterpiece of clarity and abstraction. Based on electrical circuitry, it&#8217;s very easy to find one&#8217;s way around London even on a first visit. Likewise, Paris&#8217; Metro has an almost identical type of map for it&#8217;s underground system, and it too is easy to find where you want to go. The secret is having just enough detail to use the system and not so much detail to make its use confusing. Further the coding is clear: different colors depict different named lines. Bakerloo is brown, Circle is yellow, Central is red and District is green. At a glance you see that arriving at Heathrow Airport, you take the Piccadilly line into town (purple) and switch lines wherever you see a white circle off the Piccadilly line. The details are not provided, and the Thames river does not run along the neat geometrical angles shown on the Tube Map. It&#8217;s just a reference point and lets you know whether you&#8217;re north or south of the Thames. The river&#8217;s or tube&#8217;s exact shape doesn&#8217;t matter; the whole thing is an abstraction of London&#8217;s subway system and a major feature of the city. The details would just get in the way of clarity.</p>
<p>Abstraction in OOP and Design Patterns works the same way. An object only exposes as much information as the client (requesting object) requires. The perfect abstract structure in a design pattern is an Interface because it is nothing but abstract methods. The only way to really appreciate abstraction is to see it in action; so this post concentrates on demonstrating abstractions in a fundamental design pattern—the Factory Method.</p>
<p><strong>Two Interfaces and some Concrete Classes</strong></p>
<p>The Factory Method requires a factory interface called Creator and a &#8220;product&#8221; interface called Product. The Creator interface provides a factory method function to create concrete instances of products through the abstract Product interface. In this implementation of the Factory Method Design Pattern, both abstract participants are interfaces (Creator and Product). Figure 1 shows the relationship between the participants in the design pattern:<br />
 <div id="attachment_5646" class="wp-caption alignnone" style="width: 495px"><img src="http://www.as3dp.com/wp-content/uploads/2011/05/FactoryMethodDP.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1: &lt;/strong&gt; Factory Method Design Pattern&lt;/em&gt;" title="FactoryMethodDP" width="485" height="193" class="size-full wp-image-5646" /><p class="wp-caption-text"><em><strong>Figure 1: </strong> Factory Method Design Pattern</em></p></div></p>
<p>In a step-through of the pattern you see the following:</p>
<ol>
<li>Client wants a certain product. Makes <a href="http://www.as3dp.com/2010/01/16/actionsscript-3-0-design-pattern-relations-part-i-acquaintances/">request</a> through the Creator (factory)</li>
<li>Concrete creator specifies the concrete  product and <a href="http://www.as3dp.com/2010/02/18/actionsscript-3-0-design-pattern-relations-part-iv-creation/">instantiates an instance</a>, returning it to the Client</li>
</ol>
<p>It&#8217;s a very straightforward pattern, but often beginners will puzzle over why all the work simply to get a class instance? The answer to that question lies more in the larger implementations and re-use of the pattern than it does in the small example used here. However, a good general answer is that it loosely binds the product to the request. If you change the product&#8217;s contents, you should not have to change anything else in the pattern. The Client makes the request through an abstraction and really doesn&#8217;t care about the details of the product. If the product is changed, the exact same request brings up a different object. However, nothing has to be changed to access the new object. Use the buttons below to download and play the example:<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/dp/beginners/b2/FactoryMethAb.zip" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignleft size-full wp-image-2020" /></a><a href="http://nemo.mwd.hartford.edu/~wsanders/dp/beginners/b2/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignnone size-full wp-image-1257" /></a></p>
<p>Chapter 2 in our book, and <a href="http://www.as3dp.com/category/design-patterns/factory-method/">other posts on this blog</a> go into detail about the Factory Method. Here the focus is on <em>abstraction</em>, and the Factory Method is merely used to illustrate the power of abstraction. In order to show the range of products that can be generated from the same abstract interfaces, I used both component UIs and graphic background Shape objects as products.</p>
<p><strong>Using Abstractions and Programming to the Interface</strong></p>
<p>One of the key principles of design pattern programming is,</p>
<blockquote><p>Program to the Interface and not the implementation&#8230;.</p></blockquote>
<p>In order to see both how abstractions are used  and  how relations work in the n the Factory Method pattern, Figure 2 shows code snippets related to each of the participants (classes and relationships).<br />
<div id="attachment_5664" class="wp-caption alignnone" style="width: 495px"><img src="http://www.as3dp.com/wp-content/uploads/2011/05/FactoryMethodCode.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2: &lt;/strong&gt; Coded Elements of the Factory Method pattern&lt;/em&gt;" title="FactoryMethodCode" width="485" height="400" class="size-full wp-image-5664" /><p class="wp-caption-text"><em><strong>Figure 2: </strong> Coded Elements of the Factory Method pattern</em></p></div><br />
In looking at Figure 2 code snippets, notice how many times that Creator or Product are used. Both reflect the wholly abstract interfaces in the program. By making references to the interfaces (or abstract classes), the binding between the Client and the requests is very loose. By having loose binding through abstractions, the developer can better update and change the program. The only part of the objects exposed are those that are required. If the concrete products change, the Client is not tightly bound to the concrete elements because the requests hold references to the abstract interfaces and not the concrete participants. (Read on to see how these are all put together!)<br />
<span id="more-5638"></span></p>
<p><strong>The Client, Creator and Product</strong></p>
<p>This section shows the code for all of the participants. (You can download it all if you&#8217;d like to follow this post using Flash or Flash Builder by clicking the Download button above.) I&#8217;d like to start with the Client and then examine the abstract interfaces. We&#8217;ll start with the Client:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p5638code14'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p563814"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code" id="p5638code14"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bkdp:Creator=<span style="color: #000000; font-weight: bold;">new</span> MakeBackdrop<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btn1:Creator=<span style="color: #000000; font-weight: bold;">new</span> MakeButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btn2:Creator=<span style="color: #000000; font-weight: bold;">new</span> MakeButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtA:Creator=<span style="color: #000000; font-weight: bold;">new</span> MakeTextArea<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtI:Creator=<span style="color: #000000; font-weight: bold;">new</span> MakeTextInput<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			addChild<span style="color: #66cc66;">&#40;</span>bkdp.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">12</span>,<span style="color: #cc66cc;">15</span>,<span style="color: #ff0000;">&quot;0xcc0000&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btn1.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">30</span>,<span style="color: #ff0000;">&quot;Top&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btn2.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">60</span>,<span style="color: #ff0000;">&quot;Bottom&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>txtA.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">130</span>,<span style="color: #cc66cc;">30</span>,<span style="color: #ff0000;">&quot;Lots of text can go here!&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>txtI.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">100</span>,<span style="color: #ff0000;">&quot;Input here&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In looking at the Client, you can see that all requests are through the Creator interface. Because all of the concrete creators (factories) implement the Creator, the objects can be typed to the interface and not the concrete factories. Note that while the Client is requesting concrete Products, products are not even mentioned once. The <strong>factoryMethod()</strong> takes care of making the request that delivers the product. Think of it as requesting a cup of coffee from a barista. You put in your order for a <em>Café Latte Uruguay Negro</em> (<em>my coffee house is more pretentious than yours</em>). You don&#8217;t care about how the barista makes the cup of coffee you ordered; you just know to ask him for the specific product you want, and he&#8217;ll take care of it. The Client is doing the same thing.</p>
<p>The Creator (factory) interface is simple because it is abstract. You provide the framework, and the implementations will take care of the concrete elements.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p5638code15'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p563815"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p5638code15"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">interface</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,why:<span style="color: #0066CC;">Number</span>,lbl:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you will see in the concrete implementations of the Creator, you can do a lot of different things with the three parameters.</p>
<p>Next, the Product interface looks very similar to the Creator. In fact, other than different names for the interface and method, they&#8217;re the same. The don&#8217;t have to be, but in this case they just happen to be. However, as you will see, the implementations of each are different individually and as a group.s</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p5638code16'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p563816"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p5638code16"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">interface</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> uiProduct<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,why:<span style="color: #0066CC;">Number</span>,lbl:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Going back to the example of the London Tube Map, you can see that the abstractions have very little detail. One &#8220;sounds like&#8221; it makes something (<strong>factoryMethod()</strong>) and the other &#8220;sounds like&#8221; a product (<strong>uiProduct()</strong>). However, you have enough information to know that each does something different.</p>
<p><strong>The Concrete Implementations: Factories and Products</strong></p>
<p>The final step in working with the abstractions in the interfaces is to make them do something concrete. First, we&#8217;ll deal with the factories. The following code creates factories for each of the products:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p5638code17'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p563817"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
</pre></td><td class="code" id="p5638code17"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Make buttons</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MakeButton <span style="color: #0066CC;">implements</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btn:Product = <span style="color: #000000; font-weight: bold;">new</span> UiButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,why:<span style="color: #0066CC;">Number</span>,lbl:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> btn.<span style="color: #006600;">uiProduct</span><span style="color: #66cc66;">&#40;</span>ex,why,lbl<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Make backdrop</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MakeBackdrop <span style="color: #0066CC;">implements</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bkDrop:Product = <span style="color: #000000; font-weight: bold;">new</span> UiBackdrop<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,why:<span style="color: #0066CC;">Number</span>,lbl:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> bkDrop.<span style="color: #006600;">uiProduct</span><span style="color: #66cc66;">&#40;</span>ex,why,lbl<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Make TextAreas</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MakeTextArea <span style="color: #0066CC;">implements</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtArea:Product = <span style="color: #000000; font-weight: bold;">new</span> UiTxtArea<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,why:<span style="color: #0066CC;">Number</span>,lbl:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> txtArea.<span style="color: #006600;">uiProduct</span><span style="color: #66cc66;">&#40;</span>ex,why,lbl<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Make TextInput</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MakeTextInput <span style="color: #0066CC;">implements</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtInput:Product = <span style="color: #000000; font-weight: bold;">new</span> UiTxtInput<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,why:<span style="color: #0066CC;">Number</span>,lbl:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> txtInput.<span style="color: #006600;">uiProduct</span><span style="color: #66cc66;">&#40;</span>ex,why,lbl<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, they&#8217;re all pretty much the same. They declare instances as a Product but instantiate them as concrete product implementations. There&#8217;s not a lot of variety; just the name of the instantiated concrete product.</p>
<p>The concrete products show a great deal of variation, though. This demonstrates the flexibility of abstractions and the loose bindings between a client object and the requested products.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p5638code18'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p563818"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
</pre></td><td class="code" id="p5638code18"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Button</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">Button</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UiButton <span style="color: #0066CC;">implements</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnPack:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">button</span>:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> uiProduct<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,why:<span style="color: #0066CC;">Number</span>,lbl:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">button</span>.<span style="color: #006600;">label</span> = lbl;
			<span style="color: #0066CC;">button</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,btnBusiness<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">button</span>.<span style="color: #006600;">x</span> = ex;
			<span style="color: #0066CC;">button</span>.<span style="color: #006600;">y</span> = why;
			btnPack.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">button</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> btnPack;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> btnBusiness<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Whatever business you have in mind....&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Backdrop</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UiBackdrop <span style="color: #0066CC;">implements</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> backdropHolder:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> backdrop:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> uiProduct<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,why:<span style="color: #0066CC;">Number</span>,lbl:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			backdrop.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span>lbl<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			backdrop.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,0x0000cc<span style="color: #66cc66;">&#41;</span>;
			backdrop.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span>ex, why, ex+<span style="color: #cc66cc;">100</span>, why+<span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;
			backdrop.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			backdropHolder.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>backdrop<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> backdropHolder;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Text Area</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #006600;">TextArea</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UiTxtArea <span style="color: #0066CC;">implements</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> taPack:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtArea:TextArea=<span style="color: #000000; font-weight: bold;">new</span> TextArea<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> uiProduct<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,why:<span style="color: #0066CC;">Number</span>,lbl:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			txtArea.<span style="color: #0066CC;">text</span> = lbl;
			txtArea.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">200</span>,txtArea.<span style="color: #0066CC;">height</span> = <span style="color: #cc66cc;">200</span>;
			txtArea.<span style="color: #006600;">x</span> = ex;
			txtArea.<span style="color: #006600;">y</span> = why;
			taPack.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>txtArea<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> taPack;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Text Input</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #006600;">TextInput</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UiTxtInput <span style="color: #0066CC;">implements</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> tiPack:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtInput:TextInput=<span style="color: #000000; font-weight: bold;">new</span> TextInput<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> uiProduct<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,why:<span style="color: #0066CC;">Number</span>,lbl:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			txtInput.<span style="color: #0066CC;">text</span> = lbl;
			txtInput.<span style="color: #006600;">x</span> = ex;
			txtInput.<span style="color: #006600;">y</span> = why;
			tiPack.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>txtInput<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> tiPack;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see in the different implementations of the Product object, there&#8217;s an immense variation. The Factory Method uses abstraction to make it very easy for the Client to request what&#8217;s needed, provide loose binding, and do it while allowing enough room for variation to do whatever the developer wants.</p>
<p>The best part comes when I decide to change something. Suppose I decided I want to make my own TextArea object using a TextField, TextFormat and other coded objects so I don&#8217;t have to worry too much about component differences between Flash Professional and Flash Builder. I can simply change my implementation in the concrete product without affecting anything else. In other words, I can make huge programs without having to start all over from scratch.</p>
<p>Thanks abstraction!</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%2F2011%2F05%2Fbeginning-actionscript-3-0-abstraction-the-factory-method-at-work%2F&amp;title=Beginning%20ActionScript%203.0%20Abstraction%20%3A%20The%20Factory%20Method%20at%20Work" id="wpa2a_6"><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/2011/05/beginning-actionscript-3-0-abstraction-the-factory-method-at-work/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Reuse Where Objects Change: Factory Method at Work with Lazy Instantiation</title>
		<link>http://www.as3dp.com/2011/02/reuse-where-objects-change-factory-method-at-work-with-lazy-instantiation/</link>
		<comments>http://www.as3dp.com/2011/02/reuse-where-objects-change-factory-method-at-work-with-lazy-instantiation/#comments</comments>
		<pubDate>Mon, 21 Feb 2011 01:42:42 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Abstract Classes]]></category>
		<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Factory Method]]></category>
		<category><![CDATA[Lazy instantiation]]></category>
		<category><![CDATA[Loose Coupling]]></category>
		<category><![CDATA[re-use program elements]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=4723</guid>
		<description><![CDATA[Gentle Readers: Chandima and I try different things, and this is the first time (I think) that we&#8217;ve taken an online Adobe Connect presentation and the accompanying PowerPoint slides and translated them into an article (post.) If you were able to attend the online talk, you have all of the slides along with written commentary [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.as3dp.com/wp-content/uploads/2011/02/sleepFactoryB.png" alt="sleepFactoryB" title="sleepFactoryB" width="250" height="173" class="alignleft size-full wp-image-4728" /><em><strong>Gentle Readers:</strong> Chandima and I try different things, and this is the first time (I think) that we&#8217;ve taken an online  Adobe Connect presentation and the accompanying PowerPoint slides and translated them into an article (post.) If you were able to attend the online talk, you have all of the slides along with written commentary and you can take your time to review them. Of course if you are unable to attend because it&#8217;s 3:30 am in Mumbai, this provides an asynchronous means of participating. Let us know what you think. Is it effective? Ineffective? Makes no difference? Your comments are valuable and invited. </em></p>
<p><strong>Lazy Bones</strong></p>
<p>There&#8217;re more ways than one for being lazy with design patterns. In this post we&#8217;ll look at two: First we&#8217;ll see where you can take a  program based on the Factory Method and reuse it for another program. Secondly, we&#8217;ll see where we put lazy instantiation in a Factory Method. That&#8217;s double lazy! So Homer Simpsons of the world unite! This is the design pattern for you. D&#8217;oh! (In the past we&#8217;ve examined <a href="http://www.as3dp.com/2010/05/24/actionscript-3-0-lazy-initialization-and-the-factory-method-design-pattern/">lazy initialization</a>, but in this post we&#8217;ll look at <em>lazy instantiation</em>.)</p>
<p>Last week we had our first online presentation thanks to the Adobe users in Brazil. However, many missed it, and I promised to provide everyone with the materials we covered. Besides, since this is a discussion of a Factory pattern we can reuse our Factory Button! (Even this post is lazy!) Click the two buttons to see the apps with two different implementations. Then you can download the two apps and the PowerPoint file from the presentation by clicking the download button.<br />
<div id="attachment_4179" class="wp-caption alignleft" style="width: 135px"><a href="http://www.sandlight.com/Brasil/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2010/12/factoryBtn.png" alt="Southern Brazil" title="factoryBtn" width="125" height="60" class="size-full wp-image-4179" /></a><p class="wp-caption-text">Southern Brazil</p></div>&nbsp;<div id="attachment_4179" class="wp-caption alignleft" style="width: 135px"><a href="http://www.sandlight.com/NewEngland/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2010/12/factoryBtn.png" alt="New England" title="factoryBtn" width="125" height="60" class="size-full wp-image-4179" /></a><p class="wp-caption-text">New England</p></div><a href="http://nemo.mwd.hartford.edu/~wsanders/Brasil"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a>
<p>So you may be thinking that both of the applications are doing pretty much the same thing—placing objects on the screen. That&#8217;s true. Rocket science it&#8217;s not. However, the nature of the classes is such that we have a wide range of options with the objects. As the applications are currently laid out, it&#8217;s easy to change the objects and the collection of objects without breaking the application. Let&#8217;s see how.<br />
<span id="more-4723"></span></p>
<p><strong>Talk to the Factory</strong></p>
<p>The Factory Method design pattern is made up of a Creator (Factory) interface with concrete creators that instantiate concrete products represening implementations of a Product interface. The Creator contains an abstract <em>FactoryMethod()</em> operation that each concrete creator implements. Figure 1 shows a typical factory pattern with different shapes implied from the concrete creators and products. The shapes are representatives of any objects that the developer wants to request using the <strong>FactoryMethod()</strong> operations.<br />
<div id="attachment_3279" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2010/05/FactoryMethodLazy.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt;Class Diagram Factory Method&lt;/em&gt;" title="FactoryMethodLazy" width="500" height="248" class="size-full wp-image-3279" /><p class="wp-caption-text"><em><strong>Figure 1:</strong> Factory Method Class Diagram</em></p></div></p>
<p>The class diagram accurately portrays the role of the concrete factories—they instantiate the products indicated by the dashed arrows. The factories are more like <em>car dealerships</em> than they are &#8220;factories.&#8221; The dealerships order (instantiate) a completely built car. They don&#8217;t build them—they get them. If you have parameterized products; then perhaps you can think of the instantiation process as one where the objects are &#8220;built&#8221; by the concrete factories that pass the arguments, but in this example, think of the concrete creator (factory) classes as &#8220;auto dealerships&#8221; and not &#8220;auto factories.&#8221;</p>
<p>Figure 2 shows how the client makes its request and the links between the participants:<br />
<div id="attachment_4713" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/02/Slide03.jpg" alt="&lt;em&gt;&lt;strong&gt;Figure 2:&lt;/strong&gt; Client makes request&lt;/em&gt;" title="Slide03" width="500" height="375" class="size-full wp-image-4713" /><p class="wp-caption-text"><em><strong>Figure 2:</strong> Client makes request</em></p></div> By making requests in this manner, the Client is only loosely coupled to the object. If the object changes, the Client has no knowledge of it, but as long as the concrete factory (creator) implements the concrete product correctly, the client&#8217;s request is met.</p>
<p>To get a better idea of the details of the client&#8217;s request, look at Figure 3. The object being requested is a Brazillian state named Paraná (not to be confused with the fish, <em>piranha</em> or <em>paraña</em>). Note that the variable named pirana is typed as a Factory—the interface. Then the request is made by instantiating a concrete factory class, <strong>MakeParana</strong>, an implementation of the Factory abstract class. (<em>Programming to the interface; not the implementation.</em>)<br />
<div id="attachment_4716" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/02/Slide06.jpg" alt="&lt;em&gt;&lt;strong&gt;Figure 3:&lt;/strong&gt; Client makes request using a Factory object&lt;/em&gt;" title="Slide06" width="500" height="375" class="size-full wp-image-4716" /><p class="wp-caption-text"><em><strong>Figure 3:</strong> Client makes request using a Factory object</em></p></div></p>
<p>To help get a better idea of how that request is made in the larger context of a Factory Method design pattern, Figure 4 shows just those classes involved in the request in a file diagram. The request is sent to the MakePirana concrete factory through the Factory interface (an abstract class). The factory instantiates a concrete Product instance named Pirana.<br />
<div id="attachment_4719" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/02/Slide05.png" alt="&lt;em&gt;&lt;strong&gt;Figure 4: Files used in Pirana object request&lt;/strong&gt;&lt;/em&gt;" title="Slide05" width="500" height="375" class="size-full wp-image-4719" /><p class="wp-caption-text"><em><strong>Figure 4: Files used in Pirana object request</strong></em></p></div></p>
<p>If I wanted to change the character of the Pirana object to something else, the Client could care less. It just makes the request to the concrete factory object and lets the factory worry about instantiating the object. Let&#8217;s say that instead of the state of Pirana, I wanted to use the design pattern to get the state of Connecticut. I wouldn&#8217;t change the request to the factory; I&#8217;d change the object that the factory instantiates and returns to the Client.</p>
<p><strong>The Abstract Factory and Product</strong></p>
<p>Both of the interfaces in this implementation of the Factory Method are made up of abstract classes. The Factory class holds a reference to the Product class and an abstract <strong>factoryMethod()</strong> operation. The details of the factoryMethod() are left up to the concrete factories to implement. Figure 5 shows the Factory abstract class:<br />
<div id="attachment_4725" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/02/Slide07.jpg" alt="&lt;em&gt;&lt;strong&gt;Figure 5:&lt;/strong&gt; The Factory Interface (abstract class)&lt;/em&gt;" title="Slide07" width="500" height="375" class="size-full wp-image-4725" /><p class="wp-caption-text"><em><strong>Figure 5:</strong> The Factory Interface (abstract class)</em></p></div></p>
<p>Nothing is declared and the protected variable, <strong>product</strong> is only declared as a Product type. <strong>Product</strong>, is an abstract class and so the product variable will have to be instantiated as a concrete implementation of Product and not Product itself. (You cannot instantiate an abstract class.)</p>
<p>The Product class is similar in that it is abstract and nothing is implemented. Figure 6 shows how it is built:<br />
<div id="attachment_4734" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/02/Slide09.jpg" alt="&lt;em&gt;&lt;strong&gt;Figure 6:&lt;/strong&gt; The abstract class Product&lt;/em&gt;" title="Slide09" width="500" height="375" class="size-full wp-image-4734" /><p class="wp-caption-text"><em><strong>Figure 6:</strong> The abstract class Product</em></p></div></p>
<p>Originally, I had the Product class subclass Sprite, but I realized later that I did not need to do that. Each of the product objects were already children of the Sprite class, and so I had all of the properties and methods in the Sprite class I needed. By not subclassing the Sprite, the Product class is looser because it has no binding through subclassing. Importing the Sprite package is enough to reference the Sprite as the return type of the <strong>getObject()</strong> method and <strong>objectNow</strong> property.</p>
<p><strong>Implementing the Concrete Factories (with Lazy Instantiation) and Products</strong></p>
<p>All that&#8217;s left is to implement are the Factory and Product interfaces (abstract classes). The objects that are used to actually show images on the screen are Library Sprites. I just drew the different states (<em>estados</em>) using Flash tools and converted them into MovieClip symbols. Then, I converted the MovieClip classes into Sprite classes by changing the Base class from <strong>flash.display.MovieClip</strong> to <strong>flash.display.Sprite</strong>.</p>
<p>The concrete factory that calls the requested Parana state, checks to see if the product already exists, and if not, produces one. Then, using the <strong>product</strong> object it uses the <strong>getObject()</strong> method to get the requested product. Figure 7 shows how each of the concrete factories are coded&#8211;the only difference being that they instantiated different concrete products:<br />
<div id="attachment_4756" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/02/Slide08.png" alt="&lt;em&gt;&lt;strong&gt;Figure 7:&lt;/strong&gt; Concrete factory instantiates requested object&lt;/em&gt;" title="Slide08a" width="500" height="375" class="size-full wp-image-4756" /><p class="wp-caption-text"><em><strong>Figure 7:</strong> Concrete factory instantiates requested object</em></p></div></p>
<p>We can see the <strong>lazy instantiation</strong> in the conditional statement that is looking for an uninstantiated product, and if the product does not exist as an instance, it creates one.  The <strong>product</strong> property is inherited from the Factory abstract class. The <strong>product</strong> is initialized in the Factory class <em>as a</em> Product type—that&#8217;s why you don&#8217;t see any data type assigned to the <strong>product</strong> in the concrete factory. So while the product is unimplemented, it has been initialized. This binds the product only to the Product interface and not any single product implementation. As a result, the product object can be used to instantiate any of the concrete properties. This is what makes programming to the interface and loose coupling so valuable.</p>
<p>Finally, we come to the concrete products. Each products extends the Product class and implements the <strong>getObject()</strong> function. The <strong>objNow</strong> property (inherited from the Product abstract class) instantiates a Sprite object from the Library. The Sprite can come from any source where a Sprite can be stored. The important fact is that the object is a Sprite and has key Sprite properties and methods. In this case, all we need are the horizontal (<strong>x</strong>) and vertical (<strong>y</strong>) properties. Figure 8 shows how the Parana product is created;<br />
<div id="attachment_4750" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/02/Slide10.jpg" alt="&lt;em&gt;&lt;strong&gt;Figure 8:&lt;/strong&gt; Concrete Product class&lt;/em&gt;" title="Slide10" width="500" height="375" class="size-full wp-image-4750" /><p class="wp-caption-text"><em><strong>Figure 8:</strong> Concrete Product class</em></p></div></p>
<p>The important features of this example lies not what was done initially, but rather how it was reused to create a similar application for a different region. Likewise, the concrete factory and product classes follow the OOP single responsibility principle—each has a single responsibility that is loosely bound and re-useable. For example, the following two classes show how the same application was reused to create the New England version of the same program:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p4723code20'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p472320"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code" id="p4723code20"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Connecticut Factory</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MakeConn <span style="color: #0066CC;">extends</span> Factory
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span> product<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				product=<span style="color: #000000; font-weight: bold;">new</span> ConnProd<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">return</span> product.<span style="color: #006600;">getObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Connecticut Product</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConnProd <span style="color: #0066CC;">extends</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> getObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			objNow=<span style="color: #000000; font-weight: bold;">new</span> Conn<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			objNow.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">188.45</span>, objNow.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">321.75</span>;
			<span style="color: #b1b100;">return</span> objNow;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, the concrete factory and product are the same as those for the Brazilian states. The Factory and Product abstract classes are identical.</p>
<p><strong>One Year of Programming Experience 20 times</strong></p>
<p>You may have heard programmers proclaim,</p>
<blockquote><p>I&#8217;ve been programming for 20 years, blah, blah, blah&#8230;.</p></blockquote>
<p> While such programmers may have a wealth of knowledge from their experience, they may have just been doing the same kind of programming for 20 years. Years ago, I heard a police officer remark about one of the older (and not too effective) officers as a</p>
<blockquote><p>&#8230;guy who had 1 year experience 20 times.</p></blockquote>
<p>Essentially the police officer who made that comment was saying that the old-timer never tried to keep up his skill levels with new knowledge. He just kept doing the same old thing again and again. That can happen at any age, and in computing, it&#8217;s a recipe for disaster.</p>
<p>When I discovered OOP and then Design Patterns, I realized that I had been like that old cop who kept doing the same thing over and over again. To be sure, I learned several new languages ranging from machine/assembly languages to high level languages like BASIC and even PostScript—and just about everything in between. However, while different computer languages have different lexicons, sequential and procedural programming are still S&#038;P programming no matter what language you&#8217;re in.</p>
<p>To break those (bad) habits, design patterns and the OOP that goes with them, forced me to think in different ways approaching programming problems. Instead of thinking in terms of a series of steps, I started to think in terms of loosely bound objects communicating with one another. The only sequences were little ones inside the methods and assigned values to properties—and many of those were just for using other objects. The design patterns themselves became &#8220;cheat sheets&#8221; where I could more easily envision how objects could be arranged to communicate and achieve a goal.</p>
<p>This particular example somehow brought many of the lessons I&#8217;ve learned from design patterns home. It wasn&#8217;t until the last minute that I decided to make another regional map where I could demonstrate re-use of design patterns (Southern Brazil to Northeast US). Because I waited until the last minute, that was about all the time I had—that last minute. However, once I finished the graphic sprites, everything else fell into place. I just re-used the structures and the communication systems from the original app. It may sound funny to voice it, but programming to the interface really saved my bacon. Here I had a whole new set of objects I had to get singing in the same choir, but I knew that as long as I paid attention to the interface, everything would work together—that&#8217;s 27 classes scaled down to 17 for a new application.(That&#8217;s not even counting the Sprite classes with the graphics!) And everything worked out fine.</p>
<p> Try doing that without a design pattern. Spaghetti city!</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%2F2011%2F02%2Freuse-where-objects-change-factory-method-at-work-with-lazy-instantiation%2F&amp;title=Reuse%20Where%20Objects%20Change%3A%20Factory%20Method%20at%20Work%20with%20Lazy%20Instantiation" id="wpa2a_8"><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/2011/02/reuse-where-objects-change-factory-method-at-work-with-lazy-instantiation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Lazy Initialization and the Factory Method Design Pattern</title>
		<link>http://www.as3dp.com/2010/05/actionscript-3-0-lazy-initialization-and-the-factory-method-design-pattern/</link>
		<comments>http://www.as3dp.com/2010/05/actionscript-3-0-lazy-initialization-and-the-factory-method-design-pattern/#comments</comments>
		<pubDate>Tue, 25 May 2010 04:04:17 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Factory Method]]></category>
		<category><![CDATA[Lazy Initialization]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=3219</guid>
		<description><![CDATA[The other day I was re-reading Chandima&#8217;s description of key OOP concepts used in the Factory Method design pattern. It is beautifully encapsulated in a little over a half a page (page 84). The Factory Method allows you to separate the creation of objects from their use. It says a bit more, but it is [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_3221" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2010/05/lazy.png" alt="Lazy Initialization" title="lazy" width="250" height="167" class="size-full wp-image-3221" /><p class="wp-caption-text">Lazy Initialization</p></div>The other day I was re-reading Chandima&#8217;s description of key OOP concepts used in the Factory Method design pattern. It is beautifully encapsulated in a little over a half a page (page 84). <em>The Factory Method allows you to separate the creation of objects from their use</em>. It says a bit more, but it is a nice piece in both describing a design pattern principle and explaining what the Factory Method does. This led to re-reading the Factory Method chapter in<em> Design Patterns: Elements of Reusable Object-Oriented Software</em>—the Gang of Four&#8217;s book. Normally, I just go over the main points, but I came across a little passage that read:</p>
<blockquote><p> Just be careful not to call factory methods in the Creator&#8217;s constructor—the factory method in the ConcreteCreator won&#8217;t be available yet. (page 112)</p></blockquote>
<p>The comment was directed at some issues in C++ which may or may not have any bearing on ActionScript 3.0, but just in case it did, I continued reading. The point being made was you can <em>avoid these problems by accessing products only through accessor operations</em> that create products on demand. So what are <em>accessor operations</em>? Generally these are public methods that you can call to initiate creation of a product.</p>
<p><strong>Time Out! </strong>Before we get too far ahead of ourselves, you can download all of the .as files for the Factory Method example by clicking the following button:<br />
<a href="http://www.sandlight.net/lazy.zip"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a></p>
<p>The example is a simple one that creates three different shapes—ellipse, rectangle and triangle. Figure 1 shows how the Client arranged them to display a house in front a a pond with a cloud overhead:<br />
<div id="attachment_3275" class="wp-caption alignnone" style="width: 399px"><img src="http://www.as3dp.com/wp-content/uploads/2010/05/housePond.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt;Triangle, Ellipse and Rectangle Products&lt;/em&gt;" title="housePond" width="389" height="305" class="size-full wp-image-3275" /><p class="wp-caption-text"><em><strong>Figure 1:</strong>Triangle, Ellipse and Rectangle Products</em></p></div></p>
<p>The program uses concrete creators to fetch shapes. The Client makes requests that size, color, and position the shapes.</p>
<p><strong>Don&#8217;t Enslave Your Constructor Functions</strong></p>
<p>This issue of the constructor functions reminded me of a post by Miško Hevery, the guy at Google who helps set Google&#8217;s programming standards. One of the key flaws in a program is where the<a href="http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/"> constructor does real work</a>. Besides violating the single responsibility rule, Miško goes on to explain the tell-tale signs that one has used his constructor function with outrageous disregard for both agile programming and loose coupling. As a result of reading Hevery&#8217;s work, a year or so ago, I began to seriously gear back on using a class&#8217; constructor function; primarily by not including one in most of my classes. (My Client class is a glaring exception to the rule.)  I&#8217;d usually have just the properties and operations (methods, functions) that I needed. The properties would initialize themselves—sort of. They&#8217;d be given an ID and then typed and that was all. The child classes could then use them in operations. Whenever I needed something, the Client would type a request to the interface (including abstract classes) and then make a request through concrete method in an operation. With the Factory Method, I make requests to the ConcreteCreator through the abstract Creator class.</p>
<p>In returning to  GoF, they point out,</p>
<blockquote><p>Instead of creating the concrete product in the constructor, the constructor merely initializes it to 0. (Page 112)</p></blockquote>
<p>In this discussion, they note that <em>Factory methods in C++ are always virtual functions</em>. WTF?! (<em>What&#8217;s That Function?!</em>) Simply stated, any function that can be overridden in the child classes is a virtual one. Well, we can do that in ActionScript 3.0. In fact, that&#8217;s what we do in creating Abstract classes. (Why we can override and yet not have <em>real</em> Abstract classes is still a mystery to me.)</p>
<p><span id="more-3219"></span><br />
But what about the constructor function—especially in an Abstract class that you cannot instantiate anyway? Well, there&#8217;s an automatic constructor in all classes; so if we initialize a product like this:</p>
<p><code><strong>protected var product:Product;</strong></code></p>
<p>It will be automatically initialized in the Creator. Or will it? The answer is <em>sort of</em>. The accessor is going to return the product in the ConcreteCreator, but it first checks to see if it exists. Even though we initialized the product in the Creator which is inherited by the ConcreteCreator, it does not exist until a new instance is declared.</p>
<blockquote><p>The process of first checking to see if the product exists before instantiating it is called&#8230;ta da!&#8230;<strong>lazy initialization</strong>!</p></blockquote>
<p>So, while ActionScript 3.0 may not be exactly like C++ in how it manages memory, they appear to share lazy initialization. In our Factory Method design, the following shows the generic code for this kind of lazy initialization (in a ConcreteCreator class that has inherited the product property from the Creator class):</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p3219code27'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p321927"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p3219code27"><pre class="actionscript" style="font-family:monospace;">override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Object</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span> product<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		product=<span style="color: #000000; font-weight: bold;">new</span> ConcreteProduct<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;new one&quot;</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;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;reusing existing product&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">return</span> product.<span style="color: #006600;">getProduct</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The above code includes trace statements to see both new and existing products. All of the ConcreteCreator classes include this type of lazy initialization.</p>
<p>To understand how memory is allocated you need to understand something about garbage collection, and <a href="http://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html">Grant Skinner&#8217;s post</a> on this topic is an excellent resource. Likewise,  Grant Skinner&#8217;s article on<a href="http://www.adobe.com/devnet/flashplayer/articles/resource_management.html"> memory management</a> is equally helpful in understanding the issues here that involve creating optimum code. Keep in mind that we&#8217;re dealing with lazy initialization, and garbage collection and memory management are the details of how existing objects are allocated memory space and how they come to be deallocated in the process of garbage collection. Assume that the Flash player will handle memory allocation (and deallocation) as best it possibly can, but we need to be cognizant of what we&#8217;re doing as well. In setting up our Factory Method, we have learned that initialization does not create an object.</p>
<p><strong>A Simple Factory Method Program</strong></p>
<p>I&#8217;ve created a simple Factory Method example that displays different objects. Each of the three objects is a Product subclass and each of the three objects has its own concrete creator, all subclasses of a Creator class. The Product and Creator classes are abstract and so we can think of them as interfaces—<em>program to the interface and not the implementation</em>. All of the object creation begins with the Client class with a call to the factory to create the desired object; so the Client needs to have control over object creation. Figure 2 shows the class diagram for the program:<br />
<div id="attachment_3279" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2010/05/FactoryMethodLazy.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2:&lt;/strong&gt; Factory Method with Lazy Initialization&lt;/em&gt;" title="FactoryMethodLazy" width="500" height="248" class="size-full wp-image-3279" /><p class="wp-caption-text"><em><strong>Figure 2:</strong> Factory Method with Lazy Initialization</em></p></div></p>
<p>As you can see, it&#8217;s a pretty standard Factory Method design pattern, and if you&#8217;re just getting started with this pattern, it&#8217;s a good one to use as a generic base.</p>
<p><strong>Creator Classes</strong></p>
<p>Beginning with the Creator, we&#8217;ll work our way to the Client making requests. The Creator class is set up as an Abstract class where we can lazily initialize a product object:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p3219code28'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p321928"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p3219code28"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
	<span style="color: #808080; font-style: italic;">// ABSTRACT Class (should be subclassed and not instantiated)</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> product:Product;
&nbsp;
		<span style="color: #808080; font-style: italic;">// ABSTRACT Method (must be overridden in a subclass)</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span>hor:<span style="color: #0066CC;">Number</span>,v:<span style="color: #0066CC;">Number</span>,col:uint,w:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The product instance initialization provides a type, instance name and accessor. As there is no constructor function, one is automatically created and so <strong>product</strong> is available as a property and can be passed to the Creator child classes—the ConcreteCreators.</p>
<p>The key method for creators is the <strong>factoryMethod</strong>. Here, I gave it five parameters one each for <strong>x</strong> and <strong>y</strong> position, color, and width and height. This gives the user more flexibility, but I have to admit that I&#8217;m not crazy about that many parameters. In the past, I&#8217;ve used a single array where all of the parameter values can be placed. The ConcreteCreators all implement the <strong>factoryMethod</strong> by setting up  lazy initialization (checking a product to see if it exists before creating a new one). I&#8217;ve placed all of the three ConcreteCreators in the following listing:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p3219code29'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p321929"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
</pre></td><td class="code" id="p3219code29"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Create Rectangle</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RecMaker <span style="color: #0066CC;">extends</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span>hor:<span style="color: #0066CC;">Number</span>,v:<span style="color: #0066CC;">Number</span>,col:uint,w:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span> product<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				product=<span style="color: #000000; font-weight: bold;">new</span> RecShape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;new one&quot;</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;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;reusing existing product&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">return</span> product.<span style="color: #006600;">getFigure</span><span style="color: #66cc66;">&#40;</span>hor,v,col,w,h<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Create Ellipse</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EllipseMaker <span style="color: #0066CC;">extends</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span>hor:<span style="color: #0066CC;">Number</span>,v:<span style="color: #0066CC;">Number</span>,col:uint,w:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span> product<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				product=<span style="color: #000000; font-weight: bold;">new</span> Ellipse<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;new one&quot;</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;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;reusing existing product&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">return</span> product.<span style="color: #006600;">getFigure</span><span style="color: #66cc66;">&#40;</span>hor,v,col,w,h<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Create Triangle</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TriMaker <span style="color: #0066CC;">extends</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span>hor:<span style="color: #0066CC;">Number</span>,v:<span style="color: #0066CC;">Number</span>,col:uint,w:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span> product<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				product=<span style="color: #000000; font-weight: bold;">new</span> Triangle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;new one&quot;</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;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;reusing existing product&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">return</span> product.<span style="color: #006600;">getFigure</span><span style="color: #66cc66;">&#40;</span>hor,v,col,w,h<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The manner in which <strong>lazy initialization</strong> is set up in each of the <strong>factoryMethod()</strong> methods shows when a new product is created and when an existing one is used. (If you want to clean it up, you can just use the first <strong>if</strong> statement and remove both the <strong>else</strong> clause and <strong>trace</strong> statements.)</p>
<p>Each of the creators returns a <strong>Shape</strong> object by calling the Product method, <strong>getFigure()</strong>. All of the concrete creator classes do pretty much the same thing, and their only difference is the concrete product each calls and returns.</p>
<p><strong>Product Classes</strong></p>
<p>The Product class is another abstract one with a single property and method. However, instead of a Product property, it has a Shape property. It is only initialized in the abstract class and then instantiated in the concrete products.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p3219code30'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p321930"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p3219code30"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">// ABSTRACT Class (should be subclassed and not instantiated)</span>
	<span style="color: #000000; font-weight: bold;">class</span> Product
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> figure:Shape;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getFigure<span style="color: #66cc66;">&#40;</span>hor:<span style="color: #0066CC;">Number</span>,v:<span style="color: #0066CC;">Number</span>,col:uint,w:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Each of the ConcreteProduct classes draws the desired shape by overriding the getFigure() method. and creates a new Shape instance from the <strong>figure</strong> Shape object it inherits. (Again we find lazy initialization.) I had thought of instantiating the Shape in the Product abstract class and then each method would not have to. However, I&#8217;d been warned by Miško Hevery&#8217;s article about using the <strong>new</strong> keyword in a constructor function let alone an abstract class.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p3219code31'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p321931"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
</pre></td><td class="code" id="p3219code31"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Draw Rectangle</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RecShape <span style="color: #0066CC;">extends</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> getFigure<span style="color: #66cc66;">&#40;</span>hor:<span style="color: #0066CC;">Number</span>,v:<span style="color: #0066CC;">Number</span>,col:uint,w:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			figure=<span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			figure.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>col,.5 <span style="color: #66cc66;">&#41;</span>;
			figure.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span>hor, v, w, h<span style="color: #66cc66;">&#41;</span>
			figure.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> figure;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Draw Ellipse</span>
<span style="color: #808080; font-style: italic;">//Concrete Product</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Ellipse <span style="color: #0066CC;">extends</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> getFigure<span style="color: #66cc66;">&#40;</span>hor:<span style="color: #0066CC;">Number</span>,v:<span style="color: #0066CC;">Number</span>,col:uint,w:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			figure=<span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			figure.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>col,.5 <span style="color: #66cc66;">&#41;</span>;
			figure.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawEllipse</span><span style="color: #66cc66;">&#40;</span>hor, v, w, h<span style="color: #66cc66;">&#41;</span>
			figure.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> figure;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Draw Triangle</span>
<span style="color: #808080; font-style: italic;">//Concrete Product</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Triangle <span style="color: #0066CC;">extends</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> getFigure<span style="color: #66cc66;">&#40;</span>hor:<span style="color: #0066CC;">Number</span>,v:<span style="color: #0066CC;">Number</span>,col:uint,w:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			figure=<span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			figure.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>col,.5 <span style="color: #66cc66;">&#41;</span>;
			figure.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">moveTo</span><span style="color: #66cc66;">&#40;</span>hor,v<span style="color: #66cc66;">&#41;</span>;
			figure.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span>hor+w,v+h<span style="color: #66cc66;">&#41;</span>;
			figure.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span>hor-w,v+h<span style="color: #66cc66;">&#41;</span>;
			figure.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span>hor,v<span style="color: #66cc66;">&#41;</span>;
			figure.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> figure;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>That&#8217;s pretty much it. When I ran into a hitch, I decided to try and debug it by creating my own rectangle using the moveTo and lineTo methods. It was easy to add to this pattern; so, you can assume it&#8217;s been re-use and change tested.</p>
<p><strong>The Client</strong></p>
<p>Finally we come to the Client. Initially, I had planned to include a user interface, but I thought I&#8217;d let you create your own UI. (<em>Why should I have all the fun?</em>) All it does is to take the products and display them. If you want to make changes, it&#8217;s easy to do.  All the requests are made through the Creator classes, and the only details that need concern us are getting the parameters straight.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p3219code32'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p321932"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code" id="p3219code32"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> pond:Creator;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> house:Creator;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> roof:Creator;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			pond=<span style="color: #000000; font-weight: bold;">new</span> EllipseMaker<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>pond.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">145</span>,<span style="color: #cc66cc;">160</span>,0x468966,<span style="color: #cc66cc;">300</span>,<span style="color: #cc66cc;">120</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//Add a cloud</span>
			addChild<span style="color: #66cc66;">&#40;</span>pond.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">200</span>,<span style="color: #cc66cc;">20</span>,0xcccccc,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			house=<span style="color: #000000; font-weight: bold;">new</span> RecMaker<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>house.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">250</span>,<span style="color: #cc66cc;">100</span>,0xFFB03B,<span style="color: #cc66cc;">75</span>,<span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//Add a door</span>
			addChild<span style="color: #66cc66;">&#40;</span>house.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">270</span>,<span style="color: #cc66cc;">120</span>,0x8E2800,<span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//Add a window</span>
			addChild<span style="color: #66cc66;">&#40;</span>house.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">295</span>,<span style="color: #cc66cc;">120</span>,0x8E2800,<span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			roof=<span style="color: #000000; font-weight: bold;">new</span> TriMaker<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>roof.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">285</span>,<span style="color: #cc66cc;">75</span>,0xB64926,<span style="color: #cc66cc;">50</span>,<span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>I&#8217;m sure that Miško Hevery would have a fit with the chores I gave the Client&#8217;s constructor class, but I decided that the Client doesn&#8217;t count because all it should be doing is making requests. As you can see, that&#8217;s all it does.</p>
<p><strong>Seeing Lazy at Work</strong></p>
<p>When you run the program, your trace output will be the following:</p>
<p><strong>new one<br />
reusing existing product<br />
new one<br />
reusing existing product<br />
reusing existing product<br />
new one</strong></p>
<p>It first creates an Ellipse object and when it checks to see whether it exists or not, it finds a null; so it creates a new one. However with the next Ellipse, it finds that it exists, and so reuses it but changes the parameters. Then it creates one new Rectangle and then reuses it twice. Finally, it creates a single Triangle object but does not reuse it. The trace statements are a visual display of the lazy initialization.</p>
<p>Finally, just for the fun of it, see if you can figure out what the vintage Soviet poster says at the beginning of the post.(ленивое is the key!) You really should try your hand at adding more products and the creator classes that call them. While you&#8217;re at it, add a UI in the Client and really have some fun.</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%2F2010%2F05%2Factionscript-3-0-lazy-initialization-and-the-factory-method-design-pattern%2F&amp;title=ActionScript%203.0%20Lazy%20Initialization%20and%20the%20Factory%20Method%20Design%20Pattern" id="wpa2a_10"><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/2010/05/actionscript-3-0-lazy-initialization-and-the-factory-method-design-pattern/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Design Pattern Re-use and Modification</title>
		<link>http://www.as3dp.com/2009/12/actionscript-3-0-design-pattern-re-use-and-modification/</link>
		<comments>http://www.as3dp.com/2009/12/actionscript-3-0-design-pattern-re-use-and-modification/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 15:21:11 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Factory Method]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=2280</guid>
		<description><![CDATA[Every Monday I have to separate out the paper, glass, aluminum, and plastic to recycle and take them up to the curb. This last Monday while engaged in this weekly ritual, I thought that recycling a design pattern might be an interesting exercise. Additionally, it&#8217;s the kind of design pattern advantage that can be used [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_750" class="wp-caption alignleft" style="width: 160px"><img src="http://www.as3dp.com/wp-content/uploads/2009/03/bucket.png" alt="Take This One to Work" title="bucket" width="150" height="133" class="size-full wp-image-750" /><p class="wp-caption-text">Take This One to Work</p></div>Every Monday I have to separate out the paper, glass, aluminum, and plastic to recycle and take them up to the curb. This last Monday while engaged in this weekly ritual, I thought that recycling a design pattern might be an interesting exercise. Additionally, it&#8217;s the kind of design pattern advantage that can be used at work to save re-inventing the wheel when you have a similar development task. You may remember the <a href="http://www.as3dp.com/2009/09/13/hitchhiker’s-guide-to-actionscript-30-the-dragon-factory—part-2/#more-1518">Dragon Factory</a> where we employed a Factory Method  to set up an easy-to-use drag application. In the original Dragon Factory, both the Creator and Product participants had subclasses. However, with only a slight revision, it&#8217;s possible to create a little game for making different faces subclassing only the Product interface (abstract class). Figure 1 shows the File-Class Diagram:</p>
<div id="attachment_2293" class="wp-caption alignnone" style="width: 491px"><img src="http://www.as3dp.com/wp-content/uploads/2009/12/FaceDiagram.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; Factory Method with Single Concrete Creator&lt;/em&gt;" title="FaceDiagram" width="481" height="376" class="size-full wp-image-2293" /><p class="wp-caption-text"><em><strong>Figure 1:</strong> Factory Method with Single Concrete Creator</em></p></div>
<p>You can see the design similarities between this application (MrFace) and the Dragon application. The main difference is that this one has a single concrete factory (FaceCreator) for all of the concrete products.  The factory churns out all of the Product children and puts them into an array. The Client requests the array from the Creator and pulls out the elements with a loop. As you can see, they&#8217;re all draggable elements. Try out the embedded SWF file below to see how it works:</p>
<p>[swfobj src="http://www.as3dp.com/wp-content/uploads/2009/12/Client1.swf" alt="MrFace" align="none"]</p>
<p>The same Product (from the Dragon application) is re-used so it is unnecessary to revise it. However, instead of a video and a dynamically generated graphic, all of the graphics were put into the Library as Sprite classes. (To make a Sprite class just change the Base class to flash.display.Sprite.)<br />
<span id="more-2280"></span><br />
<strong>The Abstract and Concrete Factories</strong></p>
<p>Because the main difference between this application and the Dragon app is the way in which the factory classes (Creator and FaceCreator) handle products, we&#8217;ll start with them. First, the Creator class provides the necessary interface as shown in the following listing:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2280code38'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p228038"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code" id="p2280code38"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Mr Face App</span>
	<span style="color: #808080; font-style: italic;">//Asbstract class--serves as interface</span>
	<span style="color: #808080; font-style: italic;">//Creator</span>
&nbsp;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dragNow:<span style="color: #0066CC;">Array</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createDragable<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span>
		<span style="color: #66cc66;">&#123;</span>
			dragNow=getDragon<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> dragNow;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// ABSTRACT Method (must be overridden in a subclass)</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> getDragon<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>If you&#8217;re thinking, <em>That looks a lot like the Creator class for the Dragon application</em>, you&#8217;d be absolutely right. In fact, the only difference is that this Creator returns an Array object instead of a Sprite. That&#8217;s the purpose of re-use.</p>
<p>The big change is in the way the ConcreteCreator (FaceCreator) works in this design pattern. The abstract function is <strong>getDragon()</strong>, and so it has to be overridden and set up to instantiate each of the Product children (facial parts) and stuff them into an array. That array is then returned when requested by the Client.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2280code39'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p228039"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code" id="p2280code39"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Factory</span>
&nbsp;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FaceCreator <span style="color: #0066CC;">extends</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> faceOn:<span style="color: #0066CC;">Array</span>=<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> getDragon<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> ears:Product=<span style="color: #000000; font-weight: bold;">new</span> Ears<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			faceOn.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>ears<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> ears2:Product=<span style="color: #000000; font-weight: bold;">new</span> Ears<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			faceOn.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>ears2<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> glasses:Product=<span style="color: #000000; font-weight: bold;">new</span> Glasses<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			faceOn.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>glasses<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> nose:Product=<span style="color: #000000; font-weight: bold;">new</span> Nose<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			faceOn.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>nose<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> eyes:Product=<span style="color: #000000; font-weight: bold;">new</span> Eyes<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			faceOn.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>eyes<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> mouth:Product=<span style="color: #000000; font-weight: bold;">new</span> Mouth<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			faceOn.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>mouth<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> hair:Product=<span style="color: #000000; font-weight: bold;">new</span> Hair<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			faceOn.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>hair<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> head:Product=<span style="color: #000000; font-weight: bold;">new</span> Head<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			faceOn.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>head<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">return</span> faceOn;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>By using an array, the Client only has to request a single object rather than all of the Product children individually as was the case in the Dragon application. That&#8217;s it as far as the factory classes are concerned. Next, in looking at the Product and its children classes, you will see that not much has changed from the original designs.</p>
<p><strong>The Product and Its Brood</strong></p>
<p>First of all, take a look at the Product class used as an interface for all of the children classes. Again, the Product is an abstract class instead of an Interface, and it re-uses the same methods and properties.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2280code40'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p228040"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code" id="p2280code40"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Abstract class--serves as interface</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Product <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> base:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// ABSTRACT Method</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> placeParts<span style="color: #66cc66;">&#40;</span>lr:uint,ud:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> setPart<span style="color: #66cc66;">&#40;</span>part:Sprite<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			addChild<span style="color: #66cc66;">&#40;</span>base<span style="color: #66cc66;">&#41;</span>;
			base.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>part<span style="color: #66cc66;">&#41;</span>;
			base.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_DOWN</span>,dragPart<span style="color: #66cc66;">&#41;</span>;
			base.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_UP</span>,dropPart<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> dragPart<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">startDrag</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> dropPart<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">stopDrag</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>There&#8217;s nothing new from the original in that Product class; so let&#8217;s move on to the concrete elements of the Product. The setup for this used Sprite classes stored in the Flash Library. (Creating a similar Library in Flash Builder should not be too difficult.) The <strong>setPart()</strong> method does most of the work, and the <strong>placeParts()</strong> method is overridden just to have an abstract method. (That was to demonstrate flexibility more than it was an essential function to override.)</p>
<p>Given that the abstract Product class is a workhorse, the child classes need only to call the Sprite class out of the Library. All Sprite classes end with a capital &#8220;S&#8221; to differentiate them from the Product subclasses themselves. For example, the <strong>Mouth</strong> class instantiates a <strong>MouthS</strong> class from the Library. Each of the face parts is a small subclass, and so all of them are placed together in the following listing:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2280code41'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p228041"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
</pre></td><td class="code" id="p2280code41"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Product</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Ears <span style="color: #0066CC;">extends</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> ears:Sprite = <span style="color: #000000; font-weight: bold;">new</span> EarS<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Ears<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setPart<span style="color: #66cc66;">&#40;</span>ears<span style="color: #66cc66;">&#41;</span>;
			placeParts<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> placeParts<span style="color: #66cc66;">&#40;</span>lr:uint,ud:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			base.<span style="color: #006600;">x</span>=lr,base.<span style="color: #006600;">y</span>=ud;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Product</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Eyes <span style="color: #0066CC;">extends</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> eyes:Sprite = <span style="color: #000000; font-weight: bold;">new</span> EyesS<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Eyes<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setPart<span style="color: #66cc66;">&#40;</span>eyes<span style="color: #66cc66;">&#41;</span>;
			placeParts<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">150</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> placeParts<span style="color: #66cc66;">&#40;</span>lr:uint,ud:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			base.<span style="color: #006600;">x</span>=lr,base.<span style="color: #006600;">y</span>=ud;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Product</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Glasses <span style="color: #0066CC;">extends</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> glasses:Sprite = <span style="color: #000000; font-weight: bold;">new</span> GlassesS<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Glasses<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setPart<span style="color: #66cc66;">&#40;</span>glasses<span style="color: #66cc66;">&#41;</span>;
			placeParts<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">80</span>,<span style="color: #cc66cc;">170</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> placeParts<span style="color: #66cc66;">&#40;</span>lr:uint,ud:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			base.<span style="color: #006600;">x</span>=lr,base.<span style="color: #006600;">y</span>=ud;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Product</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Hair <span style="color: #0066CC;">extends</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> hair:Sprite = <span style="color: #000000; font-weight: bold;">new</span> HairS<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Hair<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setPart<span style="color: #66cc66;">&#40;</span>hair<span style="color: #66cc66;">&#41;</span>;
			placeParts<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> placeParts<span style="color: #66cc66;">&#40;</span>lr:uint,ud:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			base.<span style="color: #006600;">x</span>=lr,base.<span style="color: #006600;">y</span>=ud;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Product</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Head <span style="color: #0066CC;">extends</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> head:Sprite = <span style="color: #000000; font-weight: bold;">new</span> HeadS<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Head<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setPart<span style="color: #66cc66;">&#40;</span>head<span style="color: #66cc66;">&#41;</span>;
			placeParts<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">120</span>,<span style="color: #cc66cc;">150</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> placeParts<span style="color: #66cc66;">&#40;</span>lr:uint,ud:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			base.<span style="color: #006600;">x</span>=lr,base.<span style="color: #006600;">y</span>=ud;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//</span>
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Product</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Mouth <span style="color: #0066CC;">extends</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> mouth:Sprite = <span style="color: #000000; font-weight: bold;">new</span> MouthS<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Mouth<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setPart<span style="color: #66cc66;">&#40;</span>mouth<span style="color: #66cc66;">&#41;</span>;
			placeParts<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> placeParts<span style="color: #66cc66;">&#40;</span>lr:uint,ud:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			base.<span style="color: #006600;">x</span>=lr,base.<span style="color: #006600;">y</span>=ud;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Using Flash Builder (Flex), just place the different graphic elements into separate classes using the same Library class names. Just be sure to subclass them from a Sprite.</p>
<p><strong>The Lazy Client</strong></p>
<p>I&#8217;ve come to believe that the less the client has to do, the better. That&#8217;s because the Client really should do nothing more than make requests. In some designs the Client is actually part of the design and has more to do. However, as a rule of thumb, the Client should only make requests and place elements on the DisplayList. In this application, that&#8217;s all the Client does as you can see in the following listing:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2280code42'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p228042"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code" id="p2280code42"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> faceParts:<span style="color: #0066CC;">Array</span>=<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> myFace:Creator=<span style="color: #000000; font-weight: bold;">new</span> FaceCreator<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			faceParts=myFace.<span style="color: #006600;">createDragable</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> saveFace:uint=faceParts.<span style="color: #006600;">length</span>-<span style="color: #cc66cc;">1</span>;
&nbsp;
			<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> count:uint =<span style="color: #cc66cc;">0</span>; count <span style="color: #66cc66;">&lt;</span>=saveFace; count++<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				addChild<span style="color: #66cc66;">&#40;</span>faceParts.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, the Client just requests the Array from the Creator and then sorts out the array. Now that&#8217;s not a lot to do!</p>
<blockquote><p><strong>Stupid dog tricks: #1 and #2.</strong> Just in case you wondered, you can instantiate an array by using aName:Array=[] instead of aName:Array= new Array(). You can do the same thing with an Object using oName:Object={}. Maybe it isn&#8217;t as clear as spelling out the object name, but we&#8217;re all entitled to a few stupid dog tricks. Those are mine.</p></blockquote>
<p><strong>Why Take This to Work?</strong></p>
<p>The whole idea behind design patterns is to generate re-usable code that contains good OOP. I realize that a drag-a-face-part game isn&#8217;t going to impress your boss (if your boss is infantile, it might) but that&#8217;s not important. What is important is to emphasize the fact that as your applications become more complex, you&#8217;re either going to have to re-start building applications with every new contract, which will probably include several hacks, make-dos, and god-awfuls. When your customer wants change, it will take time and effort and blow the stuffings out of the current application. So maybe with a simple little face, you can dispel the idea that design patterns will add to a workload. They&#8217;re just there to help.</p>
<p>Also, I think that anyone over the age of 5 can do better graphics than I can. So, I&#8217;ve provided the entire application including both CS3 and CS4 versions for you to add your own beautiful graphics. (You wonderful graphics folks can send in a fixed-up FLAs with better graphics in the Library, and we&#8217;ll gladly add them on the front of this post!). Click the download button to get the whole enchilada:</p>
<p><a href="http://www.sandlight.net/dpBlog/mrface/MrFace.zip"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a></p>
<p>This is my last post for 2009. See you all next year, and have a great holiday!</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%2F12%2Factionscript-3-0-design-pattern-re-use-and-modification%2F&amp;title=ActionScript%203.0%20Design%20Pattern%20Re-use%20and%20Modification" id="wpa2a_12"><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/12/actionscript-3-0-design-pattern-re-use-and-modification/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Hitchhiker’s Guide to ActionScript 3.0: The Dragon Factory—Part 2</title>
		<link>http://www.as3dp.com/2009/09/hitchhikers-guide-to-actionscript-30-the-dragon-factory-part-2/</link>
		<comments>http://www.as3dp.com/2009/09/hitchhikers-guide-to-actionscript-30-the-dragon-factory-part-2/#comments</comments>
		<pubDate>Sun, 13 Sep 2009 11:00:30 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Factory Method]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1518</guid>
		<description><![CDATA[You will remember the initial query that launched this two-part post started with an inquiry into the possibility of dragging a Video while it was playing. In order to make the query more relevant to design patterns, this second part looks at the process for expanding the larger issue of dragging objects inside a Sprite. [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_750" class="wp-caption alignleft" style="width: 160px"><img src="http://www.as3dp.com/wp-content/uploads/2009/03/bucket.png" alt="Take This One to Work" title="bucket" width="150" height="133" class="size-full wp-image-750" /><p class="wp-caption-text">Take This One to Work</p></div>
<p>You will remember the <a href="http://www.as3dp.com/2009/09/09/hitchhiker’s-guide-to-actionscript-30-dragging-through-the-galaxy—part-1/">initial query</a> that launched this two-part post started with an inquiry into the possibility of dragging a Video while it was playing. In order to make the query more relevant to design patterns, this second part looks at the process for expanding the larger issue of dragging objects inside a Sprite. Whenever undertaking such a project, begin with the <a href="http://www.as3dp.com/2009/06/12/the-developing-an-air-actionscript-30-design-pattern-catalog-and-the-air-magic-table/">Variation Table</a> (<strong>aka</strong> <em>Magic Table</em>) to find the possible variations. The <strong>wrong way </strong>to start developing a design pattern for a practical problem is to look at all of the different class diagrams and pick one that <em>looks like it will fit your problem</em>. Instead, always start by asking, <em>what varies</em>?<br />
<a href="http://www.sandlight.net/hitchhiker/DragonFactory.zip"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/download.png" alt="download" title="download" width="99" height="47" class="alignnone size-full wp-image-1153" /></a><br />
In this case, the &#8220;hitchhiker&#8221; to be dragged is the particular type of object in question. We saw how to drag a Video object, but what about other objects that need to hitchhike on a Sprite to be dragged? So, we&#8217;re talking about different objects—or put otherwise—<em>object variation</em>. The closest matching variation in the <em>Magic Table</em> is the variation of the subclass of the object that is instantiated. That means a Factory Method design. (See how easy that was?)<br />
<span id="more-1518"></span><br />
Next, I opened up the <a href="http://www.as3dp.com/2009/07/15/actionscript-30-design-pattern-catalog-creational-patterns/">Design Pattern Catalog</a> (which still needs to be finished) but includes all of the Creational Patterns. It provided the class diagram and other information about the Factory Method that I needed to get started on the project. Also, quite by chance the Design Pattern Catalog was developed using the Factory Method as well—in fact it uses two.</p>
<p><strong>The Dragon Factory</strong></p>
<p>One of the aids I&#8217;ve been using to develop design pattern projects is to create a class diagram made up of the files I&#8217;ll be using. This helps to give me an ongoing overview, and even though the files are devoid of content initially, they serve as a development map. Also, I like to start with the minimum number of elements needed. So I&#8217;ll need at least two different objects, or Product implementations. Since I&#8217;ve already got the Video, I&#8217;ll use it, and I&#8217;ll use the Shape object for the second hitchhiker object. Figure 1 shows the File Class Diagram for this project:<br />
<div id="attachment_1532" class="wp-caption alignnone" style="width: 505px"><img src="http://www.as3dp.com/wp-content/uploads/2009/09/classdiagram.png" alt="Figure 1: Dragon Factory File Class Diagram" title="classdiagram" width="495" height="350" class="size-full wp-image-1532" /><p class="wp-caption-text"><em><strong>Figure 1:</strong> Dragon Factory File Class Diagram</em></p></div></p>
<p>In considering any Factory Method design pattern, GoF notes that you can select from two varieties. One variety is where the Creator is an abstract class and you subclass concrete creators as factory implementations . The other variety is to have a single concrete Creator class that serves as a big factory. (During the Soviet Era in the Russia, they had a huge truck factory the size of a small city that produced huge trucks in great quantities—sort of like the single concrete Creator class.) I opted for the abstract creator to keep the design looser. With a single Creator class I would have had to used conditional statements to farm out out the different types of objects, and as you will see I was able to keep the design <em>if-free</em>.</p>
<p><strong>The Product Classes</strong></p>
<p>The design called for two different objects, but I wanted to leave the door open for further objects that I could include in the design at a later date. Thus, the Product participant in the design pattern needed to be abstract, and so I used an abstract class. It set up one abstract method, a concrete property, and two concrete methods for dragging. The concrete property (named <em>sled</em>) is employed for giving the non-Sprite objects a ride. The abstract method provides flexibility in implementation for different objects. Given the difference between a Video object, including a Camera instance, and a Shape, the design needed that flexibility.</p>
<p>The following three listings show the abstract Product class (DragProduct) and the two implementations (DragVid and DragShape).</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1518code50'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p151850"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code" id="p1518code50"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Abstract class--serves as interface</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DragProduct <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> sled:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// ABSTRACT Method (must be overridden in a subclass)</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> setElements<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> doDrag<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">startDrag</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> unDrag<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">stopDrag</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>


<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1518code51'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p151851"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code" id="p1518code51"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Product</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">media</span>.<span style="color: #0066CC;">Camera</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">media</span>.<span style="color: #0066CC;">Video</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DragVid <span style="color: #0066CC;">extends</span> DragProduct
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> const WIDE:uint=<span style="color: #cc66cc;">320</span>;
		<span style="color: #0066CC;">private</span> const HIGH:uint=<span style="color: #cc66cc;">240</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> vid:<span style="color: #0066CC;">Video</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Video</span><span style="color: #66cc66;">&#40;</span>WIDE,HIGH<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> cam:<span style="color: #0066CC;">Camera</span> = <span style="color: #0066CC;">Camera</span>.<span style="color: #006600;">getCamera</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> DragVid<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setElements<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			sled.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_DOWN</span>,doDrag<span style="color: #66cc66;">&#41;</span>;
			sled.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_UP</span>,unDrag<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> setElements<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			cam.<span style="color: #0066CC;">setMode</span><span style="color: #66cc66;">&#40;</span>WIDE,HIGH,<span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>;
			cam.<span style="color: #0066CC;">setQuality</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;
			vid.<span style="color: #0066CC;">smoothing</span>=<span style="color: #000000; font-weight: bold;">true</span>;
			vid.<span style="color: #006600;">attachCamera</span><span style="color: #66cc66;">&#40;</span>cam<span style="color: #66cc66;">&#41;</span>;
			vid.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">100</span>,vid.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">100</span>;
			addChild<span style="color: #66cc66;">&#40;</span>sled<span style="color: #66cc66;">&#41;</span>;
			sled.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>vid<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>


<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1518code52'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p151852"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code" id="p1518code52"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Product</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DragShape <span style="color: #0066CC;">extends</span> DragProduct
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> shape:Shape = <span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> DragShape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setElements<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			sled.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_DOWN</span>,doDrag<span style="color: #66cc66;">&#41;</span>;
			sled.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_UP</span>,unDrag<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> setElements<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			shape.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x009900<span style="color: #66cc66;">&#41;</span>;
			shape.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawEllipse</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">450</span>, <span style="color: #cc66cc;">10</span>, <span style="color: #cc66cc;">50</span>, <span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>sled<span style="color: #66cc66;">&#41;</span>;
			sled.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>shape<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In the two Product child classes (<strong>DragVid</strong> and <strong>DragShape</strong>) you can clearly see where the variation is in the <em>subclass of object that is instantiated</em> just like it shows in the Magic Table.</p>
<p><strong>The Factory Classes</strong></p>
<p>As noted above, I opted for the abstract Creator and relied on specific implementations for the factories that would individually create the different draggable objects. The abstract class that makes up the interface containes a single Sprite property and two methods; one public and concrete and the other, protected and abstract. The public method serves as a getter and the protected method returns the finished product. The following three listings show the Creator classes:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1518code53'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p151853"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code" id="p1518code53"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Asbstract class--serves as interface</span>
	<span style="color: #808080; font-style: italic;">//Creator</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dragNow:Sprite;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createDragable<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			dragNow = getDragon<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> dragNow;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// ABSTRACT Method (must be overridden in a subclass)</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> getDragon<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>


<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1518code54'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p151854"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p1518code54"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Factory</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> VidCreator <span style="color: #0066CC;">extends</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createDragable<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DragVid<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>


<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1518code55'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p151855"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p1518code55"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Factory</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ShapeCreator <span style="color: #0066CC;">extends</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createDragable<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DragShape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><img src="http://www.as3dp.com/wp-content/uploads/2009/08/tightprogrammer-96x150.png" alt="tightprogrammer" title="tightprogrammer" width="96" height="150" class="alignright size-thumbnail wp-image-1335" />The Creator implementations simply call for either a DragVid or DragShape instance. You may be thinking, you could have used a single Creator implementation (concrete factory) to call for one or the other with a simple conditional statement. However, I try to avoid conditional statements and keep the design loose. Why tighten up your design when you don&#8217;t have to? <em>(You&#8217;re not going to run out of silicon.</em> )</p>
<p><strong>The Lazy Client</strong></p>
<p>I&#8217;ve decided that I don&#8217;t want the client doing too much. After all, the Client&#8217;s job is to make requests; not to create things from scratch. <em>It&#8217;s here to eat the cake; not to bake it.</em> As a result, the client we have for this little project is pretty simple as you can see in the following listing:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1518code56'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p151856"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code" id="p1518code56"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dragThing:Creator=<span style="color: #000000; font-weight: bold;">new</span> VidCreator<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dragThing2:Creator=<span style="color: #000000; font-weight: bold;">new</span> ShapeCreator<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> rider:Sprite;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> rider2:Sprite;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			rider=dragThing.<span style="color: #006600;">createDragable</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rider<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			rider2=dragThing2.<span style="color: #006600;">createDragable</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rider2<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The Client implements each draggable object with just a few lines of code. It uses a Creator typed object to instantiate concrete factories. Then it uses the factories to call a method that cranks out the desired draggable products. Figure 2 shows what you can expect to see—as long as you have your camera hooked up.<br />
<div id="attachment_1546" class="wp-caption alignnone" style="width: 399px"><img src="http://www.as3dp.com/wp-content/uploads/2009/09/subjectball1.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2:&lt;/strong&gt; Easily Amused Subject Contemplating Draggable Green Ball&lt;/em&gt;" title="subjectball1" width="389" height="278" class="size-full wp-image-1546" /><p class="wp-caption-text"><em><strong>Figure 2:</strong> Easily Amused Subject Contemplating Draggable Green Ball</em></p></div></p>
<p>If you&#8217;d like to give it a run, hook up your camera and click the &#8220;Play Button&#8221;:<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/dpWork/dualDrag/"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignnone size-full wp-image-1257" /></a></p>
<p>Try dragging both objects—they drag just fine.</p>
<p><strong>Why Take This to Work?</strong></p>
<p>You might be thinking,<br />
<blockquote>This doesn&#8217;t look useful&#8230;</p></blockquote>
<p> It&#8217;s funny, the same thing was said about the electric motor and the Internet when they first came about. (It may have been <em>packet switching</em>, but you get the idea&#8230;) In their book, <em>Head First Design Patterns</em>, the Freemans have these little cut out pieces of paper you can use to help put different parts of design patterns and programs together. You&#8217;re supposed to move them around in different configurations to help you understand design patterns programming. You could do the same thing with a TextField probably. Oh, but I forgot! You can&#8217;t drag a TextField object. What <em>was</em> I thinking?</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%2Fhitchhikers-guide-to-actionscript-30-the-dragon-factory-part-2%2F&amp;title=Hitchhiker%E2%80%99s%20Guide%20to%20ActionScript%203.0%3A%20The%20Dragon%20Factory%E2%80%94Part%202" id="wpa2a_14"><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/hitchhikers-guide-to-actionscript-30-the-dragon-factory-part-2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Design Pattern Catalog: Creational Patterns</title>
		<link>http://www.as3dp.com/2009/07/actionscript-30-design-pattern-catalog-creational-patterns/</link>
		<comments>http://www.as3dp.com/2009/07/actionscript-30-design-pattern-catalog-creational-patterns/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 16:14:29 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Catalog]]></category>
		<category><![CDATA[Factory Method]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1151</guid>
		<description><![CDATA[A Tale of Two Factories: One New and One Reused This is the first of three posts on the actual AIR ActionScript 3.0 Design Pattern catalog. Previously, I had discussed the thinking that went into the design of the Catalog, and here is the first actual AIR application with the Creational Patterns portion of the [...]]]></description>
			<content:encoded><![CDATA[<p><strong>A Tale of Two Factories: One New and One Reused</strong></p>
<p>This is the first of three posts on the actual AIR ActionScript 3.0 Design Pattern catalog. Previously, <a href=http://www.as3dp.com/2009/07/07/deciding-on-a-design-pattern-a-graphic-and-text-catalog/>I had discussed the thinking that went into the design of the Catalog</a>, and here is the first actual AIR application with the Creational Patterns portion of the catalog all set up and ready to use.  Figure 1 shows what each page looks like: (By the way, the actual image in the catalog is almost twice the size as what you see in Figure 1.)</p>
<p><img src="http://www.as3dp.com/wp-content/uploads/2009/07/catalog1.png" alt="catalog1" title="catalog1" width="500" height="354" class="alignnone size-full wp-image-1152" /><br />
<em><strong>Figure 1: </strong>Design Pattern Catalog Page</em></p>
<p>Before going on, you can download all of the source code and the AIR application by clicking the Download button. Additionally, you may want to access the <a href="http://nemo.mwd.hartford.edu/~wsanders/dpcatalog/">Catalog Online</a>.<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/dpWork/Catalog1.zip"><br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/07/download.png" alt="download" title="download" width="99" height="47" class="alignnone size-full wp-image-1153" /></a></p>
<p>I changed the color combinations. I concluded that Kuler’s  <em>1944mustang </em> color set was the best for this project because I needed a white background and I liked the orange for highlight. I used red for labeling on the diagrams, but it was used sparsely and provided a consistency.<br />
<span id="more-1151"></span><br />
After several false starts, I finally settled on the Factory Method, and by the time I was finished, I was glad I did. It’s an easy design pattern, keeps the classes loosely connected and is very flexible. In fact I used two Factory Method patterns—one was reused from an earlier application where the factory method was used to send DataProvider data to the Client. (See <a href=http://www.as3dp.com/2009/06/16/friends-with-benefits-refactoring-with-multiple-design-patterns—part-i/>Refactoring with Multiple Design Patterns</a>.)</p>
<p>The new Factory Method created instances of the pages assembled from several text fields and a graphic. Initially, I was going to have a single Product class and a different ConcreteProduct class for each pattern (23 in all), and that was exactly what I started doing. However, I soon discovered that the only thing that I needed to do was to pass a string parameter with  the data name for each pattern, and a single ConcreteProduct class could handle the whole thing. Each of the classes representing a ConcreteProduct class was no smaller than the single <em> general</em> ConcreteProduct; so it did not necessitate bloating a class to cut down on the number of classes.</p>
<p>Finally, all of the data for the page are stored in folders for each individual design pattern. Each folder has a single PNG file and 12 text files. All of the contents of the 23 patterns have identical names and the pattern name is keyed to the folder name. Figure 2 shows an overview of the classes, the data folders and the Client connections to the data.<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/07/layout.png" alt="layout" title="layout" width="459" height="424" class="alignnone size-full wp-image-1156" /></p>
<p><em><strong>Figure 2: </strong>Files and Data Folders</em><br />
As you can see in Figure 2, using multiple design patterns of the same type is no different than using single design patterns or different design patterns simultaneously. All you need is to have your Client hooked in where it needs to make the request.</p>
<p><strong>Files, Files, Everywhere!</strong></p>
<p>Figure 2 shows only five of the needed 23 folders have been completed. Fortunately, only five design patterns are part of the Creational category; so I get a break between stints of the mind-numbing chore of adding text to text files. Adding the remaining folders is a matter of copying and pasting the current ones and then changing most of the text content and replacing the PNG file with the class diagram. If you’re in a hurry for the rest of the patterns you can do it yourself—most of the summaries are direct quotes from The Gang of Four or paraphrased to save space. The same is true with the rest of the materials, but I added the kinds of things that are specific to ActionScript 3.0, and of course the code snippets are all ActionScript 3.0. Figure 3 shows the contents of each folder:</p>
<p><img src="http://www.as3dp.com/wp-content/uploads/2009/07/files.png" alt="files" title="files" width="512" height="350" class="alignnone size-full wp-image-1158" /><br />
<em><strong>Figure 3: </strong>Text and Graphic files</em></p>
<p>Because all of the headers are the same, none of them have to be changed, and some of the text files are simply single words such as the name of the design pattern or the type of pattern. Using the <em>WhatVaries</em> AIR app, it’s easy to get what varies in each pattern as well. However, getting all of the text for components (participants), code examples and uses can be tricky because there’s only limited space for each.</p>
<p>While it may be tedious to enter all of the data for each and every design pattern, you’ll learn a lot—especially if you are using the Gang of Four’s book or <em>ActionScript 3.0 Design Patterns</em>. Also, there’s a lot of information on this blog you can use to fill out the data.</p>
<p>To add further design patterns here’s all you have to do:</p>
<ul>
<li>Copy and paste one of the current design pattern folders and all of its contents and then rename it using lower case no-space name. For example, the Chain of Responsibility can be named <strong>cor</strong>  </li>
<li>Add the label name and the data name of the design pattern in the CatData class. Use the <em>same</em> lowercase name for the data name as you used to name the folder for the pattern. </li>
<li>Draw a class diagram with a width no greater than 500px and save it as a PNG file using the name, <em>diagram.png</em> in the folder where you have the other files for the pattern.  </li>
<li>Open the text files and change the content to reflect the characteristics of the design pattern. The files with “head” in the filename (e.g., codehead.txt) should be left unchanged but still have to be in the folder </li>
</ul>
<p>That’s it. It also reflects what I like about design patterns. Once you get the basic structure set up; changes and reuse are easy.</p>
<p><strong>The Page-Making Factory</strong></p>
<p>Since we’re reusing a design pattern, you can review it at <a href=http://www.as3dp.com/2009/06/16/friends-with-benefits-refactoring-with-multiple-design-patterns—part-i/>Refactoring with Multiple Design Patterns </a>. However, to understand how the new use of the Factory Method works, each of the classes are reviewed here.</p>
<p>First of all, the Client is remarkably clean, and all requests, other than the initial one to provide data for the List component, relies on user-input in the List menu. <em>Only a single conditional statement</em> is used anywhere in this application, and it can be found in the Client where checking content in the DisplayObject. So you’ll find a very simple Client making requests for data and to see the content of a design pattern.  The following listing shows what little code is required in the Client class:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1151code62'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p115162"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
</pre></td><td class="code" id="p1151code62"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">List</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">DataProvider</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">events</span>.<span style="color: #006600;">ListEvent</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Client Class</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> factory:Creator = <span style="color: #000000; font-weight: bold;">new</span> ConcreteCreator  ;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> catalog:DataProvider;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">list</span>:<span style="color: #0066CC;">List</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">List</span>  ;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dp:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dpDisplay:Sprite;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setMenu<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//Re-use Factory that provides data for DataProvider</span>
			<span style="color: #000000; font-weight: bold;">var</span> create:DPCreator = <span style="color: #000000; font-weight: bold;">new</span> ConcreteDPCreator  ;
			<span style="color: #000000; font-weight: bold;">var</span> catalog:DataProvider = <span style="color: #000000; font-weight: bold;">new</span> DataProvider  ;
			catalog = create.<span style="color: #006600;">selectData</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//Set up the list</span>
			<span style="color: #0066CC;">list</span>.<span style="color: #006600;">setSize</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">126</span>,<span style="color: #cc66cc;">650</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">list</span>.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">0</span>,<span style="color: #0066CC;">list</span>.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">0</span>;
			<span style="color: #0066CC;">list</span>.<span style="color: #006600;">dataProvider</span> = catalog;
			addChild<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">list</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">list</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>ListEvent.<span style="color: #006600;">ITEM_CLICK</span>,choosePattern<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> choosePattern<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:ListEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			dp = <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">item</span>.<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//Only conditional in the entire program</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>dpDisplay<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				removeChild<span style="color: #66cc66;">&#40;</span>dpDisplay<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			dpDisplay = factory.<span style="color: #006600;">makeDP</span><span style="color: #66cc66;">&#40;</span>dp<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>dpDisplay<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, once the List menu is set up, the choosePattern() method does all the work. Further it is accomplished using very little code.</p>
<p>The next class is the Creator. It supplies the interface for the factory. As an abstract class, it cannot be instantiated, but you can include implemented methods. In this case, one method is implemented and the other is abstract.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1151code63'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p115163"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code" id="p1151code63"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Asbstract class</span>
	<span style="color: #808080; font-style: italic;">//Creator</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dpNow:Sprite;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> makeDP<span style="color: #66cc66;">&#40;</span>dp:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			dpNow = chooseDP<span style="color: #66cc66;">&#40;</span>dp<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> dpNow;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// ABSTRACT Method (must be overridden in a subclass)</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> chooseDP<span style="color: #66cc66;">&#40;</span>dp:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The factory itself is a subclass of the abstract Creator class. It must implement the abstract method, and really nothing else. Originally when I had planned on individual ConcreteProduct classes for each design pattern, I had a big <strong>switch</strong> statement with 23 cases. However, by adding a parameter to the Catalog constructor (the ConcreteProduct class), all that’s passed is a string based on the List selected item’s data value.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1151code64'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p115164"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p1151code64"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Factory</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteCreator <span style="color: #0066CC;">extends</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		override protected <span style="color: #000000; font-weight: bold;">function</span> chooseDP<span style="color: #66cc66;">&#40;</span>dp:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Catalog<span style="color: #66cc66;">&#40;</span>dp<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>The Product and Concrete Product Classes</strong></p>
<p>The <strong>big</strong> classes in this application are the two that handle the product elements. That should not come as a surprise because the product is pretty big—12 text fields with text and a graphic file. The abstract Product class has both abstract and implemented methods and lots of them.  With 284 lines of code (including spaces and comments), this was the biggest abstract class I’d ever created. Half the methods are abstract and the other half are implemented. Essentially, it works like a giant getter/setter—or loader/placer to be more precise. Once each load is complete, a mirror function (method) “catches” the loaded materials and places it where it belongs on the DisplayObject (<em>aka</em> stage).</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1151code65'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p115165"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
</pre></td><td class="code" id="p1151code65"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLLoader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldType</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Abstract class</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Product <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loadCheck:URLLoader;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtFld:<span style="color: #0066CC;">TextField</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> typeFld:<span style="color: #0066CC;">TextField</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sumHeadFld:<span style="color: #0066CC;">TextField</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sumFld:<span style="color: #0066CC;">TextField</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> varyHeadFld:<span style="color: #0066CC;">TextField</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> varyFld:<span style="color: #0066CC;">TextField</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> usesHeadFld:<span style="color: #0066CC;">TextField</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> usesFld:<span style="color: #0066CC;">TextField</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> codeHeadFld:<span style="color: #0066CC;">TextField</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> codeFld:<span style="color: #0066CC;">TextField</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> comHeadFld:<span style="color: #0066CC;">TextField</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> comFld:<span style="color: #0066CC;">TextField</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">textFormat</span>:<span style="color: #0066CC;">TextFormat</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> const <span style="color: #0066CC;">TEXTCOLOR</span>:uint = 0x263248;
		<span style="color: #0066CC;">private</span> const TYPECOLOR:uint = 0xFF9800;
		<span style="color: #0066CC;">private</span> const HEADCOLOR:uint = 0x263248;
		<span style="color: #0066CC;">private</span> const BODYCOLOR:uint = 0x000000;
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> loadInfo<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> loadType<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> loadSumHead<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> loadSum<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> loadVaryHead<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> loadVary<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> loadUsesHead<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> loadUses<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> loadCodeHead<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> loadCode<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> loadComHead<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> loadCom<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">//Already Loaded</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> nowLoaded<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			loadCheck = URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">font</span> = <span style="color: #ff0000;">&quot;Arial Black&quot;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">size</span> = <span style="color: #cc66cc;">18</span>;
			txtFld.<span style="color: #0066CC;">type</span> = TextFieldType.<span style="color: #0066CC;">DYNAMIC</span>;
			txtFld.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">300</span>;
			txtFld.<span style="color: #0066CC;">wordWrap</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			txtFld.<span style="color: #0066CC;">textColor</span> = <span style="color: #0066CC;">TEXTCOLOR</span>;
			txtFld.<span style="color: #006600;">defaultTextFormat</span> = <span style="color: #0066CC;">textFormat</span>;
			txtFld.<span style="color: #0066CC;">text</span> = loadCheck.<span style="color: #0066CC;">data</span>;
			addChild<span style="color: #66cc66;">&#40;</span>txtFld<span style="color: #66cc66;">&#41;</span>;
			txtFld.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">128</span>,txtFld.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">10</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> nowType<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			loadCheck = URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">font</span> = <span style="color: #ff0000;">&quot;Arial Black&quot;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">size</span> = <span style="color: #cc66cc;">14</span>;
			typeFld.<span style="color: #0066CC;">type</span> = TextFieldType.<span style="color: #0066CC;">DYNAMIC</span>;
			typeFld.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">300</span>;
			typeFld.<span style="color: #0066CC;">wordWrap</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			typeFld.<span style="color: #0066CC;">textColor</span> = TYPECOLOR;
			typeFld.<span style="color: #006600;">defaultTextFormat</span> = <span style="color: #0066CC;">textFormat</span>;
			typeFld.<span style="color: #0066CC;">text</span> = loadCheck.<span style="color: #0066CC;">data</span>;
			addChild<span style="color: #66cc66;">&#40;</span>typeFld<span style="color: #66cc66;">&#41;</span>;
			typeFld.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">368</span>,typeFld.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">16</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> nowSumHead<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			loadCheck = URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">font</span> = <span style="color: #ff0000;">&quot;Arial Black&quot;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">size</span> = <span style="color: #cc66cc;">14</span>;
			sumHeadFld.<span style="color: #0066CC;">type</span> = TextFieldType.<span style="color: #0066CC;">DYNAMIC</span>;
			sumHeadFld.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">175</span>;
			sumHeadFld.<span style="color: #0066CC;">wordWrap</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			sumHeadFld.<span style="color: #0066CC;">textColor</span> = HEADCOLOR;
			sumHeadFld.<span style="color: #006600;">defaultTextFormat</span> = <span style="color: #0066CC;">textFormat</span>;
			sumHeadFld.<span style="color: #0066CC;">text</span> = loadCheck.<span style="color: #0066CC;">data</span>;
			addChild<span style="color: #66cc66;">&#40;</span>sumHeadFld<span style="color: #66cc66;">&#41;</span>;
			sumHeadFld.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">148</span>,sumHeadFld.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">409</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> nowSum<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			loadCheck = URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">font</span> = <span style="color: #ff0000;">&quot;Arial&quot;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">size</span> = <span style="color: #cc66cc;">12</span>;
			sumFld.<span style="color: #0066CC;">type</span> = TextFieldType.<span style="color: #0066CC;">DYNAMIC</span>;
			sumFld.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">200</span>;
			sumFld.<span style="color: #0066CC;">wordWrap</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			sumFld.<span style="color: #0066CC;">textColor</span> = BODYCOLOR;
			sumFld.<span style="color: #006600;">defaultTextFormat</span> = <span style="color: #0066CC;">textFormat</span>;
			sumFld.<span style="color: #0066CC;">text</span> = loadCheck.<span style="color: #0066CC;">data</span>;
			addChild<span style="color: #66cc66;">&#40;</span>sumFld<span style="color: #66cc66;">&#41;</span>;
			sumFld.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">148</span>,sumFld.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">431</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> nowVaryHead<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			loadCheck = URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">font</span> = <span style="color: #ff0000;">&quot;Arial Black&quot;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">size</span> = <span style="color: #cc66cc;">14</span>;
			varyHeadFld.<span style="color: #0066CC;">type</span> = TextFieldType.<span style="color: #0066CC;">DYNAMIC</span>;
			varyHeadFld.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">175</span>;
			varyHeadFld.<span style="color: #0066CC;">wordWrap</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			varyHeadFld.<span style="color: #0066CC;">textColor</span> = HEADCOLOR;
			varyHeadFld.<span style="color: #006600;">defaultTextFormat</span> = <span style="color: #0066CC;">textFormat</span>;
			varyHeadFld.<span style="color: #0066CC;">text</span> = loadCheck.<span style="color: #0066CC;">data</span>;
			addChild<span style="color: #66cc66;">&#40;</span>varyHeadFld<span style="color: #66cc66;">&#41;</span>;
			varyHeadFld.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">684</span>,varyHeadFld.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">409</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> nowVary<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			loadCheck = URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">font</span> = <span style="color: #ff0000;">&quot;Arial&quot;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">size</span> = <span style="color: #cc66cc;">12</span>;
			varyFld.<span style="color: #0066CC;">type</span> = TextFieldType.<span style="color: #0066CC;">DYNAMIC</span>;
			varyFld.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">175</span>;
			varyFld.<span style="color: #0066CC;">wordWrap</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			varyFld.<span style="color: #0066CC;">textColor</span> = BODYCOLOR;
			varyFld.<span style="color: #006600;">defaultTextFormat</span> = <span style="color: #0066CC;">textFormat</span>;
			varyFld.<span style="color: #0066CC;">text</span> = loadCheck.<span style="color: #0066CC;">data</span>;
			addChild<span style="color: #66cc66;">&#40;</span>varyFld<span style="color: #66cc66;">&#41;</span>;
			varyFld.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">684</span>,varyFld.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">431</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> nowUsesHead<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			loadCheck = URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">font</span> = <span style="color: #ff0000;">&quot;Arial Black&quot;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">size</span> = <span style="color: #cc66cc;">14</span>;
			usesHeadFld.<span style="color: #0066CC;">type</span> = TextFieldType.<span style="color: #0066CC;">DYNAMIC</span>;
			usesHeadFld.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">175</span>;
			usesHeadFld.<span style="color: #0066CC;">wordWrap</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			usesHeadFld.<span style="color: #0066CC;">textColor</span> = HEADCOLOR;
			usesHeadFld.<span style="color: #006600;">defaultTextFormat</span> = <span style="color: #0066CC;">textFormat</span>;
			usesHeadFld.<span style="color: #0066CC;">text</span> = loadCheck.<span style="color: #0066CC;">data</span>;
			addChild<span style="color: #66cc66;">&#40;</span>usesHeadFld<span style="color: #66cc66;">&#41;</span>;
			usesHeadFld.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">148</span>,usesHeadFld.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">504</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> nowUses<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			loadCheck = URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">font</span> = <span style="color: #ff0000;">&quot;Arial&quot;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">size</span> = <span style="color: #cc66cc;">12</span>;
			usesFld.<span style="color: #0066CC;">type</span> = TextFieldType.<span style="color: #0066CC;">DYNAMIC</span>;
			usesFld.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">175</span>;
			usesFld.<span style="color: #0066CC;">wordWrap</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			usesFld.<span style="color: #0066CC;">textColor</span> = BODYCOLOR;
			usesFld.<span style="color: #006600;">defaultTextFormat</span> = <span style="color: #0066CC;">textFormat</span>;
			usesFld.<span style="color: #0066CC;">text</span> = loadCheck.<span style="color: #0066CC;">data</span>;
			addChild<span style="color: #66cc66;">&#40;</span>usesFld<span style="color: #66cc66;">&#41;</span>;
			usesFld.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">148</span>,usesFld.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">528</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> nowCodeHead<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			loadCheck = URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">font</span> = <span style="color: #ff0000;">&quot;Arial Black&quot;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">size</span> = <span style="color: #cc66cc;">14</span>;
			codeHeadFld.<span style="color: #0066CC;">type</span> = TextFieldType.<span style="color: #0066CC;">DYNAMIC</span>;
			codeHeadFld.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">175</span>;
			codeHeadFld.<span style="color: #0066CC;">wordWrap</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			codeHeadFld.<span style="color: #0066CC;">textColor</span> = HEADCOLOR;
			codeHeadFld.<span style="color: #006600;">defaultTextFormat</span> = <span style="color: #0066CC;">textFormat</span>;
			codeHeadFld.<span style="color: #0066CC;">text</span> = loadCheck.<span style="color: #0066CC;">data</span>;
			addChild<span style="color: #66cc66;">&#40;</span>codeHeadFld<span style="color: #66cc66;">&#41;</span>;
			codeHeadFld.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">350</span>,codeHeadFld.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">409</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> nowCode<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			loadCheck = URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">font</span> = <span style="color: #ff0000;">&quot;Courier New&quot;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">size</span> = <span style="color: #cc66cc;">12</span>;
			codeFld.<span style="color: #0066CC;">type</span> = TextFieldType.<span style="color: #0066CC;">DYNAMIC</span>;
			codeFld.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">323</span>;
			codeFld.<span style="color: #0066CC;">height</span> = <span style="color: #cc66cc;">215</span>;
			codeFld.<span style="color: #0066CC;">wordWrap</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			codeFld.<span style="color: #0066CC;">textColor</span> = BODYCOLOR;
			codeFld.<span style="color: #006600;">defaultTextFormat</span> = <span style="color: #0066CC;">textFormat</span>;
			codeFld.<span style="color: #0066CC;">text</span> = loadCheck.<span style="color: #0066CC;">data</span>;
			addChild<span style="color: #66cc66;">&#40;</span>codeFld<span style="color: #66cc66;">&#41;</span>;
			codeFld.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">350</span>,codeFld.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">430</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> nowComHead<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			loadCheck = URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">font</span> = <span style="color: #ff0000;">&quot;Arial Black&quot;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">size</span> = <span style="color: #cc66cc;">14</span>;
			comHeadFld.<span style="color: #0066CC;">type</span> = TextFieldType.<span style="color: #0066CC;">DYNAMIC</span>;
			comHeadFld.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">175</span>;
			comHeadFld.<span style="color: #0066CC;">wordWrap</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			comHeadFld.<span style="color: #0066CC;">textColor</span> = HEADCOLOR;
			comHeadFld.<span style="color: #006600;">defaultTextFormat</span> = <span style="color: #0066CC;">textFormat</span>;
			comHeadFld.<span style="color: #0066CC;">text</span> = loadCheck.<span style="color: #0066CC;">data</span>;
			addChild<span style="color: #66cc66;">&#40;</span>comHeadFld<span style="color: #66cc66;">&#41;</span>;
			comHeadFld.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">684</span>,comHeadFld.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">458</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> nowCom<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			loadCheck = URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">font</span> = <span style="color: #ff0000;">&quot;Arial&quot;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">size</span> = <span style="color: #cc66cc;">12</span>;
			comFld.<span style="color: #0066CC;">type</span> = TextFieldType.<span style="color: #0066CC;">DYNAMIC</span>;
			comFld.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">242</span>;
			comFld.<span style="color: #0066CC;">height</span> = <span style="color: #cc66cc;">164</span>;
			comFld.<span style="color: #0066CC;">wordWrap</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			comFld.<span style="color: #0066CC;">textColor</span> = BODYCOLOR;
			comFld.<span style="color: #006600;">defaultTextFormat</span> = <span style="color: #0066CC;">textFormat</span>;
			comFld.<span style="color: #0066CC;">text</span> = loadCheck.<span style="color: #0066CC;">data</span>;
			addChild<span style="color: #66cc66;">&#40;</span>comFld<span style="color: #66cc66;">&#41;</span>;
			comFld.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">684</span>,comFld.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">480</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//Load Image</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> loadImg<span style="color: #66cc66;">&#40;</span>img:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Each implemented method works the same. It gets the Event instance from the loading sequence (implemented in the Catalog class—a Concrete Product) and then puts it where it belongs and formats it.</p>
<p>Finally, the ConcreteProduct is the Catalog class. It loads all of the materials it needs and then uses the methods it inherited from the Product class to place the text and graphic.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1151code66'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p115166"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
</pre></td><td class="code" id="p1151code66"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLLoader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Catalog <span style="color: #0066CC;">extends</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dp:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> nameText:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> typeText:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sumHead:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sumText:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> varyHead:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> varyText:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> usesHead:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> usesText:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> codeHead:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> codeText:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> comHead:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> comText:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dpImage:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">url</span>:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loader:Loader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> fileNow:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtLoader:URLLoader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> typeLoader:URLLoader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sumHeadLoader:URLLoader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sumLoader:URLLoader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> varyHeadLoader:URLLoader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> varyLoader:URLLoader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> usesHeadLoader:URLLoader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> usesLoader:URLLoader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> codeHeadLoader:URLLoader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> codeLoader:URLLoader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> comHeadLoader:URLLoader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> comLoader:URLLoader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loadCheck:URLLoader;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Catalog<span style="color: #66cc66;">&#40;</span>dp:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">dp</span>=dp;
			dp+=<span style="color: #ff0000;">&quot;/&quot;</span>;
			nameText = dp + <span style="color: #ff0000;">&quot;name.txt&quot;</span>;
			typeText = dp + <span style="color: #ff0000;">&quot;type.txt&quot;</span>;
			sumHead = dp + <span style="color: #ff0000;">&quot;sumhead.txt&quot;</span>;
			sumText = dp + <span style="color: #ff0000;">&quot;summary.txt&quot;</span>;
			varyHead = dp + <span style="color: #ff0000;">&quot;varyhead.txt&quot;</span>;
			varyText = dp + <span style="color: #ff0000;">&quot;vary.txt&quot;</span>;
			usesHead = dp + <span style="color: #ff0000;">&quot;useshead.txt&quot;</span>;
			usesText = dp + <span style="color: #ff0000;">&quot;uses.txt&quot;</span>;
			codeHead = dp + <span style="color: #ff0000;">&quot;codehead.txt&quot;</span>;
			codeText = dp + <span style="color: #ff0000;">&quot;code.txt&quot;</span>;
			comHead = dp + <span style="color: #ff0000;">&quot;comhead.txt&quot;</span>;
			comText = dp + <span style="color: #ff0000;">&quot;com.txt&quot;</span>;
			dpImage = dp + <span style="color: #ff0000;">&quot;diagram.png&quot;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//All of these function are below</span>
			loadInfo<span style="color: #66cc66;">&#40;</span>nameText<span style="color: #66cc66;">&#41;</span>;
			loadType<span style="color: #66cc66;">&#40;</span>typeText<span style="color: #66cc66;">&#41;</span>;
			loadSumHead<span style="color: #66cc66;">&#40;</span>sumHead<span style="color: #66cc66;">&#41;</span>;
			loadSum<span style="color: #66cc66;">&#40;</span>sumText<span style="color: #66cc66;">&#41;</span>;
			loadVaryHead<span style="color: #66cc66;">&#40;</span>varyHead<span style="color: #66cc66;">&#41;</span>;
			loadVary<span style="color: #66cc66;">&#40;</span>varyText<span style="color: #66cc66;">&#41;</span>;
			loadUsesHead<span style="color: #66cc66;">&#40;</span>usesHead<span style="color: #66cc66;">&#41;</span>;
			loadUses<span style="color: #66cc66;">&#40;</span>usesText<span style="color: #66cc66;">&#41;</span>;
			loadCodeHead<span style="color: #66cc66;">&#40;</span>codeHead<span style="color: #66cc66;">&#41;</span>;
			loadCode<span style="color: #66cc66;">&#40;</span>codeText<span style="color: #66cc66;">&#41;</span>;
			loadComHead<span style="color: #66cc66;">&#40;</span>comHead<span style="color: #66cc66;">&#41;</span>;
			loadCom<span style="color: #66cc66;">&#40;</span>comText<span style="color: #66cc66;">&#41;</span>;
			loadImg<span style="color: #66cc66;">&#40;</span>dpImage<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> loadInfo<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			txtLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			txtLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>,nowLoaded<span style="color: #66cc66;">&#41;</span>;
			fileNow = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>txtInfo<span style="color: #66cc66;">&#41;</span>;
			txtLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>fileNow<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> loadType<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			typeLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			typeLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>,nowType<span style="color: #66cc66;">&#41;</span>;
			fileNow = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>txtInfo<span style="color: #66cc66;">&#41;</span>;
			typeLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>fileNow<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> loadSumHead<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			sumHeadLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			sumHeadLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>,nowSumHead<span style="color: #66cc66;">&#41;</span>;
			fileNow = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>txtInfo<span style="color: #66cc66;">&#41;</span>;
			sumHeadLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>fileNow<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> loadSum<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			sumLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			sumLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>,nowSum<span style="color: #66cc66;">&#41;</span>;
			fileNow = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>txtInfo<span style="color: #66cc66;">&#41;</span>;
			sumLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>fileNow<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> loadVaryHead<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			varyHeadLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			varyHeadLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>,nowVaryHead<span style="color: #66cc66;">&#41;</span>;
			fileNow = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>txtInfo<span style="color: #66cc66;">&#41;</span>;
			varyHeadLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>fileNow<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> loadVary<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			varyLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			varyLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>,nowVary<span style="color: #66cc66;">&#41;</span>;
			fileNow = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>txtInfo<span style="color: #66cc66;">&#41;</span>;
			varyLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>fileNow<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> loadUsesHead<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			usesHeadLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			usesHeadLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>,nowUsesHead<span style="color: #66cc66;">&#41;</span>;
			fileNow = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>txtInfo<span style="color: #66cc66;">&#41;</span>;
			usesHeadLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>fileNow<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> loadUses<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			usesLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			usesLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>,nowUses<span style="color: #66cc66;">&#41;</span>;
			fileNow = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>txtInfo<span style="color: #66cc66;">&#41;</span>;
			usesLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>fileNow<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> loadCodeHead<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			codeHeadLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			codeHeadLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>,nowCodeHead<span style="color: #66cc66;">&#41;</span>;
			fileNow = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>txtInfo<span style="color: #66cc66;">&#41;</span>;
			codeHeadLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>fileNow<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> loadCode<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			codeLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			codeLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>,nowCode<span style="color: #66cc66;">&#41;</span>;
			fileNow = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>txtInfo<span style="color: #66cc66;">&#41;</span>;
			codeLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>fileNow<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> loadComHead<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			comHeadLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			comHeadLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>,nowComHead<span style="color: #66cc66;">&#41;</span>;
			fileNow = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>txtInfo<span style="color: #66cc66;">&#41;</span>;
			comHeadLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>fileNow<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> loadCom<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			comLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			comLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>,nowCom<span style="color: #66cc66;">&#41;</span>;
			fileNow = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>txtInfo<span style="color: #66cc66;">&#41;</span>;
			comLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>fileNow<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">//Load image</span>
		override protected <span style="color: #000000; font-weight: bold;">function</span> loadImg<span style="color: #66cc66;">&#40;</span>dpImage:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">url</span> = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>dpImage<span style="color: #66cc66;">&#41;</span>;
			loader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			loader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>loader<span style="color: #66cc66;">&#41;</span>;
			loader.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">128</span>,loader.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">34</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>That’s the whole enchilada, and it is a big one. Both of the product classes are much bigger than I like, but the nature of the application requires it. However, even with all of the methods and instances in two different Factory Method class sets, there’s only a single conditional statement, and it’s in the Client—where it doesn’t count as part of the design pattern. (I could even eliminate it if I put in a dummy display sprite—but why go fanatic?)</p>
<p><strong>Finishing Up</strong></p>
<p>All that’s left are 17 more design patterns and some kind of splash page. I think that the splash page should have the different elements of the UML class diagrams—what the different arrows and symbols mean.  If you can think of anything else, let us know. Also, play around with the AIR version and let me know if you have any additional features that you think would help. Also, if you want to pitch in and help fill in the different design patterns data, contact me and I’ll be glad to recruit you! (Is there such a thing as an online <strong>Hero Medal</strong>?)</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%2F07%2Factionscript-30-design-pattern-catalog-creational-patterns%2F&amp;title=ActionScript%203.0%20Design%20Pattern%20Catalog%3A%20Creational%20Patterns" id="wpa2a_16"><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/07/actionscript-30-design-pattern-catalog-creational-patterns/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Friends with Benefits: State and Factory Method Together at Last—Part II</title>
		<link>http://www.as3dp.com/2009/06/friends-with-benefits-state-and-factory-method-together-at-last-part-ii/</link>
		<comments>http://www.as3dp.com/2009/06/friends-with-benefits-state-and-factory-method-together-at-last-part-ii/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 09:23:47 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Factory Method]]></category>
		<category><![CDATA[Polymorphism]]></category>
		<category><![CDATA[State]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1106</guid>
		<description><![CDATA[In the first part of this article, Friends with Benefits: Refactoring with Multiple Design Patterns—Part I you saw how a Factory Method design pattern was used to hold and deliver data to a DataProvider object. The Factory Method design pattern allowed for easy update by simply adding items to concrete products from a Product interface [...]]]></description>
			<content:encoded><![CDATA[<p>In the first part of this article, <a href="http://www.as3dp.com/2009/06/16/friends-with-benefits-refactoring-with-multiple-design-patterns—part-i/"> Friends with Benefits: Refactoring with Multiple Design Patterns—Part I</a> you saw how a Factory Method design pattern was used to hold and deliver data to a DataProvider object. The Factory Method design pattern allowed for easy update by simply adding items to concrete products from a Product interface (DataSupply) or new concrete Product classes. New data can be added to the Concrete Products and the Client makes requests through the Creator interface. The request is filtered through the Concrete Creator to the Product interface to the concrete Product classes. Now, all we need to do is to refactor the player with a design pattern. However, before starting <strong> download </strong> the application (With 12 classes in the design patterns, a Client class plus three folders of videos, there’s a lot!)  So first off, <a href="http://www.sandlight.net/dpBlog/FactoryMethodState.zip">click here to Download All Files </a>. You can see a <a href="http://www.sandlight.net/dpBlog/dualdp.html">working version of the player and multiple data sets here</a>. These will help pull together all of the elements used in this project.</p>
<p><strong>Here Comes the Big Bad State Machine</strong></p>
<p>Before we get going on refactoring the video player application, we need to see where it will go in the context of both the State and the Factory Method design patterns. Figure 1 shows the relationship between the refactored player (State) and the factory that delivers the data (Factory Method).<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/06/dualdpfilessm.png" alt="dualdpfilessm" title="dualdpfilessm" width="500" height="392" class="alignnone size-full wp-image-1103" /><br />
<em><strong>Figure 1:</strong> Files for two design patterns in class diagram</em></p>
<p>The images are organized in an “open” folder, and the videos are placed in separate folders showing their relative position to the classes making up the two design patterns. The Client makes a request through the Factory Method to populate a List component with data from a DataProvider. Then, the request from the Client to the player can be made through the List component by clicking on the video selection. Figure 2 shows what the final product looks like so that you can more easily follow the refactoring:<br />
<span id="more-1106"></span><br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/06/dualvid500.png" alt="dualvid500" title="dualvid500" width="500" height="365" class="alignnone size-full wp-image-1114" /></p>
<p><em><strong>Figure 2:</strong> Selected video running in state machine player</em></p>
<p>If Figure 2 looks different from what we started in Part I of this article, it’s because the application changed. Originally, the plan was to take the application made for showing F4V files (H.264 formatted HD files) using the latest version of Flash Media Server (FMS) and show how it was refactored. However, since most readers do not develop with FMS, the plan was changed to show a progressive download application that anyone could use. The State design pattern is almost identical to the one used in our book to develop a player. Likewise, we don’t plan to try your patience by rehashing Chapter 10 or the article on <a href= "http://www.adobe.com/devnet/flashmediaserver/articles/video_state_machine_as3.html" > DevNet </a> that cover statecharts, the details of state machines and the State Design Pattern. Instead, I’d like to focus on the Client and show how it makes requests to different sets of classes using different design patterns. Therefore, I’ll only <em>briefly</em> go over the State Design Pattern to help you orient yourself to its use in the current context with another design pattern.</p>
<p><strong>State Machine as Time Machine</strong></p>
<p>I like to imagine the state machine as a time machine. Each time represents a different state. As you go from one state to another, things look different and the possibilities are different. If you go to the mid 18th century you’ll find horse-powered transportation and in that state you’ll find no air transportation beyond hot air balloons. Conversely, if you go into the future to the 22nd century you may find transportation that runs only on hydrogen and air travel only occurs beyond the stratosphere. In different time periods you expect different capabilities. Likewise, in different states, you have different expectations. For example, in the video player, if you are in a <em>stop state</em> you cannot stop. That’s because in that state, you’re already stopped. Likewise, in a <em>play state</em> you cannot go to play, because you’re already there.</p>
<p>Some states are trickier than others, but you have options. In one video player I made, I would not allow the user to go from Pause to Play or Stop. The user had to first Un-Pause by going to Pause again. That was when the main Pause state was set up as a toggle. One blogger complained that he just couldn’t deal with a Player that would not allow one to go from Pause to Stop or Play. The reason behind my decision was that some funny things happened when one left the Pause state without un-pausing it first. (Now that ActionScript 3.0 has a <strong>NetStream.resume()</strong> method, the toggle is no longer required.) Of course, if one decides that it’s perfectly fine to go from a Pause state to a Play or Stop; all you’ve got to do is to change the code in the Pause state—in fact it’s really easy to do and is one of the advantages of the State Design Pattern. (In case you haven’t noticed, I have no god-like powers to enforce code decisions—you’re programmers; so change the code all you want!)</p>
<p>By setting up your program as state machines, you have the option of easily changing the states. You can change the contents of the states (as suggested in the previous paragraph) or you can add new states. As your application changes and grows, all you need to do is to change the states to reflect the application’s functionality.</p>
<p><strong>Avoiding Conditional Statements</strong></p>
<p>My favorite feature of the State Design Pattern is that it has <em>no conditional statements</em>. Count ‘em—none. In fact, in this entire program, you’ll find only a single <strong>switch</strong> statement in the <strong>ConcreteCreatorVid</strong> class in the Factory Method pattern portion of the program. If you look at different Factory Method patterns in most languages, you’ll find that the concrete creator classes contain one or more conditional statement; so, sometime you <em>do</em> need them. (I’ve concealed one more conditional in the Client that takes care of a little bling, but you’ll have to search to find it—besides it doesn’t count because it’s in the Client.)</p>
<p><a href="http://www.as3dp.com/2007/12/21/lets-get-rid-of-conditionals/">What do I have against conditional statements?</a> Well, it’s not so much that I have anything against conditionals as it is the fact I like the idea of not using them. The State design makes you stop and think of what possibly can happen in a given state. Then instead of first having to filter through conditional statements, you simply do exactly what the current state allows. The logic flow is more pleasing and forces me to be a better programmer.</p>
<p><strong>The Mighty Client</strong></p>
<p>One of the original goals of refactoring my video player was to give it more flexibility and to cut down on the size of a single big class—essentially a huge self-serving Client. The idea of the Client is to make requests, and it <strong>should not be used as</strong> <em>an everything class</em>. Several design patterns include the Client as a participant, but even they recognize the role of the Client as a request-maker.</p>
<p>In Figure 1, you can see that the Client makes requests through the Creator interface in the Factory Method pattern and through the Context class in the State pattern.  A Client making requests from multiple classes or interfaces is not unusual—consider the <a href="http://www.as3dp.com/category/design-patterns/abstract-factory-pattern/"> Abstract Factory </a> pattern. The request-making involves a set of UIs for different data sets for the DataProvider object. It provides the List UI with selections used to request the different videos available for displaying in the player. The following listing shows that the great majority of the Client is taken up by UIs and methods for making request with the UIs.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1106code71'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p110671"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
</pre></td><td class="code" id="p1106code71"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Progressive Download</span>
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">Button</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">List</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">events</span>.<span style="color: #006600;">ListEvent</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">DataProvider</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetConnection</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">NetStatusEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">media</span>.<span style="color: #0066CC;">Video</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnGp1:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnGp2:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnGp3:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> playList:<span style="color: #0066CC;">List</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">List</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> stopBtn:<span style="color: #0066CC;">Button</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> pauseBtn:<span style="color: #0066CC;">Button</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> resumeBtn:<span style="color: #0066CC;">Button</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> nc:<span style="color: #0066CC;">NetConnection</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">NetConnection</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> ns:<span style="color: #0066CC;">NetStream</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> vid:<span style="color: #0066CC;">Video</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Video</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> context:Context=<span style="color: #000000; font-weight: bold;">new</span> Context<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> flv:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> metaCap:<span style="color: #0066CC;">Object</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> stopState:<span style="color: #0066CC;">Boolean</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> mset:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> group1:DataProvider=<span style="color: #000000; font-weight: bold;">new</span> DataProvider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> group2:DataProvider=<span style="color: #000000; font-weight: bold;">new</span> DataProvider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> group3:DataProvider=<span style="color: #000000; font-weight: bold;">new</span> DataProvider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> create:Creator=<span style="color: #000000; font-weight: bold;">new</span> ConcreteCreatorVid<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			nc.<span style="color: #0066CC;">connect</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
			ns = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#40;</span>nc<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//Handle end of flv play status</span>
			ns.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>NetStatusEvent.<span style="color: #006600;">NET_STATUS</span>,flvCheck<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			setUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			setProviders<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//Set up object to capture metadata</span>
			metaCap = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Object</span>  ;
			ns.<span style="color: #006600;">client</span> = metaCap;
			metaCap.<span style="color: #006600;">onMetaData</span> = getMeta;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			addChild<span style="color: #66cc66;">&#40;</span>vid<span style="color: #66cc66;">&#41;</span>;
			vid.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">150</span>;
			vid.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">50</span>;
			vid.<span style="color: #006600;">attachNetStream</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//Video Buttons and List Components</span>
			playList.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">20</span>,playList.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">50</span>;
			addChild<span style="color: #66cc66;">&#40;</span>playList<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			group1 = create.<span style="color: #006600;">selectData</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;group1&quot;</span><span style="color: #66cc66;">&#41;</span>;
			playList.<span style="color: #006600;">dataProvider</span> = group1;
&nbsp;
			stopBtn=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			stopBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Stop&quot;</span>;
			stopBtn.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">50</span>;
			stopBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">142</span>,stopBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">352</span>;
			addChild<span style="color: #66cc66;">&#40;</span>stopBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			pauseBtn=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			pauseBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Pause&quot;</span>;
			pauseBtn.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">50</span>;
			pauseBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">206</span>,pauseBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">352</span>;
			addChild<span style="color: #66cc66;">&#40;</span>pauseBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			resumeBtn=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			resumeBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Resume&quot;</span>;
			resumeBtn.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">55</span>;
			resumeBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">270</span>,resumeBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">352</span>;
			addChild<span style="color: #66cc66;">&#40;</span>resumeBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//Add Event Listeners</span>
			playList.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>ListEvent.<span style="color: #006600;">ITEM_CLICK</span>,doPlay<span style="color: #66cc66;">&#41;</span>;
			stopBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,doStop<span style="color: #66cc66;">&#41;</span>;
			pauseBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,pauseNow<span style="color: #66cc66;">&#41;</span>;
			resumeBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,resumeNow<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setProviders<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//Data provider buttons</span>
			btnGp1.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">20</span>,btnGp1.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">160</span>;
			btnGp1.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Group1&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnGp1<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			btnGp2.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">20</span>,btnGp2.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">190</span>;
			btnGp2.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Group2&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnGp2<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			btnGp3.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">20</span>,btnGp3.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">220</span>;
			btnGp3.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Group3&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnGp3<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//Add Event Listeners</span>
			btnGp1.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,getGp1<span style="color: #66cc66;">&#41;</span>;
			btnGp2.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,getGp2<span style="color: #66cc66;">&#41;</span>;
			btnGp3.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,getGp3<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getMeta<span style="color: #66cc66;">&#40;</span>mdata:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #66cc66;">&#40;</span>mset<span style="color: #66cc66;">&#41;</span> ? removeChildAt<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #000000; font-weight: bold;">null</span>;
			mset = <span style="color: #000000; font-weight: bold;">true</span>;
			vid.<span style="color: #0066CC;">width</span> = mdata.<span style="color: #0066CC;">width</span>;
			vid.<span style="color: #0066CC;">height</span> = mdata.<span style="color: #0066CC;">height</span>;
			<span style="color: #000000; font-weight: bold;">var</span> backdrop:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			backdrop.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x611427,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
			backdrop.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span>vid.<span style="color: #006600;">x</span>,vid.<span style="color: #006600;">y</span>,vid.<span style="color: #0066CC;">width</span>,vid.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span>;
			addChildAt<span style="color: #66cc66;">&#40;</span>backdrop,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getGp1<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			group1 = create.<span style="color: #006600;">selectData</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;group1&quot;</span><span style="color: #66cc66;">&#41;</span>;
			playList.<span style="color: #006600;">dataProvider</span> = group1;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getGp2<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			group2 = create.<span style="color: #006600;">selectData</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;group2&quot;</span><span style="color: #66cc66;">&#41;</span>;
			playList.<span style="color: #006600;">dataProvider</span> = group2;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getGp3<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			group3 = create.<span style="color: #006600;">selectData</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;group3&quot;</span><span style="color: #66cc66;">&#41;</span>;
			playList.<span style="color: #006600;">dataProvider</span> = group3;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> flvCheck<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:NetStatusEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			stopState=<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">info</span>.<span style="color: #006600;">code</span>==<span style="color: #ff0000;">&quot;NetStream.Play.Stop&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#40;</span>stopState<span style="color: #66cc66;">&#41;</span> ? halt<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> halt<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">stopPlay</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
			vid.<span style="color: #0066CC;">clear</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> doPlay<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:ListEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			flv = <span style="color: #0066CC;">e</span>.<span style="color: #006600;">item</span>.<span style="color: #0066CC;">data</span>;
			context.<span style="color: #006600;">startPlay</span><span style="color: #66cc66;">&#40;</span>ns,flv<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> doStop<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			halt<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">function</span> pauseNow<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">pausePlay</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">function</span> resumeNow<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			context.<span style="color: #006600;">resumePlay</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>I’ve got to admit that for a Client class, this one seems pretty big. However, all of the data for selecting the videos and working the video player are either part of the Factory Method or State design patterns. With only slight modifications <em>in the Client</em> this same player can handle FMS files, including F4V files. I’ve used this same State pattern for several different kinds of players, including the one operating FMIS 3.2 and playing high definition videos (<a href="http://www.sandlight.net/prague/"> such as these</a>) that we discussed in Part I of this post.</p>
<p><strong>The Context Class</strong></p>
<p>The Context class is the key to understanding the State Design Pattern as a <em>pattern</em> and not simply a state machine.  When you look at it,  the class appears to be nothing more than a <em>getter/setter</em> class. However, it is far more. It establishes which state is the <strong>current state</strong>. By setting up the current state, it provides for one of the best examples of polymorphism that can be found in OOP. The State interface provides four methods, but depending on the <em>current</em> concrete state class, each method is implemented differently. The following listing shows the Context class used in this example:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1106code72'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p110672"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
</pre></td><td class="code" id="p1106code72"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Context Class</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> Context
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> playState:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> stopState:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> pauseState:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> resumeState:State;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> state:State;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Context<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Video Player is on&quot;</span><span style="color: #66cc66;">&#41;</span>;
			playState = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Play</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			stopState = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Stop</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			pauseState = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Pause</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			resumeState = <span style="color: #000000; font-weight: bold;">new</span> Resume<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
			state=stopState;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span>,flv:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			state.<span style="color: #006600;">startPlay</span><span style="color: #66cc66;">&#40;</span>ns,flv<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			state.<span style="color: #006600;">stopPlay</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pausePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			state.<span style="color: #006600;">pausePlay</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> resumePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			state.<span style="color: #006600;">resumePlay</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setState<span style="color: #66cc66;">&#40;</span>state:State<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;A new state is set&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">state</span>=state;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:State
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> state;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPlayState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:State
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">playState</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getStopState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:State
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">stopState</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPauseState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:State
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">pauseState</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getResumeState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:State
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">resumeState</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The Context class delegates requests (from the Client) to a State object. All methods either use or return a State object. The trick in the Context is to keep track of the current state so that when a requested method is returned to the Client, it is done in the appropriate manner. (e.g., a stop play request <em>will not</em> be sent from a stop state.)</p>
<p><strong>The State Interface and the States</strong></p>
<p>The state interface and the concrete states are relatively easy. Using either an interface or abstract class, the State interface provides the essential methods that can be implemented in the concrete state classes.  First, the State interface shows four simple methods—playing, stopping, pausing and resuming video play:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1106code73'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p110673"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p1106code73"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//State Interface</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
	<span style="color: #0066CC;">interface</span> State
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> startPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span>,flv:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> stopPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> pausePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> resumePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>All of the methods have the same parameters except for the <strong>startPlay()</strong> method that has an additional string parameter for the video URL. The rest are for directing actions in the <strong>NetStream()</strong> object.</p>
<p>Finally, the concrete State classes that implement the State interface are <em>set states</em>. That means that the action associated with the state has been set so that you can possibly do whatever that state allows. For example, if you’re in the Stop state, you can go to the Play state, but since you’re already in the Stop state, it doesn’t make any sense to go there. (Think of yourself at a stop sign in traffic. You can go to “drive” but since you’re at stop, you cannot “go” there.) So, without further ado, let’s look at the four state classes:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1106code74'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p110674"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
</pre></td><td class="code" id="p1106code74"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Stop State</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0066CC;">Stop</span> <span style="color: #0066CC;">implements</span> State
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> context:Context;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">Stop</span><span style="color: #66cc66;">&#40;</span>context:Context<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;--Stop State--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span>=context;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span>,flv:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ns.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span>flv<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Begin playing&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getPlayState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;You're already stopped&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pausePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Cannot go to Pause from Stop.&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> resumePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Cannot go to Resume from Stop.&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Play State</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0066CC;">Play</span> <span style="color: #0066CC;">implements</span> State
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> context:Context;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">Play</span><span style="color: #66cc66;">&#40;</span>context:Context<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;--Play State--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span>=context;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span>,flv:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;You're already playing&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ns.<span style="color: #0066CC;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Stop playing.&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getStopState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pausePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ns.<span style="color: #0066CC;">pause</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Begin pause.&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getPauseState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> resumePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Cannot resume from a play state.&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Pause State</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0066CC;">Pause</span> <span style="color: #0066CC;">implements</span> State
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> context:Context;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">Pause</span><span style="color: #66cc66;">&#40;</span>context:Context<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;--Pause State--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span>=context;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span>,flv:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;You have to go to resume&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Don't go to Stop from Pause&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//Allow going from Pause state to Stop state</span>
			<span style="color: #808080; font-style: italic;">//ns.close();</span>
			<span style="color: #808080; font-style: italic;">//context.setState(context.getStopState());</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pausePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Cannot pause from Pause state.&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> resumePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ns.<span style="color: #006600;">resume</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Quit pausing.&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getResumeState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Resume</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> Resume <span style="color: #0066CC;">implements</span> State
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> context:Context;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Resume<span style="color: #66cc66;">&#40;</span>context:Context<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;--Resume State--&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">context</span>=context;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span>,flv:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Start over&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getPlayState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> stopPlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ns.<span style="color: #0066CC;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Stop playing.&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getStopState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pausePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			ns.<span style="color: #0066CC;">pause</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Pause.&quot;</span><span style="color: #66cc66;">&#41;</span>;
			context.<span style="color: #006600;">setState</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">getPauseState</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> resumePlay<span style="color: #66cc66;">&#40;</span>ns:<span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Already in resume state.&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In addition to providing a set of methods that direct the program to the next possible set of states, the constructor function in each state class sets itself as the current state. Put differently, each state sets the current context to itself.</p>
<p><strong>Using Multiple Design Patterns</strong></p>
<p>Creating programs with multiple design patterns is no more unusual than writing programs with multiple methods or properties. Further, this particular application suggests that you can mix and match design patterns and reuse the major components for any number of projects. Further, the Client doesn’t know (or care) what changes are made in the different components as long as they follow the rules laid out by the interface and the request to the appropriate component in the pattern is unchanged.</p>
<p>The difficult part is doing it right the first time. Then, later as the program requires changes (<em>as it always does</em>), it’s easy to do so. Further, you can take the exact same design patterns and use them for different applications, which saves time as well.</p>
<p>As a final note, I’ve left several trace statements in the code to help you better see what happens in different states. Once in a given state and running the program from within either Flex or Flash, note the trace output. Also, try requesting “impossible” states—such as pressing the Stop button when the program is already stopped. Finally, if you do not like a condition placed on a state (such as disallowing movement from one state to a certain different state), go ahead and change the program.</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%2F06%2Ffriends-with-benefits-state-and-factory-method-together-at-last-part-ii%2F&amp;title=Friends%20with%20Benefits%3A%20State%20and%20Factory%20Method%20Together%20at%20Last%E2%80%94Part%20II" id="wpa2a_18"><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/06/friends-with-benefits-state-and-factory-method-together-at-last-part-ii/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Friends with Benefits: Refactoring with Multiple Design Patterns—Part I</title>
		<link>http://www.as3dp.com/2009/06/friends-with-benefits-refactoring-with-multiple-design-patterns-part-i/</link>
		<comments>http://www.as3dp.com/2009/06/friends-with-benefits-refactoring-with-multiple-design-patterns-part-i/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 21:43:03 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[Factory Method]]></category>
		<category><![CDATA[State]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1077</guid>
		<description><![CDATA[The Case of the Crowded Client After I made my video player that I planned to use to illustrate refactoring a non-design pattern program into a design pattern one, I noticed how crowded the Client class had become. Most of the crowding was caused by creating and populating UIs and event handling functions. One class [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The Case of the Crowded Client</strong></p>
<p>After I made my video player that I planned to use to illustrate refactoring a non-design pattern program into a design pattern one, I noticed how crowded the Client class had become. Most of the crowding was caused by creating and populating UIs and event handling functions. One class that I use a lot to populate UI components is the DataProvider. As I went through and edited and organized my videos from a vacation to Prague, CZ, the DataProvider kept growing. Using MP4 files converted into F4V files, I used very similar formats for the data portion of <em>all</em> of the DataProvider instance. Why not put the DataProvider data into a separate class? Then, I could just call an instance of the class with the data and not have to clutter the Client class with messy DataProvider information.</p>
<p><strong>Lazy Programming can be a Lot of Work</strong></p>
<p>After moving the DataProvider data to another class, I realized that I had two other projects using the video player that accessed the same kind of data. So, while I was at it, I might as well add classes for these other projects. In fact, why not add a common interface as well? Then I could program all data requests to the interface instead of the implementation. Further, I could add some separation between the DataProvider items and the Client requests by adding a factory, and if I were going to do that, I might as well go ahead and create a Factory Method design pattern. Then, whenever I wanted to add a class with data for different projects I could do so without having to worry about messing up the rest of the program. Each project was handled using a Concrete Product class.</p>
<p><strong>The Big Picture</strong></p>
<p>To begin with a clear idea of what is going on in this refactoring exercise, you’ll need to get the big picture. Figure 1 provides an overview of how the video player is to be refactored using two design patterns:<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/06/twodps1.png" alt="twodps1" title="twodps1" width="294" height="198" class="alignnone size-full wp-image-1089" /></p>
<p>The video player is essentially a state machine, and in Part II of this post, we’ll look at how we’ll refactor the hack-job player into a state player.<br />
<span id="more-1077"></span><br />
<strong>Getting and Using the Data</strong></p>
<p>Using a List component with ActionScript 3.0 code requires that a DataProvider object be assigned as a data source. The DataProvider object can be loaded with data in several ways—using the addItem() method, an array or XML files. For this project, I’ll be using the addItem() method, but you can substitute the other data loading techniques if you wish.</p>
<p>The DataProvider object requires two elements for this project. The first element is a label for the List component and the second is the URL to the video. The label element requires nothing more than a label indicating what video plays. Using Flash Media Interactive Server 3.5.2 (FMIS) and H. 264 format requires the following kind of reference:</p>
<p>&nbsp; &nbsp;&nbsp;<strong>mp4: folderName/fileName.f4v</strong></p>
<p>If you’re not familiar with Flash Media Server, no problem. This system works with a simple progressive download as well. So if you’d rather work with video files from a Web server, you can change the reference to:</p>
<p>&nbsp; &nbsp; &nbsp;<strong> folderName/fileName.flv</strong></p>
<p>(You cannot use F4V (H.264) files with progressive download).</p>
<p>So, the basic data-loading procedure is:</p>
<p><strong><code>dataProvider.addItem( { label: "LabelName", data:" mp4: folderName/fileName.f4v " } );</code></strong></p>
<p>In this first post, all I want to do is to look to see how the DataProvider is moved out of the Client and into a Factory Method design pattern.</p>
<p><strong>Getting Data From the DataProvider Object</strong></p>
<p>Initially, the Client contained the code for all operations involving the DataProvider.  The List instance (list) was then assigned the DataProvider instance. The following shows code snippet the relevant features in the Client</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1077code86'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p107786"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p1077code86"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> prague:DataProvider=<span style="color: #000000; font-weight: bold;">new</span> DataProvider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
prague.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Panorama&quot;</span>, <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;mp4:praguevid/panorama.f4v&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
prague.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Charles Bridge&quot;</span>, <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;mp4:praguevid/charlesbridge.f4v&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
prague.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Old &amp; New Town&quot;</span>, <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;mp4:praguevid/newtown.f4v&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
prague.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Mucha Glass&quot;</span>, <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;mp4:praguevid/mucha.f4v&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">list</span>.<span style="color: #006600;">dataProvider</span> = prague;</pre></td></tr></table></div>

<p>Once the data has been added to the List object, it is retrieved when an item in the list is clicked. The line,</p>
<p> &nbsp;&nbsp;&nbsp;<strong>list.addEventListener(ListEvent.ITEM_CLICK,startPlay);</strong></p>
<p> triggers a function used to get the URL for the video and play the selected video. The following operation shows how the selection and play events are handled:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1077code87'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p107787"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p1077code87"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> startPlay<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:ListEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//f4vid is a private string variable</span>
		f4vid = <span style="color: #0066CC;">e</span>.<span style="color: #006600;">item</span>.<span style="color: #0066CC;">data</span>;
		vid.<span style="color: #006600;">attachNetStream</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
		ns.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span>f4vid<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The key connection in the function is assigning the value of <strong>e.item.data</strong> to the string variable (f4vid). The data are used as the URL to play the selected video. That operation is fine the way it is and belongs in the Client as a request. What needs to be removed from the Client is the operation that populates the DataProvider.</p>
<p><strong>Building a Factory Method for Populating and Returning a DataProvider: A Friend with Benefits</strong></p>
<p>You might be asking yourself,<br />
<blockquote>Is this really necessary? Why not just leave the DataProvider in the Client and populate it there?</p></blockquote>
<p>The short answer is, <em>of course not!</em> It is not necessary, but you will find many <strong>benefits</strong> in doing so:</p>
<ul>
<li>Removes clutter from the Client </li>
<li>Client can make request from external source </li>
<li>Contents of DataProvider can be changed without breaking the rest of the program </li>
<li>Other programs can use the same design pattern</li>
<li>Adds flexibility for change and development </li>
</ul>
<p>To get started, Figure 2 shows a chart diagram for this project dealing with a single concrete product—data for a series of videos.<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/06/factorymethodprague.png" alt="factorymethodprague" title="factorymethodprague" width="489" height="236" class="alignnone size-full wp-image-1080" /><br />
<em><strong>Figure 2:</strong> Factory Method for a single set of data</em></p>
<p>In this case, the List object is in the Client class, but because it is the only object in the class that uses the data generated in the Factory Method design pattern, it is listed.</p>
<p>First, the Creator class provides the interface for all concrete creators. Most importantly is the factory method to be used in creating and delivering the DataProvider to the Client for use in the List component.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1077code88'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p107788"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code" id="p1077code88"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Abstract class</span>
	<span style="color: #808080; font-style: italic;">//Creator</span>
	<span style="color: #0066CC;">import</span> fl.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">DataProvider</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dataNow:DataProvider;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> selectData<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:DataProvider
		<span style="color: #66cc66;">&#123;</span>
			dataNow = createData<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> dataNow;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// ABSTRACT Method (must be overridden in a subclass)</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> createData<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:DataProvider
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>


<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1077code89'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p107789"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p1077code89"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Creator (Factory)</span>
	<span style="color: #0066CC;">import</span> fl.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">DataProvider</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteCreatorPrague <span style="color: #0066CC;">extends</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> ds:DataSupply;
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> createData<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:DataProvider
		<span style="color: #66cc66;">&#123;</span>
			ds=<span style="color: #000000; font-weight: bold;">new</span> Prague<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span>ds.<span style="color: #006600;">deliverData</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>


<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1077code90'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p107790"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p1077code90"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> fl.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">DataProvider</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Product</span>
	<span style="color: #0066CC;">interface</span> DataSupply
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> deliverData<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:DataProvider;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>


<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1077code91'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p107791"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p1077code91"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> fl.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">DataProvider</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Prague <span style="color: #0066CC;">implements</span> DataSupply
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> deliverData<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:DataProvider
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> prague:DataProvider=<span style="color: #000000; font-weight: bold;">new</span> DataProvider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			prague.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Panorama&quot;</span>, <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;mp4:praguevid/panorama.f4v&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
			prague.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Charles Bridge&quot;</span>, <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;mp4:praguevid/charlesbridge.f4v&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
			prague.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Old &amp; New Town&quot;</span>, <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;mp4:praguevid/newtown.f4v&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
			prague.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Mucha Glass&quot;</span>, <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;mp4:praguevid/mucha.f4v&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> prague;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Finally, all that the Client needs is to request the data it needs. Because, only a single concrete product has been created, this is simply a matter of creating List and DataProvider instances. Then to populate the DataProvider with data, the Client requests the data by instantiating the desired concrete creator through the Creator interface (abstract class). The following listing shows the operations in the process:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1077code92'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p107792"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p1077code92"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> create:Creator=<span style="color: #000000; font-weight: bold;">new</span> ConcreteCreatorPrague<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> prague: DataProvider=<span style="color: #000000; font-weight: bold;">new</span> DataProvider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
prague=create.<span style="color: #006600;">selectData</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">list</span>:<span style="color: #0066CC;">List</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">List</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">list</span>.<span style="color: #006600;">dataProvider</span> = prague;</pre></td></tr></table></div>

<p>Under any circumstances you need to create a DataProvider instance, a List instance and assign a DataProvider object to the List object. Using the Factory Method pattern, the only line of code required to request the contents for the DataProvider is the following:</p>
<p>&nbsp;&nbsp;&nbsp;<strong>prague=create.selectData();</strong></p>
<p>You can have hundreds of elements in the DataProvider, but using the suggested pattern, you still just need a single line in the Client to access it.</p>
<p><strong>Expanding The Concrete Products Shows the Benefits</strong></p>
<p>After going through the process we have, you may be thinking, <em>What benefits?</em>. After all, the DataProvider class itself is a pretty handy class in its own right. Why complicate things?</p>
<p>For a single data source, it is a bit hefty, but when you look at a larger picture, you can see that this Factory Method program can be used as a reliable mechanism to add more data sources. Figure 3 shows an expanded version of the original pattern:<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/06/factorymethoddata.png" alt="factorymethoddata" title="factorymethoddata" width="634" height="298" class="alignnone size-full wp-image-1081" /><br />
<em><strong>Figure 3:</strong> Factory Method for expanded set of data</em></p>
<p>Before you get started on this new set of classes, you might want to <a href="http://www.sandlight.net/dpBlog/FactoryMethodDataProvide.zip"> download the files here</a>. The Product interface (DataSupply) is the same as the original. However, because the concrete creator has more than a single option from the concrete products now, we’ll have to add a parameter to the factory method for choosing the concrete product we want. However, as you can see in the following program, there’s not much that is changed from the original Creator class:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1077code93'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p107793"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code" id="p1077code93"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Abstract class</span>
	<span style="color: #808080; font-style: italic;">//Creator</span>
	<span style="color: #0066CC;">import</span> fl.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">DataProvider</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dataNow:DataProvider;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> selectData<span style="color: #66cc66;">&#40;</span>dataSource:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:DataProvider
		<span style="color: #66cc66;">&#123;</span>
			dataNow = createData<span style="color: #66cc66;">&#40;</span>dataSource<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> dataNow;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// ABSTRACT Method (must be overridden in a subclass)</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> createData<span style="color: #66cc66;">&#40;</span>dataSource:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:DataProvider
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The concrete creator needs to be changed as well, both to add the new parameter and to set up a way to choose among the concrete products. It has been renamed, <strong>ConcreteCreatorVid</strong> to reflect that it is used in dealing with data that is used with the video player:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1077code94'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p107794"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code" id="p1077code94"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Factory</span>
	<span style="color: #0066CC;">import</span> fl.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">DataProvider</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteCreatorVid <span style="color: #0066CC;">extends</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> ds:DataSupply;
		override protected <span style="color: #000000; font-weight: bold;">function</span> createData<span style="color: #66cc66;">&#40;</span>dataSource:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:DataProvider
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>dataSource<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;prague&quot;</span> :
					ds=<span style="color: #000000; font-weight: bold;">new</span> Prague<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #b1b100;">return</span><span style="color: #66cc66;">&#40;</span>ds.<span style="color: #006600;">deliverData</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #b1b100;">break</span>;
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;graduation&quot;</span> :
					ds=<span style="color: #000000; font-weight: bold;">new</span> Graduation<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #b1b100;">return</span><span style="color: #66cc66;">&#40;</span>ds.<span style="color: #006600;">deliverData</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #b1b100;">break</span>;
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;bloomfield&quot;</span> :
					ds=<span style="color: #000000; font-weight: bold;">new</span> Bloomfield <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #b1b100;">return</span><span style="color: #66cc66;">&#40;</span>ds.<span style="color: #006600;">deliverData</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #b1b100;">break</span>;
				<span style="color: #000000; font-weight: bold;">default</span> :
					<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Invalid video set specified&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Notice that the ConcreteCreatorVid holds a reference to the product interface (DataSupply) and then instantiates the concrete products (Prague, Graduation, Bloomfield) using an object typed as an interface. The Prague concrete product class does not change; so all that’s left is to create concrete product classes for the Graduation and Bloomfield video projects.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1077code95'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p107795"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code" id="p1077code95"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Data for Graduation Project</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> fl.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">DataProvider</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Graduation <span style="color: #0066CC;">implements</span> DataSupply
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> deliverData<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:DataProvider
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> graduation:DataProvider=<span style="color: #000000; font-weight: bold;">new</span> DataProvider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			graduation.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Gathering&quot;</span>, <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;mp4:graduationvid/gathering.f4v&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
			graduation.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Walking&quot;</span>, <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;mp4:graduationvid/walk.f4v&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
			graduation.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Awarding Degrees&quot;</span>, <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;mp4:graduationvid/degree.f4v&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> graduation;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Data for Bloomfield project</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> fl.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">DataProvider</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Bloomfield <span style="color: #0066CC;">implements</span> DataSupply
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> deliverData<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:DataProvider
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> bloomfield:DataProvider=<span style="color: #000000; font-weight: bold;">new</span> DataProvider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			bloomfield.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Introduction&quot;</span>, <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;mp4:bloomfieldvid/intro.f4v&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
			bloomfield.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;My Street&quot;</span>, <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;mp4:bloomfieldvid/shadywalk.f4v&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
			bloomfield.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;History&quot;</span>, <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;mp4:bloomfieldvid/park.f4v&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
			bloomfield.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;People&quot;</span>, <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;mp4:bloomfieldvid/people.f4v&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> bloomfield;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>At this point you have all of the Factory Method classes. So now (finally!) we can look at a Client class that makes requests. Keep in mind that this is only one of two design patterns that this project is using. (In the next installment, we’ll refactor the a non-OOP video player into a State design pattern and incorporate this data-generating Factory Method design pattern.)</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1077code96'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p107796"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
</pre></td><td class="code" id="p1077code96"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">events</span>.<span style="color: #006600;">ListEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">Button</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">List</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">DataProvider</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnPrague:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnGrad:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnBloom:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">list</span>:<span style="color: #0066CC;">List</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">List</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> create:Creator=<span style="color: #000000; font-weight: bold;">new</span> ConcreteCreatorVid<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			btnPrague.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,getPrague<span style="color: #66cc66;">&#41;</span>;
			btnGrad.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,getGraduation<span style="color: #66cc66;">&#41;</span>;
			btnBloom.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,getBloomfield<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			btnPrague.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">35</span>,btnPrague.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">160</span>;
			btnPrague.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Prague&quot;</span>;
			btnPrague.<span style="color: #0066CC;">textField</span>.<span style="color: #0066CC;">background</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			btnPrague.<span style="color: #0066CC;">textField</span>.<span style="color: #0066CC;">backgroundColor</span> = 0xA19580;
			addChild<span style="color: #66cc66;">&#40;</span>btnPrague<span style="color: #66cc66;">&#41;</span>;
			btnGrad.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">35</span>,btnGrad.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">190</span>;
			btnGrad.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Graduation&quot;</span>;
			btnGrad.<span style="color: #0066CC;">textField</span>.<span style="color: #0066CC;">background</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			btnGrad.<span style="color: #0066CC;">textField</span>.<span style="color: #0066CC;">backgroundColor</span> = 0xA19580;
			addChild<span style="color: #66cc66;">&#40;</span>btnGrad<span style="color: #66cc66;">&#41;</span>;
			btnBloom.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">35</span>,btnBloom.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">220</span>;
			btnBloom.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Bloomfield&quot;</span>;
			btnBloom.<span style="color: #0066CC;">textField</span>.<span style="color: #0066CC;">background</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			btnBloom.<span style="color: #0066CC;">textField</span>.<span style="color: #0066CC;">backgroundColor</span> = 0xA19580;
			addChild<span style="color: #66cc66;">&#40;</span>btnBloom<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">list</span>.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">35</span>,<span style="color: #0066CC;">list</span>.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">50</span>;
			addChild<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">list</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getPrague<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> prague:DataProvider=<span style="color: #000000; font-weight: bold;">new</span> DataProvider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			prague = create.<span style="color: #006600;">selectData</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;prague&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">list</span>.<span style="color: #006600;">dataProvider</span> = prague;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getGraduation<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> graduation:DataProvider=<span style="color: #000000; font-weight: bold;">new</span> DataProvider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			graduation = create.<span style="color: #006600;">selectData</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;graduation&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">list</span>.<span style="color: #006600;">dataProvider</span> = graduation;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getBloomfield<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> bloomfield:DataProvider=<span style="color: #000000; font-weight: bold;">new</span> DataProvider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			bloomfield = create.<span style="color: #006600;">selectData</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;bloomfield&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">list</span>.<span style="color: #006600;">dataProvider</span> = bloomfield;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, each button calls a function to load the List instance with a different set of data loaded into the DataProvider object. It’s just like the first version of the pattern except that a parameter has been added to select from among three data sets. Figure 4 shows the different outcomes in the List component:<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/06/p1.png" alt="p1" title="p1" width="512" height="349" class="alignnone size-full wp-image-1082" /><br />
<em><strong>Figure 4:</strong> Selected List object data</em></p>
<p>Both in our book and on this blog, you&#8217;ve seen other examples of the Factory Method design pattern; so this may not be new as far as a design pattern is concerned. However, it does show a different use of the pattern and how flexible it is. In the next installment, we&#8217;ll see how this pattern that provides UI data can be integrated with a State design to develop a video player that is set up so that adding more controls is quite simple.</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%2F06%2Ffriends-with-benefits-refactoring-with-multiple-design-patterns-part-i%2F&amp;title=Friends%20with%20Benefits%3A%20Refactoring%20with%20Multiple%20Design%20Patterns%E2%80%94Part%20I" id="wpa2a_20"><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/06/friends-with-benefits-refactoring-with-multiple-design-patterns-part-i/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

