<?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; Design Patterns at Work</title>
	<atom:link href="http://www.as3dp.com/category/design-patterns-at-work/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>What if ActionScript 3.0 had no Conditional Statements? : Part I</title>
		<link>http://www.as3dp.com/2011/06/what-if-actionscript-3-0-had-no-conditional-statements-part-i/</link>
		<comments>http://www.as3dp.com/2011/06/what-if-actionscript-3-0-had-no-conditional-statements-part-i/#comments</comments>
		<pubDate>Mon, 20 Jun 2011 01:44:40 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Loose Coupling]]></category>
		<category><![CDATA[State]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=5762</guid>
		<description><![CDATA[On a couple of occasions, we&#8217;ve discussed working without conditional statements. You may recall that both the Strategy and State design patterns have no conditional statements, and so the idea isn&#8217;t particularly new. The issue came to mind when I was unraveling a PHP program that had nested conditional statements used in pulling out table [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_5772" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/06/noCon.png" alt="Think in Non-Linear Concepts" title="noCon" width="250" height="193" class="size-full wp-image-5772" /><p class="wp-caption-text">Think in Non-Linear Concepts</p></div>On a couple of occasions, we&#8217;ve discussed working without conditional statements. You may recall that both the Strategy and State design patterns have no conditional statements, and so the idea isn&#8217;t particularly new. The issue came to mind when I was unraveling a PHP program that had nested conditional statements used in pulling out table data. Even though they seem to crop up like weeds everywhere, nested conditionals and loops represent poor practices . (Dave Shuck&#8217;s solution for <a href="http://daveshuck.com/2011/03/08/refactoring-avoiding-nested-conditional-statements/">refactoring nested conditionals</a> is worth a look.)</p>
<p>Other than the fact that nested conditionals can create the worst kind of object binding as each condition sends the program further down the binding rabbit hole, we seem to really need them for working out certain kinds of problems. For example, in a visual  game scenario, the user may be faced with choices. Suppose the game state is a crossroad where the player must choose to go North, East, West or South (NEWS). Alternatively, the choice may be video perspectives of different directions. Any one of the choices carries with it a whole set of other states, conditions, capabilities and properties.</p>
<p>One way to handle this kind of choice is with a conditional statement. The following shows two things. First, it shows that when a direction is selected, it typically has more than one thing being affected. (The more than one thing is furnished by &#8220;Everything else.&#8221;) Second, the binding for each is represented by the conditions within each conditional. (You could do the same thing with a Switch statement, but the binding wouldn&#8217;t change.)</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('p5762code4'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p57624"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code" id="p5762code4"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> NEWS <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> newsArray:<span style="color: #0066CC;">Array</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;north&quot;</span>,<span style="color: #ff0000;">&quot;east&quot;</span>,<span style="color: #ff0000;">&quot;west&quot;</span>,<span style="color: #ff0000;">&quot;south&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dir:<span style="color: #0066CC;">Object</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> NEWS<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span>dir <span style="color: #b1b100;">in</span> newsArray<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>newsArray<span style="color: #66cc66;">&#91;</span>dir<span style="color: #66cc66;">&#93;</span>==<span style="color: #ff0000;">&quot;north&quot;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Hellloo Siberia&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Everything else....&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>newsArray<span style="color: #66cc66;">&#91;</span>dir<span style="color: #66cc66;">&#93;</span>==<span style="color: #ff0000;">&quot;east&quot;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Visiting Bejing&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Everything else....&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>newsArray<span style="color: #66cc66;">&#91;</span>dir<span style="color: #66cc66;">&#93;</span>==<span style="color: #ff0000;">&quot;west&quot;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Dude! You made it!&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Everything else....&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>newsArray<span style="color: #66cc66;">&#91;</span>dir<span style="color: #66cc66;">&#93;</span>==<span style="color: #ff0000;">&quot;south&quot;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;¡Recepción a la Argentina!&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Everything else....&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&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>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>That certainly can solve the issue of generating different responses from different object properties. It&#8217;s compact and saves ground-fills of wasted bits. It  illustrates a common method of dealing with multiple options. The loop iterates through the object, and each iteration is examined by four conditional statements.</p>
<p><strong>Unconditional Programming</strong></p>
<p>Let&#8217;s go back to the NEWS example, and instead of having trace statements representing different directions, this next example uses short videos pointing in different directions. (I live in the woods so the videos are fairly woodsy—but they show the four points of the compass—honest.) I want to make an app with four buttons that plays a video pointing in the selected direction. Plus, I don&#8217;t want to use any conditional statements. (Easier than you think.) Click the Play and Download buttons to see a working example and get the code and videos.<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/blog/nocon/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignleft size-full wp-image-1257" /></a><a href="http://nemo.mwd.hartford.edu/~wsanders/blog/nocon/nocon.zip" arget="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a></p>
<p>In the next section where we examine the code you will see two things: No conditional statements and a non-linear solution. The user has several choices, and simply by making a choice she/he goes directly to the object desired—a directional video.<br />
<span id="more-5762"></span></p>
<p><strong>The Client, the UI and Requests</strong></p>
<p>Like any good Client class, this client takes care of the UI and makes requests from other classes. Most of it is just providing a non-conditonal and non-linear solution.</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('p5762code5'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p57625"><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
</pre></td><td class="code" id="p5762code5"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">Button</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> 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> dummy: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> nBtn:<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> eBtn:<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> wBtn:<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> sBtn:<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> north:North;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> east:East;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> west:West;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> south:South;
&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>
			nBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,goNorth<span style="color: #66cc66;">&#41;</span>;
			nBtn.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">70</span>;
			nBtn.<span style="color: #006600;">x</span>=<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">550</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>-<span style="color: #66cc66;">&#40;</span>nBtn.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>,nBtn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">10</span>;
			nBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;North&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>nBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			eBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,goEast<span style="color: #66cc66;">&#41;</span>;
			eBtn.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">70</span>;
			eBtn.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">550</span>-<span style="color: #66cc66;">&#40;</span>eBtn.<span style="color: #0066CC;">width</span>+<span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span>,eBtn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">210</span>;
			eBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;East&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>eBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			wBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,goWest<span style="color: #66cc66;">&#41;</span>;
			wBtn.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">70</span>;
			wBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">5</span>,wBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">210</span>;
			wBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;West&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>wBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			sBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,goSouth<span style="color: #66cc66;">&#41;</span>;
			sBtn.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">70</span>;
			sBtn.<span style="color: #006600;">x</span>=<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">550</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>-<span style="color: #66cc66;">&#40;</span>sBtn.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>,sBtn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">370</span>;
			sBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;South&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>sBtn<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> goNorth<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>
			north=<span style="color: #000000; font-weight: bold;">new</span> North<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>north<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> goEast<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>
			east=<span style="color: #000000; font-weight: bold;">new</span> East<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>east<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> goWest<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>
			west=<span style="color: #000000; font-weight: bold;">new</span> West<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>west<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> goSouth<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>
			south=<span style="color: #000000; font-weight: bold;">new</span> South<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>south<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 from the Client and testing the app, the Client just fires a function that calls one of four classes that run a video. The user makes requests through the buttons that fire methods that call classes. These classes contain what&#8217;s required for each of the four states. We&#8217;ll use North as a generic example. (The other three classes are identical except for the name of the video they call.)</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('p5762code6'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p57626"><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
</pre></td><td class="code" id="p5762code6"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetConnection</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">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;">Video</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">NetStatusEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> North <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> nc:<span style="color: #0066CC;">NetConnection</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">NetConnection</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> ns:<span style="color: #0066CC;">NetStream</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> vid:<span style="color: #0066CC;">Video</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Video</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> hookup:<span style="color: #0066CC;">Boolean</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dir:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;north.f4v&quot;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sniffer:<span style="color: #0066CC;">Object</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Object</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> North<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			nc.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>NetStatusEvent.<span style="color: #006600;">NET_STATUS</span>,doConnect<span style="color: #66cc66;">&#41;</span>;
			nc.<span style="color: #0066CC;">connect</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
			vid.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">137.5</span>, vid.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">80</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> doConnect<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:NetStatusEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			hookup=<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #006600;">info</span>.<span style="color: #006600;">code</span>==<span style="color: #ff0000;">&quot;NetConnection.Connect.Success&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>hookup<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				ns=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#40;</span>nc<span style="color: #66cc66;">&#41;</span>;
				ns.<span style="color: #006600;">client</span>=sniffer;
				sniffer.<span style="color: #006600;">onMetaData</span>=getMeta;
				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>dir<span style="color: #66cc66;">&#41;</span>;
				addChild<span style="color: #66cc66;">&#40;</span>vid<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getMeta <span style="color: #66cc66;">&#40;</span>mdata:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//Use if metadata required</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 sharp-eyed among you will have spotted the conditional statement. It is fired by the <strong>NetConnection.Connect </strong>method and is more of a good practice than anything else. I could probably get away without it given that the videos are so small and load quickly; however, it is a good practice to check to make sure that a video is connected. So rather than trying to prove a point at the expense of a good practice, I&#8217;ll allow this one conditional to check connections and preserve a good practice. (I&#8217;ll also be tracking down another way to do the same thing without a conditional.)</p>
<p>You may be thinking that you could do the whole thing in the Client class by stuffing all the required methods and properties into the Client. You&#8217;d be right. In fact, while you&#8217;re at it you could stuff improvements in error checking and remove each video as a new one was played. However, as you put more and more into a class, you cease to <strong>delegate</strong> and continue to bind. Re-use and change becomes difficult because the less delegation you use, the more dependencies you stack up.</p>
<p>Next week, we&#8217;ll see how to refactor this set of classes into a state machine and a State design pattern. The State design pattern has a magic Context participant and other features that you&#8217;ll enjoy when you begin to abandon linear thinking and give OOP thinking a go.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2011%2F06%2Fwhat-if-actionscript-3-0-had-no-conditional-statements-part-i%2F&amp;title=What%20if%20ActionScript%203.0%20had%20no%20Conditional%20Statements%3F%20%3A%20Part%20I" id="wpa2a_2"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/06/what-if-actionscript-3-0-had-no-conditional-statements-part-i/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Reuse Where Objects Change: Factory Method at Work with Lazy Instantiation</title>
		<link>http://www.as3dp.com/2011/02/reuse-where-objects-change-factory-method-at-work-with-lazy-instantiation/</link>
		<comments>http://www.as3dp.com/2011/02/reuse-where-objects-change-factory-method-at-work-with-lazy-instantiation/#comments</comments>
		<pubDate>Mon, 21 Feb 2011 01:42:42 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Abstract Classes]]></category>
		<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Factory Method]]></category>
		<category><![CDATA[Lazy instantiation]]></category>
		<category><![CDATA[Loose Coupling]]></category>
		<category><![CDATA[re-use program elements]]></category>

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

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p4723code8'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p47238"><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="p4723code8"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Connecticut Factory</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MakeConn <span style="color: #0066CC;">extends</span> Factory
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span> product<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				product=<span style="color: #000000; font-weight: bold;">new</span> ConnProd<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">return</span> product.<span style="color: #006600;">getObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Connecticut Product</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConnProd <span style="color: #0066CC;">extends</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> getObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			objNow=<span style="color: #000000; font-weight: bold;">new</span> Conn<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			objNow.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">188.45</span>, objNow.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">321.75</span>;
			<span style="color: #b1b100;">return</span> objNow;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

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

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

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2280code14'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p228014"><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="p2280code14"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Mr Face App</span>
	<span style="color: #808080; font-style: italic;">//Asbstract class--serves as interface</span>
	<span style="color: #808080; font-style: italic;">//Creator</span>
&nbsp;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dragNow:<span style="color: #0066CC;">Array</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createDragable<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span>
		<span style="color: #66cc66;">&#123;</span>
			dragNow=getDragon<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> dragNow;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// ABSTRACT Method (must be overridden in a subclass)</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> getDragon<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

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

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

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

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2280code16'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p228016"><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="p2280code16"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Abstract class--serves as interface</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Product <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> base:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// ABSTRACT Method</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> placeParts<span style="color: #66cc66;">&#40;</span>lr:uint,ud:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> setPart<span style="color: #66cc66;">&#40;</span>part:Sprite<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			addChild<span style="color: #66cc66;">&#40;</span>base<span style="color: #66cc66;">&#41;</span>;
			base.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>part<span style="color: #66cc66;">&#41;</span>;
			base.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_DOWN</span>,dragPart<span style="color: #66cc66;">&#41;</span>;
			base.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_UP</span>,dropPart<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> dragPart<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">startDrag</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> dropPart<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">stopDrag</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

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

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

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

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

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

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

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


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


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

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

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1518code29'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p151829"><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="p1518code29"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Asbstract class--serves as interface</span>
	<span style="color: #808080; font-style: italic;">//Creator</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dragNow:Sprite;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createDragable<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			dragNow = getDragon<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> dragNow;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// ABSTRACT Method (must be overridden in a subclass)</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> getDragon<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>


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


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

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

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

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

		<guid isPermaLink="false">http://www.as3dp.com/?p=1092</guid>
		<description><![CDATA[It’s about time for another Golden Lunch Bucket Contest, and once again you can reap fame and fortune! (And again, we’ll provide the former if you provide the latter. This time, though, we must insist that you provide better prizes for yourself if you’re one of the winners!) If you’re feeling lucky, then enter the [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.as3dp.com/wp-content/uploads/2009/04/goldenbucket.png" alt="goldenbucket" title="goldenbucket" width="150" height="150" class="alignleft size-full wp-image-879" />It’s about time for another Golden Lunch Bucket Contest, and once again you can reap fame and fortune! (And again, we’ll provide the former if you provide the latter. This time, though, we must insist that you provide better prizes for yourself if you’re one of the winners!)</p>
<p>If you’re feeling lucky, then enter the contest. Who knows? You might just win, and then you can go nuts! We know, you’ll have to put up with the paparazzi and the inevitable groupies, but that’s just the cost of fame. (Our former winners will give you advice on how to handle it all.) Just remember Dirty Harry&#8217;s advice:</p>
<blockquote><p>You gotta ask yourself one question: Do I feel lucky? Well, do ya, punk?</p></blockquote>
<p><span id="more-1092"></span><br />
<strong>What is it?</strong></p>
<p>In more than one post by design pattern enthusiasts, I’ve seen warnings against <strong>Design Pattern Fever</strong> with dire warnings about using design patterns for petty projects. Well, I don’t agree! From our survey, we found that while most would like to include good OOP and design patterns in their projects at work, <em>most good programmers are under so much pressure to get the job done that they don’t have time for design patterns.</em> So instead of kick-starting your design pattern work with a big project, start small with a <strong>Hello World!</strong> project.</p>
<p>The goal is to use a design pattern (any one you want <em>except the Singleton</em>) to output “Hello world!” in eight (8) different languages. In our last contest, we had winners from all over the world, and so having more than one language seems appropriate. (You get bonus points if one of the languages is Icelandic, Yoruba or Navaho.) The contest is from when you read this post until <strong>July 15, 2009</strong>. So get cracking and get lucky!</p>
<p><strong>The Rules</strong></p>
<ol>
<li>You must use a Design Pattern from the original Gang of Four’s book</li>
<li> All entries must use ActionScript 3.0</li>
<li>Either Flash or Flex formats are acceptable </li>
<li>You may not use a Singleton design pattern (They’re pathological liars—that’s why.) </li>
<li>The screen output must show “Hello World” in eight (8) languages—either separately or all together. You cannot use <strong>trace()</strong> statements for the output, but all output must be visible on the screen. </li>
<li> You must specify which design pattern(s) you are using.</li>
<li> Extra points are given for Icelandic, Yoruba and Navaho</li>
<li>All entries must be in by July 15, 2009</li>
<li>All entries must be placed in a ZIP file. (Nothing fancy for my Mac)</li>
<li>Send entries to wdsanders@comcast.net with the subject line <strong>Golden Lunch Bucket Contest #2</strong></li>
<li>Include your name, age, where you’re from, and a 100 x 100 jpeg/png photo</li>
</ol>
<p>Remember, you can use any design pattern you want with the exception of the Singleton (shudder). This is a simple contest, and there’s no pressure—you’ve got until July 15! So grab that can of Red Bull and hop to it!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2009%2F06%2Fhello-world-the-second-golden-lunch-bucket-contest%2F&amp;title=Hello%20World%21%20%3A%20The%20Second%20Golden%20Lunch%20Bucket%20Contest%21" 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/hello-world-the-second-golden-lunch-bucket-contest/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>The Developing an AIR ActionScript 3.0 Design Pattern Catalog and the AIR Magic Table</title>
		<link>http://www.as3dp.com/2009/06/the-developing-an-air-actionscript-30-design-pattern-catalog-and-the-air-magic-table/</link>
		<comments>http://www.as3dp.com/2009/06/the-developing-an-air-actionscript-30-design-pattern-catalog-and-the-air-magic-table/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 13:49:05 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Catalog]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1070</guid>
		<description><![CDATA[Gentle Reader, this post includes one completed application and one in the works. The AIR ActionScript 3.0 Design Pattern Catalog is meant to be a developer’s aid, and so your ideas of what to include in the catalog is important. We would really appreciate your comments on what you think would be useful in developing [...]]]></description>
			<content:encoded><![CDATA[<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" /><br />
<em><strong>Gentle Reader,</strong> this post includes one completed application and one in the works. The AIR ActionScript 3.0 Design Pattern Catalog is meant to be a developer’s aid, and so your ideas of what to include in the catalog is important. We would really appreciate your comments on what you think would be useful in developing an information template to be used for all of the design patterns.</em></p>
<p>I recently returned from a trip to Prague in the Czech Republic, and I went to work creating a video player for my HD videos I made using a Flip Mino HD camcorder (http://www.sandlight.net/prague). Rather than pulling out and reusing my trusty State Design Pattern player (see Chapter 10), I decided to start from scratch and create a player not using a design pattern. Then I would take the completed video player and use it as an example to show how to refactor a program to a design pattern. (<em>This will be in an upcoming post.</em>) Since I’ve never had to refactor the original program (tweak, maybe but not refactor), and I hadn’t worked with a State pattern lately, I found myself digging up all of the materials on the State Design Pattern. What I wished I had was a handy desktop app that I could click that would show me the essentials of the different design patterns. It would be an <strong>abbreviated design pattern catalog</strong> that could be used like the little <a href="http://www.as3dp.com/2009/02/26/oop-designs-pattern-principles-ready-for-work/">Design Pattern Principles and Lunch Bucket Rules</a> AIR app. Then when I need a quick reference I could use the desktop app to look up the essentials of the pattern.<br />
<span id="more-1070"></span><br />
<strong>What Does a Design Pattern Catalog Need?</strong></p>
<p>Right off the top of my head I came up with the following elements for a quick look-up:</p>
<ul>
<li>Class diagram</li>
<li>Description of what elements vary</li>
<li>Component roles</li>
<li>Common usage </li>
<li>Chapter/Blog reference</li>
<li>Key Code examples</li>
</ul>
<p>The catalog is meant to be a quick reference for someone who has some idea design patterns and wants to check out some elements of different patterns to see if it&#8217;s appropriate for a project. It is not meant to be a complete reference and certainly not a tutorial. Like the little AIR application that brings up the principles in abbreviated format, this application is meant to be a simple guide that will save time over pouring through Web searches or books and articles for basic design pattern information. Figure 1 shows an idea of what each page of the application would look like (given the above criteria). We’d like your comments on further ideas both in content and design. <em>I’m not a designer</em>.<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/06/catalog.png" alt="catalog" title="catalog" width="500" height="500" class="alignnone size-full wp-image-1072" /></p>
<p><em><strong>Figure 1:</strong> Proposed Template for ActionScript 3.0 Design Pattern Catalog</em></p>
<p>Take a look at Figure 1 and see if you have any suggestions as to how we could make it more useful without going into a detailed explanation of the pattern. Would it be handy? Should anything be added or deleted? Is there a better design? Send in your comments.</p>
<p><strong>The Magic Table in AIR</strong></p>
<p>A while back we had a post on this blog that included what I called <a href="http://www.as3dp.com/2009/03/10/variation-table/"> The Magic Table</a>.  It is a reference to the Gang of Four’s table where they list all of the design patterns and what can vary for the pattern. Anyway, I’ve always found that table useful, and so I created one using a simple DataGrid component. You can <a href="http://www.sandlight.com/magic/">download it here</a>. You can put the AIR app on your desktop and quickly find what can vary with each design pattern. Figure 2 shows what you’ll see.<br />
<!--more--><br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/06/vary.png" alt="vary" title="vary" width="630" height="522" class="alignnone size-full wp-image-1073" /></p>
<p><em><strong>Figure 2:</strong> Encapsulate what varies—Design Pattern Variation Table in AIR</em></p>
<p>If you can make something with better aesthesis, please do so, and we’ll be glad to make it available to our blog readers if it is sent in an AIR file.  In the meantime, we’d like to get your comments on the utility of such tools and ideas to improve them.</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%2Fthe-developing-an-air-actionscript-30-design-pattern-catalog-and-the-air-magic-table%2F&amp;title=The%20Developing%20an%20AIR%20ActionScript%203.0%20Design%20Pattern%20Catalog%20and%20the%20AIR%20Magic%20Table" 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/06/the-developing-an-air-actionscript-30-design-pattern-catalog-and-the-air-magic-table/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Design Patterns and OOP At Work: Where Are You?</title>
		<link>http://www.as3dp.com/2009/06/actionscript-30-design-patterns-and-oop-at-work-where-are-you/</link>
		<comments>http://www.as3dp.com/2009/06/actionscript-30-design-patterns-and-oop-at-work-where-are-you/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 21:36:27 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns at Work]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1030</guid>
		<description><![CDATA[A few weeks ago we had a survey to see the practices used by professional ActionScript 3.0 developers, and the results are quite interesting. Roughly, 31% of the respondents indicated that they used design patterns and OOP either as a strict policy in developing their products or from within a template. Another 51% indicated that [...]]]></description>
			<content:encoded><![CDATA[<p>A few weeks ago we had a survey to see the practices used by professional ActionScript 3.0 developers, and the results are quite interesting. Roughly, 31% of the respondents indicated that they used design patterns and OOP either as a strict policy in developing their products or from within a template. Another 51% indicated that given the time, they’d employ design patterns and all of the good practices, but because of time pressure, often they could not. The smallest category, representing 12% of the respondents, indicated that they didn’t pay attention either to design patterns or good OOP practices. In other words, 88% of the respondents indicated that they were aware of and attempted to use good development practices as a general policy except where it was impossible because of time and/or resource constraints.<br />
<span id="more-1030"></span><br />
<strong>General Results</strong></p>
<p>Table 1 shows the results of the survey’s 163 records. These results are based on the following questions:</p>
<ul>
<li>Which API do you usually use for development? <strong>API</strong></li>
<li>At work, which of the following best describes the programming practices used? <strong>Practices</strong></li>
<li>In hiring developers, which of the following do you actually ask applicants about? <strong>Applicants</strong></li>
</ul>
<p>The queries about API and Practices are mutually exclusive and so their rows add up to 100% (<em>mas o menos</em> depending on rounding errors), but the third question is not mutually exclusive and each cell could be up to 100%.<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/06/survey.jpg" alt="survey" title="survey" width="496" height="236" class="alignnone size-full wp-image-1031" /><br />
<em><strong>Table 1: </strong>General Survey Results</em><br />
<!--more--><br />
<strong>Who’s Your API?</strong></p>
<p>The API of choice is roughly equal between Flash and Flex with about 6% more using Flash than Flex. Another 21% indicate that they use both. We only had 2% whose shop used something other than Flash or Flex.</p>
<p><strong>What Are Applicants Asked?</strong></p>
<p>I found that the responses to the question about what applicants are asked when being considered for a job were quite revealing. The applicants were questioned about Flex and/or Flash roughly along the lines expected given the API choices. The great majority indicated that they questioned applicants about ActionScript 3.0 (85%), but I expected more like 100% would be. Likewise, most respondents said they asked applicants about OOP (77%). So whether you’re a Flash or Flex developer applying for a job, expect questions about OOP. Oddly, though, only 44% asked about the principles upon which OOP is based. (I’d be curious to find out exactly what kind of OOP questions were asked that did not include OOP principles.) Finally, 44% asked about design patterns, suggesting that a sizable proportion of software shops are interested in a programmer’s background in terms of some of the more advanced elements of ActionScript 3.0 programming.</p>
<p><strong>Who’s Got the Practices?</strong></p>
<p>From the raw data, I could not tell which group—Flex, Flash or Both—were the most likely to employ the best practices. In order to find out, I created a cross tabulation treating  the API as the independent variable. I have to admit that the cross-tabulation I created using PHP did not adhere to the best practices—I’m just not that good of a PHP programmer to follow the practices that I do in ActionScript 3.0. (We’re all entitled to a hack every now and again.) Table 2 shows the results. (I dropped the cases that were neither Flash nor Flex.)<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/06/surveymatrix.jpg" alt="surveymatrix" title="surveymatrix" width="320" height="207" class="alignnone size-full wp-image-1032" /></p>
<p><em><strong>Table 2: </strong>General Survey Results</em><br />
Examining the results of Table 2, about the only significant difference is that developers who reported using both Flash and Flex in their shops were far less likely to wholly disregard good OOP practices and design patterns and end up in the “Hack” category. Those who identified themselves as Flex or Flash developers, were about as likely to be in the Strict or Hack categories—15/15 percent for Flex and 13/15 percent for Flash.</p>
<p>About the only thing that can be said is that those developers who report using both Flex and Flash seem to use slightly better practices than those who only use one or the other. However, even when we consider the highest percent of those who are essentially using non-OOP/DP practices (Hack category), it’s very small—15% at most.</p>
<p><strong>Where to Go From Here?</strong></p>
<p>It is clear that the modal practices are ones where the developers recognize the importance of OOP and Design Patterns but have difficulty executing them. Overall, it seems that the ActionScript 3.0 developer community who completed the survey would like to employ good practices, which is a good thing.</p>
<p>We’d like to get your feedback on this topic and explore ways that good OOP practices and design pattern can be implemented quickly and easily implement at work.</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%2Factionscript-30-design-patterns-and-oop-at-work-where-are-you%2F&amp;title=ActionScript%203.0%20Design%20Patterns%20and%20OOP%20At%20Work%3A%20Where%20Are%20You%3F" 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/06/actionscript-30-design-patterns-and-oop-at-work-where-are-you/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Design Patterns at Work Survey</title>
		<link>http://www.as3dp.com/2009/05/actionscript-30-design-patterns-at-work-survey/</link>
		<comments>http://www.as3dp.com/2009/05/actionscript-30-design-patterns-at-work-survey/#comments</comments>
		<pubDate>Fri, 22 May 2009 20:25:47 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Principles]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=993</guid>
		<description><![CDATA[A Three-Question Survey I&#8217;m working on a new post about ActionScript 3.0 Design Patterns at work. To help get an accurate measure of what folks are doing in the ActionScript work world, I put together a simple survey. The survey is made with radio buttons and check boxes; so it only takes a minute to [...]]]></description>
			<content:encoded><![CDATA[<p><strong>A Three-Question Survey</strong></p>
<p>I&#8217;m working on a new post about ActionScript 3.0 Design Patterns at work. To help get an accurate measure of what folks are doing in the ActionScript work world, I put together a simple survey. The survey is made with radio buttons and check boxes; so it only takes a minute to fill out. Click below to take the World&#8217;s Easiest Survey:</p>
<p><a href="http://www.sandlight.com/survey/">ActionScript Design Patterns at Work Survey</a></p>
<p>We thank you in advance, and when we get the post up, you&#8217;ll see the results from the survey.</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%2F05%2Factionscript-30-design-patterns-at-work-survey%2F&amp;title=ActionScript%203.0%20Design%20Patterns%20at%20Work%20Survey" id="wpa2a_16"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/05/actionscript-30-design-patterns-at-work-survey/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Naked Flash Designer and the ActionScript 3.0 Algorithm Dope</title>
		<link>http://www.as3dp.com/2009/05/the-naked-flash-designer-and-the-actionscript-30-algorithm-dope/</link>
		<comments>http://www.as3dp.com/2009/05/the-naked-flash-designer-and-the-actionscript-30-algorithm-dope/#comments</comments>
		<pubDate>Sat, 16 May 2009 09:48:44 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Principles]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=978</guid>
		<description><![CDATA[The Naked Flash Designer As a general way of making sense out of projects, they are divided into design and development. The design component usually means graphic design, but it can also include the UI (HCI) and information design—the kinds of things Edward Tufte talks about. In other words, design refers to everything that the [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The Naked Flash Designer</strong></p>
<p>As a general way of making sense out of projects, they are divided into <strong>design</strong> and <strong>development</strong>. The design component usually means graphic design, but it can also include the UI (HCI) and information design—the kinds of things Edward Tufte talks about. In other words, design refers to everything that the user actually sees and interacts with. Those involved in design must <em>expose their work</em> to the user. It is in no way shrouded, and hence <em>naked</em>.</p>
<p>Developers’ work, on the other hand, has better cloaking than a Romulan space ship. If something goes wrong, and the program blows a gasket, the user can see a problem, but if things work, even in the most pedestrian program, the programming is invisible. The entire program can be one hack piled on top of another (as was the case with ActionScript prior to ActionScript 3.0), and no one would ever know. So, we programmers are safe to write any kind of code we want, and as long as it works, everyone is happy.</p>
<p>In many respects, most projects are design ones. The look, feel, navigation and interaction of a program makes or breaks it. A beautifully programmed project isn’t worth a bean if it looks poor, is difficult to navigate or interacts awkwardly with the user. Conversely, if a program looks and acts good, no one cares about what’s under the hood as long as it works.<br />
<span id="more-978"></span><br />
<strong>An Exoskeleton for Programs</strong></p>
<p>Suppose for a moment, that all programs had exoskeletons. The exoskeletons would be made up of the code used in the program. Everyone would be able to see your code and the kind of programmer you really are. Not only would they be able to see the kinds of work that designers do, they could simultaneously see under the hood. Ask yourself this: <em>Would you clean up your code if everyone could see it?</em> I know that I would and do. If you write <em>about</em> programming whether its in a book, an article, or a blog, you think about those things a lot, and your code certainly is visible. All of your examples are open to scrutiny.</p>
<p>Oddly, the <em>worse</em> reason that I can imagine to improve my own coding habits is to impress others. At some level, motivation matters. If I am not clear on why programming practices embedded in design patterns and OOP are important and matter for creating better programs, then I doubt I could sustain an interest in design patterns. For me, the practices are important for what they do—namely create reusable code subject to change and renewal.</p>
<p><strong>Confessions of an Algorithm Dope</strong></p>
<p>I doubt there’s anyone who reads this blog who doesn’t want to be a better programmer. (That includes Chandima and me—we’ve read all the comments in addition to the posts we’ve created looking for clues to better programming.) In working with design patterns we’re confronted with the issue of what it means to be a good programmer and write a good program. The other day in thinking about what good programming means I remembered when I was getting started writing code , all I wanted to do was to create better algorithms—<br />
<blockquote>I was an algorithm dope.</p></blockquote>
<p> At the time I was fascinated by sort algorithms and spent endless hours trying to understand all of the different sort algorithms. To me, a better algorithm meant better programming. I still believe that, but I’ve also come to realize that’s only part of what it means to be a better programmer. However, being an algorithm dope for many years, it blinded me to the larger picture in programs.</p>
<p><strong>A Good Pilot and a Good Programmer</strong></p>
<p>Some years ago when I lived in San Diego I owned an airplane. Looking for things to do with my airplane, I joined a couple of groups, <em>The Flying Samaritans</em> and <em>Liga: The Flying Doctors</em>. We flew medical supplies, doctors, nurses, interpreters, and other medical folks down to clinics in remote areas of Mexico. All of us had instrument ratings and many were commercial pilots. The talent pool of those groups was wide and deep. Navigation was often a challenge, and the landing strips were dirt or sandy soil. We had to check for people, cows and other surprises on these strips prior to touchdown and land a full planeload of people and supplies. For pilots, these flights were a challenge to skills honed by practice and study—really what flying was all about. That’s why pilots who flew commercial jetliners would often fly with us. We got to do the fun stuff.</p>
<p>At one point I was assigned the job of Safety Officer. I was responsible for making sure that the pilots were current and followed the flying rules we had established plus those of the FAA. Also, if any mishap occurred, my job included finding out what happened and integrate new guidelines to avoid a repeat occurrence. On one flight, one of our planes crashed with severe injuries but thankfully no deaths. (A television movie was actually made of the incident!)</p>
<p>When we had a hearing to determine the events that led up to the crash, we learned that the pilot was attempting to fly under a bridge. At the last moment, he changed his mind. When he pulled up to fly over the bridge, he ran into a metal cable that brought down the aircraft. For his defense, another pilot testified that he was a very good pilot and had landed safely at small remote strips without incident many times. Naturally, we asked what was he thinking by flying so low as to endanger himself and his passengers. He angrily demanded,<br />
<blockquote>Are you saying I’m not a good pilot!</p></blockquote>
<p>What he meant was, “Are you saying that I can’t fly an airplane?” He wanted to know if we were implying that he lacked the skills to make the airplane do what he wanted. We were saying something very different—<em>namely that he lacked good judgment</em>. It wasn’t that he couldn’t fly an airplane, but rather he put himself, his aircraft and his passengers into dangerous situations.</p>
<p>Recalling the landing of Flight 1549 on the Hudson River by Captain Sullenberger, most people saw the ability to land a powerless aircraft on water without serious injury as a marvel in flying ability. Successfully ditching an aircraft on water is no mean feat, but what was more important than the ability to safely make a water landing was <em>the decision to do so</em>. When the power was lost, “Sully” had several options. He was cleared to return to LaGuardia Airport or go to nearby Teterboro Airport, and initially he announced that he was returning to LaGuardia. Landing at an airport is a far more attractive option and in some situations, the safest. The pilot quickly realized, however, that an airport landing was not viable, and so <em>he decided that the best option was to bring the aircraft down on the Hudson River</em>. To me (and I imagine to most pilots), that kind of judgment was the most important feature that Sullenberger possessed as a good pilot.</p>
<p>On top of that, Sully and his co-pilot had a plan. The plan was in the form of a pre-prepared checklist for ditching that particular make of aircraft on water. One of the steps in the checklist was to press the “ditch button.” The button triggered events that closed the <strong>outflow valve</strong> along with several other open values. By closing the valves, the plane was able to stay afloat longer. Had they not had a checklist to guide them through the steps, they may well have overlooked pressing the ditch button, and the results may not have been as successful as they actually were.</p>
<p><strong>Good Programmers and Stupid Dog Tricks</strong></p>
<p>When I program, I actually believe that there’s nothing I can’t make ActionScript 3.0 do—within the limitations of the computer, Flex Builder or Flash. When I want to test a concept I’ll whip together something that I think of as a <em>stupid dog trick</em>. It gets the result I want. Sometimes I’ll have to work at it for hours or even days or weeks, but by hook or by crook I can get it done. Once I get the stupid dog trick code finished, I’ll start thinking about doing it right if it’s something beyond a test of concept. However, what a good programmer does is to <em>plan first</em> and then write the code. In David M. Bourg’s book, <em>Physics for Game Developers</em> (O’Reilly, 2002), he discusses making a flight simulator. However, prior to writing the code, he suggests beginning with a <em>flight model</em> that lays out what physics are involved in making an airplane fly. The most basic physics for an aircraft are lift, thrust, gravity and drag; so in the modeling process you want to plan for those forces. This comes way before you start thinking about how you’re going to simulate this in 3-D or how you’ll set up the user interface so that the gamer can make the aircraft perform a split-S. You have a basic outline of what you’re going to do before you start writing your code that makes the application.</p>
<p>With all serious projects in programming, I have forced myself to plan first and then start the coding.<br />
<blockquote>One thing that I discovered is that planning can be fun. </p></blockquote>
<p>When I used to fly, I’d spend hours planning a flight. I’d check the weather along the route, the navigation aids (e.g., VOR’s, radio stations) and make notes about the topography (e.g., mountains, rivers, visible landmarks). Now in flying, this can be a life or death decision. On one flight my wings iced up, and with no de-icer I began losing altitude over a mountain range. From planning, I knew at what altitude the ice would melt, and it was well above the mountaintops. The ATC was a little panicked and instructed me to maintain an assigned altitude, but because I was unable, I told him I would do so as soon as I could. I wasn’t about to attempt something that the aircraft was incapable of doing and possibly stall. No icing had been reported by ATC or in the weather forecast, but having flown the route often, I knew that it was a possibility. I was quite satisfied with myself for good planning. I know that sounds smug, but like good programming that’s been carefully planned and crafted, when everything comes together, there’s a feeling of doing something right. Maybe it is a bit smug, but so what?</p>
<p><strong>A Program Plan is a Principle Plan</strong></p>
<p>Not long ago I had to refactor a disaster done with ActionScript 2.0. The program looked good when executed, but it was one stupid dog trick piled on another. (<em>A stupid dog trick pile!</em>) I pulled all of the code out of the Actions panel, and began work on getting it right. Because I wanted to maintain the UI and graphic design, I had some constraints, but I knew it could be done. So the first thing I did was to create an abstract class to handle navigation. The navigation bar had been split in two, each with a set of buttons displaying an image of a video that would play by pressing the button. Having an abstract class for navigation, I was able to create two concrete classes, one for each segment of the navigation bar. Not only did the navigation buttons trigger a video, they also triggered two text files—one for a header and one for the body text that accompanied the video. So naturally I wanted a class for the text and so created an abstract class to handle text and then implemented concrete classes for the body text and header text.</p>
<p>The focal principles were, <em> (1) program to an interface, not an implementation; </em>and <em>(2) encapsulate what varies</em>. By setting up abstract classes, I was able to make my references to the interface (typing the instances to the interfaces). Anyway, I finished it up, and while my plan was rudimentary—it was jotted down on the back of a discarded daily calendar page,— <em>it was a plan</em>. The other day I ran into a friend who had seen the page where we had the application, and she wanted one like it for her videos. We sat down, and after showing her what to change, she was able to adopt it for her own application.</p>
<p>I did not use a design pattern. Had I used a design pattern, building it would have been easier, but because I followed the principles used in building design patterns, it worked very well. Most importantly, it was easily re-usable on a different application.</p>
<p><strong>Good Programmer’s Checklist</strong></p>
<p>In flying, we used a checklist to make sure that we didn’t forget anything. Usually, the walk-around with the checklist was uneventful, but every now and then, I’d find a problem. One day I found water in the gas tank, and drained out the water as required. Had I not used the checklist, I may have found an unpleasant surprise while a couple of miles up in the sky. Recently, I learned that hospitals are beginning to use similar checklists to make sure that all of the steps in medical procedures are met. The reason for checklists in hospitals is that they have been sued for medical mishaps. (One poor guy had the wrong leg amputated!)</p>
<p>As an algorithm dope, I never needed a checklist. However, as I began paying attention to OOP principles, I decided it was a good idea to have one. The one I made is a bit silly (<a href= "http://www.as3dp.com/2009/02/26/oop-designs-pattern-principles-ready-for-work/" >Programming Checklist</a>) but as an AIR application I can keep it handy on my desktop and it’s easy to use. More recently, I <a href= "http://www.as3dp.com/2009/05/13/index-principles-and-work/">indexed all of the principles</a> discussed so that I could find needed details about the principles easily.</p>
<p>So unlike Flash designers, we developers work in the dark and our projects have no exoskeleton. As long as our code works and the user doesn’t have to wait too long for the program to load, everything is Jake. What’s more, we can create junk code that works, and instead of elegantly adjusting a class or method to make changes, we can cut and paste more junk code and get things to work. If we don’t know any better that’s one thing. However, if we know about good programming practices and don’t use them, eventually our programming souls wither and die. So I’m going to add one more item to the checklist—<br />
<blockquote>Save your programmer’s soul!</p></blockquote>
<p> Remember, unlike the naked designer, only we know when we’re doing it right.</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%2F05%2Fthe-naked-flash-designer-and-the-actionscript-30-algorithm-dope%2F&amp;title=The%20Naked%20Flash%20Designer%20and%20the%20ActionScript%203.0%20Algorithm%20Dope" id="wpa2a_18"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/05/the-naked-flash-designer-and-the-actionscript-30-algorithm-dope/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Index: ActionScript 3.0 Design Patterns Principles and Design Patterns at Work</title>
		<link>http://www.as3dp.com/2009/05/index-principles-and-work/</link>
		<comments>http://www.as3dp.com/2009/05/index-principles-and-work/#comments</comments>
		<pubDate>Wed, 13 May 2009 12:04:00 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Index]]></category>
		<category><![CDATA[Principles]]></category>
		<category><![CDATA[Principles and Work]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=955</guid>
		<description><![CDATA[Easy to Find We got to the point where the posts on the Principles of OOP and Design Patterns and the discussions of introducing Design Patterns to the workplace became so numerous that they were difficult to locate on the blog. This is the first index that we have, and as we continue to grow, [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Easy to Find</strong></p>
<p>We got to the point where the posts on the Principles of OOP and Design Patterns and the discussions of introducing Design Patterns to the workplace became so numerous that they were difficult to locate on the blog. This is the first index that we have, and as we continue to grow, we’ll have to add more. (We are certainly nearing that point with MVC and Pure MVC posts.) So, in the “Categories” section of our blog, we’ll be adding indices as needed. Look for the <strong>Index</strong> category to locate all indices.<br />
<span id="more-955"></span><br />
<strong>Principles:</strong></p>
<ul>
OOP and Design Patterns Ready for Work</p>
<li><img src="http://www.as3dp.com/wp-content/uploads/2009/02/airimg.png" alt="airimg" title="airimg" width="104" height="69" class="alignleft size-full wp-image-736" />This post includes a quick guide to all of the principles discussed in the form of an AIR application that you can store on your desktop for a quick reference.
</li>
<p><a href="http://www.as3dp.com/2009/02/26/oop-designs-pattern-principles-ready-for-work/"> Link </a></p>
<p>Magic Table: What Varies in a Design Pattern</p>
<li><a href="http://www.sandlight.com/magic/">Download Magic Table AIR Application</a>
</ul>
<ul>
The Liskov Substitution Principle<br />
<a href="http://www.as3dp.com/2009/02/08/design-pattern-principles-for-actionscript-30-the-liskov-substitution-principle/"> Link </a></p>
<p>Program to an Interface; not an implementation<br />
<a href="http://www.as3dp.com/2009/02/14/design-pattern-principles-for-actionscript-30-program-to-an-interface-not-an-implementation/"> Link </a></p>
<p>Favor Object Composition over Inheritance<br />
<a href="http://www.as3dp.com/2009/02/21/design-pattern-principles-for-actionscript-30-favor-object-composition-over-class-inheritance/"> Link </a></p>
<p>Separate what varies from what stays the same and encapsulate what varies<br />
<a href="http://www.as3dp.com/2009/03/10/variation-table/"> Link </a></p>
<p>Objects should be loosely coupled<br />
<a href="http://www.as3dp.com/2009/03/12/design-pattern-principles-for-actionscript-30-loose-coupling/"> Link </a></p>
<p>Classes should be open for extension but closed for modification<br />
<a href="http://www.as3dp.com/2009/03/31/design-pattern-principles-for-actionscript-30-the-openclosed-principle/"> Link </a></p>
<p>High-level and low-level modules should depend on abstractions (Dependency Inversion Principle)<br />
<a href="http://www.as3dp.com/2009/04/07/design-pattern-principles-for-actionscript-30-the-dependency-inversion-principle/"> Link </a></p>
<p>Only communicate with closely related object (Least Knowledge Principle)<br />
<a href="http://www.as3dp.com/2009/04/23/design-pattern-principles-for-actionscript-30-the-least-knowledge-principle/"> Link </a></p>
<p>High-level components can call low-level components, but low level components never call high level components (Hollywood Principle)<br />
<a href="http://www.as3dp.com/2009/05/07/hollywood-principle-don’t-call-us-we’ll-call-you—actionscript-30-template-design-pattern-goes-hollywood/"> Link </a></p>
<p>Your class should have one and only one responsibility (Single Responsibility Principle)<br />
<a href="http://www.as3dp.com/2009/05/12/design-pattern-principles-for-actionscript-30-single-responsibility-principle/"> Link </a></p>
<p>Is Your ActionScript 3.0 Design Good? : The Three Keys of Good Design<br />
<a href="http://www.as3dp.com/2009/04/02/is-your-actionscript-30-design-good-the-three-keys-of-good-design/"> Link </a></p>
<p>No New is Good New: Using Inheritance, Composition, Delegation and anything else other than New in ActionScript 3.0 Design Patterns<br />
<a href="http://www.as3dp.com/2008/09/19/no-new-is-good-new-using-inheritance-composition-delegation-and-anything-else-other-than-new-in-actionscript-30-design-patterns/"> Link </a>
</ul>
<p><strong>Design Patterns at Work</strong></p>
<ul>
Why Design Patterns?<br />
<a href="http://www.as3dp.com/2007/05/12/why-design-patterns/"> Link </a></p>
<p>Strategy Design Pattern for Work<br />
<a href="http://www.as3dp.com/category/design-patterns/strategy-pattern/">Link</a></p>
<p>Decorator Design Pattern for Work<br />
<a href="http://www.as3dp.com/category/design-patterns/decorator/">Link</a></p>
<p>Not at the Children’s Table Anymore<br />
<a href="http://www.as3dp.com/2008/04/20/actionscript-30-not-at-the-childrens-table-anymore/"> Link </a></p>
<p>ActionScript 3.0 Developers: The Children’s Table Revisited<br />
<a href="http://www.as3dp.com/2009/03/15/actionscript-30-developers-the-children’s-table-revisited/"> Link </a></p>
<p>No Time for OOP and Design Patterns<br />
<a href="http://www.as3dp.com/2008/12/07/no-time-for-oop-and-design-patterns/"> Link </a></p>
<p>Take a Design Pattern to Work Part I: Identifying the Problem<br />
<a href="http://www.as3dp.com/2009/01/01/take-a-design-pattern-to-work-part-i-identifying-the-problem/"> Link </a></p>
<p>Take a Design Pattern to Work Part II: A Little OOP<br />
<a href="http://www.as3dp.com/2009/01/05/take-a-design-pattern-to-work-part-ii-a-little-oop-2/"> Link </a></p>
<p>Take a Design Pattern to Work Part III: Loosening Up<br />
<a href="http://www.as3dp.com/2009/01/07/take-a-design-pattern-to-work-part-iii-loosening-up/"> Link </a></p>
<p>Take a Design Pattern to Work Part IV: Establishing a Design Pattern Foundation<br />
<a href="http://www.as3dp.com/2009/01/15/take-a-design-pattern-to-work-part-iv-establishing-a-design-pattern-foundation/"> Link </a></ul>
<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%2F05%2Findex-principles-and-work%2F&amp;title=Index%3A%20ActionScript%203.0%20Design%20Patterns%20Principles%20and%20Design%20Patterns%20at%20Work" id="wpa2a_20"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/05/index-principles-and-work/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

