<?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; Examples</title>
	<atom:link href="http://www.as3dp.com/category/examples/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>Hitchhiker’s Guide to ActionScript 3.0:  Dragging through the Galaxy—Part 1</title>
		<link>http://www.as3dp.com/2009/09/hitchhikers-guide-to-actionscript-30-dragging-through-the-galaxy-part-1/</link>
		<comments>http://www.as3dp.com/2009/09/hitchhikers-guide-to-actionscript-30-dragging-through-the-galaxy-part-1/#comments</comments>
		<pubDate>Wed, 09 Sep 2009 09:00:08 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Closer Look]]></category>
		<category><![CDATA[Examples]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1455</guid>
		<description><![CDATA[I have no idea how things pop into my head or why. However, they do, and like unscratched itches, if I don’t attend to them they stay put. One such idea that cropped up was… How do you drag a live video across the screen? I have no idea why someone would want to do [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_1393" class="wp-caption alignleft" style="width: 160px"><img src="http://www.as3dp.com/wp-content/uploads/2009/08/closerlook.png" alt="A Closer Look" title="closerlook" width="150" height="143" class="size-full wp-image-1393" /><p class="wp-caption-text">A Closer Look</p></div>
<p>I have no idea how things pop into my head or why. However, they do, and like unscratched itches, if I don’t attend to them they stay put. One such idea that cropped up was… </p>
<blockquote><p><em>How do you drag a live video across the screen?</em></p>
</blockquote>
<p> I have no idea <em>why</em> someone would want to do that either; so don’t ask. For 95% of you reading this, the answer is simple, but for the 5% of you in rehab this post may be of some use—at least until the pink elephants go away.</p>
<p>Because this is a Design Pattern blog, I should include <em>some</em> design pattern. I could slap a Singleton in front of it, but I pretty well shot my chances of doing that with my ongoing persecution of that miserable pattern. So I need to pose my question in a broader context,</p>
<blockquote><p>What design pattern could be used to drag <em>anything</em> across the screen?</p>
</blockquote>
<p>Well, put <em>that way</em>, we can make a more interesting project. This short post simply examines how to embed and drag a non-Sprite object in a Sprite object. In Part II, we’ll see how to create a design pattern that easily allows production of objects and placing them into a Sprite instance for dragging <em>anything</em>.</p>
<p><strong>Hitch Your Wagon to a Sprite</strong></p>
<p>Back in the days before ActionScript 3.0, about the only thing you could address on the stage was a <strong>MovieClip</strong> instance—or a derivative of a MovieClip. For some of us, the lesson was too well learned, and I often think, <em>MovieClip</em> instead of <em>Sprite</em>. Anyway, you can address a Sprite just as well and you don’t have the Timeline to go along for the ride.  The trick back in the day was to dump anything you wanted to go along with the MovieClip into the movie clip. So if you had a graphic symbol, for instance, that you wanted to drag, you’d just put it into the MovieClip object and drag the movie clip. However, because the key methods of <strong>startDrag()</strong> and <strong>stopDrag()</strong> are part of the Sprite class, you can use the Sprite to do the same thing.</p>
<p><span id="more-1455"></span></p>
<p>Adding one object to another object in ActionScript 3.0 takes many forms, but when it comes to adding an object to a Sprite, a simple method is to use the <strong>addChild()</strong> method. The statement,</p>
<p>&nbsp; &nbsp; &nbsp; mySprite.addChild(myObject);</p>
<p>does the trick. By referencing all drag methods to the Sprite instance, you can drag the Sprite and anything that’s been added to it. The following program takes care of the initial query I had—it allows you to drag a video.</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('p1455code2'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p14552"><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
</pre></td><td class="code" id="p1455code2"><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;">media</span>.<span style="color: #0066CC;">Camera</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">media</span>.<span style="color: #0066CC;">Video</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DragVid <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> const WIDE:uint=<span style="color: #cc66cc;">320</span>;
		<span style="color: #0066CC;">private</span> const HIGH:uint=<span style="color: #cc66cc;">240</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> vidSled: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> vid:<span style="color: #0066CC;">Video</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Video</span><span style="color: #66cc66;">&#40;</span>WIDE,HIGH<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> cam:<span style="color: #0066CC;">Camera</span> = <span style="color: #0066CC;">Camera</span>.<span style="color: #006600;">getCamera</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> DragVid<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
		setMedia<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		vidSled.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_DOWN</span>,doDrag<span style="color: #66cc66;">&#41;</span>;
		vidSled.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_UP</span>,unDrag<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setMedia<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			cam.<span style="color: #0066CC;">setMode</span><span style="color: #66cc66;">&#40;</span>WIDE,HIGH,<span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span>;
			cam.<span style="color: #0066CC;">setQuality</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;
			vid.<span style="color: #0066CC;">smoothing</span>=<span style="color: #000000; font-weight: bold;">true</span>;
			vid.<span style="color: #006600;">attachCamera</span><span style="color: #66cc66;">&#40;</span>cam<span style="color: #66cc66;">&#41;</span>;
			vid.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">100</span>,vid.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">100</span>;
			addChild<span style="color: #66cc66;">&#40;</span>vidSled<span style="color: #66cc66;">&#41;</span>;
			vidSled.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>vid<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> doDrag<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">startDrag</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> unDrag<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">stopDrag</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>You may notice that no <strong>net</strong> elements are included, such as NetConnection or NetStream. However, they could be added for anything from a video playback to a Flash Media Interactive Server application. The objects associated with the dragging lose none of their functionality. As you can see the code sets the mode and quality of the Camera instance. Likewise, the Video instance takes advantage of mipmapping by setting the <strong>smoothing</strong> property to true.</p>
<p> Hook up your camera and give it a try by clicking the &#8220;Play&#8221; button:<br />
<a href="http://www.sandlight.net/hitchhiker/"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignnone size-full wp-image-1257" /></a></p>
<p>Figure 1 shows this simple process. The hand icon in the upper right corner of the video object shows where the mouse has seized the image and is dragging the video embedded in the Sprite object.<br />
<div id="attachment_1456" class="wp-caption alignleft" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2009/09/hitch.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; Video hitchhiking on a Sprite gets dragged as startled subject looks on&lt;/em&gt;" title="hitch" width="500" height="385" class="size-full wp-image-1456" /><p class="wp-caption-text"><em><strong>Figure 1:</strong> Video hitchhiking on a Sprite gets dragged as startled subject looks on</em></p></div></p>
<p>Originally, I added a graphic in the Sprite to provide a visual background, but that was just added <em>bling</em> with no function other than distracting readers from the purpose of the Sprite. However, for those who like backdrops, you can add one using the graphics property and methods in the Sprite object.</p>
<p><strong>What Varies?</strong></p>
<p><img src="http://www.as3dp.com/wp-content/uploads/2009/06/magicapp.png" alt="magicapp" title="magicapp" width="111" height="65" class="alignleft size-full wp-image-1071" /></p>
<p>For some food for thought for Part II of this post, get the <a href="http://www.sandlight.com/magic/"> Magic Table</a> (if you don’t already have it) to determine what variation you need to control. That will give you a guide to the design pattern we can use. (Hint: We <em>do not need to vary the sole instance of a class</em>). </p>
<p>Essentially, here’s what we need to figure out:</p>
<blockquote><p> What varies that we need to deal with when creating <em>any</em> dragable object?</p>
</blockquote>
<p>Once that question is answered, we can begin working on the program using the design pattern that takes into account the variation. In the meantime, we’d like to get your comments on both the variation and the task of dragging objects using OOP and ActionScript 3.0.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2009%2F09%2Fhitchhikers-guide-to-actionscript-30-dragging-through-the-galaxy-part-1%2F&amp;title=Hitchhiker%E2%80%99s%20Guide%20to%20ActionScript%203.0%3A%20%20Dragging%20through%20the%20Galaxy%E2%80%94Part%201" 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/2009/09/hitchhikers-guide-to-actionscript-30-dragging-through-the-galaxy-part-1/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Artists, Animators and ActionScript 3.0</title>
		<link>http://www.as3dp.com/2009/08/artists-animators-and-actionscript-30/</link>
		<comments>http://www.as3dp.com/2009/08/artists-animators-and-actionscript-30/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 21:44:25 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1380</guid>
		<description><![CDATA[Artists and Graphic Designers For me, graphic designers and artists are angels. No matter how I try, I can only get so far in graphic design. Tools like clip art, templates, and Kuler help me achieve not awful , but that’s it. (I can even screw up clip art.) So, for anything serious, I’ve got [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Artists and Graphic Designers</strong></p>
<p><img src="http://www.as3dp.com/wp-content/uploads/2009/08/designer-295x300.png" alt="designer" title="designer" width="295" height="300" class="alignleft size-medium wp-image-1381" /></p>
<p>For me, graphic designers and artists are angels. No matter how I try, I can only get so far in graphic design. Tools like clip art, templates, and Kuler help me achieve <em>not awful </em>, but that’s it. (I can even screw up clip art.) So, for anything serious, I’ve got to work with graphic artists.  That’s no problem—I like working with angels.</p>
<p>Some graphic artist have made the transition to some version of ActionScript, but with ActionScript 3.0 most complained that they were getting left behind. Early Flash had few ActionScript options and a system for entering code that didn’t require any programming background at all. With ActionScript 2.0, things got better for developers, but designers started voicing concern over increased complexity. With ActionScript 3.0 and the loss of the ability to put code into buttons and MovieClip objects directly, some graphic artists became furious with Flash over what they saw as a betrayal. It was like a carload of kids on the way to do something fun ditched the artists and designers on the roadside.<br />
<span id="more-1380"></span><br />
Other than a handful of developer/designers, most designers aren’t going to become competent with ActionScript 3.0, let alone OOP or design patterns—just like I’m never going to be a competent designer or artist. So I was thinking an interesting project for this blog would be to develop some “Clip Code” for artists. However, I’m not sure what kind of code they’d need. Most of the “Clip Art” that I use is from photographs, but while I know the kind of artwork I need, I don’t know what kind code graphic designers need.</p>
<p>In the same way that I don’t want to have shoddy graphics or photographs (I can do that myself), the “Clip Code” I have in mind is grounded in good but simple design patterns and OOP that artists can use. So, I’d like to throw that one out to the readers of this blog for ideas of what would be useful to graphic designers.</p>
<p><strong>Animation and Dante’s 10th Circle of ActionScript Hell</strong></p>
<p>We all remember from our Italian Literature course that Dante Alighieri’s <em>Divine Comedy</em> describes hell as having nine circles—the last circle being reserved for treason. Were Dante an ActionScript developer, he’d have a 10th Circle for those animators who spread 50 little ActionScript code blocks all over the timeline in different frames and on different layers. (In the last mention of Dante’s inferno on this blog, we only got to the 9th circle.)</p>
<p><img src="http://www.as3dp.com/wp-content/uploads/2009/08/animator-295x300.png" alt="animator" title="animator" width="295" height="300" class="alignleft size-medium wp-image-1382" /></p>
<p>Don’t get me wrong. I love animation, and in fact I got Flash initially so that I could do animations on the Web. When first working with Flash, I ran out and got Tony White’s <em>The Animator’s Workbook: Step-by-Step Techniques of Drawn Animation</em>, and I was delighted with what Flash could do following traditional animation techniques. (Joe Cartoon is still one of my heroes.)</p>
<p>What drives me nuts is using animations for some little effect that will be forgotten after one or two views that screw up an entire application. I’m convinced that animation is the <em>sine qua non</em> of Flash, and so I would never suggest that it be reduced in any way. Rather, I think that animation in movie clips needs to be controlled outside of the confines of code embedded in frames and the timeline. For example, pressing the green Play button shows a simple movie clip added to an existing fire-the-weapon animation:</p>
<p><a href="http://www.sandlight.net/testBench/weapon/"  target="_blank"><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" /><br />
</a></p>
<p>The relevant code for the operation is the following:</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('p1380code4'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13804"><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="p1380code4"><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;">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>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</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> 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> m249:<span style="color: #0066CC;">MovieClip</span>=<span style="color: #000000; font-weight: bold;">new</span> M249<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> mc:<span style="color: #0066CC;">MovieClip</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> weapon:SoundChannel;
		<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;M249.mp3&quot;</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>
			m249.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,fireWeapon<span style="color: #66cc66;">&#41;</span>;
			m249.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">50</span>,m249.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">60</span>;
			addChild<span style="color: #66cc66;">&#40;</span>m249<span style="color: #66cc66;">&#41;</span>;
			m249.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</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> fireWeapon<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>
			playOnce<span style="color: #66cc66;">&#40;</span>m249<span style="color: #66cc66;">&#41;</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: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> playOnce<span style="color: #66cc66;">&#40;</span>mc:<span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">mc</span>=mc;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">mc</span>.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">mc</span>.<span style="color: #006600;">addFrameScript</span><span style="color: #66cc66;">&#40;</span>mc.<span style="color: #006600;">totalFrames</span>-<span style="color: #cc66cc;">1</span>,reset<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">function</span> reset<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">mc</span>.<span style="color: #0066CC;">gotoAndStop</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</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  <strong>m249</strong> instance is of a movie clip that uses a shape tween to simulate muzzle flashes. (The sound routine was borrowed from <a href="http://www.as3dp.com/2009/08/19/wrong-way-warrior-getting-flexibility-with-design-patterns—part-ii/">another application</a>.)</p>
<p>Initially, the problem I encountered, and I am sure all Flash animators encounter, is the requirement of keeping at least <em>some code</em> on the timeline. After a few false starts, I was able to control it with relatively simple functions with <em>no timeline code</em>. The above code simply represents the possibility of allowing animators to do whatever they want while not worrying about code.</p>
<p>Thanks to Steve Mathews and Colin Holgate who introduced me to the undocumented <strong>MovieClip.addFrameScript()</strong>, I was able to stop the looping movie clip after a single iteration. My initial solution used <strong>Event.ENTER_FRAME</strong>, but I liked that offered up by Steve and Colin better. Anyway, there’s a good post on <strong>addFrameScript()</strong> at <a href= "http://troyworks.com/blog/2007/09/22/as3-movieclipaddframescript/"> Troy Gardner’s blog</a> if you’d like to learn more about it. Like all undocumented code, I’m often at a loss to understand why it’s undocumented.</p>
<p>The huge advantage of an approach that is not in anyway tied to the timeline is <em>re-usability</em>. If all of the coding is external to the development of the movie clip with the animation, there’s less worry about getting tangled up in unknown code lurking on the timeline. You can reuse the animating movie clips with the assurance that what you see is what you get and you can add code without tangling it up with code tucked away in some unseen layer on an unseen frame.</p>
<p>For complex animations, planning is essential, and I’d like to find out what animators would find useful included in the examples and projects examined on this blog. Like artist, animators work is some kind of context and I’d like to refactor animations so that <em>no timeline code</em> is included in the application of the animations.</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%2Fartists-animators-and-actionscript-30%2F&amp;title=Artists%2C%20Animators%20and%20ActionScript%203.0" 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/2009/08/artists-animators-and-actionscript-30/feed/</wfw:commentRss>
		<slash:comments>26</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('p1304code9'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13049"><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="p1304code9"><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('p1304code10'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p130410"><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="p1304code10"><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('p1304code11'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p130411"><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="p1304code11"><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('p1304code12'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p130412"><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="p1304code12"><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>The Wrong Way Warrior: Where OOP Alone is Not Enough—Part I</title>
		<link>http://www.as3dp.com/2009/08/the-wrong-way-warrior-where-oop-alone-is-not-enough-part-i/</link>
		<comments>http://www.as3dp.com/2009/08/the-wrong-way-warrior-where-oop-alone-is-not-enough-part-i/#comments</comments>
		<pubDate>Sat, 15 Aug 2009 15:54:18 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Examples]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1290</guid>
		<description><![CDATA[In preparing for this year’s OOPSLA conference where my session will focus on demonstrating good practices by showing examples of poor practices, I was reminded of the first chapter of the Freemans’ wonderful book, Head First Design Patterns (O’Reilly). To introduce the reader to design patterns in Java, the Freemans set up an example where [...]]]></description>
			<content:encoded><![CDATA[<p>In preparing for this year’s OOPSLA conference where my session will focus on demonstrating good practices by showing examples of poor practices, I was reminded of the first chapter of the Freemans’ wonderful book, <em>Head First Design Patterns</em> (O’Reilly). To introduce the reader to design patterns in Java, the Freemans set up an example where a developer got in trouble because his program could not be expanded or changed without taking the whole thing apart and putting it back together again. In their wisdom, the Freemans wanted to kick things off with <em>practical reasons</em> for using design patterns—not just because doing so is the <em>right thing to do</em>.</p>
<p>In some recent discussions, I’ve heard references to OOP and design patterns as a <em>religion</em> or mockingly called “doing it right” as though design patterns are some kind of moral imperative by obsessive-compulsive developers rather than practical solutions to programming problems. Any kind of programming solution should be a solution that does a good job of solving a problem and nothing more. Discussions of OOP and design patterns as <em>the right way</em> to get things done has no resonance with me at all; so I’m the wrong person to suggest that faith in design patterns is sufficient reason to use them.<br />
<span id="more-1290"></span><br />
Perhaps the biggest problem with design patterns is that they’re difficult to learn and other simpler solutions are available. By <em>simpler</em> I’m referring to the fact that the programming and thought processes that go into the solutions are not as difficult. That’s true. However, in the long run, OOP and design patterns make programming much easier because the solutions lend themselves to revision and change. The larger and more complex that projects become, the more important design patterns are for maintaining and updating the program.</p>
<p><strong>The Wrong Way Warrior</strong></p>
<p>The first part of this post will look at a well-meaning OOP programmer who has been given the job of making a prototype for a battle game. To get started the programmer wants to kick things off by creating a Warrior class with several methods that represent what <em>any</em> warrior would have. So he puts the class together with the following methods:</p>
<ul>
<li> setID() – provide a unique ID for each warrior</li>
<li> attackRight() – move character to the right</li>
<li> attackLeft() – move character to the left </li>
<li> retreatRight() – move character to the right</li>
<li> retreatLeft() – move character to the left </li>
<li> defend() – character does not move but fires weapon</li>
<li> takeHit() – character is hit by enemy fire</li>
</ul>
<p>By subclassing the Warrior class, the developer can build armies with each individual warrior having the properties of the Warrior. That makes perfect sense, and our programmer is creating an OOP-like object. The following listing shows the Warrior 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('p1290code16'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p129016"><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
</pre></td><td class="code" id="p1290code16"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Warrior <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;">var</span> id:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setID <span style="color: #66cc66;">&#40;</span>idNow:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">id</span> = idNow;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> attackRight <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;">this</span>.<span style="color: #006600;">x</span> += <span style="color: #cc66cc;">10</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> attackLeft <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;">this</span>.<span style="color: #006600;">x</span>-=<span style="color: #cc66cc;">10</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> defend <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: #0066CC;">this</span>.<span style="color: #006600;">id</span> + <span style="color: #ff0000;">&quot; holds and fires weapon&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> retreatLeft <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;">this</span>.<span style="color: #006600;">x</span>-=<span style="color: #cc66cc;">10</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> retreatRight <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;">this</span>.<span style="color: #006600;">x</span>+=<span style="color: #cc66cc;">10</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> takeHit <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;">this</span>.<span style="color: #006600;">rotation</span>+=<span style="color: #cc66cc;">5</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>Right away you might be thinking, “<em>That’s not an abstract class, and it should be.</em>” Well, it does not have a constructor, and giving the programmer his due, we can treat it as an abstract class by not instantiating it. (If that doesn’t work, we’ll blame it on the programmer.)</p>
<p><strong>Subclassing Armies and Building MovieClip Warriors</strong></p>
<p>For images of warriors, we’ll create a couple of movie clips representing a Red Army Warrior and a Blue Army Warrior. Both are placed in the Library and set for Export for ActionScript. One is named RedWarrior and the other, BlueWarrior and each is treated as a separate class. In this way the programmer can create as many instances of each as he wants for the Red and Blue armies. Figure 1 shows each of these creations:</p>
<p><img src="http://www.as3dp.com/wp-content/uploads/2009/08/mcwarriors.png" alt="mcwarriors" title="mcwarriors" width="199" height="112" class="alignnone size-full wp-image-1291" /><br />
<strong><em>Figure 1:</strong> Movie Clip Warrior Characters</em></p>
<p>Now that the characters are ready as addressable classes, the next step is to create subclasses from the Warrior class for each as shown in the following listings:</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('p1290code17'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p129017"><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
</pre></td><td class="code" id="p1290code17"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Red Warrior</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> Red <span style="color: #0066CC;">extends</span> Warrior
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//RedWarrior is a movie clip in Library</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> red1:Sprite=<span style="color: #000000; font-weight: bold;">new</span> RedWarrior<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> Red <span style="color: #66cc66;">&#40;</span>x1:uint,y1:uint<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			red1.<span style="color: #006600;">x</span> = x1,red1.<span style="color: #006600;">y</span> = y1;
			addChild <span style="color: #66cc66;">&#40;</span>red1<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;">//Blue Warrior</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> Blue <span style="color: #0066CC;">extends</span> Warrior
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//RedWarrior is a movie clip in Library</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> blue1:Sprite=<span style="color: #000000; font-weight: bold;">new</span> BlueWarrior<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> Blue<span style="color: #66cc66;">&#40;</span>x1:uint,y1:uint<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			blue1.<span style="color: #006600;">x</span>=x1,blue1.<span style="color: #006600;">y</span>=y1;
			addChild<span style="color: #66cc66;">&#40;</span>blue1<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>With concrete classes now for the warriors, including a sprite image representing both the red and blue sides, we’re all set to create a test bench to see if this prototype is ready to develop further. (Note that the programmer even typed the RedWarrior and BlueWarrior instances as Sprite ones—programming to the interface like he should be.)</p>
<p><strong>Testing the Warriors</strong></p>
<p>Now that the basics are completed, we can set up a test bench to see how the program works. All we need is a Client class that implements the warriors and see if the methods fly. This test incorporates buttons to fire the movement methods and trace firing weapons.</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('p1290code18'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p129018"><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
</pre></td><td class="code" id="p1290code18"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">Button</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> redWarrior1:Warrior = <span style="color: #000000; font-weight: bold;">new</span> Red<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> blueWarrior1:Warrior = <span style="color: #000000; font-weight: bold;">new</span> Blue<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">400</span>,<span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> redAttack:<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> redDefend:<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> redRetreat:<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> redHit:<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> blueAttack:<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> blueDefend:<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> blueRetreat:<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> blueHit:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			UIsetup <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> UIsetup <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			redWarrior1.<span style="color: #006600;">setID</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Red Hook&quot;</span><span style="color: #66cc66;">&#41;</span>;
			addChild <span style="color: #66cc66;">&#40;</span>redWarrior1<span style="color: #66cc66;">&#41;</span>;
			blueWarrior1.<span style="color: #006600;">setID</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Blue Moon&quot;</span><span style="color: #66cc66;">&#41;</span>;
			addChild <span style="color: #66cc66;">&#40;</span>blueWarrior1<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			redAttack.<span style="color: #006600;">addEventListener</span> <span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, redCharge<span style="color: #66cc66;">&#41;</span>;
			redDefend.<span style="color: #006600;">addEventListener</span> <span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, redHold<span style="color: #66cc66;">&#41;</span>;
			redRetreat.<span style="color: #006600;">addEventListener</span> <span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, redBugout<span style="color: #66cc66;">&#41;</span>;
			redHit.<span style="color: #006600;">addEventListener</span> <span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, redWound<span style="color: #66cc66;">&#41;</span>;
			blueAttack.<span style="color: #006600;">addEventListener</span> <span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, blueCharge<span style="color: #66cc66;">&#41;</span>;
			blueDefend.<span style="color: #006600;">addEventListener</span> <span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, blueHold<span style="color: #66cc66;">&#41;</span>;
			blueRetreat.<span style="color: #006600;">addEventListener</span> <span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, blueBugout<span style="color: #66cc66;">&#41;</span>;
			blueHit.<span style="color: #006600;">addEventListener</span> <span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, blueWound<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			addChild <span style="color: #66cc66;">&#40;</span>redAttack<span style="color: #66cc66;">&#41;</span>;
			redAttack.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Attack&quot;</span>;
			redAttack.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">20</span>,redAttack.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">300</span>;
			addChild <span style="color: #66cc66;">&#40;</span>redDefend<span style="color: #66cc66;">&#41;</span>;
			redDefend.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Defend&quot;</span>;
			redDefend.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">20</span>,redDefend.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">322</span>;
			addChild <span style="color: #66cc66;">&#40;</span>redRetreat<span style="color: #66cc66;">&#41;</span>;
			redRetreat.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Retreat&quot;</span>;
			redRetreat.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">20</span>,redRetreat.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">344</span>;
			addChild <span style="color: #66cc66;">&#40;</span>redHit<span style="color: #66cc66;">&#41;</span>;
			redHit.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Outch!&quot;</span>;
			redHit.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">20</span>,redHit.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">366</span>;
&nbsp;
			addChild <span style="color: #66cc66;">&#40;</span>blueAttack<span style="color: #66cc66;">&#41;</span>;
			blueAttack.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Attack&quot;</span>;
			blueAttack.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">400</span>,blueAttack.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">300</span>;
			addChild <span style="color: #66cc66;">&#40;</span>blueDefend<span style="color: #66cc66;">&#41;</span>;
			blueDefend.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Defend&quot;</span>;
			blueDefend.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">400</span>,blueDefend.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">322</span>;
			addChild <span style="color: #66cc66;">&#40;</span>blueRetreat<span style="color: #66cc66;">&#41;</span>;
			blueRetreat.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Retreat&quot;</span>;
			blueRetreat.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">400</span>,blueRetreat.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">344</span>;
			addChild <span style="color: #66cc66;">&#40;</span>blueHit<span style="color: #66cc66;">&#41;</span>;
			blueHit.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Outch!&quot;</span>;
			blueHit.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">400</span>,blueHit.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">366</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> redCharge <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>
			redWarrior1.<span style="color: #006600;">attackRight</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> redHold <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>
			redWarrior1.<span style="color: #006600;">defend</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> redBugout <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>
			redWarrior1.<span style="color: #006600;">retreatLeft</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> redWound <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>
			redWarrior1.<span style="color: #006600;">takeHit</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> blueCharge <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>
			blueWarrior1.<span style="color: #006600;">attackLeft</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> blueHold <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>
			blueWarrior1.<span style="color: #006600;">defend</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> blueBugout <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>
			blueWarrior1.<span style="color: #006600;">retreatRight</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> blueWound <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>
			blueWarrior1.<span style="color: #006600;">takeHit</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>That looks like a pretty big Client class, but all it does is to create a single instance of the red and blue warriors and lots of buttons to test the methods. The set of buttons on the left operate the Red Warrior and the ones on the right operate the Blue Warrior. Again, the programmer is no straw man—he programs to the interface to instantiate both the Red and Blue warriors. What we are questioning is how sustainable and expandable is this design?</p>
<p>When you test the program you should see the initial image shown in Figure 2:</p>
<p><img src="http://www.as3dp.com/wp-content/uploads/2009/08/initial.png" alt="initial" title="initial" width="558" height="430" class="alignnone size-full wp-image-1292" /></p>
<p><strong><em>Figure 2:</strong> Initial Positions of the Instances</em></p>
<p>Go ahead and test it. (You’ll find yourself having seconds of fun!). Everything works, and if you add additional characters, they’ll show up on the screen, but you’ll need separate sets of buttons to work them. However, that’s not the point. In developing the game all of the methods will be triggered either by program-generated events or a more sophisticated UI. So, for the time being just think of the buttons as tools for testing and not an integral part of the program.<br />
<a href="http://www.sandlight.net/dpBlog/warrior/"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignnone size-full wp-image-1257" /></a></p>
<p><strong>What’s Wrong with this Picture?</strong></p>
<p>First of all, it works. However, that’s not the point. The concept, “<em>If it ain’t broke, don’t fix it</em>” either fails to recognize it will break down the road or flees from innovation. (It’s sort of like the wanker without a girlfriend—he thinks this is a good as it gets.)</p>
<p>What will happen when this game is expanded? (Keep in mind the buttons are just a temporary testing device.) How do we accommodate changes in the weapons? What if we want to have special capabilities for each side? How can movement be enhanced (with rocket boots or magic capabilities). How can we make this game model more flexible?</p>
<p>My next post follows the Freemans’ lead and provides an alternative way to develop the game by introducing a design pattern. In the meantime, I’d like to hear readers’ ideas and comments on how to improve the development process. What would be a better way to start off creating a project like this? Are design patterns available to make this more sustainable? Expandable? Let’s hear what you think.</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%2Fthe-wrong-way-warrior-where-oop-alone-is-not-enough-part-i%2F&amp;title=The%20Wrong%20Way%20Warrior%3A%20Where%20OOP%20Alone%20is%20Not%20Enough%E2%80%94Part%20I" 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/08/the-wrong-way-warrior-where-oop-alone-is-not-enough-part-i/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Friends with Benefits: Refactoring with Multiple Design Patterns—Part I</title>
		<link>http://www.as3dp.com/2009/06/friends-with-benefits-refactoring-with-multiple-design-patterns-part-i/</link>
		<comments>http://www.as3dp.com/2009/06/friends-with-benefits-refactoring-with-multiple-design-patterns-part-i/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 21:43:03 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[Factory Method]]></category>
		<category><![CDATA[State]]></category>

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

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

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

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

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

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


<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1077code33'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p107733"><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="p1077code33"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Concrete Creator (Factory)</span>
	<span style="color: #0066CC;">import</span> fl.<span style="color: #0066CC;">data</span>.<span style="color: #006600;">DataProvider</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteCreatorPrague <span style="color: #0066CC;">extends</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> ds:DataSupply;
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> createData<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:DataProvider
		<span style="color: #66cc66;">&#123;</span>
			ds=<span style="color: #000000; font-weight: bold;">new</span> Prague<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span>ds.<span style="color: #006600;">deliverData</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>


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


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

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

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

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

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

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

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

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

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

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

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

<p>As you can see, each button calls a function to load the List instance with a different set of data loaded into the DataProvider object. It’s just like the first version of the pattern except that a parameter has been added to select from among three data sets. Figure 4 shows the different outcomes in the List component:<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/06/p1.png" alt="p1" title="p1" width="512" height="349" class="alignnone size-full wp-image-1082" /><br />
<em><strong>Figure 4:</strong> Selected List object data</em></p>
<p>Both in our book and on this blog, you&#8217;ve seen other examples of the Factory Method design pattern; so this may not be new as far as a design pattern is concerned. However, it does show a different use of the pattern and how flexible it is. In the next installment, we&#8217;ll see how this pattern that provides UI data can be integrated with a State design to develop a video player that is set up so that adding more controls is quite simple.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2009%2F06%2Ffriends-with-benefits-refactoring-with-multiple-design-patterns-part-i%2F&amp;title=Friends%20with%20Benefits%3A%20Refactoring%20with%20Multiple%20Design%20Patterns%E2%80%94Part%20I" id="wpa2a_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/2009/06/friends-with-benefits-refactoring-with-multiple-design-patterns-part-i/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Design Pattern Principles for ActionScript 3.0: The Open/Closed Principle</title>
		<link>http://www.as3dp.com/2009/03/design-pattern-principles-for-actionscript-30-the-openclosed-principle/</link>
		<comments>http://www.as3dp.com/2009/03/design-pattern-principles-for-actionscript-30-the-openclosed-principle/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 15:37:13 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Principles]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=836</guid>
		<description><![CDATA[In the little AIR menu with the 10 principles one of the clearest is the Open/Closed Principle. At one time this principle suggested that all updates be created using an implementation or extension of virtually any class. That could get tricky, especially if someone understood that to mean implementing an update or extending a subclass. [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-760" title="bucketrule" src="http://www.as3dp.com/wp-content/uploads/2009/02/bucketrule.png" alt="bucketrule" width="150" height="133" /><br />
In the little <a href="http://www.as3dp.com/2009/02/26/oop-designs-pattern-principles-ready-for-work"> AIR menu with the 10 principles</a> one of the clearest is the <strong>Open/Closed Principle</strong>. At one time this principle suggested that all updates be created using an implementation or extension of virtually any class. That could get tricky, especially if someone understood that to mean implementing an update or extending a subclass. However, later, the extension came to mean the extension of an <em>abstract base class</em>. In other words the interface is extended but never modified. Given these caveats, we can understand the basic principle as it is now understood:</p>
<blockquote><p>Classes should be open for extension but closed for modification.</p></blockquote>
<p><strong>Easy to Take to Work</strong></p>
<p>(<strong>Note:</strong> In talking about a program and changes, we’re not including the Client class. It just makes requests, and you can add requests and change them all you want in the Client.)</p>
<p>The idea that when you want to change a program, the only way you are able to make changes is by extension may seem a little restrictive. However, what the principle is really doing is providing a way to make changes without having to rewrite the whole program. The dictum, <em>New behaviors are only available through extension</em> <strong>should not</strong> be phrased in a way to make it sound like it&#8217;s tying your hands. Rather, it should say something like,</p>
<blockquote><p>Hey! The Open/Closed principle makes it easy to add new behaviors without having to mess up your whole program.</p></blockquote>
<p><span id="more-836"></span><br />
Looking at the principle in this new light, we can see how it works with a simple example. Suppose you’re doing a lot of work with Shape class objects. Right away you need circles and rectangles, but you might need other shapes later. So to get started, you set up your classes that you know you’ll need, but you want to leave the door open for easy extension. To get started you create an abstract class that leaves an abstract method to add different shape types. The following script is your initial starting point:</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('p836code47'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p83647"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code" id="p836code47"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">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: #808080; font-style: italic;">//Asbstract class do not instantiate</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> IOpenClosed <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> shape:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> makeShape<span style="color: #66cc66;">&#40;</span>rw:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span>,c:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setShape<span style="color: #66cc66;">&#40;</span>fillCol:uint,borderCol:uint,lw:<span style="color: #0066CC;">Number</span>,rw:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span>,c:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			shape.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>fillCol<span style="color: #66cc66;">&#41;</span>;
			shape.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span>lw,borderCol<span style="color: #66cc66;">&#41;</span>;
			makeShape<span style="color: #66cc66;">&#40;</span>rw,h,c<span style="color: #66cc66;">&#41;</span>;
			shape.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>shape<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>You will notice that the makeShape() function in the setShape() method only has three parameters. That’s because the first two parameters are the x and y positions. These will be set to 0 using a literal. The stage position is placed using an instance of the shape.</p>
<p>Next, two little classes extend the abstract class to make the circle and rectangle.  First the circle-making class extends the abstract class and then overrides the makeShape() function to add the graphics method for creating a circle .</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('p836code48'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p83648"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p836code48"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> OCround <span style="color: #0066CC;">extends</span> IOpenClosed
	<span style="color: #66cc66;">&#123;</span>
		override protected <span style="color: #000000; font-weight: bold;">function</span> makeShape<span style="color: #66cc66;">&#40;</span>rw:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span>,c:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			shape.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawCircle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, rw<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>Next, we do the same thing for a rectangle.</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('p836code49'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p83649"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p836code49"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> OCrectangle <span style="color: #0066CC;">extends</span> IOpenClosed
	<span style="color: #66cc66;">&#123;</span>
		override protected <span style="color: #000000; font-weight: bold;">function</span> makeShape<span style="color: #66cc66;">&#40;</span>rw:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span>,c:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			shape.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, rw, h<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Okay, we’re all done. We’ll create a Client class to test it:</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('p836code50'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p83650"><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
</pre></td><td class="code" id="p836code50"><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: #808080; font-style: italic;">//Parameters for setShape are:</span>
	<span style="color: #808080; font-style: italic;">//setShape(fillCol:uint,borderCol:uint,lw:Number,rw:Number,h:Number,c:Number):void</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> rec:IOpenClosed=<span style="color: #000000; font-weight: bold;">new</span> OCrectangle<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> cir:IOpenClosed=<span style="color: #000000; font-weight: bold;">new</span> OCround<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> rndrec:IOpenClosed=<span style="color: #000000; font-weight: bold;">new</span> OCroundrec<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>
			rec.<span style="color: #006600;">setShape</span><span style="color: #66cc66;">&#40;</span>0xC03000,0x32331D,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">60</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
			rec.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">50</span>, rec.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">50</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rec<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			cir.<span style="color: #006600;">setShape</span><span style="color: #66cc66;">&#40;</span>0x787746,0x40411E,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">50</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
			cir.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">100</span>, cir.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">200</span>;
			addChild<span style="color: #66cc66;">&#40;</span>cir<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>When tested, we can see that we get our shapes as shown in Figure 1.<br />
<img class="alignnone size-full wp-image-837" title="oc1" src="http://www.as3dp.com/wp-content/uploads/2009/03/oc1.png" alt="oc1" width="183" height="253" /></p>
<p><em><strong>Figure 1:</strong> Two shapes created with common interface</em></p>
<p>Obviously not rocket science, but it’s a handy tool for setting up different kinds of shapes.</p>
<p><strong>What about Rounded Rectangles? </strong></p>
<p>This part is the important part as far as both the Open/Closed principle and design patterns are concerned. Suppose you decide you need a rounded rectangle. With the Open/Closed principle in mind, you <em>cannot modify the code</em> but you can add to it through extension. So, let’s add a rounded rectangle:</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('p836code51'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p83651"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p836code51"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> OCroundrec <span style="color: #0066CC;">extends</span> IOpenClosed
	<span style="color: #66cc66;">&#123;</span>
		override protected <span style="color: #000000; font-weight: bold;">function</span> makeShape<span style="color: #66cc66;">&#40;</span>rw:<span style="color: #0066CC;">Number</span>,h:<span style="color: #0066CC;">Number</span>,c:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			shape.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRoundRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, rw,h,c<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>That wasn’t hard. Add the following lines to the Client, and let’s test it:</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('p836code52'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p83652"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p836code52"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> rndrec:IOpenClosed=<span style="color: #000000; font-weight: bold;">new</span> OCroundrec<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
….
<span style="color: #006600;">rndrec</span>.<span style="color: #006600;">setShape</span><span style="color: #66cc66;">&#40;</span>0x32331D,0xC03000,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">60</span>,<span style="color: #cc66cc;">24</span><span style="color: #66cc66;">&#41;</span>
rndrec.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">200</span>, rndrec.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">50</span>;
addChild<span style="color: #66cc66;">&#40;</span>rndrec<span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>Figure 2 shows that a rounded rectangle has joined the circle and the rectangle on the stage:<br />
<img class="alignnone size-full wp-image-838" title="oc2" src="http://www.as3dp.com/wp-content/uploads/2009/03/oc2.png" alt="oc2" width="304" height="241" /><br />
<em><strong>Figure 2: </strong> A third shape is created through extension.</em></p>
<p>As you can see, while it was more work to set it up initially, it’s a lot less work to create new shapes. Also, requesting the shapes in the clients is very simple as well.</p>
<p><strong>Into the Lunch Bucket</strong></p>
<p>Here’s a handy and simple principle that you can see reflected in many different design patterns. For example, the Decorator, Composite, and Builder offer change through extension. So, to add this principle to your lunch bucket, add a class that creates ellipses. If you can do that, you can both understand the principle and apply it. That means it&#8217;s ready for work. Figure 3 shows the missing ellipse:</p>
<p><img class="alignnone size-full wp-image-848" title="oc3" src="http://www.as3dp.com/wp-content/uploads/2009/03/oc3.png" alt="oc3" width="311" height="342" /><br />
<em><strong>Figure 3: </strong>Test your understanding by adding an ellipse to the mix.</em></p>
<p>One caveat to using this principle is one of the first design pattern principles: <em>favor composition over inheritance</em>. That doesn’t mean you cannot create new elements for your program using extension—clearly some design patterns do so. Rather, just keep in mind that the principles in both OOP and design patterns work in conjunction with one another and you need to realize that not all OOP solutions are grounded in a single principle.</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%2Fdesign-pattern-principles-for-actionscript-30-the-openclosed-principle%2F&amp;title=Design%20Pattern%20Principles%20for%20ActionScript%203.0%3A%20The%20Open%2FClosed%20Principle" id="wpa2a_12"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/03/design-pattern-principles-for-actionscript-30-the-openclosed-principle/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Where&#039;s the Real World?: Design Pattern Examples in ActionScript 3.0</title>
		<link>http://www.as3dp.com/2009/01/wheres-the-real-world-design-pattern-examples-in-actionscript-30/</link>
		<comments>http://www.as3dp.com/2009/01/wheres-the-real-world-design-pattern-examples-in-actionscript-30/#comments</comments>
		<pubDate>Mon, 26 Jan 2009 09:47:05 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=590</guid>
		<description><![CDATA[Gentle Readers: This short post is a request for feedback. The whole issue of appropriate level examples both in our books and this blog is an important one because it speaks to the utility of the writings and posts. So, your thoughts are not only welcomed; they&#8217;re essential. I had a meeting with a computer [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Gentle Readers</strong>: This short post is a request for feedback. The whole issue of appropriate level examples both in our books and this blog is an important one because it speaks to the utility of the writings and posts. So, your thoughts are not only welcomed; they&#8217;re essential.</em></p>
<p>I had a meeting with a computer scientist who was teaching a game class. In our short chat, he must have used the term <strong>real world</strong> a dozen times. Well, I&#8217;m all for the real world (in contrast to the unreal world of unicorns, fairy dust, and honest politicians). However, the real world for one is not the same as it is for another. Recently, I got a comment about a blog entry thanking us for a solution to a practical problem that one of our readers encountered in programming. The same post was criticized by another reader as not being <em>real world</em>. Therein lies the dilemma.</p>
<p><strong>Abstract vs. Concrete</strong></p>
<p>Chandima and I use a range of examples in our book. We start with an abstract minimalist example so that the reader can see the participants in a design pattern and then move on to something more concrete to illustrate a practical application. On this blog, most of the examples start with the more abstract elements and move into a fairly general (somewhat abstract) example that is more practical. The more abstract an example, the more general its applicability—not unlike an abstract class. However, the more concrete an example, the better the reader can use it to model a like problem in an eminently practical way. Each has its benefits. The abstract examples have generalizability and the concrete examples have needed detail.</p>
<p>Were I to do all of my examples using <strong>real world</strong> examples that I deal with, most would involve streaming video and Flash Media Server. My customers usually approach me for just that kind of problem. Obviously,using streaming video and FMS is real world, but its not very generalizable. Likewise, some readers complain that the abstract examples don&#8217;t help because they&#8217;re not practical.</p>
<p>We&#8217;d like your thoughts on this issue. Obviously, the most useful examples would be those that you deal with directly in your work, but like my practical work, it&#8217;s pretty narrow. Keeping these concerns in mind, tell us what&#8217;s most helpful to you.</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%2F01%2Fwheres-the-real-world-design-pattern-examples-in-actionscript-30%2F&amp;title=Where%26%23039%3Bs%20the%20Real%20World%3F%3A%20Design%20Pattern%20Examples%20in%20ActionScript%203.0" id="wpa2a_14"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/01/wheres-the-real-world-design-pattern-examples-in-actionscript-30/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>

