<?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; Strategy Pattern</title>
	<atom:link href="http://www.as3dp.com/category/design-patterns/strategy-pattern/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>Flash Builder Mobile ActionScript 3.0 Design Pattern</title>
		<link>http://www.as3dp.com/2011/07/flash-builder-mobile-actionscript-3-0-design-pattern/</link>
		<comments>http://www.as3dp.com/2011/07/flash-builder-mobile-actionscript-3-0-design-pattern/#comments</comments>
		<pubDate>Wed, 27 Jul 2011 16:05:57 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Strategy Pattern]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6065</guid>
		<description><![CDATA[Easy Update A couple of days ago, we had an example for beginners creating a mobile application using Flash Professional. We used an Android example because everyone can get the tools they need for emulating an Android. (By the way, partnering up with Google-Android and Flash was a brilliant move by Android. Being an &#8220;Apple [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6064" class="wp-caption alignleft" style="width: 258px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/phoneFB.png" alt="Flexible Design Pattern Re-used with Flash Builder Development" title="phoneFB" width="248" height="502" class="size-full wp-image-6064" /><p class="wp-caption-text">Flexible Design Pattern Re-used with Flash Builder Development</p></div><strong>Easy Update</strong></p>
<p>A couple of days ago, we had an example for beginners creating a mobile application using <strong>Flash Professional</strong>.  We used an Android example because everyone can get the tools they need for emulating an Android. (By the way, partnering up with Google-Android and Flash was a brilliant move by Android.  Being an &#8220;Apple Guy,&#8221;  I thought I&#8217;d be doing nothing but apps for my iPhone and iPads; however, Android made it a lot easier to develop apps for Android using some kind of ActionScript 3.0 development tool, and Blackberry&#8217;s not far behind. So I am very happy to know that iOS is now available, but they&#8217;ll just have to wait their turn for a beginner&#8217;s post.)  As promised, I wanted to create a beginner&#8217;s example using Flash Builder.  This example uses the same Strategy Pattern as the <a href="http://www.as3dp.com/2011/07/25/beginning-mobile-actionscript-3-0-design-patterns-with-flash/">Flash mobile tutorial</a>. However, as you will see, there are some key differences when dealing with Flash Pro and Flash Builder. Also, I hope you see that a very different logo (FB instead of Sandlight) and text. All that was changed for this difference was swapping the image used for the logo and the text in the external text file. Also, I had to change some settings in the BringText class, but that too was easy and didn&#8217;t upset the program. Why? <em>Because it was a design pattern!</em></p>
<p><strong>Stepping Through Flash Builder Mobile</strong></p>
<p>This is going to be the same walk-through with Flash Builder as with Flash Pro but with a few differences.  The Flash Builder work flow is slightly different, but you will find more similarities than differences. Before we get going download Flash Builder 4.5.1. (You can use the 30-day free trial version.)  You can probably work with FB 4 with all of the updates, including the Android SDK, but everything in this example was done with FB 4.5.1. (If you are a student or faculty, you can get a free educational version of FB if you contact Adobe with your student or faculty ID. Faculty can get it free for their classrooms and labs as well.) Install to the defaults. Go to <a href="http://labs.adobe.com/">Adobe Labs</a> download and install both Adobe AIR 3 (beta) and Adobe Flash Player 11 (beta) and let&#8217;s get started:</p>
<p><strong>First</strong>, open Flash Builder and select File > New > ActionScript Mobile Project. (<strong>Note:</strong> You will find both a <em>Flex Mobile Project</em> and an <em>ActionScript Mobile Project</em>; be sure to select <em><strong>ActionScript Mobile Project</strong></em>.) In the Project name window, name the project <strong>FBmoStrat </strong>as shown in Figure 1 :</p>
<div id="attachment_6072" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/Fig11.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; New ActionScript Mobile Project window&lt;/em&gt;" title="Fig1" width="500" height="476" class="size-full wp-image-6072" /><p class="wp-caption-text"><em><strong>Figure 1:</strong> New ActionScript Mobile Project window</em></p></div>
<p>I used the default location and the default SDK, which was Flex 4.5.1. Click Next.</p>
<p><strong>Second</strong>, when <strong>The Create an ActionScript Mobile AIR Project </strong>window appears with the <strong>Mobile Settings</strong> tab selected, you will see that all Target platforms (Apple iOS, BlackBerry Tablet OS and Google Android) are checked as shown in Figure 2.<br />
<div id="attachment_6075" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/Fig21.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2:&lt;/strong&gt;Initial Create Window&lt;/em&gt;" title="Fig2" width="500" height="476" class="size-full wp-image-6075" /><p class="wp-caption-text"><em><strong>Figure 2:</strong>Initial Create Window</em></p></div></p>
<p><strong>Third</strong>, uncheck <strong>both </strong>the Apple iOS and BlackBerry Tablet OS. When you do that, the Permissions for Android appear as shown in Figure 3:</p>
<div id="attachment_6080" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/Fig31.png" alt="&lt;em&gt;&lt;strong&gt;Figure 3: &lt;/strong&gt; Android Permissions&lt;/em&gt;" title="Fig3" width="500" height="624" class="size-full wp-image-6080" /><p class="wp-caption-text"><em><strong>Figure 3: </strong> Android Permissions</em></p></div>
<p>Be sure that the Platform is Google Android and that the only permission checked is INTERNET. The default is to Automatically reorient the screen in the Application settings. Leave that default checked and click the Next button.</p>
<p><strong>Fourth,</strong> in the Build Paths step, you will see a set of default selections you can leave alone except for the Main application file that you want to change from<strong> FBmoStrat.as</strong> to <strong>Client.as</strong>. Figure 4 shows the step correctly configured:</p>
<div id="attachment_6085" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/Fig4.png" alt="&lt;em&gt;&lt;strong&gt;Figure 4:&lt;/strong&gt; Build Paths step&lt;/em&gt;" title="Fig4" width="500" height="616" class="size-full wp-image-6085" /><p class="wp-caption-text"><em><strong>Figure 4:</strong> Build Paths step</em></p></div>
<p>If your Build Paths steps looks like Figure 4, click Finish. You should see the following script automatically generated:</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('p6065code3'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p60653"><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="p6065code3"><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;">StageAlign</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageScaleMode</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;">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: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// support autoOrients</span>
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">align</span> = StageAlign.<span style="color: #006600;">TOP_LEFT</span>;
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">scaleMode</span> = StageScaleMode.<span style="color: #006600;">NO_SCALE</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 Stage Align and StageScaleMode built-in ActionScript 3.0 classes will help align the application when it is turned in different directions.  The <strong>super()</strong> method invokes the parent class <strong>Sprite</strong>.  You will be leaving the constructor function as is but add more materials for the Client class. Before doing that, go ahead and test the problem and download all of the files with the buttons below:<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/dp/mobileFB/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignleft size-full wp-image-1257" /></a><a href="http://nemo.mwd.hartford.edu/~wsanders/dp/mobileFB/FBmoStrat.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>You still will need to test the file from Flash Builder and contend with one last set of settings. So gather up all of the files and read on before testing.</p>
<p><span id="more-6065"></span></p>
<p><strong>Testing from Flash Builder</strong></p>
<p>Once you all of the classes in the project are saved in the Flash Builder src folder, along with the <em>graphic file and text file</em>, click the Run button to test the application. The first time you do this, you will see the Run Configurations window shown in Figure 5.</p>
<div id="attachment_6090" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/Fig5.png" alt="&lt;em&gt;&lt;strong&gt;Figure 5:&lt;/strong&gt; Run Configurations window&lt;/em&gt;" title="Fig5" width="500" height="549" class="size-full wp-image-6090" /><p class="wp-caption-text"><em><strong>Figure 5:</strong> Run Configurations window</em></p></div>
<p>In the Launch method area, select the<strong> On desktop</strong> radio button, and select <strong>Mororola Droid 2</strong> and click the <strong>Apply</strong> button. Finally, click the <strong>Run</strong> button.</p>
<p><strong>The Strategy Design Pattern in a Mobile Context</strong></p>
<p>If you read the post on creating this app using Flash Pro, there&#8217;s not much new here. This post is only meant to show how to add a design pattern into a mobile application using Flash Builder.  If you use apps on mobile devices, you must be aware of the updating that your app goes through. Using the Strategy pattern, adding more concrete strategies is easy, and changing the graphic and text contents is equally easy. In other words, this is a very flexible tool to both create and update mobile applications just as it is with traditional applications.  The following classes are little changed from the previous post using Flash Pro, but they are included for review and the small differences that could impact your understanding.</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('p6065code4'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p60654"><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
</pre></td><td class="code" id="p6065code4"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Client</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;">StageAlign</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageScaleMode</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>
		protected <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: #000000; font-weight: bold;">new</span> BringGraphic<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> contextTxt:Context=<span style="color: #000000; font-weight: bold;">new</span> Context<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> BringText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> mobileSprite:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> textSprite: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> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// support autoOrients</span>
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">align</span> = StageAlign.<span style="color: #006600;">TOP_LEFT</span>;
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">scaleMode</span> = StageScaleMode.<span style="color: #006600;">NO_SCALE</span>;
			setLogo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			setText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> setLogo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			mobileSprite.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">bringLoad</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mobilelogo.png&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			mobileSprite.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">5</span>,mobileSprite.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">10</span>;
			addChild<span style="color: #66cc66;">&#40;</span>mobileSprite<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> setText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			textSprite.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>contextTxt.<span style="color: #006600;">bringLoad</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mobile.txt&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			textSprite.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">5</span>,textSprite.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">150</span>;
			addChild<span style="color: #66cc66;">&#40;</span>textSprite<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;">//Context</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> Context <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> bring:IStrategy;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bringObj: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> Context<span style="color: #66cc66;">&#40;</span>bring:IStrategy<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">bring</span> = bring;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> bringLoad<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			bringObj.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>bring.<span style="color: #006600;">contentLoader</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> bringObj;
		<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;">//IStrategy 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>;
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IStrategy
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> contentLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<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>
&nbsp;
<span style="color: #808080; font-style: italic;">//Bring Graphic--loads graphics and SWF files</span>
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;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> BringGraphic <span style="color: #0066CC;">implements</span> IStrategy
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> urlReq: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> objNow: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> contentLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			urlReq = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><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>urlReq<span style="color: #66cc66;">&#41;</span>;
			objNow.<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> objNow;
		<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;">//BringText -- loads external text and xml files and formats text</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: #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: #0066CC;">TextFormat</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;">net</span>.<span style="color: #006600;">URLRequest</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> BringText <span style="color: #0066CC;">implements</span> IStrategy
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> textContainer: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> urlNow:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtLoader:URLLoader=<span style="color: #000000; font-weight: bold;">new</span> URLLoader<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> 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> txtFormat:<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> <span style="color: #000000; font-weight: bold;">var</span> uLoader:URLLoader;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> contentLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			urlNow=<span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</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>, addTxtFld<span style="color: #66cc66;">&#41;</span>;
			txtLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>urlNow<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> textContainer;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> addTxtFld<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>
			uLoader=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>;
			txtFormat=<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>;
			txtFormat.<span style="color: #0066CC;">font</span>=<span style="color: #ff0000;">&quot;Verdana&quot;</span>;
			txtFormat.<span style="color: #0066CC;">size</span>=<span style="color: #cc66cc;">24</span>;
			txtFormat.<span style="color: #0066CC;">color</span>=0x000000;
			txtFld.<span style="color: #006600;">defaultTextFormat</span>=txtFormat;
			txtFld.<span style="color: #0066CC;">multiline</span>=<span style="color: #000000; font-weight: bold;">true</span>;
			txtFld.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">300</span>;
			txtFld.<span style="color: #0066CC;">height</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: #006600;">x</span>=<span style="color: #cc66cc;">10</span>,txtFld.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">50</span>;
			txtFld.<span style="color: #0066CC;">text</span>=uLoader.<span style="color: #0066CC;">data</span>;
			textContainer.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>txtFld<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>If someone would like to add their own logo and text about their company, services or products using this application, we&#8217;d like to show them off. It&#8217;d be a good use of the design pattern, and you&#8217;d get some worldwide notoriety for your company!</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%2F07%2Fflash-builder-mobile-actionscript-3-0-design-pattern%2F&amp;title=Flash%20Builder%20Mobile%20ActionScript%203.0%20Design%20Pattern" 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/07/flash-builder-mobile-actionscript-3-0-design-pattern/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Beginning Mobile ActionScript 3.0 Design Patterns with Flash</title>
		<link>http://www.as3dp.com/2011/07/beginning-mobile-actionscript-3-0-design-patterns-with-flash/</link>
		<comments>http://www.as3dp.com/2011/07/beginning-mobile-actionscript-3-0-design-patterns-with-flash/#comments</comments>
		<pubDate>Mon, 25 Jul 2011 14:54:35 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Strategy Pattern]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6018</guid>
		<description><![CDATA[Get Your App Up! The very first mobile app I want to build is one that I can give away to promote my company or my clients&#8217; companies. Yes, I&#8217;d like to make lots of money with a mobile action game like Angry Birds, but in order to get started, I want something that I [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6017" class="wp-caption alignleft" style="width: 258px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/phone1.png" alt="ActionScript 3.0 Design Patterns in Mobile Apps" title="phone" width="248" height="502" class="size-full wp-image-6017" /><p class="wp-caption-text">ActionScript 3.0 Design Patterns in Mobile Apps</p></div><strong>Get Your App Up!</strong></p>
<p>The very first mobile app I want to build is one that I can give away to promote my company or my clients&#8217; companies. Yes, I&#8217;d like to make lots of money with a mobile action game like <em>Angry Birds</em>, but in order to get started, I want something that I can easily update and change. It seems like every time I check my phone apps, I&#8217;ve got updates. What is one of the main features of Design Patterns? Easy updates! So, if you want to create mobile applications in ActionScirpt 3.0 that are easy to update, you&#8217;ll find design patterns your BFF!</p>
<p>This first one is built with Flash CS5.5. If you do not have Flash CS5.5; download the 30-day trial version. If you&#8217;re using Flash Builder, use FB 4.5.1.  or download the trial version. (I hope to have a FB tutorial using this same example later in the week. In the meantime, take a look at <a href="http://www.as3dp.com/2011/06/28/puremvc-goes-mobile-1-mvc-driven-ready-to-compile-mobile-application-with-the-flex-framework-by-christian-peters/">Christian Peters&#8217; mobile example</a> for building an app using design patterns with Flash Builder.)</p>
<p>This first example uses a <strong>Strategy</strong> pattern with algorithms for loading external images (or .SWF files) and text.  That is exactly what I need for a mobile app promoting my company. With the image loading capability, I can load my logo and any images that show off products and services. The concrete text strategy loads up data from external text files. So this ought to be the easiest app to update imaginable!</p>
<p><strong>Easy Android</strong></p>
<p>I was going to start off with an iOS example, but I realized that most of you probably didn&#8217;t want to shell out the $99 developer fee required by Apple for iOS development. So, instead I decided to use an Android example that you could test using an Android emulator. To get started, go ahead and download the source code and see what the app displays:<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/dp/mobile1/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignleft size-full wp-image-1257" /></a><a href="http://www.mwd.hartford.edu/mwd11/downloads/MWDdownloads.php"><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>
<blockquote><p>All of the Downloads have been moved to the University of Hartford&#8217;s  <strong>Multimedia Web Design and Development Program</strong> page. The last entry under &#8220;ActionScript 3.0/ Flash&#8221; should have what you want. (I&#8217;m still working on it to include a short description of each entry&#8211;it&#8217;s in the database; just not on the screen).</p></blockquote>
<p>When you run it, you will quickly see that a logo and block of text pops up. That&#8217;s it. Swap out your own logo and texts, and you&#8217;re in business. You can change any of the parameters in the <strong>BringText</strong> concrete strategy class to alter the text appearance, it&#8217;s placement and other features to suite your own tastes or business brand.</p>
<p><strong>Starting from Scratch</strong></p>
<p>Since this is a first time experience for beginners and mobile (which may include experienced ActionScript 3.0 programmers), we&#8217;ll step through the set-up beginning with the FLA file. So read on through the following steps:</p>
<p><span id="more-6018"></span><br />
<strong>First</strong>, download and install the <a href="http://developer.android.com/index.html">Android SDK</a>. If your system is not automatically selected, choose the correct OS. Install the SDK.</p>
<p><strong>Second</strong>, install the <strong>Adobe AIR 3 beta</strong> and <strong>Adobe Flash Player 11 beta</strong>, both at <a href="http://labs.adobe.com/">Adobe Labs</a>.</p>
<p><strong>Third</strong>, download and install Flash CS5.5 trial if you don&#8217;t have it. (If you have Flash CS5 and have installed all of the updates, you ought to be ok.)</p>
<p><strong>Fourth</strong>, in your root directory, create a folder named &#8220;Developer.&#8221; For example, on a Mac, you&#8217;d open up your  Macintosh HD and add the Developer folder.( A hammer icon may automatically appear on the folder.)</p>
<p><strong>Fifth</strong>, open Flash CS5.5 and select  <strong>AIR for Android</strong>. Configure the screen for 320 x 470. (If you have an Android phone with a different screen size, configure it for your phone size.) Create a folder named <strong>MobileApp</strong> inside of the<strong> Developer</strong> folder, and save the file as <strong>AndroidStrategy.fla.</strong></p>
<p><strong>Sixth</strong>, with the stage selected, open the Properties panel. In the text window labeled &#8220;Class&#8221; enter the Class name &#8220;Client.&#8221;</p>
<p><strong>Seventh</strong>, in the Properties panel in the Publish grouping, you should see <strong>AIR for Android</strong> next to &#8220;Player.&#8221; You will see a <strong>wrench icon</strong> to the right. Click the icon to open the AIR for Android Settings window. Figure 1 shows what you should see. If anything looks different, check to make sure you followed the previous steps.<br />
<div id="attachment_6037" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/Fig1.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt;General AIR for Android Settings&lt;/em&gt;" title="Fig1" width="500" height="694" class="size-full wp-image-6037" /><p class="wp-caption-text"><em><strong>Figure 1:</strong>General AIR for Android Settings</em></p></div></p>
<p><strong>Eighth</strong>, in the AIR for Android window, click the <strong>Deployment</strong> tab (near the top of the  window next to the General tab.) Figure 2 shows the Deployment selections. You will need to create a Certificate and Password. Click the Create button to create a certificate. You can use any names you want; something simple works fine. You can use the same certificate for other projects. I had placed the Android SDK in the Development folder and once I created the certificate, the full URL is:</p>
<p> <strong>/Developer/android-sdk-mac_x86/platform-tools/adb.p12</strong>.</p>
<p>If you do not see a &#8220;platform-tools&#8221; folder in your SDK folder, you&#8217;ll need to update your Android SDK by downloading the most recent release. (The earlier ones had the needed files in a different folder.)<br />
<div id="attachment_6042" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/Fig2.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2:&lt;/strong&gt; Deployment Tab in AIR for Android Settings Window&lt;/em&gt;" title="Fig2" width="500" height="694" class="size-full wp-image-6042" /><p class="wp-caption-text"><em><strong>Figure 2:</strong> Deployment Tab in AIR for Android Settings Window</em></p></div></p>
<p>Be sure to remember your password, and it helps to click the Remember password for this session so you don&#8217;t have to keep re-entering it. Set the Android deployment to <strong>Emulator release</strong> and don&#8217;t select any of the <strong>After publishing</strong> checkboxes. When you&#8217;re all finished  click OK. You&#8217;re all set for a mobile app using design patterns!</p>
<p><strong>Adding the Design Pattern</strong></p>
<p>This final section uses a simple Strategy design pattern. (If you&#8217;re not familiar with the Strategy pattern, we have a <a href="http://www.as3dp.com/2010/09/28/saturated-strategy-1-a-look-at-the-participants/">saturated series</a> on it as well as a <a href="http://www.as3dp.com/2009/03/08/actionscript-30-easy-and-practical-strategy-design-pattern/">simple introduction</a>.) As noted at the outset, this implementation of the Strategy pattern pulls in graphics and text through Client requests. However, because of the pattern design, there&#8217;s minimal object binding, and any updates or changes will not unravel the whole program. Also, because it&#8217;s simple, it&#8217;s a good pattern to use to understand the basics of the Strategy pattern.</p>
<p><strong>The Client</strong></p>
<p>Without a UI, the pattern is quite easy. The Client simply requests what it wants using literal names for graphic images and the text in an external text file. The following shows the Client code:</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('p6018code10'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p601810"><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
</pre></td><td class="code" id="p6018code10"><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>
		protected <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: #000000; font-weight: bold;">new</span> BringGraphic<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> contextTxt:Context=<span style="color: #000000; font-weight: bold;">new</span> Context<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> BringText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> mobileSprite:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> textSprite: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> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setLogo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			setText<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> setLogo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			mobileSprite.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">bringLoad</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mobilelogo.png&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			mobileSprite.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">5</span>,mobileSprite.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">10</span>;
			addChild<span style="color: #66cc66;">&#40;</span>mobileSprite<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> setText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			textSprite.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>contextTxt.<span style="color: #006600;">bringLoad</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mobile.txt&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			textSprite.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">5</span>,textSprite.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">150</span>;
			addChild<span style="color: #66cc66;">&#40;</span>textSprite<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 protected accessors have been employed so that when the app is developed in Flash Builder in the Flex element, it&#8217;ll feel right at home.  However, in this Client, private access would have worked fine.  This particular implementation of the Strategy returns Sprite objects containing both Text and Graphics, including other Sprite objects. As a result, even the text is added to the stage inside a Sprite. Notice that all requests go through the Context class.</p>
<p><strong>The Context Class and Strategy Interface</strong></p>
<p>Even though the pattern diagrams for the State and Strategy patterns are identical, the intent is quite different. Most important is the Context class in the Strategy design.<em> It does not keep track of a current state </em>as does the State 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('p6018code11'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p601811"><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="p6018code11"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Context Participant</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> Context <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> bring:IStrategy;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bringObj: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> Context<span style="color: #66cc66;">&#40;</span>bring:IStrategy<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">bring</span> = bring;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> bringLoad<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			bringObj.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>bring.<span style="color: #006600;">contentLoader</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> bringObj;
		<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 some respects the Context class in the Strategy resembles a factory because the request for strategies is made through the Context, but it does not really instantiate a concrete strategy as a factory would. The Sprite instantiated in the Context (bringObj) acts as a carrying container to return the requested strategy to the requesting class (Client in this case.) However, no concrete strategies are created.</p>
<p>The IStrategy interface has a single method to be implemented. The method is used to pass requests back to the requesting 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('p6018code12'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p601812"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p6018code12"><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: #0066CC;">interface</span> IStrategy
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> contentLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The IStrategy interface leaves a lot of flexibility for adding more concrete strategies for returning Sprite (or Sprite encased) materials to the client.</p>
<p><strong>Concrete Strategies</strong></p>
<p>Finally, the two concrete strategies both load external content. The first is an image/SWF loader and the latter is a text/xml file loader.  It is simple and flexible:</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('p6018code13'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p601813"><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
</pre></td><td class="code" id="p6018code13"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Graphic and SWF Loader</span>
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;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> BringGraphic <span style="color: #0066CC;">implements</span> IStrategy
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> urlReq: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> objNow: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> contentLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			urlReq = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><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>urlReq<span style="color: #66cc66;">&#41;</span>;
			objNow.<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> 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>The BringText class required an event handler function for insuring that the text was completely loaded. The event handler method is a convenient place to put the TextFormat object and properties. Once the text is pulled out of the text or xml file and placed into the formatted text field, it was packed up and put into a Sprite object ready to return to 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('p6018code14'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p601814"><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
</pre></td><td class="code" id="p6018code14"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Text and XML Loader</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: #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: #0066CC;">TextFormat</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;">net</span>.<span style="color: #006600;">URLRequest</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> BringText <span style="color: #0066CC;">implements</span> IStrategy
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> textContainer: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> urlNow:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtLoader:URLLoader=<span style="color: #000000; font-weight: bold;">new</span> URLLoader<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> 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> txtFormat:<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> <span style="color: #000000; font-weight: bold;">var</span> uLoader:URLLoader;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> contentLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			urlNow=<span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</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>, addTxtFld<span style="color: #66cc66;">&#41;</span>;
			txtLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>urlNow<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> textContainer;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> addTxtFld<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>
			uLoader=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>;
			txtFormat=<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>;
			txtFormat.<span style="color: #0066CC;">font</span>=<span style="color: #ff0000;">&quot;Verdana&quot;</span>;
			txtFormat.<span style="color: #0066CC;">size</span>=<span style="color: #cc66cc;">24</span>;
			txtFormat.<span style="color: #0066CC;">color</span>=0x000000;
			txtFld.<span style="color: #006600;">defaultTextFormat</span>=txtFormat;
			txtFld.<span style="color: #0066CC;">multiline</span>=<span style="color: #000000; font-weight: bold;">true</span>;
			txtFld.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">300</span>;
			txtFld.<span style="color: #0066CC;">height</span>=<span style="color: #cc66cc;">250</span>;
			txtFld.<span style="color: #0066CC;">wordWrap</span>=<span style="color: #000000; font-weight: bold;">true</span>;
			txtFld.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">10</span>,txtFld.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">90</span>;
			txtFld.<span style="color: #0066CC;">text</span>=uLoader.<span style="color: #0066CC;">data</span>;
			textContainer.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>txtFld<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 noted, this pattern is simple, but very handy. The classes are reusable and simple to update and change. Further, adding more classes is just as easy, and that means updating the mobile application is simple.</p>
<p><strong>On to Flex and Flash Builder</strong></p>
<p>The next step is to take the ActionScript 3.0 in this mobile application and do the same thing in Flash Builder.  The Client class will have to reside in an MXML file instead of an AS file. Next up, we&#8217;ll see how to take this app and bundle it up using Flash builder. I&#8217;d also like to see other simple Flex/Flash Builder mobile design pattern examples that might help our readers—even implementations of this code in the Flash Builder IDE.</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%2F07%2Fbeginning-mobile-actionscript-3-0-design-patterns-with-flash%2F&amp;title=Beginning%20Mobile%20ActionScript%203.0%20Design%20Patterns%20with%20Flash" 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/07/beginning-mobile-actionscript-3-0-design-patterns-with-flash/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Wrong Way Warrior: Getting Flexibility with Design Patterns—Part II</title>
		<link>http://www.as3dp.com/2009/08/wrong-way-warrior-getting-flexibility-with-design-patterns-part-ii/</link>
		<comments>http://www.as3dp.com/2009/08/wrong-way-warrior-getting-flexibility-with-design-patterns-part-ii/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 21:41:53 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[Principles]]></category>
		<category><![CDATA[Strategy Pattern]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1304</guid>
		<description><![CDATA[Gentle Reader: This is the second part of a two-part set of posts. For this one to be useful, please take a look at Part I. Also, I’m not an expert on military operations or organizations; so if there’s any error in a basic infantry platoon, feel free to correct me. I am aware, however, [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Gentle Reader:</strong> This is the second part of a two-part set of posts. For this one to be useful, please take a look at <a href="http://www.as3dp.com/2009/08/15/the-wrong-way-warrior-where-oop-alone-is-not-enough—part-i/">Part I</a>. Also, I’m not an expert on military operations or organizations; so if there’s any error in a basic infantry platoon, feel free to correct me. I am aware, however, of the 7-1 ratio of Service to Combat units in the modern military, and that this is only a simple component of a far more sophisticated structure—that’s why I selected it!</em></p>
<p>In the first installment of the <em>Wrong Way Warrior</em>, we saw how an OOP developer put together a simple proof-of-concept using what he thought was a prudent approach to a battle simulation. He’d provide the Warrior with certain characteristics and then subclass those characteristics to concrete warriors that would share the capabilities of the parent class. In addition, the concrete warriors would be given a movie clip representation of the warrior.</p>
<p>After the first design was sent to the customer, the response was less than favorable. It was described as “a children’s game” at best. The military advisor described it as a <em>caveman battle plan</em> where all of the combatants are similarly armed with a club to attack adversaries. The problem was that it was bound to a fairly static design, and it would be impossible to be used for a simulation that had more complex behaviors. However, the other submissions were not much better, and so the customer provided a simple organization within the military to simulate—the basic infantry platoon. After all, they’re paying your company $1.5 million to develop the simulation. (This was news to the developer!) Figure 1 shows the organization in terms of a new set of movie clips:<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/08/platoon.png" alt="platoon" title="platoon" width="496" height="599" class="alignnone size-full wp-image-1305" /><br />
<em><strong>Figure 1:</strong> Movie clip representation of platoon</em><br />
<span id="more-1304"></span><br />
As you can see, far from being a herd of cavemen with clubs, each of the members of the platoon has a specific role. A lieutenant with the aid of an experienced platoon sergeant runs the show. A radio operator keeps in contact with the higher-level organization (company) and other assets than can be brought to bear (e.g. artillery and air support). Broken down into four squads, 3 rifle squads and a weapons squad, the organization is fairly generic of those in the US Army and is instructive for some more complex (and realistic) applications in Flex and Flash.</p>
<p><strong>What Varies?</strong></p>
<p>To get started, it helps to have some kind of perspective on the problem. If we look at the basic military hierarchy, we can get an idea:<br />
<nl></p>
<li>Platoon (Smallest) </li>
<li>Company (3-5 platoons) </li>
<li>Battalion (4-6 companies) </li>
<li>Brigade (2-5 battalions) </li>
<li>Division (3 brigades) </li>
<li>Corps (2-5 divisions) </li>
<li>Army (2 or more corps) </li>
<p></nl></p>
<p>Can the simulation be expanded even to company size? The sample platoon is set up for 38 soldiers, and the Company for between 62-190, depending on specialized assignments. These are only the combat units and do not include such service and support units as medics, transport, military police and other service units.</p>
<p>In looking at Figure 1, we can immediately see that roles vary. Put another way, <em>their behaviors vary</em>. Initially, a single Warrior class was used and subclasses of Red and Blue classes made up two opposing sides.  However, other than the appearance of their movie clip icons, they were identical. If we added more and more functions, we’d have to override virtually ever other method. So, we need another way.</p>
<p>For this round of development, we need to think about one of the first principles of design patterns,</p>
<blockquote><p>
Separate what varies from what stays the same and encapsulate what varies.
</p></blockquote>
<p>Since we’ve identified that behaviors vary because of the different roles (<em>not a bunch of undifferentiated cavemen with clubs</em>), we’ll start with the weapons and firing behavior. We’ll put the behaviors into an interface and set up implementations of the different weapons. At the same time, we’ll set up the Warrior class so that they can use the different weapons through <em>composition</em>. That is, each instance of each subclass will <em>have a</em> different weapon to use instead of the weapon and its related behavior as an integral part of the particular Warrior class.</p>
<p><strong>Sound On!</strong></p>
<p>To see where all of this is leading <strong>turn up your sound</strong> and click the Play button. You will see different organizational options when you press the buttons along the top. When you click on the individual “soldiers”, they will play a sound representing their weapons. In the case of the officer and NCO’s, the entire unit’s weapons fire simultaneously representing commanded fire. Because the anti-armor and machine gun units require two people to use the weapons, clicking either of the two sprites serve to fire the weapon. After you’ve had a look, download the files—only selected few were placed in this post.<br />
<a href="http://www.sandlight.net/dpBlog/dpwarrior/"><br />
<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><br />
<a href="http://www.sandlight.net/dpBlog/dpwarrior/PatternWarrior.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>To kick things off we’ll be looking at a very simplified Warrior class and an interface for firing weapons shown in the following code:</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('p1304code19'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p130419"><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
</pre></td><td class="code" id="p1304code19"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Warrior abstract class</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: #808080; font-style: italic;">//Abstract class. Do not instantiate</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Warrior <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> fire:Fire;
		<span style="color: #808080; font-style: italic;">//protected var movement:Movement;</span>
		<span style="color: #808080; font-style: italic;">//protected var comm:Communicate;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Encapsulating behaviors</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">interface</span> Fire
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> fireWeapon<span style="color: #66cc66;">&#40;</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>Those two elements should be viewed as <em>structuring elements</em> that help guide the development of the program. The Warrior abstract class includes a reference to the Fire interface and the set of behaviors that will be derived from implementations of that interface. The concrete Warrior classes will <em>not</em> derive their behaviors from the parent class but from the aggregate relationship between the Warrior interface (abstract class) and the Fire interface. (You can see two commented out variables, <em>movement</em> and <em>comm</em> that I’ll be referring to in a later post, but they can be ignored for the rest of this post.)</p>
<p><strong>Different Characters and Behaviors</strong></p>
<p>If you’ve ever been admonished to differentiate between a <em>bad action</em> and a <em>bad person</em> you’ll understand what’s going on here. The <em>person</em> belongs to the <strong>Warrior</strong> class and the <em>action</em> belongs to the <strong>Fire</strong> interface. They have the relationship shown in Figure 2:<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/08/warriordesign.png" alt="warriordesign" title="warriordesign" width="422" height="161" class="alignnone size-full wp-image-1306" /></p>
<p><em><strong>Figure 2:</strong> Class diagram of Warrior and Fire </em></p>
<p>As you can see in Figure 2, all of the algorithms for weapons actions are encapsulated in the Fire implementations. The Warrior subclass objects get their behaviors through the Warrior interface through the Fire interface.</p>
<p><strong>Marrying the Warrior to the Action</strong></p>
<p>In bringing together the behavior and character, you’ll be glad to know there’s no <em>bridezilla</em> in the coupling. In fact, to quote another fundamental OOP principle,</p>
<blockquote><p>
Objects should be loosely coupled.
</p></blockquote>
<p>So even better, by having loosely coupled objects, when something changes, there won’t be a collapse in the entire system just in case a particular coupling doesn’t work as planned.</p>
<p>In Part I of <em>The Wrong Way Warrior</em>, the developer used movement with buttons to illustrate how each instance inherited the ability to move on the screen. This time around, the developer is demonstrating how the different types of soldiers carry out a behavior—firing a weapon—that is composed into his object through aggregation. So, instead of shooting up the screen, this time the developer decided to use sound effects of the different weapons. Also, to fire the weapons, instead of having another set of buttons, clicking on individual concrete Warrior objects launches the appropriate sound effects.</p>
<p>As noted above, the concrete Warrior objects are coupled with a Fire implementation, and as you will see, they’re very similar. In that respect they’re interchangeable; and that’s just what we want. Starting off, the following listings show the Rifleman (<strong>RifleMn</strong>) character and the <strong>FireRifle </strong>behavior.</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('p1304code20'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p130420"><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
</pre></td><td class="code" id="p1304code20"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Creates concrete Warrior instance</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;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RifleMn <span style="color: #0066CC;">extends</span> Warrior
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Rifleman is movie clip in Library</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> rm1:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Rifleman<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> RifleMn<span style="color: #66cc66;">&#40;</span>fx:uint,fy:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			rm1.<span style="color: #006600;">x</span> = fx,rm1.<span style="color: #006600;">y</span> = fy;
			rm1.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, fireNow<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rm1<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> fireNow<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>
			fire = <span style="color: #000000; font-weight: bold;">new</span> FireRifle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</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;">//Implements Fire behavior</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FireRifle <span style="color: #0066CC;">implements</span> Fire
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">media</span>.<span style="color: #0066CC;">Sound</span>;
		<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">media</span>.<span style="color: #006600;">SoundChannel</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;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;WeaponSounds/M16.mp3&quot;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> weapon:SoundChannel;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> fireWeapon<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: #000000; font-weight: bold;">var</span> urlReq:URLRequest = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> gun:<span style="color: #0066CC;">Sound</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Sound</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			gun.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>urlReq<span style="color: #66cc66;">&#41;</span>;
			weapon = gun.<span style="color: #0066CC;">play</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;Firing M16--1 shot burst&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>The concrete Warrior selects a movie clip that represents its character first. Typed as a Sprite, it is used as a clickable event generator. In this case, the MovieClip object is named, <strong>Rifleman</strong>. The Rifleman instance is event-linked to a function <strong>fireNow()</strong> which then uses the <strong>fire</strong> object <em>inherited</em> from the parent class—Warrior. Using the <strong>fire</strong> object, the program instantiates the particular implementation of the behavior. In this case, it wants an instance of <strong>FireRifle</strong>. Keep in mind that <strong>fire</strong> inherited from Warrior is typed as a Fire object—only committing to the interface. This allows any implementation of the interface and its methods.</p>
<p>In looking at the Fire implementation of <strong>FireRifle</strong>, it basically sets up the sound file that is played. However, it only represents <em>any</em> algorithm that could be placed there instead. For example, it could be used in an algorithm to measure the expenditure of force against a target and keep track of the amount of ammunition a concrete Warrior instance expends and has remaining.</p>
<p>When you download and look at the different classes, you’ll see they’re pretty similar. However, some characters, like the Lieutenant, have a much more robust use of the Fire implementations 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('p1304code21'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p130421"><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
</pre></td><td class="code" id="p1304code21"><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;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Lieutenant <span style="color: #0066CC;">extends</span> Warrior
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Lieut is movie clip in Library</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> lt1:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Lieut<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> Lieutenant<span style="color: #66cc66;">&#40;</span>fx:uint,fy:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			lt1.<span style="color: #006600;">x</span> = fx,lt1.<span style="color: #006600;">y</span> = fy;
			lt1.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, fireNow<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>lt1<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> fireNow<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: #808080; font-style: italic;">//Fire Personal Weapon</span>
			fire = <span style="color: #000000; font-weight: bold;">new</span> FireRifle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//PlatoonFire</span>
			squadFire<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			weaponsSquadFire<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			squadFire<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			squadFire<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> squadFire<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;">//Fire Team 1</span>
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire = <span style="color: #000000; font-weight: bold;">new</span> FireSAW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire = <span style="color: #000000; font-weight: bold;">new</span> FireGrenade<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//Fire Team 2</span>
			fire = <span style="color: #000000; font-weight: bold;">new</span> FireRifle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire = <span style="color: #000000; font-weight: bold;">new</span> FireSAW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire = <span style="color: #000000; font-weight: bold;">new</span> FireGrenade<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</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> weaponsSquadFire<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			fire=<span style="color: #000000; font-weight: bold;">new</span> FireMG<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire=<span style="color: #000000; font-weight: bold;">new</span> FireRocket<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</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>As you can see, the Lieutenant fired every weapon in the platoon, as did the Platoon Sergeant. This illustrates how flexible this design is. Because the behaviors are separated from the objects that use them, you can put them in any combination you want.</p>
<p><strong>The Client</strong></p>
<p>If you look at the Client carefully, you’ll see no reference at all to the Fire operations. That’s because the concrete Warrior objects all access Fire behaviors themselves. There’s no reason (nor would you want one!) to implement a concrete Fire object from the Client directly. Client constructor creates buttons used to display different arrangements of the troops. The bigger problem was getting rid of one configuration when another had been placed on the stage. Keith Peters provided a handy algorithm for clearing all children, and so I put it in the piedPiper() function that clears everything—including the buttons! So the piedPiper() method replaces the buttons after blowing them and the rest of the children off the 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('p1304code22'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p130422"><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
</pre></td><td class="code" id="p1304code22"><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;">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>;
&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> rocketBtn:<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> mgBtn:<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> ftBtn:<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> sqBtn:<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> hqBtn:<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;">private</span> <span style="color: #000000; font-weight: bold;">var</span> lieut:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> platoonSgt:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> radio:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sqLdr:Warrior;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> tmLdr:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sawMan:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grenMan:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> rifMan:Warrior;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> tmLdr2:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sawMan2:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grenMan2:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> rifMan2:Warrior;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> mgunner:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> asstMgunner:Warrior;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> rocket:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> asstRocket:Warrior;
&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>
			ftBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, fireTeam<span style="color: #66cc66;">&#41;</span>;
			ftBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;FireTeam&quot;</span>;
			ftBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">10</span>,ftBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">10</span>;
			addChild<span style="color: #66cc66;">&#40;</span>ftBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			sqBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, squad<span style="color: #66cc66;">&#41;</span>;
			sqBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Squad&quot;</span>;
			sqBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">120</span>,sqBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">10</span>;
			addChild<span style="color: #66cc66;">&#40;</span>sqBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			mgBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, mgCrew<span style="color: #66cc66;">&#41;</span>;
			mgBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Machine Gun&quot;</span>;
			mgBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">230</span>,mgBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">10</span>;
			addChild<span style="color: #66cc66;">&#40;</span>mgBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			rocketBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, rocketCrew<span style="color: #66cc66;">&#41;</span>;
			rocketBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Anti Armor&quot;</span>;
			rocketBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">340</span>,rocketBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">10</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rocketBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			hqBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, hq<span style="color: #66cc66;">&#41;</span>;
			hqBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Headquarters&quot;</span>;
			hqBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">450</span>,hqBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">10</span>;
			addChild<span style="color: #66cc66;">&#40;</span>hqBtn<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> fireTeam<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			piedPiper<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			tmLdr = <span style="color: #000000; font-weight: bold;">new</span> TeamLdr<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">300</span>,<span style="color: #cc66cc;">424</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>tmLdr<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			rifMan = <span style="color: #000000; font-weight: bold;">new</span> RifleMn<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">428</span>,<span style="color: #cc66cc;">338</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rifMan<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			sawMan = <span style="color: #000000; font-weight: bold;">new</span> SAW<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">480</span>,<span style="color: #cc66cc;">424</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>sawMan<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			grenMan = <span style="color: #000000; font-weight: bold;">new</span> Grenadier<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">428</span>,<span style="color: #cc66cc;">512</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>grenMan<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> squad<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			piedPiper<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			sqLdr = <span style="color: #000000; font-weight: bold;">new</span> SquadLdr<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">82</span>,<span style="color: #cc66cc;">300</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>sqLdr<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			tmLdr = <span style="color: #000000; font-weight: bold;">new</span> TeamLdr<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">300</span>,<span style="color: #cc66cc;">424</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>tmLdr<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			rifMan = <span style="color: #000000; font-weight: bold;">new</span> RifleMn<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">428</span>,<span style="color: #cc66cc;">338</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rifMan<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			sawMan = <span style="color: #000000; font-weight: bold;">new</span> SAW<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">480</span>,<span style="color: #cc66cc;">424</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>sawMan<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			grenMan = <span style="color: #000000; font-weight: bold;">new</span> Grenadier<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">428</span>,<span style="color: #cc66cc;">512</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>grenMan<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			tmLdr2 = <span style="color: #000000; font-weight: bold;">new</span> TeamLdr<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">420</span>,<span style="color: #cc66cc;">162</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>tmLdr2<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			rifMan2 = <span style="color: #000000; font-weight: bold;">new</span> RifleMn<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">548</span>,<span style="color: #cc66cc;">76</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rifMan2<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			sawMan2 = <span style="color: #000000; font-weight: bold;">new</span> SAW<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">600</span>,<span style="color: #cc66cc;">162</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>sawMan2<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			grenMan2 = <span style="color: #000000; font-weight: bold;">new</span> Grenadier<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">548</span>,<span style="color: #cc66cc;">250</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>grenMan2<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> mgCrew<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			mgunner = <span style="color: #000000; font-weight: bold;">new</span> MG<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">520</span>,<span style="color: #cc66cc;">500</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>mgunner<span style="color: #66cc66;">&#41;</span>;
			asstMgunner = <span style="color: #000000; font-weight: bold;">new</span> AssistantMG<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">550</span>,<span style="color: #cc66cc;">570</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>asstMgunner<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> rocketCrew<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			rocket = <span style="color: #000000; font-weight: bold;">new</span> RocketMan<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">650</span>,<span style="color: #cc66cc;">250</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rocket<span style="color: #66cc66;">&#41;</span>;
			asstRocket = <span style="color: #000000; font-weight: bold;">new</span> AssistRocket<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">700</span>,<span style="color: #cc66cc;">300</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>asstRocket<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> hq<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			lieut = <span style="color: #000000; font-weight: bold;">new</span> Lieutenant<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">132</span>,<span style="color: #cc66cc;">98</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>lieut<span style="color: #66cc66;">&#41;</span>;
			platoonSgt = <span style="color: #000000; font-weight: bold;">new</span> PltSgt<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">66</span>,<span style="color: #cc66cc;">188</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>platoonSgt<span style="color: #66cc66;">&#41;</span>;
			radio = <span style="color: #000000; font-weight: bold;">new</span> RadioMan<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">194</span>,<span style="color: #cc66cc;">188</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>radio<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> clearFireTeam<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			removeChild<span style="color: #66cc66;">&#40;</span>tmLdr<span style="color: #66cc66;">&#41;</span>;
			removeChild<span style="color: #66cc66;">&#40;</span>sawMan<span style="color: #66cc66;">&#41;</span>;
			removeChild<span style="color: #66cc66;">&#40;</span>grenMan<span style="color: #66cc66;">&#41;</span>;
			removeChild<span style="color: #66cc66;">&#40;</span>rifMan<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> clearSquad<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			clearFireTeam<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			removeChild<span style="color: #66cc66;">&#40;</span>sqLdr<span style="color: #66cc66;">&#41;</span>;
			removeChild<span style="color: #66cc66;">&#40;</span>tmLdr2<span style="color: #66cc66;">&#41;</span>;
			removeChild<span style="color: #66cc66;">&#40;</span>sawMan2<span style="color: #66cc66;">&#41;</span>;
			removeChild<span style="color: #66cc66;">&#40;</span>grenMan2<span style="color: #66cc66;">&#41;</span>;
			removeChild<span style="color: #66cc66;">&#40;</span>rifMan2<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> piedPiper<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>numChildren <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				removeChildAt<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			addChild<span style="color: #66cc66;">&#40;</span>ftBtn<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>sqBtn<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>mgBtn<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rocketBtn<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>hqBtn<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>If you run the program from within the Flash/Flex IDE, you should be able to see trace statements in the Output window as well to tell you what’s going on—in addition to the sound effects.</p>
<p><strong>The Next Steps</strong></p>
<p>I doubt there’s anyone who’s read this blog who doesn’t recognize the design pattern in use; so I won’t go into that. However, this project is yet to be completed. That will come in future posts. Now that we’re on the right track, we (that means you as well) can envision what other behaviors—or class of behaviors and their accompanying algorithms—could be added to the current set.</p>
<p>If you’re interested in game development, I hope that this little demonstration has shown the practicality of using design patterns and how easy they can make it. This is especially true as your projects become more complex. You might also want to review Chapter 13 of our book where we cover the <strong>Symmetric Proxy Pattern</strong>. There you will find a solution to the problem of simultaneous movement and multi-person play over the Internet using Flash Media Server. Setting up a platoon or even squad level game using simultaneous movement would be a lot of fun, and you could use several of the design patterns we discussed on this blog and in our book.</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%2F08%2Fwrong-way-warrior-getting-flexibility-with-design-patterns-part-ii%2F&amp;title=Wrong%20Way%20Warrior%3A%20Getting%20Flexibility%20with%20Design%20Patterns%E2%80%94Part%20II" 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/2009/08/wrong-way-warrior-getting-flexibility-with-design-patterns-part-ii/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Easy and Practical Strategy Design Pattern</title>
		<link>http://www.as3dp.com/2009/03/actionscript-30-easy-and-practical-strategy-design-pattern/</link>
		<comments>http://www.as3dp.com/2009/03/actionscript-30-easy-and-practical-strategy-design-pattern/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 01:53:21 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Strategy Pattern]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=749</guid>
		<description><![CDATA[Recently on this blog I created a little application to be used for a quick lookup of the different principles used in both OOP and Design Patterns—The Lunch Bucket Rules. The app uses a Strategy design pattern that is both simple and practical. The pattern is designed to load external SWF and graphic files (.gif, [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.as3dp.com/wp-content/uploads/2009/03/bucket.png" alt="bucket" title="bucket" width="150" height="133" class="alignleft size-full wp-image-750" />Recently on this blog I created a little <a href="http://www.as3dp.com/2009/02/26/oop-designs-pattern-principles-ready-for-work/">application</a> to be used for a quick lookup of the different principles used in both OOP and Design Patterns—<strong>The Lunch Bucket Rules</strong>. The app uses a <strong>Strategy</strong> design pattern that is both simple and practical. The pattern is designed to load external SWF and graphic files (.gif, .png, and .jpg) and nothing more. You can pop it on a USB drive and put it on your keychain, and you’ll have a simple design pattern that you can take to work and use whenever you want. If you need a simple design pattern starting point, this should do the trick. (You can <a href="http://nemo.mwd.hartford.edu/~wsanders/lunchbucket/">download the whole thing</a>, including source code and swf files if you like.)</p>
<p><strong>Swiss Army Knife of Design Patterns: The Strategy Pattern</strong></p>
<p>The Strategy pattern defines a family of algorithms, encapsulates each and makes them interchangeable. Figure 1 shows the basic look of the Strategy pattern:<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/03/strategyclassic21.png" alt="strategyclassic2" title="strategyclassic2" width="518" height="272" class="alignnone size-full wp-image-3467" /><br />
<em><strong>Figure 1:</strong> Strategy pattern Class Diagram</em></p>
<p>An interface (Strategy) describes the abstract signatures for the algorithm to be used with a <em>family of algorithms</em>. The key here is understanding that when developing a Strategy pattern that you need to think through the implications of the abstract methods you’ve got as part of a cluster of <em>related</em> algorithms. Given that this example only has a single Concrete Strategy class, you can assume that you have an opportunity to add more concrete strategies, and at the end of the post I’ll suggest a few.<br />
<span id="more-749"></span><br />
<strong>The Strategy and Its Subclasses</strong></p>
<p>In developing projects using design patterns, you can start in a lot of different places, and I seem to have started in all of them. For this one, I started with the Client and imagined what I wanted to <strong>request</strong>. Keeping in mind that the only reason a Client should change is to make a request.<br />
<blockquote>The Client is the <em>gimme</em> class.</p></blockquote>
<p> Once figuring out what I wanted by fiddling around with the Client, I decided on the Strategy pattern. All I needed <em>just for this particular application </em> was for it to load an SWF file. However, I was thinking, what if I wanted to load materials from XML or text files? I’d have to use slightly different algorithms but different enough for different concrete strategies. So, the Strategy pattern looked to be a good place to add algorithms for loading XML and text files if somewhere down the road I wanted more flexibility in what I could load.</p>
<p>So first off, let’s look at the Strategy interface (an abstract class in this case) and then look at the concrete 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('p749code27'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p74927"><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="p749code27"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Abstract class: Strategy 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>;
	<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> ILoadMaster <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loadStuff<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<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>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see it’s got a single abstract method with a single string parameter. The string parameter is for the name (URL if you wish) of the file to be loaded. The implementation of this abstract class loads a SWF or graphic files.</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('p749code28'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p74928"><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
</pre></td><td class="code" id="p749code28"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Concrete Strategy</span>
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>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> PixLoader <span style="color: #0066CC;">extends</span> ILoadMaster
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> urlReq:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loader:Loader;
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loadStuff<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			urlReq = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><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>urlReq<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>loader<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 string parameter is passed to the URLRequest class initializer, which in turn is used in the load() parameter. All in all it’s pretty simple, but it resides in a loose pattern and is easy to extend.</p>
<p><strong>The Simple Beauty of the Context Class</strong></p>
<p>In most of the Strategy patterns I’ve written, the Context class has subclasses. If you peruse the Web, you’ll find lots of examples of the Strategy pattern using multiple Context subclasses. (In our book this was the case and in one of the <a href="http://www.as3dp.com/2008/11/09/actionscript-30-strategy-design-pattern-under-no-conditionals/">blog posts</a> as well.)  In retrospect, the Context class left in its original concrete form has much to offer. It’s like a <em>polymorphism machine</em>. The following Context class follows the GoF model (as does the class diagram in Figure 1).</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('p749code29'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p74929"><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="p749code29"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Context class</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> Context <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> lm:ILoadMaster;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Context<span style="color: #66cc66;">&#40;</span>lm:ILoadMaster<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: #006600;">lm</span> = lm;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doLoad<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			lm.<span style="color: #006600;">loadStuff</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>lm<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>Even though the Context class is concrete, everything about it is abstract. The references are to <strong>ILoadMaster</strong> (the Strategy interface) and an unnamed string parameter. So you could add as many concrete strategies as you want, and they could all pass though the Context class without a concrete reference at all.</p>
<p><strong>The Client Requests a Context</strong></p>
<p>Finally, the Client requests whatever it wants. The Client makes a request through the Context class by instantiating an ILoadMaster instance in the instantiation process. The line,</p>
<p><code><strong>contextDy=new Context(new PixLoader());</strong></code></p>
<p>(in the seeNow() method), sends the request to the concrete strategy class. The Context passes the request on to the concrete strategy without knowing what concrete strategy is being requested. As long as the correct format is observed, the Context is perfectly happy.</p>
<p>Keeping in mind that <em>you can make the requests any way you want</em> the following shows how the requests were made using a List component in the version I did. However, go ahead and use any request mechanism you want.</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('p749code30'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p74930"><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
</pre></td><td class="code" id="p749code30"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Client class</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> 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: #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> context:Context=<span style="color: #000000; font-weight: bold;">new</span> Context<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> PixLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
                <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> contextDy:Context;
		<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: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> lunchBucket:DataProvider;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> swf:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> container: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> 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>;
			context.<span style="color: #006600;">doLoad</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;swf/p0.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
			container.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">180</span>,context.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">0</span>;
			container.<span style="color: #006600;">addChildAt</span><span style="color: #66cc66;">&#40;</span>context,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>container<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>
			lunchBucket=<span style="color: #000000; font-weight: bold;">new</span> DataProvider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			lunchBucket.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Liskov Substitution&quot;</span>, <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;p1.swf&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
			lunchBucket.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Program to Interface&quot;</span>, <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;p2.swf&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
			lunchBucket.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Favor Composition&quot;</span>,  <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;p3.swf&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
			lunchBucket.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Encapsulate What Varies&quot;</span>, <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;p4.swf&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
			lunchBucket.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Loosely Coupled Objects&quot;</span>, <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;p5.swf&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
			lunchBucket.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Open/Closed&quot;</span>, <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;p6.swf&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
			lunchBucket.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Dependency Inversion&quot;</span>,  <span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;p7.swf&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
			lunchBucket.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Least Knowledge Principle&quot;</span>,<span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;p8.swf&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
			lunchBucket.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Hollywood Principle&quot;</span>,<span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;p9.swf&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</span>;
			lunchBucket.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#123;</span> label: <span style="color: #ff0000;">&quot;Only 1 Reason to Change&quot;</span>,<span style="color: #0066CC;">data</span>:<span style="color: #ff0000;">&quot;p10.swf&quot;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#41;</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;">setSize</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">150</span>,<span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">list</span>.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">16</span>,<span style="color: #0066CC;">list</span>.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">64</span>;
			<span style="color: #0066CC;">list</span>.<span style="color: #006600;">dataProvider</span> = lunchBucket;
			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>,seeNow<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> seeNow<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>
			container.<span style="color: #006600;">removeChildAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
			contextDy=<span style="color: #000000; font-weight: bold;">new</span> Context<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> PixLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			swf=<span style="color: #ff0000;">&quot;swf/&quot;</span>+<span style="color: #0066CC;">e</span>.<span style="color: #006600;">item</span>.<span style="color: #0066CC;">data</span>;
			contextDy.<span style="color: #006600;">doLoad</span><span style="color: #66cc66;">&#40;</span>swf<span style="color: #66cc66;">&#41;</span>;
			container.<span style="color: #006600;">addChildAt</span><span style="color: #66cc66;">&#40;</span>contextDy,<span style="color: #cc66cc;">0</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>You will need a folder named “swf” for the SWF files and use the naming convention shown in the <strong>lunchBucket</strong> DataProvider. (Of course, you can use any naming convention you want; just change the code in the DataProvider if you do so.) Also, if you’re using Flash, you’ll need a copy of the List component in the Library.</p>
<p><strong>You’re On Your Own</strong></p>
<p>The purpose of this little design pattern is to make it simple yet practical. Take it to work and see if it comes in handy when you have a project loading external files. The <em>family</em> of algorithms I had in mind when I decided to use this pattern is the one of <em>loadable external data</em> such as data from XML or text files. I believe that this pattern will do the job.</p>
<p>I would be delighted to see extensions of more concrete strategies using this pattern. Also I think it’s probably a good exercise for learning more about design patterns in ActionScript 3.0. Of course if you’d like to make any artful graphic improvements to my efforts, those would be a delight as well.</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%2F03%2Factionscript-30-easy-and-practical-strategy-design-pattern%2F&amp;title=ActionScript%203.0%20Easy%20and%20Practical%20Strategy%20Design%20Pattern" 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/2009/03/actionscript-30-easy-and-practical-strategy-design-pattern/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Strategy Design Pattern: Under No Conditionals</title>
		<link>http://www.as3dp.com/2008/11/actionscript-30-strategy-design-pattern-under-no-conditionals/</link>
		<comments>http://www.as3dp.com/2008/11/actionscript-30-strategy-design-pattern-under-no-conditionals/#comments</comments>
		<pubDate>Mon, 10 Nov 2008 03:11:52 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Strategy Pattern]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=286</guid>
		<description><![CDATA[Strategies Eliminate Conditional Statements If this entry evokes a sense déjà vu, you’d be half right. It is not another jibe to condemn conditional statements, but instead it is a discovery of another pattern where you can eliminate them. The consequences listed by Gamma, et al for the Strategy pattern includes, Strategies eliminate conditional statements. [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Strategies Eliminate Conditional Statements</strong></p>
<p>If this entry evokes a sense <em>déjà vu</em>, you’d be half right. It is not another jibe to condemn conditional statements, but instead it is a discovery of another pattern where you can eliminate them. The consequences listed by Gamma, <em>et al</em> for the Strategy pattern includes,</p>
<blockquote><p> Strategies eliminate conditional statements.</p>
</blockquote>
<p>While I am quite familiar with the fact that the State design pattern eschews conditional statements, I was unaware that the Strategy pattern has the same feature. Rather, like everyone else I focused on the dictum, <em> encapsulate the concept that varies </em> when dealing with the Strategy pattern. Here I do not plan to re-hash what we covered in Chapter 11 of our book, but instead I want to look at a simple Strategy design pattern where no conditional statements are used. Also, I want to walk through a thought process when working on a simple application and show how flexible it is because of the Strategy design pattern.</p>
<p><strong>It All Began with a simple MP3 Player</strong></p>
<p>Creating an MP3 player in Flash is something I’ve done often and never gave it a second thought using the NetConnection and NetStream classes. However, recently I was working on one using the Sound and SoundChannel classes. Turning it on and off was trivial, but I learned that unlike the NetStream class, the SoundChannel class has no pause method. (NetStream has both toggle and non-toggle versions of pause methods.) The SoundChannel has a position property that returns the position of the MP3 file currently playing. By passing the position value to a variable and passing it to a <code>play()</code> method as a parameter, the play would resume where it left off. So building a player using either NetStream or SoundChannel is not rocket science.</p>
<p>However, suppose you decide to have a structure that could use either the NetStream or SoundChannel class. To that you might also add whether you wanted the MP3 to play on the NetStream as a progressive download or as streaming audio through <strong>Flash Media Server 3</strong>. All you have to change is the content of the algorithms to accommodate any of the three versions you envision. Of course you don’t want to dismantle all of the other work if you decide to change the program. You just need to change the algorithms.</p>
<p><strong>The Abstract Architecture of a Strategy MP3 Player</strong></p>
<p>When you get right down to it, you only need four algorithms:</p>
<ul>
<li>Start Play</li>
<li>Stop Play</li>
<li>Pause Play</li>
<li>Unpause Play</li>
</ul>
<p>Depending on what you have set up in the Client class for a UI and the types of classes you’re using to play the MP3 files, the details of the algorithms will vary, and you may even have a class to organize the Sound/SoundChannel or NetConnection/NetStream methods.  For now, though, all I want is architecture to handle starting and stopping play and pause. This will also help reveal the structure of the Strategy design pattern. Figure 1 shows the Strategy pattern that is used in this example:<br />
<img src="http://www.as3dp.com/wp-content/uploads/2008/11/stratuncondfit.png" alt="stratuncondfit" title="stratuncondfit" width="500" height="163" class="alignleft size-full wp-image-696" />
</p>
<p><em><strong>Figure 1:</strong> MP3 Player Strategy Design Pattern</em></p>
<p>The <em><strong>IPlayer</strong></em> interface is the Strategy portion of the pattern. All of the algorithms go into implementations of IPlayer, and so all we need will be four concrete strategies—one for each of the actions on the MP3 player. For this example, I’ll just use <code>trace()</code> statements to act as stand-ins for actual algorithms.</p>
<p><strong>Context Class</strong></p>
<p>The left part of the class diagram in Figure 1 is the Context participant in the pattern. As shown in the diagram, the Context class holds a reference to the Strategy (IPlayer) participant. That reference to the IPlayer can be seen 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('p286code35'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p28635"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p286code35"><pre class="actionscript" style="font-family:monospace;">&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Context
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> player:IPlayer;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doControl<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			player.<span style="color: #0066CC;">control</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><span id="more-286"></span></p>
<p>The reference is in typing the <strong>player</strong> instance as an <strong> IPlayer </strong> and then holding a reference to the<strong> control()</strong>  method in the player. The control() method is an algorithm developed in the strategy implementations. The Context class has no idea what that reference to the player method will be but serves to make it available to a client and subclasses that inherit the Context class. In this case, four Context subclasses are created, one for each of the operations—playing, stopping play, starting pause, and turning pause off. The following listings show all of the classes that extend the Context 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('p286code36'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p28636"><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="p286code36"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Starter.as</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Starter <span style="color: #0066CC;">extends</span> Context
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Starter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			player= <span style="color: #000000; font-weight: bold;">new</span> StartPlay<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;">//Stopper.as</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Stopper <span style="color: #0066CC;">extends</span> Context
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Stopper<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			player= <span style="color: #000000; font-weight: bold;">new</span> StopPlay<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;">//Pauser.as</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Pauser <span style="color: #0066CC;">extends</span> Context
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Pauser<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			player= <span style="color: #000000; font-weight: bold;">new</span> StartPause<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;">//UnPauser.as</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UnPauser <span style="color: #0066CC;">extends</span> Context
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> UnPauser<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			player= <span style="color: #000000; font-weight: bold;">new</span> StopPause<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>Each of the subclasses of Context instantiates a single strategy implementation, and no reference is made to the strategy’s class key method. As a result, the design pattern demonstrates a high degree of loose coupling.</p>
<p><strong>The Strategy Interface and Implementations</strong></p>
<p>The right side of the Strategy class diagram (Figure 1) shows the interface and implementations of the Strategy interface (IPlayer). Each of the algorithms is developed in the concrete strategies to be accessed by a client through the Context class. The algorithms can be as simple or complex as required by the goals of the application. In this particular case, the “algorithms” are nothing more than <strong>trace</strong> statements that are placeholders for whatever algorithm one wishes to use. For the practical programmer this means that when change occurs, all you need to do is to change the algorithm without worrying about conflict with other parts of the program. The following shows the Strategy interface (IPlayer) and its four concrete 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('p286code37'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p28637"><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="p286code37"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//IPlayer.as</span>
package
<span style="color: #66cc66;">&#123;</span>
<span style="color: #808080; font-style: italic;">//Strategy interface</span>
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IPlayer
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">control</span><span style="color: #66cc66;">&#40;</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>
&nbsp;
<span style="color: #808080; font-style: italic;">//StartPlay.as</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Strategy</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StartPlay <span style="color: #0066CC;">implements</span> IPlayer
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//var playMP3:PlayTune</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">control</span><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;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Play tune&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;">// StopPlay.as</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Strategy</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StopPlay <span style="color: #0066CC;">implements</span> IPlayer
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//var playMP3:PlayTune</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">control</span><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;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Stop play&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;">//StartPause.as</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Strategy</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StartPause <span style="color: #0066CC;">implements</span> IPlayer
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//var playMP3:PlayTune</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">control</span><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;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Start pause&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;">// StopPause.as</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Strategy</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StopPause <span style="color: #0066CC;">implements</span> IPlayer
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//var playMP3:PlayTune</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">control</span><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;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Stop pause&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>As you can see, each of the concrete classes has a commented line with a reference to:</p>
<p><code>playMP3:PlayTune</code></p>
<p>This line represents some reference to a class that handles either the Sound/SoundChannel or NetConnection/NetStream classes.</p>
<p><strong>The Client</strong></p>
<p>The Gang of Four note that <em>[a] context forwards requests from its clients to its strategy</em>, and that <em> [clients] usually create and pass a ConcreteStrategy object to the context.</em> This is exactly what we have done with the Client class. As you can see in the <strong>Client.as</strong> listing below all references are to the Context class or one of its extensions where the specific algorithm from the Strategy classes are referenced via the concrete context class and not the strategy implementation.</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('p286code38'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p28638"><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
</pre></td><td class="code" id="p286code38"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Client.as</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> 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> musicMaster1:Context;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> musicMaster2:Context;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> musicMaster3:Context;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> musicMaster4:Context;
&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>
			musicMaster1=<span style="color: #000000; font-weight: bold;">new</span> Starter<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			musicMaster1.<span style="color: #006600;">doControl</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			musicMaster2=<span style="color: #000000; font-weight: bold;">new</span> Stopper<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			musicMaster2.<span style="color: #006600;">doControl</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			musicMaster3=<span style="color: #000000; font-weight: bold;">new</span> Pauser<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			musicMaster3.<span style="color: #006600;">doControl</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			musicMaster4=<span style="color: #000000; font-weight: bold;">new</span> UnPauser<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			musicMaster4.<span style="color: #006600;">doControl</span><span style="color: #66cc66;">&#40;</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>The <strong>doControl()</strong> method from the Context class is a reference to the IPlayer’s sole method, <strong>control()</strong>.  However, as you can see the Client accesses the Strategy through the Context enabling a loose but effective connection between the client and the algorithms in the program.</p>
<p>When you test the Client, you will see each of the “algorithm’s” results.<br />
<code><br />
Play tune<br />
Stop play<br />
Start pause<br />
Stop pause<br />
</code></p>
<p>Each of the instantiations of the different Context classes could just as well be functions fired by a button or some other UI. Also, note that not a single conditional statement can be found in the entire program. A selection from the Client assumes a certain response with no <em>ifs, ands or buts</em>. As a result, there’s no conditional linkage to get tangled up in if you decide to make a change.</p>
<p><strong>Do-It-Yourself MP3 Player</strong></p>
<p>Now that you have the basic architecture for an MP3 player, we’d like to see if any of you can actually put something together that will play/pause/stop/unpause an actual MP3 application using this pattern. If you’re new to Design Patterns, it’ll be a chance to see if you can use them to accomplish a task beyond the abstract traces done in this example. If you’re an old hand with Design Patterns, it’s a little toy problem to solve like a crossword puzzle. The challenge is to do it in the design pattern provided. You can use either Flex or Flash (the example was developed in Flex), and the Client class should have all of the UIs and instances you’d normally have. You can add classes to support the client and you need to change all of the algorithms in the Strategy concrete classes, but it must maintain the basic class structure shown.</p>
<p>If you&#8217;re interested in the Strategy patterns in ActionScript 3.0, here&#8217;s an article by Sean Moore that I think you&#8217;ll like:<a href="http://www.insideria.com/2008/11/exploring-the-strategy-design.html">http://www.insideria.com/2008/11/exploring-the-strategy-design.html</a></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2008%2F11%2Factionscript-30-strategy-design-pattern-under-no-conditionals%2F&amp;title=ActionScript%203.0%20Strategy%20Design%20Pattern%3A%20Under%20No%20Conditionals" 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/2008/11/actionscript-30-strategy-design-pattern-under-no-conditionals/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

