<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>ActionScript 3.0 Design Patterns</title>
	
	<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>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/as3dp/gQKb" /><feedburner:info uri="as3dp/gqkb" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><image><link>http://oreilly.com/catalog/9780596528461/</link><url>http://oreilly.com/catalog/covers/9780596528461_cat.gif</url></image><item>
		<title>ActionScript 3.0 GestureEvent: Working with Gestures on Mobile</title>
		<link>http://feedproxy.google.com/~r/as3dp/gQKb/~3/GtGVUYi6Qwc/</link>
		<comments>http://www.as3dp.com/2012/01/actionscript-3-0-gestureevent-working-with-gestures-on-mobile/#comments</comments>
		<pubDate>Sun, 29 Jan 2012 17:00:46 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Mobile]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6415</guid>
		<description><![CDATA[Using Gestures When working with high level and complex structures like Design Patterns, the focus is squarely on the relationship between objects—classes, inheritance, composition, implementation, interfaces and related concepts and structures. The &#8220;details&#8221; are the details of these various relationships and how they work in concert. Using Algorithm 1 or Algorithm 2 is only pertinent [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.as3dp.com/wp-content/uploads/2012/01/billz.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/billz.png" alt="Developing Gesture Apps Requires Actions with No Equivalent in MouseEvents" title="billz" width="250" height="188" class="alignleft size-full wp-image-6445" /></a><strong>Using Gestures</strong></p>
<p>When working with high level and complex structures like <strong>Design Patterns</strong>, the focus is squarely on the relationship between objects—classes, inheritance, composition, implementation, interfaces and related concepts and structures. The &#8220;details&#8221; are the details of these various relationships and how they work in concert. Using <strong>Algorithm 1</strong> or <strong>Algorithm 2</strong> is only pertinent insofar as it relates to a method or property that needs to be loosely bound to some other object. If you start thinking about algorithm details as a primary concern and fretting over internals, you can easily lose sight of the larger structures that are under development and never really understand <strong>Design Patterns</strong>.</p>
<p><strong>Seismic Shift</strong></p>
<p>Every now and again we encounter a <em>seismic shift</em> in the way things are done, and you have to make adjustments.(Some of you may remember the <strong>Commodore 64</strong> that went the way of the Dodo Bird. At one time it was the most popular personal computer on earth, but if you insisted on sticking with it, you probably don&#8217;t have any clients for your services.) In my own case, shift to mobile devices forced me to spend some quality time with the event processes in ActionScript 3.0. Going to the base, I reviewed how the Display List is handled in Flash and by AS3. Then I went to look at the AS3 Event Flow and among the several articles I read, one of the best is <a href="http://www.actionscript.org/resources/articles/860/1/Understanding-the-AS3-Event-Flow/Page1.html">Jody Hall&#8217;s</a>. Adobe has several good ones as well, and I&#8217;m sure many more are available that some of you may want to share.</p>
<p><strong>On to Gestures</strong></p>
<p>I wrote some gesture events and handlers. The results were unsatisfactory. It wasn&#8217;t that they didn&#8217;t work, but after one would work, another would not. Very baffling and frustrating. My goal was to create a &#8216;workbench&#8217; where I could try all kinds of gesture events, but I had very poor luck with just about everything at some point. So I backed away and tried an application with only two gestures; neither available with MouseEvent. One was <strong>Rotate</strong> and the other <strong>Zoom</strong>. Christian Cantrell has a <a href="http://www.adobe.com/devnet/flash/articles/multitouch_gestures.html">good article</a> on using gestures, and in addition to getting some good information on using gesture events, I also got a nice little piece on using gestures with bitmaps. (I had been having a devil of a time with Sprite objects in the Library with gestures.) Click the download button below to get the source code and FLA files for Flash Pro 5.5, 5 and 4 along with the image files and ActionScript 3.0 code:</p>
<p><a href="http://www.sandlight.com/DesignPatterns/PicFlip.zip"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/download.png" alt="download this sucker" title="download" width="136" height="48" class="alignnone size-full wp-image-6426" /></a></p>
<p>In my last post, I was convinced that <strong>TOUCH_TAP</strong> from <strong>TouchEvent</strong> works better than <strong>CLICK</strong> from <strong>MouseEvent</strong> with a mobile device. That conclusion was not based on the inner-workings of events and event handling, but rather from testing the different event handlers using my iOS device, a 4S iPhone. In the Adobe documentation, they point out that it <em>might be better</em> to use a <strong>MouseEvent</strong> rather than a <strong>TouchEvent</strong> or <strong>GestureEvent</strong>. It depends on the app, device and device OS. That pretty much makes it an empirical question.</p>
<p>In this application, there was no choice to use <strong>MouseEvent</strong> because it does not contain the required finger-flipping gestures I can make on my iPhone—<strong>GESTURE_ZOOM</strong> and <strong>GESTURE_ROTATE</strong>. Likewise, <strong>TouchEvent</strong> did not have the kinds of gestures; so I had to use <strong>TransformGestureEvent</strong>. The following code shows how it is employed in a simple picture flipping/zooming app:</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('p6415code3'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p64153"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code" id="p6415code3"><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;">Bitmap</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;">TransformGestureEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">ui</span>.<span style="color: #006600;">Multitouch</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">ui</span>.<span style="color: #006600;">MultitouchInputMode</span>;
&nbsp;
	<span style="color: #66cc66;">&#91;</span>SWF<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">640</span>, <span style="color: #0066CC;">height</span>=<span style="color: #cc66cc;">960</span>, frameRate=<span style="color: #cc66cc;">24</span>, <span style="color: #0066CC;">backgroundColor</span>=0xcc0000<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PicFlip <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source = <span style="color: #ff0000;">&quot;vanBillz.png&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> BillzImage:<span style="color: #000000; font-weight: bold;">Class</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> billzBitmap:Bitmap = <span style="color: #000000; font-weight: bold;">new</span> BillzImage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> carrier:Sprite = <span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> PicFlip<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			carrier.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">320</span>, carrier.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">550</span>;
			carrier.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>billzBitmap<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			billzBitmap.<span style="color: #006600;">x</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">320</span> - <span style="color: #66cc66;">&#40;</span>billzBitmap.<span style="color: #006600;">bitmapData</span>.<span style="color: #0066CC;">width</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> -<span style="color: #cc66cc;">1</span>;
			billzBitmap.<span style="color: #006600;">y</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">480</span> - <span style="color: #66cc66;">&#40;</span>billzBitmap.<span style="color: #006600;">bitmapData</span>.<span style="color: #0066CC;">height</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span>-<span style="color: #cc66cc;">1</span>;
&nbsp;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>carrier<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			Multitouch.<span style="color: #006600;">inputMode</span> = MultitouchInputMode.<span style="color: #006600;">GESTURE</span>;
			carrier.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TransformGestureEvent.<span style="color: #006600;">GESTURE_ZOOM</span>, doZoom<span style="color: #66cc66;">&#41;</span>;
			carrier.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TransformGestureEvent.<span style="color: #006600;">GESTURE_ROTATE</span>, doRotate<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> final <span style="color: #000000; font-weight: bold;">function</span> doZoom<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TransformGestureEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			carrier = <span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span> as Sprite;
			carrier.<span style="color: #006600;">scaleX</span> <span style="color: #66cc66;">*</span>=  <span style="color: #0066CC;">e</span>.<span style="color: #006600;">scaleX</span>;
			carrier.<span style="color: #006600;">scaleY</span> <span style="color: #66cc66;">*</span>=  <span style="color: #0066CC;">e</span>.<span style="color: #006600;">scaleY</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> final <span style="color: #000000; font-weight: bold;">function</span> doRotate<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TransformGestureEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			carrier = <span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span> as Sprite;
			carrier.<span style="color: #006600;">rotation</span> +=  <span style="color: #0066CC;">e</span>.<span style="color: #006600;">rotation</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>I tested it on an iPhone, but there&#8217;s no reason it won&#8217;t work perfectly well on an Android or some other mobile device. Further, I found it to be very responsive. The zooming seemed to work quite well with bitmapped graphics, and so I assumed that it would work just as well, if not better with a vector object in the Library. So I put together a little test symbol using the drawing tools in Flash and stored it as  Sprite class in the Library. (By the way, in case you were not aware, to make a Sprite class in the Library, when you create a New Symbol, just classify it as a MovieClip and in the Base Class window change the type from <strong>flash.display.MovieClip</strong> -> <strong>flash.display.Sprite</strong>. The Library icon turns from Blue to Green to indicate it is now a Sprite.)</p>
<p>I used the following code:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6415code4'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p64154"><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
</pre></td><td class="code" id="p6415code4"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">TransformGestureEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">ui</span>.<span style="color: #006600;">Multitouch</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">ui</span>.<span style="color: #006600;">MultitouchInputMode</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PicFlipLib <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> carrier: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: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> billzVector:Sprite=<span style="color: #000000; font-weight: bold;">new</span> BillzVector<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> PicFlipLib<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			carrier.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">220</span>, carrier.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">350</span>;
			carrier.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>billzVector<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>carrier<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			Multitouch.<span style="color: #006600;">inputMode</span> = MultitouchInputMode.<span style="color: #006600;">GESTURE</span>;
			carrier.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TransformGestureEvent.<span style="color: #006600;">GESTURE_ZOOM</span>, doZoom<span style="color: #66cc66;">&#41;</span>;
			carrier.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TransformGestureEvent.<span style="color: #006600;">GESTURE_ROTATE</span>, doRotate<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> final <span style="color: #000000; font-weight: bold;">function</span> doZoom<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TransformGestureEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			carrier = <span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span> as Sprite;
			carrier.<span style="color: #006600;">scaleX</span> <span style="color: #66cc66;">*</span>=  <span style="color: #0066CC;">e</span>.<span style="color: #006600;">scaleX</span>;
			carrier.<span style="color: #006600;">scaleY</span> <span style="color: #66cc66;">*</span>=  <span style="color: #0066CC;">e</span>.<span style="color: #006600;">scaleY</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> final <span style="color: #000000; font-weight: bold;">function</span> doRotate<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TransformGestureEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			carrier = <span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span> as Sprite;
			carrier.<span style="color: #006600;">rotation</span> +=  <span style="color: #0066CC;">e</span>.<span style="color: #006600;">rotation</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>One of the features that seemed to make a difference was that instead of having the event listener connected directly to the Library Sprite (<strong>BillzVector</strong>), I used an object container Sprite, <strong>carrier</strong>. Figure 1 shows the zoom and rotation on an iPhone 4S. This was used with both the bitmap and the vector Sprite objects, and they both worked quite well.<br />
<div id="attachment_6460" class="wp-caption alignnone" style="width: 510px"><a href="http://www.as3dp.com/wp-content/uploads/2012/01/rotateZoom.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/rotateZoom.png" alt="rotate" title="rotateZoom" width="500" height="372" class="size-full wp-image-6460" /></a><p class="wp-caption-text">Figure 1: Both rotation and zoom worked fined with the vector-based Sprite</p></div></p>
<p><strong>Back to Design Patterns</strong></p>
<p>I was going to post my final matrix motion solution, but I got jammed up with event handling. Next time, I should be able to post the next stage in my Samurai game. It uses a State design pattern to move through nine cells in a 3 X 3 matrix. Everything was working well except that my event handlers were not as crisp as I wanted. I&#8217;ll have a chance to see now whether the time-out with AS3 event handlers was enough. The Design Pattern does its job; I just was not getting the event handlers to do theirs. In the next post you&#8217;ll be able to see whether they are all working well together in a mobile environment.</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%2F2012%2F01%2Factionscript-3-0-gestureevent-working-with-gestures-on-mobile%2F&amp;title=ActionScript%203.0%20GestureEvent%3A%20Working%20with%20Gestures%20on%20Mobile" 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><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=GtGVUYi6Qwc:enhoFoyNZIM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=GtGVUYi6Qwc:enhoFoyNZIM:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=GtGVUYi6Qwc:enhoFoyNZIM:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=GtGVUYi6Qwc:enhoFoyNZIM:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=GtGVUYi6Qwc:enhoFoyNZIM:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=GtGVUYi6Qwc:enhoFoyNZIM:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/as3dp/gQKb/~4/GtGVUYi6Qwc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2012/01/actionscript-3-0-gestureevent-working-with-gestures-on-mobile/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.as3dp.com/2012/01/actionscript-3-0-gestureevent-working-with-gestures-on-mobile/</feedburner:origLink></item>
		<item>
		<title>ActionScript 3.0 TouchEvent: Guaranteed Mobile Speedup</title>
		<link>http://feedproxy.google.com/~r/as3dp/gQKb/~3/J05OqC36Tzc/</link>
		<comments>http://www.as3dp.com/2012/01/actionscript-3-0-touchevent-guaranteed-mobile-speedup/#comments</comments>
		<pubDate>Sun, 15 Jan 2012 16:44:06 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Speed Tweaks]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6400</guid>
		<description><![CDATA[The TouchEvent Object I suppose old habits die hard, especially when those habits still work. (Some programmers are fond of saying, If it ain&#8217;t broke, don&#8217;t fix it! and the similarly dull-witted proverb, Don&#8217;t re-invent the wheel!) Well, with mobile devices, you&#8217;d better think about breaking old habits, and I put myself at the top [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6413" class="wp-caption alignleft" style="width: 260px"><a href="http://www.as3dp.com/wp-content/uploads/2012/01/touch.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/touch.png" alt="Tap screen" title="touch" width="250" height="333" class="size-full wp-image-6413" /></a><p class="wp-caption-text">MouseEvent Works but TouchEvent Works better</p></div><strong>The TouchEvent Object</strong></p>
<p>I suppose old habits die hard, especially when those habits still work. (Some programmers are fond of saying, <em>If it ain&#8217;t broke, don&#8217;t fix it!</em> and the similarly dull-witted proverb, <em>Don&#8217;t re-invent the wheel!</em>) Well, with mobile devices, you&#8217;d better think about breaking old habits, and I put myself at the top of the dullard list for taking so long to make some adoptions. One of the first changes to make when creating mobile apps is to use the <strong>TouchEvent</strong> instead of the <strong>MouseEvent</strong>. In working on a game, I merrily kept using <strong>MouseEvent</strong> until I noticed that my apps kept getting stuck in the mud when I put them on a mobile device. So, I decided to give TouchEvent a try. Naturally, I couldn&#8217;t test in Flash Pro CS5.5; so I had to wait until I launched it in my iPhone. When I did, the difference was night and day. The sluggishness disappeared.</p>
<p>The good news is that <strong>MouseEvent</strong> and <strong>TouchEvent</strong> classes work pretty much the same as far as programming them into an app. I have no idea what&#8217;s going on with either under the hood, and I shouldn&#8217;t have to concern myself with those issues other than to know that one works better with mobile devices than the other. To get started, we&#8217;ll take a look at the sequence for setting up for TouchEvents, and while almost identical to MouseEvent, there are programming differences. First, though, click the Play button to see the little testing app on your computer. You can also download the Flash 5.5 Fla file and Client class by clicking on the Download button.<br />
<a href="http://www.sandlight.com/Touch/touchdown.zip"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/download.png" alt="" title="download" width="136" height="48" class="alignleft size-full wp-image-6426" /></a><a href="http://www.sandlight.com/Touch/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/play.png" alt="play button" title="play" width="136" height="48" class="alignnone size-full wp-image-6372" /></a></p>
<p>As you can see it&#8217;s a simple little app to move a &#8220;Samurai&#8221; character left and right; up and down. The buttons are in Japanese, and if you read Japanese, you&#8217;ll realize that the characters look like a first-grader&#8217;s attempts to learn how to write in Japanese. (Feel free to make some better simple buttons using Japanese characters and send them along with any comments you may have.)</p>
<blockquote><p><strong>Possible Controversy</strong>: After I had finished this post, I ran across the following:</p>
<p>http://help.adobe.com/en_US/as3/dev/WSb2ba3b1aad8a27b0-6ffb37601221e58cc29-8000.html</p>
<p>In it is a note of caution:</p>
<p><em><strong>Note</strong>: Listening for touch and gesture events can consume a significant amount of processing resources (equivalent to rendering several frames per second), depending on the computing device and operating system. It is often better to use mouse events when you do not actually need the extra functionality provided by touch or gestures. When you do use touch or gesture events, consider reducing the amount of graphical changes that can occur, especially when such events can be dispatched rapidly, as during a pan, rotate, or zoom operation. For example, you could stop animation within a component while the user resizes it using a zoom gesture.</em></p>
<p>That certainly makes sense, but I found that even using TOUCH_TAP, it was a quicker and far more responsive than listening for a CLICK event. There&#8217;s only one way to resolve this issue; try it yourself with a mobile app on a mobile device&#8211;any one you want. Use the Comment section to report what you&#8217;ve found. The sample app is as good as any for a trial, but feel free to create one of your own choosing.</p>
<p>As Chico Marx remarked in <strong>Duck Soup</strong>, <em>Well, who you gonna believe, me or your own eyes? </em> Believe your eyes and give it a whirl!
</p></blockquote>
<p><strong>Just the Right Touch</strong></p>
<p>Setting up the TouchEvent includes three imports:</p>
<ul>
<li><strong>import flash.events.TouchEvent;</strong></li>
<li> <strong>import flash.ui.Multitouch;</strong></li>
<li><strong>import flash.ui.MultitouchInputMode;</strong></li>
</ul>
<p>That contrasts with the single <strong>MouseEvent</strong> import required when using the mouse event. However, prior to using the TouchEvent class you have to set up your mobile context with the following line:</p>
<p>     <strong>Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT; </strong></p>
<p>That statement focuses the input mode to touch instead of a mouse event. I was unable to get the <strong>TouchEvent</strong> to work without the statement; so, don&#8217;t forget to include it in your program.</p>
<p>Finally, you need an event listener, and it has the same format as a mouse event except you use <strong>TouchEvent</strong>. Also, instead of using CLICK, use TOUCH_TAP, as the following illustrates:</p>
<p>     <strong>upBtn.addEventListener(TouchEvent.TOUCH_TAP,moveNow);</strong></p>
<p>With that knowledge, you&#8217;re all set to set up an app using TouchEvent.</p>
<p><strong>Many Ears; One Handler</strong></p>
<p>One of the big memory hogs in programming, especially noticeable in mobile devices, is event listening. Each event listener sets up a series of operations that have to keep listening for an event. Having a single event won&#8217;t help, but it&#8217;s a step in the direction of having a single listener (which will help). The following <strong>Client</strong> sets up an example of using the <strong>TouchEvent</strong>. All of the buttons are <strong>SimpleButton</strong> objects and the background is a big <strong>Sprite</strong>. Likewise, <strong>Sam3</strong> is a Sprite that you can move around using tap on your mobile device.</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('p6400code6'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p64006"><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
</pre></td><td class="code" id="p6400code6"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">SimpleButton</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">TouchEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">ui</span>.<span style="color: #006600;">Multitouch</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">ui</span>.<span style="color: #006600;">MultitouchInputMode</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> leftBtn:SimpleButton=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Left</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> rightBtn:SimpleButton=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Right</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> upBtn:SimpleButton=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Up</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> downBtn:SimpleButton=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Down</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> bkground:Sprite=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Background</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> sam3:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sam3<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>
			addChildAt<span style="color: #66cc66;">&#40;</span>bkground,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
			setControls<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			setSamurai<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> final <span style="color: #000000; font-weight: bold;">function</span> setControls<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			Multitouch.<span style="color: #006600;">inputMode</span> = MultitouchInputMode.<span style="color: #006600;">TOUCH_POINT</span>;
			upBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">825</span>,upBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">19</span>;
			upBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TouchEvent.<span style="color: #006600;">TOUCH_TAP</span>,moveNow<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>upBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			leftBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">740</span>,leftBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">106</span>;
			leftBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TouchEvent.<span style="color: #006600;">TOUCH_TAP</span>,moveNow<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>leftBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			rightBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">873</span>,rightBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">105</span>;
			rightBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TouchEvent.<span style="color: #006600;">TOUCH_TAP</span>,moveNow<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rightBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			downBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">825</span>,downBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">202</span>;
			downBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>TouchEvent.<span style="color: #006600;">TOUCH_TAP</span>,moveNow<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>downBtn<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> final <span style="color: #000000; font-weight: bold;">function</span> setSamurai<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			sam3.<span style="color: #006600;">x</span>=<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">960</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>; sam3.<span style="color: #006600;">y</span>=<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">640</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>+<span style="color: #cc66cc;">100</span>;
			addChild<span style="color: #66cc66;">&#40;</span>sam3<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> final <span style="color: #000000; font-weight: bold;">function</span> moveNow<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:TouchEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #0066CC;">name</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;instance11&quot;</span>:
				sam3.<span style="color: #006600;">y</span> -=<span style="color: #cc66cc;">5</span>;
				<span style="color: #b1b100;">break</span>;
&nbsp;
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;instance1&quot;</span>:
				sam3.<span style="color: #006600;">x</span> -=<span style="color: #cc66cc;">5</span>;
				<span style="color: #b1b100;">break</span>;
&nbsp;
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;instance6&quot;</span>:
				sam3.<span style="color: #006600;">x</span> +=<span style="color: #cc66cc;">5</span>;
				<span style="color: #b1b100;">break</span>;
&nbsp;
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;instance16&quot;</span>:
				sam3.<span style="color: #006600;">y</span> +=<span style="color: #cc66cc;">5</span>;
				<span style="color: #b1b100;">break</span>;
&nbsp;
			<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>I set it up to be viewed from a landscape perspective because of a larger project I&#8217;m developing. Also, as you can see in the <strong>moveNow</strong> method, there are no &#8220;brakes&#8221; on the movement, and so the character can move anywhere, including right off the stage. Currently, I have a nicely working global movement system set up using a State Pattern, and I&#8217;d like to integrate both local and global movement into a single movement. Not only would this cut down on the number of listeners, but it would also provide a more interesting game. Figure 1 shows how it looks on an iPhone, and I&#8217;d like any feedback if tried on Android or other non-iOS devices.<br />
<div id="attachment_6422" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/touchMob.png" alt="" title="touchMob" width="500" height="333" class="size-full wp-image-6422" /><p class="wp-caption-text">The 960 x 640 size is compressed to fit in an iPhone environment</p></div></p>
<p>You also might want to swap out the <strong>TouchEvent</strong> for a <strong>MouseEvent</strong> and try it again in a mobile device. You&#8217;ll find that it gets &#8220;stuck&#8221; after a few clicks. As always, we&#8217;re interested in your comments and feedback.</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%2F2012%2F01%2Factionscript-3-0-touchevent-guaranteed-mobile-speedup%2F&amp;title=ActionScript%203.0%20TouchEvent%3A%20Guaranteed%20Mobile%20Speedup" 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><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=J05OqC36Tzc:dmHVwkfvzNQ:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=J05OqC36Tzc:dmHVwkfvzNQ:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=J05OqC36Tzc:dmHVwkfvzNQ:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=J05OqC36Tzc:dmHVwkfvzNQ:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=J05OqC36Tzc:dmHVwkfvzNQ:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=J05OqC36Tzc:dmHVwkfvzNQ:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/as3dp/gQKb/~4/J05OqC36Tzc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2012/01/actionscript-3-0-touchevent-guaranteed-mobile-speedup/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		<feedburner:origLink>http://www.as3dp.com/2012/01/actionscript-3-0-touchevent-guaranteed-mobile-speedup/</feedburner:origLink></item>
		<item>
		<title>ActionScript 3.0 Single Class Event Handler</title>
		<link>http://feedproxy.google.com/~r/as3dp/gQKb/~3/725Jqmf7Z-8/</link>
		<comments>http://www.as3dp.com/2012/01/actionscript-3-0-single-class-event-handler/#comments</comments>
		<pubDate>Fri, 13 Jan 2012 16:08:28 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Speed Tweaks]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6359</guid>
		<description><![CDATA[One Object:Several Events The first time I read Scott Petersen&#8217;s article on optimizing code for iOS devices, I got it wrong in terms of what he was trying to do in one of the optimizations. In re-reading it, I discovered an interesting structure Scott included, and while not a speed tweak in and of itself, [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6398" class="wp-caption alignleft" style="width: 260px"><a href="http://www.as3dp.com/wp-content/uploads/2012/01/reachout.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/reachout.png" alt="reachout" title="reachout" width="250" height="165" class="size-full wp-image-6398" /></a><p class="wp-caption-text">Reaching out to Single Class for Event Handling</p></div><strong>One Object:Several Events</strong></p>
<p>The first time I read <a href="http://www.adobe.com/devnet/flash/articles/optimize_content_ios.html">Scott Petersen&#8217;s</a> article on optimizing code for iOS devices, I got it wrong in terms of what he was trying to do in one of the optimizations. In re-reading it, I discovered an interesting structure Scott included, and while not a speed tweak in and of itself, I thought it deserved a short discussion and example.</p>
<p>The idea is to have one object with several methods handle all event requests. A single method handles a single request. As you know, event-requests expect one parameter&#8211;the event-object listening for the event. Once initiated, the <strong>event.target</strong> object can be used to determine an appropriate course of action. To see how this works, I put together a simple class for handling requests. It does nothing more than trace back the label of the UI that makes the initial request.</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('p6359code9'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p63599"><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="p6359code9"><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;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">class</span> OneNav
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> navNorth<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">label</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> navSouth<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">label</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> navEast<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">label</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> navWest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">label</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>With the <strong>OneNav</strong> class, the Client can then address all event handlers to the same class (object) and just append the specific method to the object. The general format is:</p>
<blockquote><p>listeningObj.addEventListener(MouseEvent.CLICK,actionObj.actionMethod);</p></blockquote>
<p>So now the developer can have everything to be handled in a single class with methods taking care of the details. If the details change, the developer can just go change the methods to reflect the changed details. To test it out, I&#8217;ve included the following <strong>Client</strong> class:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6359code10'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p635910"><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
</pre></td><td class="code" id="p6359code10"><pre class="actionscript" style="font-family:monospace;">package 
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">Button</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> final <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> btnNorth:<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> btnSouth:<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> btnEast:<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> btnWest:<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> navNow:OneNav=<span style="color: #000000; font-weight: bold;">new</span> OneNav<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>
			btnNorth.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,navNow.<span style="color: #006600;">navNorth</span><span style="color: #66cc66;">&#41;</span>;
			btnNorth.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">50</span>;btnNorth.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">50</span>;
			btnNorth.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;North&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnNorth<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			btnSouth.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,navNow.<span style="color: #006600;">navSouth</span><span style="color: #66cc66;">&#41;</span>;
			btnSouth.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">50</span>;btnSouth.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">80</span>;
			btnSouth.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;South&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnSouth<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			btnEast.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,navNow.<span style="color: #006600;">navEast</span><span style="color: #66cc66;">&#41;</span>;
			btnEast.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">50</span>;btnEast.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">110</span>;
			btnEast.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;East&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnEast<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			btnWest.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,navNow.<span style="color: #006600;">navWest</span><span style="color: #66cc66;">&#41;</span>;
			btnWest.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">50</span>;btnWest.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">140</span>;
			btnWest.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;West&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnWest<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 some respects, this technique can add more work for the developer as he tries to figure out how to get the events to spark something on the viewing area even if he&#8217;s using a design pattern. This particular implementation reflects work I&#8217;ve been doing on the navigation portion of a game I&#8217;m developing, but the technique could apply to virtually any project. I would have to have the <strong>OneNav</strong> method call a State Pattern to get the appropriate image to show up on the stage. The would take a bit of doing. However, for some other kind of application, it might be just the thing you need.</p>
<p><strong>A <em>Final</em> Word</strong></p>
<p>One of the speed tweaks that Scott points out in this same article is using <strong>final</strong> in marking classes and methods. If you&#8217;re not subclassing a given class, you can mark it <strong>final</strong> as done in the <strong>Client</strong> class. Marking a class <strong>final</strong> allows compile-time determination of the exact function being called. When some methods of a class can be overridden, you cannot mark the class <strong>final</strong> but you can mark the individual method as <strong>final</strong>, as was done in the <strong>OneNav</strong> class.</p>
<p>As a matter of fact, I cannot think of a single use case where I would subclass a Client class, and so, it&#8217;s probably a safe bet to mark all Client classes as final. When developing with ActionScript 3.0 for mobile devices, every single speed tweak matters.</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%2F2012%2F01%2Factionscript-3-0-single-class-event-handler%2F&amp;title=ActionScript%203.0%20Single%20Class%20Event%20Handler" 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><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=725Jqmf7Z-8:cUwpu0iShTk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=725Jqmf7Z-8:cUwpu0iShTk:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=725Jqmf7Z-8:cUwpu0iShTk:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=725Jqmf7Z-8:cUwpu0iShTk:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=725Jqmf7Z-8:cUwpu0iShTk:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=725Jqmf7Z-8:cUwpu0iShTk:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/as3dp/gQKb/~4/725Jqmf7Z-8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2012/01/actionscript-3-0-single-class-event-handler/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		<feedburner:origLink>http://www.as3dp.com/2012/01/actionscript-3-0-single-class-event-handler/</feedburner:origLink></item>
		<item>
		<title>From ActionScript 3.0 to JavaScript Chain of Responsibility: Part II The Help Desk</title>
		<link>http://feedproxy.google.com/~r/as3dp/gQKb/~3/zQJp_uYUrfM/</link>
		<comments>http://www.as3dp.com/2012/01/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-ii-the-help-desk/#comments</comments>
		<pubDate>Wed, 04 Jan 2012 22:29:47 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Chain of Responsibility]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://new.as3dp.com/?p=6352</guid>
		<description><![CDATA[A JavaScript Chain of Responsibility I&#8217;ve always liked JavaScript, but after going through this last project of creating a design pattern with JS, I find that I like ActionScript 3.0 even more than I did before. The biggest problems that I encountered with JavaScript is that different users slipped in different frameworks (e.g., Prototype.js) and [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6360" class="wp-caption alignleft" style="width: 260px"><a href="http://www.as3dp.com/wp-content/uploads/2012/01/jscor250.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/jscor250.png" alt="corjs" title="jscor250" width="250" height="333" class="size-full wp-image-6360" /></a><p class="wp-caption-text">JavaScript can do Design Patterns</p></div><strong>A JavaScript Chain of Responsibility</strong></p>
<p>I&#8217;ve always liked JavaScript, but after going through this last project of creating a design pattern with JS, I find that I like ActionScript 3.0 even more than I did before. The biggest problems that I encountered with JavaScript is that different users slipped in different frameworks (e.g., Prototype.js) and didn&#8217;t seem to mention it. My goal was to create a JS design pattern with pure unadulterated JavaScript—not using JQuery, JSon or some other helpful framework. Now, don&#8217;t get me wrong—I appreciate a good framework as much as the next guy. However, when I&#8217;m trying to create a design pattern for a general language like JavaScript, I&#8217;d rather not have to have readers run out and get one framework or another. I wanted this to work with Plain Vanilla JavaScript. So that&#8217;s what you&#8217;re going to get.</p>
<p>To get started, I took a look at some articles on JavaScript and classes, inheritance and OOP in general. Then I looked at some implementations of JavaScript with a Chain of Responsibility pattern. A lot of them didn&#8217;t make a bit of sense, and that&#8217;s when I realized that they were slipping in different &#8220;helpers,&#8221; and so I went back to the most basic JavaScript OOP I knew of&#8211;the 1999 book, <em>JavaScript Objects</em> by Nakhimovsky and Myers.  Starting there, I pretty much followed that Chain of Responsibility pattern we had in <a href="http://www.as3dp.com/2011/12/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-i/">Part I</a> of this two-part series. However, instead of a mobile browser detector, I created a &#8220;Help Desk.&#8221; </p>
<p>Before continuing, take a look at the &#8220;Help Desk&#8221; app made with the JS CoR:<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/dp/jscor/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/play.png" alt="" title="play" width="136" height="48" class="alignnone size-full wp-image-6372" /></a><br />
<div id="attachment_6385" class="wp-caption alignnone" style="width: 510px"><a href="http://www.as3dp.com/wp-content/uploads/2012/01/helpdesk.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/helpdesk.png" alt="helpdesk" title="helpdesk" width="500" height="500" class="size-full wp-image-6385" /></a><p class="wp-caption-text">Figure 1: JavaScript Chain of Responsibility Help Desk</p></div></p>
<p><strong>Inheritance is a &#8216;IS-A&#8217;</strong></p>
<p>First I made an abstract Handler class using a single concrete and single abstract method. It seemed to me that classes in JavaScript could be abstract simply by adding a method with no content. I came up with the following class:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6352code16'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p635216"><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="p6352code16"><pre class="javascript" style="font-family:monospace;">function Handler()
{ 
	//Handler 'abstract class'
} 
&nbsp;
Handler.prototype.setSuccessor=function(successor)
{ 
	this.successor=successor;
} 
&nbsp;
Handler.prototype.handleRequest=function(req)
{ 
	//Abstract method overridden in concrete implementation
}</pre></td></tr></table></div>

<p>I realize that it looks strange compared to more mature OOP languages like ActionScript 3.0 (not to mention Java and C++), but remember, I&#8217;m using pure native JavaScript. You create the class by the simple expedient of a function. Then, I create the methods for the class by using a <strong>Class.prototype</strong>. The class Handler now has the <strong>setSuccessor</strong> and <strong>handleRequest</strong> methods, just as we had in the ActionScript 3.0 version. So, while it looks a little goofy for creating classes and their methods and properties, that&#8217;s how it&#8217;s done.</p>
<p>So far so good, and it&#8217;s not rocket science. However, the next part gets a little dicey and may pop a few brain nodes. When you extend a class in JavaScript, you first instantiate the parent class. As we all know, you do not instantiate abstract classes or any other interface. They&#8217;re extended or implemented. However, that&#8217;s not the case with JavaScript. Take a look at this first child class of Handler:</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('p6352code17'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p635217"><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="p6352code17"><pre class="javascript" style="font-family:monospace;">//AS3DP inherits handler
AS3DP.prototype= new Handler();
&nbsp;
function AS3DP()
{
	//AS3DP 'class'
}
&nbsp;
AS3DP.prototype.handleRequest=function(req)
{
	this.req=req;
	if(this.req==&quot;as3dp&quot;)
	{
		document.write(&quot;Naturally you want to read &lt;em&gt;ActionScript 3.0 Design Patterns&lt;/em&gt; and visit our blog at as3dp.com.&quot;);
	}
	else if(this.successor != null)
	{
		document.write(&quot;Not AS3DP&lt;br/&gt;&quot;);
		this.successor.handleRequest(this.req);
	}
}</pre></td></tr></table></div>

<p>If you recall, basic design pattern principles, a child class &#8220;Is-A&#8221; parent class. When a JavaScript object (like AS3DP) is instantiated, it is done by declaring itself as a <strong>new Handler()</strong>. So rather than becoming a child of the parent through extension, JavaScript uses the object prototype to declare itself as an instance of the parent. It&#8217;s actually easier to see that a child class indeed &#8220;Is-A&#8221; parent because the prototype declares itself as such.</p>
<p>As we have seen, the methods and properties have been created <em>outside of the class constructors</em>. Instead of being <em>in the class</em>, they are <em>in the prototype</em>. For now, you can think of the class as the &#8220;concept of an object&#8221; and it creates and stores the properties for that concept in prototypes. So, knowing that, we can create the rest of the concrete handlers:</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('p6352code18'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p635218"><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
</pre></td><td class="code" id="p6352code18"><pre class="javascript" style="font-family:monospace;">//Game inherits Handler
Game.prototype= new Handler();
&nbsp;
function Game()
{
	//Game 'class'
}
&nbsp;
Game.prototype.handleRequest=function(req)
{
	this.req=req;
	if(this.req==&quot;game&quot;)
	{
		document.write(&quot;As the states change, you want a good state machine like the State Design Pattern.&quot;);
	}
	else if(this.successor != null)
	{
		document.write(&quot;Not State pattern&lt;br/&gt;&quot;);
		this.successor.handleRequest(this.req);
	}
}
&nbsp;
//Algorithm inherits Handler
Algorithm.prototype= new Handler();
&nbsp;
function Algorithm()
{
	//Algorithm 'class'
}
&nbsp;
Algorithm.prototype.handleRequest=function(req)
{
	this.req=req;
	if(this.req==&quot;algorithm&quot;)
	{
		document.write(&quot;The Strategy Design Pattern lets you access different algorithms directly without having to use conditional statements!&quot;);
	}
	else if(this.successor != null)
	{
		document.write(&quot;Not Strategy pattern&lt;br/&gt;&quot;);
		this.successor.handleRequest(this.req);
	}
}
&nbsp;
//Factory inherits Handler
Factory.prototype= new Handler();
&nbsp;
function Factory()
{
	//Factory 'class'
}
&nbsp;
Factory.prototype.handleRequest=function(req)
{
	this.req=req;
	if(this.req==&quot;factory&quot;)
	{
		document.write(&quot;The Factory Method design pattern will unlink the request to build an object from the actual object creation.&quot;);
	}
	else if(this.successor != null)
	{
		document.write(&quot;Not Factory&lt;br/&gt;&quot;);
		this.successor.handleRequest(this.req);
	}
}
&nbsp;
//ToInterface inherits Handler
ToInterface.prototype= new Handler();
&nbsp;
function ToInterface()
{
	//ToInterface 'class'
}
&nbsp;
ToInterface.prototype.handleRequest=function(req)
{
	this.req=req;
	if(this.req==&quot;tointerface&quot;)
	{
		document.write(&quot;The first principle is to &lt;em&gt;Program to the interface and not the implementation.&lt;/em&gt; When you declare a new object, type it to the object's parent class; not the object itself.&quot;);
	}
	else if(this.successor != null)
	{
		document.write(&quot;Not Program to Interface&lt;br/&gt;&quot;);
		this.successor.handleRequest(this.req);
	}
}
&nbsp;
//Truth inherits Handler
Truth.prototype= new Handler();
&nbsp;
function Truth()
{
	//Truth 'class'
}
&nbsp;
Truth.prototype.handleRequest=function(req)
{
&nbsp;
	document.write(&quot;You know I can't handle the truth! Go visit a muse, a swami, or 3-year old.&lt;br/&gt;&quot;);
&nbsp;
}</pre></td></tr></table></div>

<p>As you no doubt have noticed, this Chain of Responsibility implementation is not about mobile devices and their languages. Instead, it&#8217;s a &#8220;Help Desk&#8221; that I think is more typical of what you may actually use a CoR pattern for—either in JavaScript or ActionScript 3.0.</p>
<p>All that&#8217;s left is to do with the JavaScript is to create instances of the different handlers and set the chain of successors. The &#8220;client&#8221; that makes the requests is the HTML5 UI.</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('p6352code19'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p635219"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p6352code19"><pre class="javascript" style="font-family:monospace;">//'Instantiate Concrete Handlers'
var as3dp = new AS3DP();
var game=new Game();
var algorithm=new Algorithm();
var factory=new Factory();
var tointerface=new ToInterface();
var truth=new Truth();
//Set successors
as3dp.setSuccessor(game); 
game.setSuccessor(algorithm); 
algorithm.setSuccessor(factory);
factory.setSuccessor(tointerface);
tointerface.setSuccessor(truth);</pre></td></tr></table></div>

<p>You can set up the successors in any way you want except the last one has to be to the <strong>Truth</strong> class. It has no successor and is meant to be the caboose of the chain. Before going on, place all of the JavaScript into a single file and save it as &#8220;HelpDesk.js&#8221; and put it in the same folder (directory) as the HTML5 program that follows.</p>
<p><strong>The HTML5 Client</strong></p>
<p>Like most work with JavScript, the UI is in HTML. This is no different. All it does is to call the top of the chain (<strong>AS3DP</strong> instance) and make a request. All of the requests look like the following:</p>
<blockquote><p>onclick=&#8221;as3dp.handleRequest(&#8216;algorithm&#8217;);</p></blockquote>
<p>The click handler calls the top of the chain, and the Chain of Responsibility just chugs on through until it finds what you want. Here&#8217;s the whole HTML5 code:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6352code20'); return false;">View Code</a> HTML5</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p635220"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
</pre></td><td class="code" id="p6352code20"><pre class="html5" style="font-family:monospace;">&lt;!DOCTYPE HTML&gt;
&lt;html&gt;
&lt;head&gt;
&lt;style type=&quot;text/css&quot;&gt;
body {
	font-family:Verdana, Geneva, sans-serif;
	color:#1D232;
	background-color:#dddcc5;
	font-size:12px;
}
h2 {
	font-family:&quot;Arial Black&quot;, Gadget, sans-serif;
	font-size:24px;
	text-align:center;
	color:#611427;
}
form {
	color:#6A6A61;
}
h3 {
	background-color:#611427;
	color:#958976;
	font-size:18px;
}
&lt;/style&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;HelpDesk.js&quot;&gt;
&lt;/script&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;Billz Help Desk&lt;/title&gt;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&lt;header&gt;
  &lt;h2&gt;Billz Help Desk&lt;/h2&gt;
&lt;/header&gt;
&lt;article&gt;
&lt;header&gt;
  &lt;h3&gt;&amp;nbsp;Chain of Responsibility: JavaScript&lt;/h3&gt;
&lt;/header&gt;
&lt;section&gt; This is a simple Chain of Responsibility (CoR) application. Each button represents a different request that the CoR will handle. In this case, the help is in the form of a 'Help Desk' and the CoR finds the appropriate response to the query.&lt;br/&gt;
  &lt;br/&gt;
  &lt;form&gt;
    &lt;input type=button value=&quot;Make Request&quot; onclick=&quot;as3dp.handleRequest('as3dp');&quot; /&gt;
    How do I learn about design patterns for ActionScript 3.0 &lt;br/&gt;
    &lt;input type=button value=&quot;Make Request&quot; onclick=&quot;as3dp.handleRequest('game');&quot; /&gt;
    I'm making a game where states keep changing. What would be a good design pattern for dealing with changing states?&lt;br/&gt;
    &lt;input type=button value=&quot;Make Request&quot; onclick=&quot;as3dp.handleRequest('algorithm');&quot; /&gt;
    My app involves a lot of problem solving with different algorithms? What's a good pattern for dealing with multiple algorithms?&lt;br/&gt;
    &lt;input type=button value=&quot;Make Request&quot; onclick=&quot;as3dp.handleRequest('factory');&quot; /&gt;
    Whenever I create a new object with my Client object, I build up dependencies. Is there a pattern to avoid such dependencies?&lt;br/&gt;
    &lt;input type=button value=&quot;Make Request&quot; onclick=&quot;as3dp.handleRequest('tointerface');&quot; /&gt;
    What is a primary guiding design pattern principle?&lt;br/&gt;
    &lt;input type=button value=&quot;Make Request&quot; onclick=&quot;as3dp.handleRequest('truth');&quot; /&gt;
    I want to know the truth!&lt;br/&gt;
  &lt;/form&gt;
&lt;/section&gt;
&lt;br/&gt;
&lt;section&gt;The CoR pattern works sequentially so that as it moves through the chain, you can see each rejected attempt to find the correct handler. (Normally, you wouldn't see those rejected handlers.) It is a very easy pattern to update and add additional handlers.&lt;/section&gt;
&lt;/article&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>So, now you&#8217;re all set to use JavaScript for design patterns. The plain vanilla JS has limitations doing OOP, but you can harness it and bend it to your OOP will. The many different frameworks that work in conjunction with basic JavaScript will make it look more like an OOP language, but you can get by without. There&#8217;s a lot to be done with even the most unadorned JavaScript, and I tested the app on an iPhone, and it works fine. However, I&#8217;ll bet that several of you can find better ways to handle either the JavaScript or HTML5 (or both) with design patterns, and we&#8217;d like to hear from you. Naturally, if you have a favored framework that makes OOP work more efficient, let us know—even better send a design pattern example.</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%2F2012%2F01%2Ffrom-actionscript-3-0-to-javascript-chain-of-responsibility-part-ii-the-help-desk%2F&amp;title=From%20ActionScript%203.0%20to%20JavaScript%20Chain%20of%20Responsibility%3A%20Part%20II%20The%20Help%20Desk" 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><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=zQJp_uYUrfM:SRDNlD4MkSI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=zQJp_uYUrfM:SRDNlD4MkSI:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=zQJp_uYUrfM:SRDNlD4MkSI:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=zQJp_uYUrfM:SRDNlD4MkSI:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=zQJp_uYUrfM:SRDNlD4MkSI:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=zQJp_uYUrfM:SRDNlD4MkSI:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/as3dp/gQKb/~4/zQJp_uYUrfM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2012/01/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-ii-the-help-desk/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.as3dp.com/2012/01/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-ii-the-help-desk/</feedburner:origLink></item>
		<item>
		<title>From ActionScript 3.0 to JavaScript Chain of Responsibility: Part I</title>
		<link>http://feedproxy.google.com/~r/as3dp/gQKb/~3/MqgHyLce6N4/</link>
		<comments>http://www.as3dp.com/2011/12/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-i/#comments</comments>
		<pubDate>Mon, 26 Dec 2011 12:19:30 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Chain of Responsibility]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6348</guid>
		<description><![CDATA[Just Follow the Chain of Responsibility To get started on the quest to see whether useful design patterns can be created with JavaScript, I thought it&#8217;d be a good idea to go from the known to the unknown. In this case, those familiar with this blog know ActionScript 3.0 design patterns, and the unknown (for [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6349" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/12/cordad.png" alt="After Mom, Ask Dad, and then the Dog" title="cordad" width="250" height="201" class="size-full wp-image-6349" /><p class="wp-caption-text">After Mom, Ask Dad, and then the Dog</p></div><strong>Just Follow the Chain of Responsibility</strong></p>
<p>To get started on the quest to see whether useful design patterns can be created with JavaScript, I thought it&#8217;d be a good idea to go from the known to the unknown. In this case, those familiar with this blog know ActionScript 3.0 design patterns, and the unknown (for some at least) is JavaScript. Well, I suppose just about everyone who reads this blog knows something about JavaScript, but several may not have been involved with either JavaScript OOP or design patterns. By creating an ActionScript 3.0 app using a pattern and then going through the app step-by-step, readers may be better able to understand how we might approach design patterns using JavaScript. I decided to use the <strong>Chain of Responsibility</strong> (CoR) pattern to go through the types of mobile operating systems that we examined in the <a href="http://www.as3dp.com/2011/11/28/can-javascript-create-design-patterns/">previous post</a>. In this way, we can create something both familiar and practical.</p>
<p>Every time I use the <strong>Chain of Responsibility</strong> pattern, I feel that I&#8217;m swatting a fly with a weapon of mass destruction. I see all of these classes, and I&#8217;m thinking that I could have done the same thing with a switch statement or something even slicker. Then I need to remind myself why I&#8217;d use the <strong>CoR </strong>in the first place.</p>
<blockquote><p>The Chain of Responsibility separates the handling of an event from the request to handle it.</p></blockquote>
<p> In a broader context, this allows the developer to make changes to either the request or the handling of the event without disrupting some other part of the program. It&#8217;s quite simple as well. Just imagine several people lined up with different kinds of expertise and/or authority. A request is issued, and once it reaches the person with the right expertise or authority, it is handled.  The request is separated from the way it will be handled. That&#8217;s up to the expert. Figure 1 shows the class design:</p>
<div id="attachment_57" class="wp-caption alignnone" style="width: 499px"><img src="http://www.as3dp.com/wp-content/uploads/2008/01/corcldiagram.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1: &lt;/strong&gt; Chain of Responsibility Pattern&lt;/em&gt;" title="Chain of Responsibility Pattern" width="489" height="253" class="size-full wp-image-57" /><p class="wp-caption-text"><em><strong>Figure 1: </strong> Chain of Responsibility Pattern</em></p></div>
<p><strong>Getting Started with CoR</strong></p>
<p>For details about ways to setup and use <strong>CoR</strong> see our <a href="http://www.as3dp.com/2008/01/14/actionscript-30-chain-of-responsibility-design-pattern-decoupling-request-and-request-handler/">original discussion </a>. The focal points are in the following:</p>
<ul>
<li>An interface (an abstract class or interface) establishes a handler operation</li>
<li>Each request handler has a separate class derived from the handler interface</li>
<li>Each handler class has a successor which is another handler class</li>
<li>The last handler in the chain has no successor</li>
<li>As soon as a handler can handle the request, the chain stops and the handler takes care of the request.</li>
</ul>
<p>You may be thinking that this is not very efficient since it requires a sequential path. We&#8217;ve considered ways to get around a sequence (see our posts on <a href="http://www.as3dp.com/2010/05/11/actionscript-3-0-skip-lists-1-the-quickest-route-home/">Skip Lists</a>), but for now we&#8217;ll stick with the traditional CoR and use a sequential search through the list of handlers.</p>
<p><strong>The Chain of Mobile Operating Systems</strong></p>
<p>This CoR simulates finding one of several types of mobile OS. Given the type of OS, it looks for ways to handle each one optimally. In the JavaScript version of this, we want to have it select a CSS file or another JS file to best use the kind of mobile device involved.  The concrete handler classes are the objects used to deal with whatever requirements are necessary. In the example, each handler simply traces out &#8220;Set up for xxOS,&#8221;  where &#8220;xx&#8221; is the found mobile OS. It also traces out which handlers were rejected so that you can better see how the chain works. Since Android is at the top of the chain, you see no rejections, while Windows CE at the bottom of the chain displays the whole chain.</p>
<p>It might be helpful if you take a look at the whole concept of <a href="http://www.as3dp.com/2010/05/07/actionscript-3-0-linked-lists-the-road-to-skip-lists/">Linked Lists</a>. The Chain of Responsibility is something like a linked list, but instead of linking lists, it links handlers.</p>
<p>The Client class makes the initial request and sets up a simple requesting UI for testing the application. It makes requests using a string. I just used the lowercase ID of the main mobile OS types as the &#8220;request-to-handle.&#8221;  After making the request, the &#8220;chain&#8221; takes over. In this example the top of the chain is the Android, but it could be any of the others as well. The developer has complete control over the sequence.</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('p6348code24'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p634824"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
</pre></td><td class="code" id="p6348code24"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">Button</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #006600;">TextInput</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #006600;">Label</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldAutoSize</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: #808080; font-style: italic;">//Handlers + Request</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> android:Handler= <span style="color: #000000; font-weight: bold;">new</span> Android<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> iphone:Handler= <span style="color: #000000; font-weight: bold;">new</span> IPhone<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> blackberry:Handler= <span style="color: #000000; font-weight: bold;">new</span> Blackberry<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> series60:Handler=<span style="color: #000000; font-weight: bold;">new</span> Series60<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> windowsce:Handler=<span style="color: #000000; font-weight: bold;">new</span> WindowsCE<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> cannothandle:Handler=<span style="color: #000000; font-weight: bold;">new</span> CannotHandle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">//UI</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btn:<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> iput:TextInput=<span style="color: #000000; font-weight: bold;">new</span> TextInput<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> lbl:Label=<span style="color: #000000; font-weight: bold;">new</span> Label<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>
			setupChain<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			setupUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setupChain<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//Sequence set up here</span>
			android.<span style="color: #006600;">SetSuccessor</span><span style="color: #66cc66;">&#40;</span>iphone<span style="color: #66cc66;">&#41;</span>;
			iphone.<span style="color: #006600;">SetSuccessor</span><span style="color: #66cc66;">&#40;</span>blackberry<span style="color: #66cc66;">&#41;</span>;
			blackberry.<span style="color: #006600;">SetSuccessor</span><span style="color: #66cc66;">&#40;</span>series60<span style="color: #66cc66;">&#41;</span>;
			series60.<span style="color: #006600;">SetSuccessor</span><span style="color: #66cc66;">&#40;</span>windowsce<span style="color: #66cc66;">&#41;</span>;
			windowsce.<span style="color: #006600;">SetSuccessor</span><span style="color: #66cc66;">&#40;</span>cannothandle<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> setupUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//This simulates getting the mobile OS from the using system</span>
			lbl.<span style="color: #0066CC;">autoSize</span> = TextFieldAutoSize.<span style="color: #0066CC;">LEFT</span>;
			lbl.<span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;Enter name of mobile device: (Use lower case.)&quot;</span>;
			lbl.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">50</span>,lbl.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">30</span>;
			addChild<span style="color: #66cc66;">&#40;</span>lbl<span style="color: #66cc66;">&#41;</span>;
			btn.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">50</span>, btn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">75</span>;
			btn.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Start the chain&quot;</span>;
			btn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,requestOS<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btn<span style="color: #66cc66;">&#41;</span>;
			iput.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">50</span>,iput.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">50</span>;
			addChild<span style="color: #66cc66;">&#40;</span>iput<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> requestOS<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>
			android.<span style="color: #006600;">HandleRequest</span><span style="color: #66cc66;">&#40;</span>iput.<span style="color: #0066CC;">text</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>For the end of the chain, I added a &#8220;<strong>CannotHandle</strong>&#8221; class that extends the <strong>Handler</strong> interface. This functions something like a <strong>default</strong> option in a <strong>switch</strong> statement. If none of the handlers can handle the request, you need to provide the user with some kind of feedback.<br />
<span id="more-6348"></span><br />
<strong>The Abstract and Concrete Handlers</strong></p>
<p>In this application the interface is an abstract class, named <strong>Handler</strong>.  The abstract method, <strong>HandleRequest()</strong> takes a string argument and checks if it&#8217;s the appropriate class to handle the request, and if not, passes it on to the successor. The last concrete <strong>Handler</strong> object is always the <strong>CannotHandle</strong> class.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6348code25'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p634825"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code" id="p6348code25"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Abstract class</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Handler
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> successor:Handler;
		protected <span style="color: #000000; font-weight: bold;">var</span> mobileType:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SetSuccessor <span style="color: #66cc66;">&#40;</span>successor:Handler<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">successor</span>=successor;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> HandleRequest <span style="color: #66cc66;">&#40;</span>req:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//Provide details in subclasses</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>With ActionScript 3.0, you have to make up abstract classes by including at least one method that you want to be abstract and must be overridden when implemented. In this case the <strong>HandleRequest(reg:String)</strong> method expects to be overridden. On the other hand, the <strong>SetSuccessor</strong> method that expects a concrete handler as an argument is inherited all set to use. Just add an argument. In deciding where to implement the <strong>SetSuccessor</strong> method, I decided to use the Client. In this way the developer can clearly see the order of the chain. Further, if changes are made, the <strong>CannotHandle</strong> class can easily be moved to the end of the chain without having to change the sequencing within the concrete handler classes.</p>
<p>The two handler properties, <strong>successor</strong> and <strong>mobileType</strong> are used to identify the successor class (object) and store the name of the operating system, respectively. All that&#8217;s left is to provide the concrete implementations which involves two steps:</p>
<ol>
<li>Overidding the HandleRequest() method and adding whatever you want the class to do if it&#8217;s to handle the request.</li>
<li>Sending the search off to the successor if it cannot handle the request.</li>
</ol>
<p>That&#8217;s not exactly rocket science. This is a very easy design pattern to implement. I added a private  method to each class, <strong>sendBack()</strong> that represents a more complex handling of any request outside of the conditional statement that identifies the handler as the appropriate one for the request. The more differentiated and complex the handlers, the more that you can appreciate the CoR. You can add anything you need to handle a request without having to worry about the requester (Client) or the other ways available to handle a request. Further, when you add or change a handler to the chain, it will not disrupt the rest of the program.</p>
<p>The following six classes represent concrete handlers. All of them are almost identical other than having different mobile OS&#8217;s to sort out. The CannotHandle concrete handler is just a caboose the both mark the end of the chain and let the user know that his request cannot be handled. It has no successor and the <strong>sendBack()</strong> function tells the user that her request cannot be handled.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6348code26'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p634826"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
</pre></td><td class="code" id="p6348code26"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Android</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Android <span style="color: #0066CC;">extends</span> Handler
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Concrete handler</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> HandleRequest<span style="color: #66cc66;">&#40;</span>req:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>req ==<span style="color: #ff0000;">&quot;android&quot;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				mobileType = <span style="color: #ff0000;">&quot;Now setting up &quot;</span> + req + <span style="color: #ff0000;">&quot; environment.&quot;</span>;
				sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>successor <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</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;Not Android&quot;</span><span style="color: #66cc66;">&#41;</span>;
				successor.<span style="color: #006600;">HandleRequest</span><span style="color: #66cc66;">&#40;</span>req<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mobileType<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;">//iPhone</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> IPhone <span style="color: #0066CC;">extends</span> Handler
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Concrete handler</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> HandleRequest<span style="color: #66cc66;">&#40;</span>req:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>req ==<span style="color: #ff0000;">&quot;iphone&quot;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				mobileType = <span style="color: #ff0000;">&quot;Now setting up &quot;</span> + req + <span style="color: #ff0000;">&quot; environment.&quot;</span>;
				sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>successor <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</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;Not iPhone&quot;</span><span style="color: #66cc66;">&#41;</span>;
				successor.<span style="color: #006600;">HandleRequest</span><span style="color: #66cc66;">&#40;</span>req<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mobileType<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;">//BlackBerry</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Blackberry <span style="color: #0066CC;">extends</span> Handler
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Concrete handler</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> HandleRequest<span style="color: #66cc66;">&#40;</span>req:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>req ==<span style="color: #ff0000;">&quot;blackberry&quot;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				mobileType = <span style="color: #ff0000;">&quot;Now setting up &quot;</span> + req + <span style="color: #ff0000;">&quot; environment.&quot;</span>;
				sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>successor <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</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;Not Blackberry&quot;</span><span style="color: #66cc66;">&#41;</span>;
				successor.<span style="color: #006600;">HandleRequest</span><span style="color: #66cc66;">&#40;</span>req<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mobileType<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;">//Series60</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Series60 <span style="color: #0066CC;">extends</span> Handler
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Concrete handler</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> HandleRequest<span style="color: #66cc66;">&#40;</span>req:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>req ==<span style="color: #ff0000;">&quot;series60&quot;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				mobileType = <span style="color: #ff0000;">&quot;Now setting up &quot;</span> + req + <span style="color: #ff0000;">&quot; environment.&quot;</span>;
				sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>successor <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</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;Not Series60&quot;</span><span style="color: #66cc66;">&#41;</span>;
				successor.<span style="color: #006600;">HandleRequest</span><span style="color: #66cc66;">&#40;</span>req<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mobileType<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;">//Windows CE</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> WindowsCE <span style="color: #0066CC;">extends</span> Handler
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Concrete handler</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> HandleRequest<span style="color: #66cc66;">&#40;</span>req:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>req ==<span style="color: #ff0000;">&quot;windowsce&quot;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				mobileType = <span style="color: #ff0000;">&quot;Now setting up &quot;</span> + req + <span style="color: #ff0000;">&quot; environment.&quot;</span>;
				sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>successor <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</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;Not WindowsCE&quot;</span><span style="color: #66cc66;">&#41;</span>;
				successor.<span style="color: #006600;">HandleRequest</span><span style="color: #66cc66;">&#40;</span>req<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mobileType<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;">//CannotHandle</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CannotHandle <span style="color: #0066CC;">extends</span> Handler
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Concrete handler</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> HandleRequest<span style="color: #66cc66;">&#40;</span>req:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			mobileType = <span style="color: #ff0000;">&quot;Rats! We looked and couldn't find that mobile OS. <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
			mobileType += <span style="color: #ff0000;">&quot;We'll just have to get to work and see if we can <span style="color: #000099; font-weight: bold;">\n</span>develop a way to handle it for you.&quot;</span>;
			sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mobileType<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>Testing the Chain</strong></p>
<p><div id="attachment_6382" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/12/ui.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2: &lt;/strong&gt; UI provides a way to test the chain.&lt;/em&gt;" title="ui" width="250" height="100" class="size-full wp-image-6382" /><p class="wp-caption-text"><em><strong>Figure 2: </strong> UI provides a way to test the chain.</em></p></div>When all is said and done, this pattern is pretty simple. Essentially the Client makes a request through the Handler interface and one of the concrete handlers deals with the request. If it cannot deal with it, it sends it along the chain until one of the handlers can or report that it cannot handle the request. The UI in this example (see Figure 2) simulates a JavaScript method for finding the current user&#8217;s mobile OS.</p>
<p>If an OS near the end of the chain is selected, you can see all of the links in the chain used to find the correct handler. Figure 3 shows what occurs if &#8220;windowsce&#8221; is requested. Had &#8220;android&#8221; been the search OS, it would have popped up without any links in the chain showing it was not found.<br />
<div id="attachment_6383" class="wp-caption alignnone" style="width: 443px"><img src="http://www.as3dp.com/wp-content/uploads/2011/12/output.png" alt="&lt;em&gt;&lt;strong&gt;Figure 3: &lt;/strong&gt; Output showing steps in sequence and feedback.&lt;/em&gt;" title="output" width="433" height="151" class="size-full wp-image-6383" /><p class="wp-caption-text"><em><strong>Figure 3: </strong> Output showing steps in sequence and feedback.</em></p></div></p>
<p>Finally, if the request cannot be handled, an important development step is to add a handler that tells the user that his OS cannot be found. The final handler in a chain has no successor, and <em>you definitely <strong>do not</strong> want to link back to the beginning of the chain!</em> That would set up an infinite loop. Figure 4 shows the feedback when a request cannot be handled:<div id="attachment_6386" class="wp-caption alignnone" style="width: 505px"><img src="http://www.as3dp.com/wp-content/uploads/2011/12/cannot.png" alt="&lt;em&gt;&lt;strong&gt;Figure 4: &lt;/strong&gt; Feedback for an unhandled request.&lt;/em&gt;" title="cannot" width="495" height="193" class="size-full wp-image-6386" /><p class="wp-caption-text"><em><strong>Figure 4: </strong> Feedback for an unhandled request.</em></p></div></p>
<p><strong>JavaScript is the Next Link</strong></p>
<p>In Part II, I&#8217;d like to step through these same classes using JavaScript prototype classes and see if we can use the same pattern. I think that if each concrete handler can configure the screen for a particular mobile device, it would be quite useful. As new devices are added, such as iPad, Kindle Fire, and various other tablets, you will find that the flexibility afforded by the Chain of Responsibility. To get ready for Part II, go over the ActionScript 3.0 version and make sure everything makes sense. Send in comments if you have a question.</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%2F12%2Ffrom-actionscript-3-0-to-javascript-chain-of-responsibility-part-i%2F&amp;title=From%20ActionScript%203.0%20to%20JavaScript%20Chain%20of%20Responsibility%3A%20Part%20I" id="wpa2a_10"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=MqgHyLce6N4:dcWYHh7vJtE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=MqgHyLce6N4:dcWYHh7vJtE:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=MqgHyLce6N4:dcWYHh7vJtE:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=MqgHyLce6N4:dcWYHh7vJtE:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=MqgHyLce6N4:dcWYHh7vJtE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=MqgHyLce6N4:dcWYHh7vJtE:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/as3dp/gQKb/~4/MqgHyLce6N4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/12/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-i/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.as3dp.com/2011/12/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-i/</feedburner:origLink></item>
		<item>
		<title>Can JavaScript Create Design Patterns?</title>
		<link>http://feedproxy.google.com/~r/as3dp/gQKb/~3/lLk_5XSgp3I/</link>
		<comments>http://www.as3dp.com/2011/11/can-javascript-create-design-patterns/#comments</comments>
		<pubDate>Mon, 28 Nov 2011 12:48:29 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Chain of Responsibility]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6322</guid>
		<description><![CDATA[Ever since Adobe announced that they&#8217;d no longer be making a Flash player for mobile devices, I thought that it&#8217;d be a good idea to have some kind of filter to distinguish between desktop browsers and mobile ones. On this blog, I&#8217;d like to introduce a topic that is related to the continuing efforts to [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6321" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/11/badDecision.jpg" alt="Face the Facts: We&#039;ve got to deal with Mobile" title="badDecision" width="250" height="185" class="size-full wp-image-6321" /><p class="wp-caption-text">Face the Facts: We've got to deal with Mobile</p></div>Ever since Adobe announced that they&#8217;d no longer be making a Flash player for mobile devices, I thought that it&#8217;d be a good idea to have some kind of filter to distinguish between desktop browsers and mobile ones. On this blog, I&#8217;d like to introduce a topic that is related to the continuing efforts to build apps and Web pages with ActionScript 3.0 using OOP and Design Patterns. The topic is a language: JavaScript.</p>
<p>At this point in time I can easily create a script to determine whether a browser is mobile or non-mobile. I want to get to the point where I can put together a Chain of Responsibility pattern so that I have a flexible design for tweaking  a specific kind of mobile device (e.g., calling up appropriate CSS). This requires OOP JavaScript, and so I had to revisit my old JavaScript work. One of the best books on both OOP and JavaScript is <em>Javascript Objects: Object Use and Data Manipulation with JavaScript</em> by Alenander Nakhimovsky and Tom Myers published by Wrox in 1998. It was written by a couple of professors at Colgate University and  is pitched at a pretty high level that most readers of this blog will appreciate. (You can still get it from Amazon, and it&#8217;s a gem to have no matter what language you favor.) Another good book that deals with JavaScript objects is Douglas Crockford&#8217;s <em>JavaScript: The Good Parts</em> (O&#8217;Reilly, 2008). Crockford understands JavaScript down to the metal and has great explanations and examples.</p>
<p>So the question is:</p>
<blockquote><p>Can JavaScript handle OOP and Design Patterns?</p></blockquote>
<p>To find out, I started writing some prototype classes but found I&#8217;d have to spend some more time trying to cobble something together that was both reasonable (it maintains the DP value of flexibility, update and reuse) and real (I didn&#8217;t want to make a phony DP with hack classes.) However, it didn&#8217;t take long to put together a little code that works to filter out both different mobile devices and non-mobile devices. My plan is to take this code and change it so that I can do the same thing with a <strong>Chain of Responsibility</strong> design pattern. Here&#8217;s the code:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6322code28'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p632228"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code" id="p6322code28"><pre class="javascript" style="font-family:monospace;">// JavaScript Document
var sniffer=new Object();
var agentNow=navigator.userAgent.toLowerCase();
sniffer.android=(agentNow.search(&quot;android&quot;)&gt;=0);
sniffer.series60=(agentNow.search(&quot;series60&quot;)&gt;=0);
sniffer.iphone=(agentNow.search(&quot;iphone&quot;)&gt;=0);
sniffer.blackberry=(agentNow.search(&quot;blackberry&quot;)&gt;=0);
sniffer.windowsce=(agentNow.search(&quot;windows ce&quot;)&gt;=0);
var counter=0;
var flag=new Object();
flag.nonMobile=true;
for(var mobile in sniffer)
{
	if(sniffer[mobile])
	{
		switch(counter) {
		case 0:
		document.write(&quot;android&lt;br/&gt;&quot;);
		flag.nonMobile=false;
		break;
		case 1:
		document.write(&quot;series60&lt;br/&gt;&quot;);
		flag.nonMobile=false;
		break;
		case 2:
		document.write(&quot;iphone&lt;br/&gt;&quot;);
		flag.nonMobile=false;
		break;
		case 3:
		document.write(&quot;blackberry&lt;br/&gt;&quot;);
		flag.nonMobile=false;
		break;
		case 4:
		document.write(&quot;windows ce&lt;br/&gt;&quot;);
		flag.nonMobile=false;
		break;
		}
	}
	counter++;
}
if(flag.nonMobile)
{
		document.write(&quot;Non-mobile or unrecognized device.&quot;);
		document.write(&quot;&lt;br/&gt;&quot;);
}</pre></td></tr></table></div>

<p>Before you say anything (or think anything), I realize that this code is about as flexible as a rebar (AKA: reinforcing bar), and if you&#8217;re into JavaScript, you can think of 10 ways that this can be done better. However, the goal is to see if this can be  refactored into a design pattern. Here&#8217;s my plan:</p>
<ol>
<li>Set up a dummy Chain of Responsibility that links the different kinds of responsibilities.</li>
<li>Have each responsibility be actions taken for different kinds of mobile devices or a desktop.</li>
<li>Use JavaScript objects to do the work of the concrete handlers</li>
</ol>
<p>However, before I get started on this project, I&#8217;d really like to hear from you and even better, see what you can do with this challenge. So between now and the next post on this topic, let&#8217;s hear from you!  Also feel free to add different kinds of mobile devices such as tablets and other phone OSs.</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%2F11%2Fcan-javascript-create-design-patterns%2F&amp;title=Can%20JavaScript%20Create%20Design%20Patterns%3F" 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><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=lLk_5XSgp3I:hPi-MvS0cUU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=lLk_5XSgp3I:hPi-MvS0cUU:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=lLk_5XSgp3I:hPi-MvS0cUU:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=lLk_5XSgp3I:hPi-MvS0cUU:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=lLk_5XSgp3I:hPi-MvS0cUU:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=lLk_5XSgp3I:hPi-MvS0cUU:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/as3dp/gQKb/~4/lLk_5XSgp3I" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/11/can-javascript-create-design-patterns/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://www.as3dp.com/2011/11/can-javascript-create-design-patterns/</feedburner:origLink></item>
		<item>
		<title>ActionScript Design Patterns Transcend Changes at Adobe</title>
		<link>http://feedproxy.google.com/~r/as3dp/gQKb/~3/kFrIBAtY8cQ/</link>
		<comments>http://www.as3dp.com/2011/11/actionscript-design-patterns-transcend-changes-at-adobe/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 20:15:05 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[Mobile]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6299</guid>
		<description><![CDATA[A lot of you may have heard the announcement from Adobe about dropping support for mobile browsers. It&#8217;s been a year and a half since we posted the article about the spat between Adobe and Apple, and now again, an announcement from Adobe has shaken things up. According to The Economist (Oct 8-14, 2011), in [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6301" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/11/ShiftAdobe.png" alt="If you think everyone is heading for HTML5 now, you&#039;re nuts!" title="ShiftAdobe" width="250" height="274" class="size-full wp-image-6301" /><p class="wp-caption-text">If you think everyone is heading for HTML5 now, you're nuts!</p></div>A lot of you may have heard the <a href="http://blogs.adobe.com/conversations/2011/11/flash-focus.html">announcement from Adobe</a> about dropping support for mobile browsers. It&#8217;s been a year and a half since we posted the article about the <a href="http://www.as3dp.com/2010/04/29/actionscript-3-0-developers-caught-in-the-middle/">spat between Adobe and Apple</a>, and now again, an announcement from Adobe has shaken things up.</p>
<p>According to <em>The Economist</em> (Oct 8-14, 2011), in 1993 there were 100 million PCs. By 2008, PCs had topped 1 billion. Many of us tapped into that market one way or another, and some of us came out well rewarded—Adobe certainly did. By 2020 (barely 8 years from now), they expect 10 billion mobile connected devices. In looking at the number consider this: The current #1 Free App on iOS devices is called &#8216;Hair Plucker&#8217; and who knows how many people bought &#8216;Angry Birds&#8217;, 2010&#8242;s top paid for app? Among other apps I saw was a &#8216;Halloween Mouth&#8217; selling for 99¢&#8211;it&#8217;s a picture of a mouth you hold up to your face. (How long would that take to create?) Another popular app is not one, <em>but several</em>, that make farting sounds&#8211;with prices ranging from free to $1.99.</p>
<blockquote><p>
<strong>Latest Update on Flex&#8217;s Fate!</strong>: Because so many questions about Flex were asked (e.g. Is this the end of Flex?) Adobe posed the following:</p>
<p><a href="http://blogs.adobe.com/flex/2011/11/your-questions-about-flex.html">Adobe settles concerns about Flex and Flash.</a></p>
</blockquote>
<p>Now let&#8217;s say, that you create a simple app using a Factory Method. You&#8217;ve got 10 billion units out there. We&#8217;ll say that you have a &#8216;Sneezing App&#8217; that makes different sneezing sounds, and it sells for 99¢. If just 1% of 1% of 1% of 1% of 1% of 1% bought your app, that&#8217;s $10,000&#8211;a half million Indian rupees. If 1 person in 10,000 bought that app, you&#8217;d be a millionaire. (The good thing about language-independent apps like the &#8216;Halloween Mouth&#8217; and &#8216;Fart Factory&#8217; is that they are language independent.)</p>
<blockquote><p>20 Billion Mobile Connected Devices Can&#8217;t Be Wrong!</p></blockquote>
<p>I have no idea how many people will be using PCs as we know them. Maybe they&#8217;ll go the way of the 5 1/4 inch floppy and all we&#8217;ll have is some kind of tablet that we can connect up to the Internet. Development may be done with apps that we rent from a cloud. What is important and key now and later are mobile devices. At this point in time very few Web developers are actively developing apps for mobile devices, and tools like AIR for iOS, Android and Blackberry provide us with a key opportunity.</p>
<p>So, you can be certain that Adobe did not bow out of the Flash-Player-in-the-Mobile-Browser market because they thought it was not going to be a huge market. Look for Dreamweaver to be optimized for HTML5 and some HTML5 development tools. <em>The development tool </em>from Adobe for mobile, though, will be Flash Builder and Flash Pro working with AIR.</p>
<p><strong>A Browser/AIR Mobile Strategy</strong></p>
<p>Keeping in mind that I have nothing against HTML5 and it&#8217;s ability to do many things Flash has done for years—Canvas features some great elements and attributes—let&#8217;s look at a new strategy for development in Flash (including AS3 and the different flavors of AIR).</p>
<p>First off, if you&#8217;ve done much with with mobile browsers, you must have learned they have little to offer, and they are a mixed bag of HTML5 implementation. (The non-mobile browsers are an equal mixed bag as far as having implemented HTML5.) It&#8217;s <strong>only</strong> the <em>mobile browsers</em> that Adobe is no longer supporting. Not too many years ago, there were no mobile browsers; so basically, Adobe is doing what it has always done as far as providing a Flash plugin for the browsers on our computers. It&#8217;s just not going to make them for mobile browsers. I developed several apps for iOS, and while most have been simple and small, I was able to use the same programming structures I did for &#8220;regular&#8221; AS3.</p>
<p>Second, Adobe has been making big strides with Flash Builder and Flash Professional in creating AIR tools for working with mobile devices. This allows us to create ActionScirpt 3.0 apps for mobile devices—forget Web pages. Imagine knocking together an app that does something useful (like calculating the best point in relationship to a parabolic mirror to place a kettle to boil water) that can be used worldwide. Or even a game using some of the little structures we&#8217;ve been developing and discussing on this blog that you can sell for 99¢ (or more!). So quit thinking of yourself as just a &#8220;Web site developer/designer.&#8221; <em>You&#8217;re an app developer/designer</em>.</p>
<p><strong>Everyone Runs to HTML5! Not!</strong></p>
<p>Earlier this year I wrote a book (<em>Smashing HTML5</em>) published by Wiley, and one of the chores I encountered was testing the examples against several different browsers, including mobile ones. There was not a lot of compatibility, and most mobile browsers  were successful with only a small subset of the HTML5 elements. Over time, let&#8217;s hope this improves because if it doesn&#8217;t we&#8217;ll enter the Second Dark Age of incompatible browsers.</p>
<p>In order to get most of the cooler stuff to work, you need either JavaScript or PHP, and so I included chapters on both. (PHP seems to be more compatible for all browsers than anything else&#8211;probably because of the simpler subset of HTML it generates.) I added as much OOP to both as possible, and some of the reviewers didn&#8217;t like the fact that it was not more oriented to the DOM or that I didn&#8217;t use more procedural examples in the PHP/JavaScript section. What I was trying to do was to write an HTML5 book for 2011 and not 2001, which meant that I used several examples of dealing with a mobile environment. Further, some OOP in JavaScript/PHP is a good thing; so I hope that will move some folks towards structure in both JavaScript and PHP.</p>
<p>However, without Flash in the mix, creating Web apps and pages is frustrating, not because JavaScript and PHP aren&#8217;t good programming tools but rather because of browser incompatibility.  I&#8217;ve got a feeling that to effectively use HTML5 and all of the related CSS3, developers are going to be spending a lot of JavaScript code determining which browser is being used and making the appropriate accommodations. Who wants to sub-code for every possible browser? This includes mobile browsers.  My hunch is that they&#8217;ll all head for the lowest common denominator, which is probably one of the mobile browsers that is just slightly better than HTML4.</p>
<p>So, I&#8217;ve started thinking &#8220;Build an App; Not a Web Page.&#8221; Let&#8217;s hear your thoughts on Adobe&#8217;s decisions.</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%2F11%2Factionscript-design-patterns-transcend-changes-at-adobe%2F&amp;title=ActionScript%20Design%20Patterns%20Transcend%20Changes%20at%20Adobe" 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><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=kFrIBAtY8cQ:G0Nb6gafM8o:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=kFrIBAtY8cQ:G0Nb6gafM8o:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=kFrIBAtY8cQ:G0Nb6gafM8o:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=kFrIBAtY8cQ:G0Nb6gafM8o:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=kFrIBAtY8cQ:G0Nb6gafM8o:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=kFrIBAtY8cQ:G0Nb6gafM8o:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/as3dp/gQKb/~4/kFrIBAtY8cQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/11/actionscript-design-patterns-transcend-changes-at-adobe/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		<feedburner:origLink>http://www.as3dp.com/2011/11/actionscript-design-patterns-transcend-changes-at-adobe/</feedburner:origLink></item>
		<item>
		<title>Speed Tweaks: Reuse Objects</title>
		<link>http://feedproxy.google.com/~r/as3dp/gQKb/~3/YOZgADtsy-4/</link>
		<comments>http://www.as3dp.com/2011/10/speed-tweaks-reuse-objects/#comments</comments>
		<pubDate>Sun, 23 Oct 2011 19:17:28 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Speed Tweaks]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6284</guid>
		<description><![CDATA[Green Code When I recycle, I&#8217;m doing green. The same idea for functions is equally helpful in avoiding garbage collection. Instead of writing a separate function, reuse one you&#8217;ve got. I got the idea from an article by Scott Pertersen, but while working on this post I realized that Scott was talking about something else. [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6391" class="wp-caption alignleft" style="width: 260px"><a href="http://www.as3dp.com/wp-content/uploads/2011/10/speedtweak.png"><img src="http://www.as3dp.com/wp-content/uploads/2011/10/speedtweak.png" alt="speedy" title="speedtweak" width="250" height="270" class="size-full wp-image-6391" /></a><p class="wp-caption-text">Reuse Static Objects to Reduce Garbage Collection</p></div><strong>Green Code</strong></p>
<p>When I recycle, I&#8217;m <em>doing green</em>. The same idea for functions is equally helpful in avoiding garbage collection.  Instead of writing a separate function, reuse one you&#8217;ve got. I got the idea from an article by <a href="http://www.adobe.com/devnet/flash/articles/optimize_content_ios.html">Scott Pertersen</a>, but while working on this post I realized that Scott was talking about something else. He noted that using function objects instead of events cuts down on program allocations. Following that thought, he suggests listening for an event just once. One event handler deals with the event for multiple clients. (Sounds like an Observer design pattern—always a good idea.)</p>
<p>Taking these two thoughts together, I made a single pair of event handlers for multiple objects. That wasn&#8217;t Scott&#8217;s meaning. He&#8217;s pointing out that the fewer events the better; not lots of events handled by fewer functions. However, creating a static method that handles any number of different static properties seemed to be a good idea because both the static method and properties were re-used. (Some other time, I&#8217;ll look into how to listen for an event once. It makes sense to do so.)</p>
<p><strong>Static Drag</strong></p>
<p>For now, I&#8217;d like to take a look at reusing two event handlers—start and stop drag with mouse down/up. I realize that this doesn&#8217;t use any kind of special mobile ActionScript 3.0 (AIR statements to be used in conjunction with mobile devices), and we&#8217;ll get to that in further posts. For now, I just want to focus on writing a single pair of static methods for event targets. Using the drag states is simple and explains the idea. (You can download the files if you wish—click the download button.)<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/dp/loadonce/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignleft size-full wp-image-1257" /></a><a href="http://www.mwd.hartford.edu/mwd11/downloads/MWDdownloads.php"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a><br />
To keep things clean, I created a class that makes shapes that can be used by a direct call. Then, I used a Client class to call the shapes (in Sprite objects) for dragging around a mobile stage. Note that it is flush with static 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('p6284code31'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p628431"><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
</pre></td><td class="code" id="p6284code31"><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> ShapeSelect <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> ball:Sprite = <span style="color: #000000; font-weight: bold;">new</span> Sprite  ;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> box:Sprite = <span style="color: #000000; font-weight: bold;">new</span> Sprite  ;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> triangle:Sprite = <span style="color: #000000; font-weight: bold;">new</span> Sprite  ;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doBall<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> makeBall<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doBox<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> makeBox<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doTriangle<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> makeTriangle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//Color: B9121B,4C1B1B,F6E497,FCFAE1,BD8D46</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> makeBall<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			ball.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xB9121B<span style="color: #66cc66;">&#41;</span>;
			ball.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawCircle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span>,<span style="color: #cc66cc;">50</span>,<span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> ball;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> makeBox<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			box.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xBD8D46,.8<span style="color: #66cc66;">&#41;</span>;
			box.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">80</span>,<span style="color: #cc66cc;">80</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> box;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> makeTriangle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			triangle.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x4C1B1B,.7<span style="color: #66cc66;">&#41;</span>;
			triangle.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawTriangles</span><span style="color: #66cc66;">&#40;</span>Vector.<span style="color: #66cc66;">&lt;</span>number <span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">90</span>,<span style="color: #cc66cc;">150</span>,<span style="color: #cc66cc;">90</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			triangle.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> triangle;
		<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 public functions return a ball, box or triangle Sprite pulled from three private static functions. No great shakes, but it&#8217;s clean and clear. All of the static functions mean that the objects are built in compile-time.</p>
<p>Next, they&#8217;re called from a Client class. The MOUSE_DOWN and MOUSE_UP events are called and all objects use the same start/stop drag functions.  This reduces the number of methods required, but it is not doing anything for reducing the resources used in calling an event. That I&#8217;d like to take up in a future post.</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('p6284code32'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p628432"><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="p6284code32"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> ball: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: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> box: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: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> triangle: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: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> shapeSelect:ShapeSelect=<span style="color: #000000; font-weight: bold;">new</span> ShapeSelect<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>
			ball = shapeSelect.<span style="color: #006600;">doBall</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			ball.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">50</span>,ball.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">20</span>;
			addChild<span style="color: #66cc66;">&#40;</span>ball<span style="color: #66cc66;">&#41;</span>;
			ball.<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>;
			ball.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_UP</span>,noDrag<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			box = shapeSelect.<span style="color: #006600;">doBox</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			box.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">70</span>,box.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">80</span>;
			addChild<span style="color: #66cc66;">&#40;</span>box<span style="color: #66cc66;">&#41;</span>;
			box.<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>;
			box.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_UP</span>,noDrag<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			triangle = shapeSelect.<span style="color: #006600;">doTriangle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			triangle.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">100</span>,triangle.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">120</span>;
			addChild<span style="color: #66cc66;">&#40;</span>triangle<span style="color: #66cc66;">&#41;</span>;
			triangle.<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>;
			triangle.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">MOUSE_UP</span>,noDrag<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//All of the event calls use the same two static functions</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> doDrag<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</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>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> noDrag<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>
			<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>I tried this out using iOS AIR and when I tried it out in my iPhone, it worked quite smoothly.</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%2F10%2Fspeed-tweaks-reuse-objects%2F&amp;title=Speed%20Tweaks%3A%20Reuse%20Objects" 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><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=YOZgADtsy-4:vV7Eld_SnnU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=YOZgADtsy-4:vV7Eld_SnnU:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=YOZgADtsy-4:vV7Eld_SnnU:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=YOZgADtsy-4:vV7Eld_SnnU:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=YOZgADtsy-4:vV7Eld_SnnU:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=YOZgADtsy-4:vV7Eld_SnnU:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/as3dp/gQKb/~4/YOZgADtsy-4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/10/speed-tweaks-reuse-objects/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.as3dp.com/2011/10/speed-tweaks-reuse-objects/</feedburner:origLink></item>
		<item>
		<title>Speed Tweaks: Static Objects and Methods</title>
		<link>http://feedproxy.google.com/~r/as3dp/gQKb/~3/2VaUDCkDyn8/</link>
		<comments>http://www.as3dp.com/2011/10/speed-tweaks-static-objects-and-methods/#comments</comments>
		<pubDate>Sun, 23 Oct 2011 18:14:16 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Speed Tweaks]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6270</guid>
		<description><![CDATA[Time to Look at the Details When I first started writing ActionScript 3.0 for mobile devices, I wrote it pretty much like I did for desktop or laptop computers. One of the early programs included a simple tween that would move a logo from the middle of the page to the top left corner revealing [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6269" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/10/slowMob.png" alt="Add ActionScript 3.0 Speed Tweaks to Mobile Apps" title="slowMob" width="250" height="480" class="size-full wp-image-6269" /><p class="wp-caption-text">Add ActionScript 3.0 Speed Tweaks to Mobile Apps</p></div><strong>Time to Look at the Details</strong></p>
<p>When I first started writing ActionScript 3.0 for mobile devices, I wrote it pretty much like I did for desktop or laptop computers. One of the early programs included a simple tween that would move a logo from the middle of the page to the top left corner revealing a center-page main image. Instead of getting a spiffy movement to get the viewer&#8217;s attention, on my mobile phone I got a slow-as-mud tween that seemed to get stuck in the middle. (The image to the left  shows the logo taking its sweet time moving from the middle to the top of an iPhone.)</p>
<p>Both on this blog and in our book, the focus has been on design patterns and related topics—loose binding, reusability and class/object relationships. The issue of speed has been relegated to the back seat with only occasional references to optimizing speed in programs. The Flyweight, skip-list and a few other posts are among those exceptions. Now, we&#8217;re going to look at speed tweaks (and invite you to share your own.) Our first is using <strong>static</strong> properties and methods.</p>
<p><strong>What&#8217;s Up with Static?</strong></p>
<p>The idea of a static property or function is usually one of immutability—unchangeable. Isn&#8217;t that that same thing as a <strong>constant</strong>? In some respects, yes, but it&#8217;s more than that. Besides, why is a static object faster? After all, that&#8217;s the important consideration for a speed tweak.</p>
<p>At the base of a static object is what is called <em>static memory allocation.</em> Basically, this process occurs during compilation before run-time when the program is executed. Think of it as calling in an order (compile-time) at a take-out restaurant. When you show up at the restaurant, your order is ready. That&#8217;s a lot faster than making the order and then waiting around while they&#8217;re fixing the order at run-time.</p>
<p>Static objects are something like private accessors and something like CONSTANTS. First, static properties are accessible only through a defining class or when returned by arguments. Second, when immutable values are used when a variable is statically defined, it does not have to be re-defined. In our book in both Chapter 3 (Singletons) and Chapter 6 (Composite) you will find several examples of static variables at work. They are especially popular in use with Singletons, so be careful. (For an unbiased discussion of Singletons, <a href="http://www.as3dp.com/category/design-patterns/singleton/">see these posts</a>.)</p>
<p><strong>Static Easy</strong></p>
<p>Static properties are not all that difficult to create. In the following listing, which I created using an Android AIR template, you can see a simple constant literal set as a static property and a static method called from a Client class, which also uses static variables:</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('p6270code34'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p627034"><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="p6270code34"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Static property and method</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> StaticObject <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> const AS3DP:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;ActionScript 3.0 Design Patterns&quot;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> StaticObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			showStatic<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> showStatic<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>AS3DP<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;">//Static variable</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">var</span> statObject:StaticObject;
		<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>
			statObject=<span style="color: #000000; font-weight: bold;">new</span> StaticObject<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>I tend to think about static variables in the same way as I do private accessors—but for different purposes. I try and use private accessors for<em> encapsulation</em> and forcing myself to <em>write better OOP</em>. For static properties, I just think<em> more speed</em>. <a href="http://www.adobe.com/devnet/flash/articles/optimize_content_ios.html">Scott Petersen</a> has a good article on these and other issues involving mobile device optimization for iOS, and you should take a look at what he has to say about static objects as well.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2011%2F10%2Fspeed-tweaks-static-objects-and-methods%2F&amp;title=Speed%20Tweaks%3A%20Static%20Objects%20and%20Methods" 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><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=2VaUDCkDyn8:hQBqzpiZ-v0:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=2VaUDCkDyn8:hQBqzpiZ-v0:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=2VaUDCkDyn8:hQBqzpiZ-v0:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=2VaUDCkDyn8:hQBqzpiZ-v0:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=2VaUDCkDyn8:hQBqzpiZ-v0:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=2VaUDCkDyn8:hQBqzpiZ-v0:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/as3dp/gQKb/~4/2VaUDCkDyn8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/10/speed-tweaks-static-objects-and-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.as3dp.com/2011/10/speed-tweaks-static-objects-and-methods/</feedburner:origLink></item>
		<item>
		<title>Back to Statements and Algorithms: New Wine in Old Bottles</title>
		<link>http://feedproxy.google.com/~r/as3dp/gQKb/~3/f6F0DPkg6XY/</link>
		<comments>http://www.as3dp.com/2011/10/back-to-statements-and-algorithms-new-wine-in-old-bottles/#comments</comments>
		<pubDate>Sun, 23 Oct 2011 09:08:49 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Mobile]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6257</guid>
		<description><![CDATA[Chandima and I have exhausted the set of design patterns that the Gang of Four published in 1995, and we even started looking at the new design patterns developed for parallel programming in Parallel Programming with Microsoft.NET: Design Patterns for Decomposition and Coordination on Multicore Architectures (2010), whose authors include Ralph Johnson, one of the [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6258" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/10/wine.png" alt="As ActionScript 3.0 grows and changes, we can apply the old design patterns and begin looking for new patters to accommodate some changes." title="wine" width="250" height="307" class="size-full wp-image-6258" /><p class="wp-caption-text">As ActionScript 3.0 grows and changes, we can apply the old design patterns and begin looking for new patters to accommodate some changes.</p></div>Chandima and I have exhausted the set of design patterns that the Gang of Four published in 1995, and we even started looking at the new design patterns developed for parallel programming in <i>Parallel Programming with Microsoft.NET: Design Patterns for Decomposition and Coordination on Multicore Architectures</i> (2010), whose authors include Ralph Johnson, one of the original GoF. The problem with that route (at this time) is that C# has a bunch of statements for parallel programming and ActionScirpt 3.0 does not. There&#8217;s a lot edging its way to the front of the shelf as developers and Adobe work on Flex, Flash and Flash Builder. One technology,<a href="http://labs.adobe.com/technologies/pixelbenderplugin/"> Pixel Bender</a> provides a way to work with both the CPU and GPU. Pixel Bender has been around for a while, but it represents a feature of the future—accessing multiple processors from ActionScript 3.0.</p>
<p><strong>AIR for Androids, iOS and Blackberry</strong></p>
<p>Currently, the focus of most of the posts on this blog have been design patterns using ActionScript 3.0 with an occasional visit to some interesting structures such as dependency injection and skip lists. Now, a lot of the discussion will shift to newer statements and elements that make up the Flex/Flash family of APis and platforms. In particular you will want to take a look at the<a href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/index.html?filter_air=3"> AIR 3 reference guide</a>.  Herein lie the statements, packages and namespaces that make up a lot of the new features we need to consider. Adobe helpfully marks each AIR class with a little red triangles with balls on the points.<img src="http://www.as3dp.com/wp-content/uploads/2011/10/AIR.png" alt="AIR" title="AIR" width="19" height="19" class="alignleft size-full wp-image-6262" /> In creating an AIR app, whether for a mobile device or computer, there are differences depending on the hardware device.  Once we&#8217;ve covered several and created sample examples, we can cast them into design patterns. So, consider this next set of posts to be a temporary detour that will take the knowledge set forward but keep the utility of what has passed.</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%2F10%2Fback-to-statements-and-algorithms-new-wine-in-old-bottles%2F&amp;title=Back%20to%20Statements%20and%20Algorithms%3A%20New%20Wine%20in%20Old%20Bottles" 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><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=f6F0DPkg6XY:y2yG_FfbyxE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=f6F0DPkg6XY:y2yG_FfbyxE:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=f6F0DPkg6XY:y2yG_FfbyxE:D7DqB2pKExk" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=f6F0DPkg6XY:y2yG_FfbyxE:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/as3dp/gQKb?a=f6F0DPkg6XY:y2yG_FfbyxE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/as3dp/gQKb?i=f6F0DPkg6XY:y2yG_FfbyxE:V_sGLiPBpWU" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/as3dp/gQKb/~4/f6F0DPkg6XY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/10/back-to-statements-and-algorithms-new-wine-in-old-bottles/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.as3dp.com/2011/10/back-to-statements-and-algorithms-new-wine-in-old-bottles/</feedburner:origLink></item>
	</channel>
</rss>

