<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ActionScript 3.0 Design Patterns &#187; Design Patterns</title>
	<atom:link href="http://www.as3dp.com/category/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.as3dp.com</link>
	<description>OOP Techniques for Flash and Flex Developers</description>
	<lastBuildDate>Sun, 29 Jan 2012 17:00:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>From ActionScript 3.0 to JavaScript Chain of Responsibility: Part II The Help Desk</title>
		<link>http://www.as3dp.com/2012/01/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-ii-the-help-desk/</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('p6352code6'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p63526"><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="p6352code6"><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('p6352code7'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p63527"><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="p6352code7"><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('p6352code8'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p63528"><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="p6352code8"><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('p6352code9'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p63529"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p6352code9"><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('p6352code10'); return false;">View Code</a> HTML5</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p635210"><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="p6352code10"><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_2"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2012/01/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-ii-the-help-desk/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>From ActionScript 3.0 to JavaScript Chain of Responsibility: Part I</title>
		<link>http://www.as3dp.com/2011/12/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-i/</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('p6348code14'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p634814"><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="p6348code14"><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('p6348code15'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p634815"><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="p6348code15"><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('p6348code16'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p634816"><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="p6348code16"><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_4"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/12/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-i/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Can JavaScript Create Design Patterns?</title>
		<link>http://www.as3dp.com/2011/11/can-javascript-create-design-patterns/</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('p6322code18'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p632218"><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="p6322code18"><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_6"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/11/can-javascript-create-design-patterns/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Back to Statements and Algorithms: New Wine in Old Bottles</title>
		<link>http://www.as3dp.com/2011/10/back-to-statements-and-algorithms-new-wine-in-old-bottles/</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_8"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/10/back-to-statements-and-algorithms-new-wine-in-old-bottles/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Removing Decorations: Here Come the Repo Men</title>
		<link>http://www.as3dp.com/2011/10/removing-decorations-here-come-the-repo-men/</link>
		<comments>http://www.as3dp.com/2011/10/removing-decorations-here-come-the-repo-men/#comments</comments>
		<pubDate>Sun, 02 Oct 2011 18:33:32 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Decorator]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6228</guid>
		<description><![CDATA[We&#8217;ve Come to Take Back Your Kidney The 2010 film, &#8220;Repo Men&#8221; is about a dark future where body parts are purchased much like homes. You get the needed part (like Steve Jobs got his liver) and pay for it with a mortgage. If you fall behind in your payments, the Repo Men come and [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6227" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/10/Striptease.png" alt="Removing Decorations can be Tricky" title="Striptease" width="250" height="325" class="size-full wp-image-6227" /><p class="wp-caption-text">Removing Decorations can be Tricky</p></div><strong>We&#8217;ve Come to Take Back Your Kidney</strong></p>
<p>The 2010 film, &#8220;Repo Men&#8221; is about a dark future where body parts are purchased much like homes. You get the needed part (like Steve Jobs got his liver) and pay for it with a mortgage. If you fall behind in your payments, the Repo Men come and take it away so that it can be sold to another customer. It turns out that removing a decorator from a component in the Decorator Design Pattern is about as draconian. All along I had imagined it about as simple as removing ornaments from a Christmas tree. Apparently others have issued similar claims, but try and find a decent example, and you&#8217;re likely to come up blank. I finally was able to track down <a href="http://forums.oreilly.com/topic/20910-decorator-pattern-removing-decorator-from-the-decorated-object/">an example</a> in O&#8217;Reilly Media&#8217;s <strong>Head First Labs</strong>, and it works; sort of. First, though, consider why adding and removing decorations is worthwhile investigating.</p>
<p><strong>Why Removing Concrete Decorations is Important</strong></p>
<p>Now that I&#8217;ve been programming madly to get ActionScript 3.0 to work with mobile devices (the iPhone in particular), I&#8217;m finding that creating graphic objects is sloooow. The idea behind developing games with patterns like Decorator is that I can take a component and add and remove decorations without having to recreate the object with all of elements that go with it. I can just add and subtract concrete decorators and leave the component as is. This would seem much faster because I&#8217;m instantiating less. (Yes, the Flyweight pattern would be another for adding some speed to the process.) Of course you don&#8217;t have to be developing for mobile devices; it&#8217;s just as handy for computers.</p>
<p><strong>Removing <em>One</em> Decoration Works Fine</strong></p>
<p>I tried out the fix suggested in Head First Labs, and it works dandy. Go ahead and download all the files (they&#8217;re two sets in a single zip file; one for removing a single decorator and another for multiple) and then click the Play button to see how a single removal works:<br />
<a href="http://www.mwd.hartford.edu/mwd11/downloads/MWDdownloads.php" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignleft size-full wp-image-2020" /></a><br />
<a href="http://www.mwd.hartford.edu/mwd11/dp/undecorate/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignnone size-full wp-image-1257" /></a></p>
<blockquote><p>All downloads are now on the University of Hartford&#8217;s MWDD Download Page. Select the last window to the right (ActionScript 3.0/Flash) and click on the bottom link and then click the Download button.</p></blockquote>
<p>donE<br />
The movement to the left and right was to show how the decorated component is a &#8220;single object,&#8221; but even that is a bit of fakery. 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('p6228code25'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p622825"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code" id="p6228code25"><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;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Abstract class. Do not instantiate directly</span>
	<span style="color: #808080; font-style: italic;">//Extend the class for concrete child classes</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Component;
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> armor:Sprite;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getarmor<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> armor;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//remove</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> removeArmor<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Component
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In the Component abstract class, you can see the addition of a single added method, <strong>removeArmor()</strong>. What you may be scratching your head about is why a Component object is returned. That&#8217;s what&#8217;s decorated, no? Shouldn&#8217;t it be zapping a decoration? What the function appears to be doing is stripping the component of all decorations. But let&#8217;s move on to the Decorator, both abstract and concrete:<span id="more-6228"></span></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('p6228code26'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p622826"><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="p6228code26"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Abstract Class Decorator</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Abstract class. Do not instantiate directly</span>
	<span style="color: #808080; font-style: italic;">//Extend the class for concrete child classes</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Decorator <span style="color: #0066CC;">extends</span> Component
	<span style="color: #66cc66;">&#123;</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getarmor<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> armor;
		<span style="color: #66cc66;">&#125;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> char:Component;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Concrete Decorator Class</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #808080; font-style: italic;">//Concrete decorator Shield</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DecShield <span style="color: #0066CC;">extends</span> Decorator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> DecShield<span style="color: #66cc66;">&#40;</span>char:Component<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">char</span> = char;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//Shield is a Sprite class</span>
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> getarmor<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			armor=<span style="color: #000000; font-weight: bold;">new</span> Shield<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			armor.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">75</span>,armor.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">250</span>;
			<span style="color: #b1b100;">return</span> armor;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//remove Shield</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> removeArmor<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Component
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> char;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The <strong>char</strong> variable is a protected Component one inherited from the Decorator abstract class. What is &#8220;removed&#8221; is any decoration, and in this case, it&#8217;s the last decoration. In other words, it just returns the &#8220;naked&#8221; concrete component.</p>
<p>The base component is in the concrete component, Warrior. It&#8217;s just a Sprite stored in the library. (<strong>StickMan()</strong>)</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('p6228code27'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p622827"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p6228code27"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Concrete component</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Concrete component</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Warrior <span style="color: #0066CC;">extends</span> Component
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> warrior:Sprite=<span style="color: #000000; font-weight: bold;">new</span> StickMan<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Warrior<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			armor=warrior;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>So, the trick in this implementation of decoration removal is simply to return a component with no decorations; hence, &#8216;remove&#8217; the decoration.</p>
<p>The Client class takes care of everything by first adding a graphic component, and then providing a single decoration and then removing that decoration.</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('p6228code28'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p622828"><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
</pre></td><td class="code" id="p6228code28"><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: #808080; font-style: italic;">//Client class</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> warrior:Component=<span style="color: #000000; font-weight: bold;">new</span> Warrior<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> addBtn:<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> removeBtn:<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> moveBtn:<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> moveLBtn:<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> warriorSpirit:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> warriorObj:Sprite;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> shieldObj:Sprite;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			warriorObj = warrior.<span style="color: #006600;">getarmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>warriorSpirit<span style="color: #66cc66;">&#41;</span>;
			warriorSpirit.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>warriorObj<span style="color: #66cc66;">&#41;</span>;
			addBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">250</span>,addBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">100</span>;
			addBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Add Shield&quot;</span>;
			addBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,addShield<span style="color: #66cc66;">&#41;</span>;
			removeBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">250</span>,removeBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">140</span>;
			removeBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Remove Shield&quot;</span>;
			removeBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,removeShield<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>addBtn<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>removeBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			moveBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Move Right&quot;</span>;
			moveBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,moveRight<span style="color: #66cc66;">&#41;</span>;
			moveBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">250</span>,moveBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">180</span>;
			addChild<span style="color: #66cc66;">&#40;</span>moveBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			moveLBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Move Left&quot;</span>;
			moveLBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,moveLeft<span style="color: #66cc66;">&#41;</span>;
			moveLBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">360</span>,moveLBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">180</span>;
			addChild<span style="color: #66cc66;">&#40;</span>moveLBtn<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> addShield<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>
			warrior = <span style="color: #000000; font-weight: bold;">new</span> DecShield<span style="color: #66cc66;">&#40;</span>warrior<span style="color: #66cc66;">&#41;</span>;
			shieldObj = warrior.<span style="color: #006600;">getarmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			warriorSpirit.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>shieldObj<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> removeShield<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>
			warrior = warrior.<span style="color: #006600;">removeArmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			warriorSpirit.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span>shieldObj<span style="color: #66cc66;">&#41;</span>;
			warriorSpirit.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>warrior.<span style="color: #006600;">getarmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> moveRight<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>
			warriorSpirit.<span style="color: #006600;">x</span> +=  <span style="color: #cc66cc;">30</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> moveLeft<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>
			warriorSpirit.<span style="color: #006600;">x</span> -=  <span style="color: #cc66cc;">30</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>You will also notice that the buttons that moved the objects to the left and right did so within a single sprite (<strong>warriorSpirit</strong>) where both the component and its decoration were placed as returned Sprite objects. While the decoration is composed in the pattern, it and the concrete component are in no way &#8220;locked&#8221; together as sprites. In order for them to be locked, they had to be placed in a wrapper sprite which was moved across the stage.<br />
<div id="attachment_6239" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/10/TwoDecs.png" alt="Adding more than a single decoration does not do well with removal" title="TwoDecs" width="250" height="388" class="size-full wp-image-6239" /><p class="wp-caption-text">Adding more than a single decoration does not do well with removal</p></div><strong>Problems With Removing Multiple Decorations</strong></p>
<p>In making a game, you want to be able to add or remove decorations at will. You should be able to remove one, two or all without a problem. However, using the only decoration removal method I could find, I was unable to do so. When you try out the following implementation, here&#8217;s what happens: If you add one decoration and then remove it, everything works fine. However, if you add more than a single decoration before removing it, only the last one added will be removed. What appears to happen is that when the remove() method fires, it returns an undecorated component. The current one is replaced, but the previous decoration stays put as an independent entity. That&#8217;s why with only one decoration, everything seems to work ok.</p>
<p>Click the Play button to see what happens:<br />
<a href="http://www.mwd.hartford.edu/mwd11/dp/undecorate2/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignnone size-full wp-image-1257" /></a></p>
<p>The only added class is a DecSword concrete decorator class to call up the Sword class and position it on the stage:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6228code29'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p622829"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code" id="p6228code29"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #808080; font-style: italic;">//Concrete decorator Sword</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DecSword <span style="color: #0066CC;">extends</span> Decorator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> DecSword<span style="color: #66cc66;">&#40;</span>char:Component<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">char</span> = char;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//Shield is a Sprite class</span>
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> getarmor<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			armor=<span style="color: #000000; font-weight: bold;">new</span> Sword<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			armor.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">90</span>, armor.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">180</span>;
			armor.<span style="color: #006600;">rotation</span>=<span style="color: #cc66cc;">20</span>;
			<span style="color: #b1b100;">return</span> armor;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//remove Sword</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> removeArmor<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Component
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> char;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>It&#8217;s identical to <strong>DecShield</strong> except for the different position  instantiates the <strong>Sword</strong> sprite instead of the <strong>Shield</strong>.</p>
<p>In addition to adding a sword decoration, it also changes the way that the sprites are placed on the stage in the <strong>Client</strong> class. It does not have the <strong>spriteWarrior</strong> sprite to acts as a holder for all of the decorations added to the stage. Instead, the concrete component and decoration sprites are placed directly on the stage.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6228code30'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p622830"><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
</pre></td><td class="code" id="p6228code30"><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: #808080; font-style: italic;">//Client class</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> warrior:Component=<span style="color: #000000; font-weight: bold;">new</span> Warrior<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> addBtn:<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> removeBtn:<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> addSwBtn:<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> removeSwBtn:<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> warriorObj:Sprite;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> shieldObj:Sprite;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> swordObj:Sprite;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			warriorObj=warrior.<span style="color: #006600;">getarmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>warriorObj<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			addBtn.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">250</span>,addBtn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">100</span>;
			addBtn.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Add Shield&quot;</span>;
			addBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,addShield<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>addBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			removeBtn.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">250</span>,removeBtn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">140</span>;
			removeBtn.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Remove Shield&quot;</span>;
			removeBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,removeShield<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>removeBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			addSwBtn.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">360</span>,addSwBtn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">100</span>;
			addSwBtn.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Add Sword&quot;</span>;
			addSwBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,addSword<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>addSwBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			removeSwBtn.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">360</span>,removeSwBtn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">140</span>;
			removeSwBtn.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Remove Sword&quot;</span>;
			removeSwBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,removeSword<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>removeSwBtn<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> addShield<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>
			warrior=<span style="color: #000000; font-weight: bold;">new</span> DecShield<span style="color: #66cc66;">&#40;</span>warrior<span style="color: #66cc66;">&#41;</span>;
			shieldObj=warrior.<span style="color: #006600;">getarmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>shieldObj<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> removeShield<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>
			warrior = warrior.<span style="color: #006600;">removeArmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			removeChild<span style="color: #66cc66;">&#40;</span>shieldObj<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>warrior.<span style="color: #006600;">getarmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> addSword<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>
			warrior=<span style="color: #000000; font-weight: bold;">new</span> DecSword<span style="color: #66cc66;">&#40;</span>warrior<span style="color: #66cc66;">&#41;</span>;
			swordObj=warrior.<span style="color: #006600;">getarmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>swordObj<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> removeSword<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>
			warrior = warrior.<span style="color: #006600;">removeArmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			removeChild<span style="color: #66cc66;">&#40;</span>swordObj<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>warrior.<span style="color: #006600;">getarmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>By using <strong>removeChild(obj)</strong>, it would be possible to remove the sprite objects of the shield and sword from the stage, but I do not believe it would remove them as a decoration from the component. Currently, the r<strong>emoveChild()</strong> function targets whichever sprite-object-as-decorator was the last created decoration.</p>
<p><strong>Be The Repo Man!</strong></p>
<p>I&#8217;ve been butting my head against this issue for a while, and I have a feeling that either I&#8217;m missing something quite obvious or I&#8217;m very close to a solution that still eludes me. So here&#8217;s a chance for you, the reader, to work out a way to retrieve the decorations from the component in any order. So be the Repo Man (or Woman) and see if you can work out a solution.</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%2Fremoving-decorations-here-come-the-repo-men%2F&amp;title=Removing%20Decorations%3A%20Here%20Come%20the%20Repo%20Men" id="wpa2a_10"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/10/removing-decorations-here-come-the-repo-men/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Flash Builder App in Apple iOS: Decorator and State</title>
		<link>http://www.as3dp.com/2011/09/actionscript-3-0-flash-builder-app-in-apple-ios-decorator-and-state/</link>
		<comments>http://www.as3dp.com/2011/09/actionscript-3-0-flash-builder-app-in-apple-ios-decorator-and-state/#comments</comments>
		<pubDate>Tue, 06 Sep 2011 01:31:41 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Decorator]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[State]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6182</guid>
		<description><![CDATA[Decorator and State Patterns in Mobile Game Development While I&#8217;m trying to find the best graphic tool to use in conjunction with mobile devices and ActionScript 3.0, I&#8217;ve decided to stick with wholly programmed images. (I&#8217;m a better programmer than artist, but my images do not improve because they&#8217;re programmed.) For this example, I used [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6181" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/flip.png" alt="Change orientation with device position" title="flip" width="250" height="251" class="size-full wp-image-6181" /><p class="wp-caption-text">Change orientation with device position</p></div><strong>Decorator and State Patterns in Mobile Game Development</strong><br />
 While I&#8217;m trying to find the best graphic tool to use in conjunction with mobile devices and ActionScript 3.0, I&#8217;ve decided to stick with wholly programmed images. (I&#8217;m a better programmer than artist, but my images do not improve because they&#8217;re programmed.) For this example, I used the <strong>ActionScript Mobile Project</strong> option in <em><strong>Flash Builder 4.5.1</strong></em>.  If you&#8217;re using <em><strong>Flash Pro 5.5</strong></em>, you can use the same code by selecting <strong>AIR for iOS</strong> from the Flash main start-up menu. With both, everything is pure ActionScript 3.0—none of the drawing tools, components or other gizmos available in either FB or FP are employed—<em>código puro</em> in both. Click the button below to download the files for Flash Builder. (I also included the files for Flash Pro; so no one is left out.)<br />
<a href="http://www.mwd.hartford.edu/mwd11/downloads/MWDdownloads.php"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a></p>
<p><strong>Reminder:</strong> We&#8217;ve placed all of the downloads on the University of Hartford&#8217;s <strong>Multimedia Web Design and Development download page</strong>. Just select the bottom on the ActionScript 3.0 files and click the download button.</p>
<blockquote><p><strong>Flash Builder Tip</strong>: First create an <em>ActionScript Project</em> so that you can test it as you develop. Once you have it all developed, create an  <em>ActionScript <strong>Mobile</strong> Project</em> and just copy and paste the files from the AS Project.</p></blockquote>
<p><strong>Re-Decorating</strong></p>
<p>In this first part all I want to do with this mobile game is to build and decorate the warrior and put it on an iOS mobile device using Flash Builder. The mechanics are very similar to previous posts on using the Decorator pattern to arm a character (See <a href="http://www.as3dp.com/2011/06/14/well-armored-warrior-decorator-for-beginners/">here</a> and <a href="http://www.as3dp.com/2011/06/15/the-warriors-new-clothes-using-as3-in-flash-builder-to-create-sprite-graphic-classes/">here</a>.) Figure 1 shows a file diagram of the Decorator implementation:<br />
<div id="attachment_6195" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/mobileDec.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt;Decorator File Diagram&lt;/em&gt;" title="mobileDec" width="500" height="381" class="size-full wp-image-6195" /><p class="wp-caption-text"><em><strong>Figure 1:</strong>Decorator File Diagram</em></p></div></p>
<p>Since this thing is going to be in pure code, we need to create a &#8220;warrior&#8221; in code, which was not done in the previous posts linked above. So outside of the pattern, we need a graphic image, as provided in the following code:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6182code34'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p618234"><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
</pre></td><td class="code" id="p6182code34"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BuildWarrior <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> head:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<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> torso:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<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> lftArm:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<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> rtArm:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<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> lftLeg:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<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> rtLeg:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> assemble<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>makeHead<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>makeTorso<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>makeLftArm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>makeRtArm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>makeLftLeg<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>makeRtLeg<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> makeHead<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,0x6F74B3<span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x6F91A6,.50<span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span>,<span style="color: #cc66cc;">17</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0.25</span>,0x0066ff<span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x035AA6,.40<span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawCircle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">35</span>, <span style="color: #cc66cc;">7</span>,<span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,0x6F74B3<span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">moveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span>,<span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">34</span>,<span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">moveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">36</span>,<span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">40</span>,<span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">moveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span>,<span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span>;
			head.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">38</span>,<span style="color: #cc66cc;">12</span><span style="color: #66cc66;">&#41;</span>;
			graphics.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> head;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> makeTorso<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			torso.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,0x6F74B3<span style="color: #66cc66;">&#41;</span>;
			torso.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x6F91A6,.50<span style="color: #66cc66;">&#41;</span>;
			torso.<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;">60</span>,<span style="color: #cc66cc;">20</span>, <span style="color: #cc66cc;">35</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>;
			torso.<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> torso;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> makeLftArm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			lftArm.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,0x6F74B3<span style="color: #66cc66;">&#41;</span>;
			lftArm.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x6FB3A7,.75<span style="color: #66cc66;">&#41;</span>;
			lftArm.<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;">20</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">40</span><span style="color: #66cc66;">&#41;</span>;
			lftArm.<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;">60</span>,<span style="color: #cc66cc;">40</span>,<span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>;
			lftArm.<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> lftArm;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> makeRtArm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			rtArm.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,0x6F74B3<span style="color: #66cc66;">&#41;</span>;
			rtArm.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x6FB3A7,.75<span style="color: #66cc66;">&#41;</span>;
			rtArm.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span>,<span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">40</span><span style="color: #66cc66;">&#41;</span>;
			rtArm.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">51</span>,<span style="color: #cc66cc;">60</span>,<span style="color: #cc66cc;">8</span>,<span style="color: #cc66cc;">40</span><span style="color: #66cc66;">&#41;</span>;
			rtArm.<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> rtArm;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> makeLftLeg<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			lftLeg.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,0x6F74B3<span style="color: #66cc66;">&#41;</span>;
			lftLeg.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x6FB3A7<span style="color: #66cc66;">&#41;</span>;
			lftLeg.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">23</span>,<span style="color: #cc66cc;">70</span>,<span style="color: #cc66cc;">12</span>,<span style="color: #cc66cc;">45</span><span style="color: #66cc66;">&#41;</span>;
			lftLeg.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">24</span>,<span style="color: #cc66cc;">115</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">45</span><span style="color: #66cc66;">&#41;</span>;
			lftLeg.<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> lftLeg;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> makeRtLeg<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			rtLeg.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,0x6F74B3<span style="color: #66cc66;">&#41;</span>;
			rtLeg.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x6FB3A7<span style="color: #66cc66;">&#41;</span>;
			rtLeg.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">36</span>,<span style="color: #cc66cc;">70</span>,<span style="color: #cc66cc;">12</span>,<span style="color: #cc66cc;">45</span><span style="color: #66cc66;">&#41;</span>;
			rtLeg.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">37</span>,<span style="color: #cc66cc;">115</span>,<span style="color: #cc66cc;">10</span>,<span style="color: #cc66cc;">45</span><span style="color: #66cc66;">&#41;</span>;
			rtLeg.<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> rtLeg;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>All of the concrete decorators similarly implement coded graphic representations. Of course, all of these are &#8220;outside&#8221; of the design pattern but are useful to see in the contact of the pattern.</p>
<p><strong>The Client</strong></p>
<p>In the actual mobile client, you begin with a <strong>super();</strong> call and support for auto orientation in the iOS device. The code is automatically generated:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6182code35'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p618235"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p6182code35"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// support autoOrients</span>
<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">align</span> = StageAlign.<span style="color: #006600;">TOP_LEFT</span>;
<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">scaleMode</span> = StageScaleMode.<span style="color: #006600;">NO_SCALE</span>;</pre></td></tr></table></div>

<p>I tossed out the <strong>super();</strong> call, and generated the following client:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6182code36'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p618236"><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="p6182code36"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageAlign</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageScaleMode</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MobileDecState <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> warrior:Component=<span style="color: #000000; font-weight: bold;">new</span> Warrior<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> warriorSpirit:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> MobileDecState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			warriorSpirit.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>warrior.<span style="color: #006600;">getarmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			warriorSpirit.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">50</span>,warriorSpirit.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">50</span>;
			addChild<span style="color: #66cc66;">&#40;</span>warriorSpirit<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			warrior=<span style="color: #000000; font-weight: bold;">new</span> DecBodyArmor<span style="color: #66cc66;">&#40;</span>warrior<span style="color: #66cc66;">&#41;</span>;
			warriorSpirit.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>warrior.<span style="color: #006600;">getarmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			warrior=<span style="color: #000000; font-weight: bold;">new</span> DecShield<span style="color: #66cc66;">&#40;</span>warrior<span style="color: #66cc66;">&#41;</span>;
			warriorSpirit.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>warrior.<span style="color: #006600;">getarmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			warrior=<span style="color: #000000; font-weight: bold;">new</span> DecHelmet<span style="color: #66cc66;">&#40;</span>warrior<span style="color: #66cc66;">&#41;</span>;
			warriorSpirit.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>warrior.<span style="color: #006600;">getarmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			warrior=<span style="color: #000000; font-weight: bold;">new</span> DecParticleCompressor<span style="color: #66cc66;">&#40;</span>warrior<span style="color: #66cc66;">&#41;</span>;
			warriorSpirit.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>warrior.<span style="color: #006600;">getarmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// support autoOrients</span>
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">align</span> = StageAlign.<span style="color: #006600;">TOP_LEFT</span>;
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">scaleMode</span> = StageScaleMode.<span style="color: #006600;">NO_SCALE</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>Publishing for Mobile</strong></p>
<p>For some reason, more steps are involved in publishing for Flash Builder than Flash Pro. However, once you have your certificate (it has a .p12 extension) and your provisioning file created using the tools available to Apple developers; it&#8217;s pretty simple. If you don&#8217;t know about the <a href="http://developer.apple.com/programs/ios/">Apple iOS developer program</a> for both Windows and Mac users, you need to go there first.</p>
<p>Here&#8217;s how to publish using Flash Builder: (<a href="http://www.adobe.com/support/flex/downloads_updaters.html">Download</a> the Beta version/update of Flash Builder 4.5.1. While you&#8217;re at it, download the beta version of <a href="http://labs.adobe.com/technologies/flashplatformruntimes/air3/">Adobe AIR 3.</a>)</p>
<p>1. Open a New > New ActionScript Mobile Project (Figure 2)<br />
<div id="attachment_6207" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/mobile1.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2: &lt;/strong&gt; Open a New ActionScript Mobile Project &lt;/em&gt;" title="mobile1" width="500" height="417" class="size-full wp-image-6207" /><p class="wp-caption-text"><em><strong>Figure 2: </strong> Open a New ActionScript Mobile Project </em></p></div></p>
<p>2.  Specify your target platform as Apple iOS with Automatically reorient selected and click Next. (Figure 3)<br />
<div id="attachment_6208" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/mobile2.png" alt="&lt;em&gt;&lt;strong&gt;Figure3: &lt;/strong&gt; Mobile Settings &lt;/em&gt;" title="mobile2" width="500" height="463" class="size-full wp-image-6208" /><p class="wp-caption-text"><em><strong>Figure3: </strong> Mobile Settings </em></p></div></p>
<p>3. Click the Platform Setting and select Platform and Target devices (Figure 4)<br />
<div id="attachment_6210" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/mobile3.png" alt="&lt;em&gt;&lt;strong&gt;Figure 4: &lt;/strong&gt; Specify Platformsettings &lt;/em&gt;" title="mobile3" width="500" height="463" class="size-full wp-image-6210" /><p class="wp-caption-text"><em><strong>Figure 4: </strong> Specify Platformsettings </em></p></div></p>
<p>4. Build Paths provides the opportunity to add SWC folders or a project. Figure 5 shows using the default settings.<br />
<div id="attachment_6212" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/mobile4.png" alt="&lt;em&gt;&lt;strong&gt;Figure 5: &lt;/strong&gt; Include additional or different paths if requires &lt;/em&gt;" title="mobile4" width="500" height="616" class="size-full wp-image-6212" /><p class="wp-caption-text"><em><strong>Figure 5: </strong> Include additional or different paths if requires </em></p></div></p>
<p>5. The next step in creating your app will not come until you are finished coding it all. Then when you press the green arrow (Run) button at the top, you will begin the Run Configurations sequence. Figure 6 shows the On device radio button selected. You are likely to see some red circles with white &#8216;X&#8217;s in them, but don&#8217;t worry.  If there&#8217;s one next to the Packaging setting click the [Configure] link.<br />
<div id="attachment_6213" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/mobile5.png" alt="&lt;em&gt;&lt;strong&gt;Figure 6: &lt;/strong&gt; Run Configurations &lt;/em&gt;" title="mobile5" width="500" height="354" class="size-full wp-image-6213" /><p class="wp-caption-text"><em><strong>Figure 6: </strong> Run Configurations </em></p></div></p>
<p>6. Set the Certificate and Provisioning file. Both of these should be ready using the software that the Apple iOS Developer program provides. Use the Browse buttons to locate yours. (These cannot be generated using Adobe software.)<br />
<div id="attachment_6216" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/mobile6.png" alt="&lt;em&gt;&lt;strong&gt;Figure 7: &lt;/strong&gt; Setting Certificate and Provisioning files &lt;/em&gt;" title="mobile6" width="500" height="301" class="size-full wp-image-6216" /><p class="wp-caption-text"><em><strong>Figure 7: </strong> Setting Certificate and Provisioning files </em></p></div></p>
<p>Once the certificate and provisioning file are set, click the Apply button and then click OK.</p>
<p>7. If all has gone right you will see the Packaging Completed window shown in Figure 8. At this point you will have to open iTunes on your computer, and make sure that your mobile device is connected to your computer.  You will need to locate the .ipa package and drop it into the iTunes library. It&#8217;s easiest to just click the (Reveal package in Finder) to get the .ipa package.<br />
<div id="attachment_6218" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/mobile7.png" alt="&lt;em&gt;&lt;strong&gt;Figure 8: &lt;/strong&gt; Completed Window &lt;/em&gt;" title="mobile7" width="500" height="177" class="size-full wp-image-6218" /><p class="wp-caption-text"><em><strong>Figure 8: </strong> Completed Window </em></p></div></p>
<p>8. Figure 9 shows the bin-debug folder opened with the .ipa package. Be sure that the Apps window on your iTunes library is selected and <strong>drag the .ipa package into the iTunes library</strong>.<br />
<div id="attachment_6219" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/mobile8.png" alt="&lt;em&gt;&lt;strong&gt;Figure 9 &lt;/strong&gt; Drag .ipa package into iTunes Apps Library &lt;/em&gt;" title="mobile8" width="500" height="463" class="size-full wp-image-6219" /><p class="wp-caption-text"><em><strong>Figure 9 </strong> Drag .ipa package into iTunes Apps Library </em></p></div></p>
<p>9. The final step requires that you select the connected device (shown right above Little Richard Greatest Songs in Figure 10 ) and then find your new app in the Sync Apps window. Be sure to check the box next to the app. Then click the Apply button. Clicking the Apply button will then begin the Sync process. Once it is finished you should see the icon.<br />
<div id="attachment_6221" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/09/mobile9.png" alt="&lt;em&gt;&lt;strong&gt;Figure 10: &lt;/strong&gt;Adding the app to an iOS device&lt;/em&gt;" title="mobile9" width="500" height="452" class="size-full wp-image-6221" /><p class="wp-caption-text"><em><strong>Figure 10: </strong>Adding the app to an iOS device</em></p></div><br />
When you click the icon on your iOS mobile device, you will see the image shown at the top of this post. If you turn your device, the object will re-orient itself to the object&#8217;s orientation.</p>
<p><strong>Adding the State Design</strong></p>
<p>With this and the previous posts for mobile devices, you&#8217;ve seen that design patterns work well with mobile app development. All of the advantages found in design pattern development for computers can be found in creating applications for mobile devices.</p>
<p>In the next post, this process will be continued by adding a State pattern to the app to create a game. If you don&#8217;t have a mobile device, you can use the same coding for making the app for your computer.</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%2F09%2Factionscript-3-0-flash-builder-app-in-apple-ios-decorator-and-state%2F&amp;title=ActionScript%203.0%20Flash%20Builder%20App%20in%20Apple%20iOS%3A%20Decorator%20and%20State" id="wpa2a_12"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/09/actionscript-3-0-flash-builder-app-in-apple-ios-decorator-and-state/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Design Patterns and iOS:  A New Home for Apps</title>
		<link>http://www.as3dp.com/2011/08/actionscript-3-0-design-patterns-and-ios-a-new-home-for-apps/</link>
		<comments>http://www.as3dp.com/2011/08/actionscript-3-0-design-patterns-and-ios-a-new-home-for-apps/#comments</comments>
		<pubDate>Mon, 15 Aug 2011 13:42:26 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Decorator]]></category>
		<category><![CDATA[Factory Method]]></category>
		<category><![CDATA[Mobile]]></category>

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

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

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

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

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

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

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

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

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

<p>In looking at the code, there seems to be a lot of code for some pretty simple operations. I considered adding a  string parameter to the factory method to call the required SWF file through a product. I could conceivably create a program with one concrete factory and one concrete product with all of the differences handled through the parameter. However, this design is a nice simple tool to use in working with an iOS design and having multiple concrete factories and products seems to be a better test of using a design pattern with iOS.</p>
<p><strong>Preparation for the iOS Building</strong></p>
<p>This post is designed to give you some time to familiarize yourself with the design pattern prior to putting it into an iOS application. To prepare for the iOS application, you have two routes:</p>
<ol>
<li>Go to http://developer.apple.com/programs/ios/ and cough up $99/year to enroll in the iOS developer program.</li>
<li>Go to http://developer.apple.com/programs/ios/university/ and sign up for the iOS Developer University Program for <strong>free</strong>. This requires someone in your major/department (usually the dept chair) to sign off on it. It can be handled easily via email and constant whining.</li>
</ol>
<p>If you don&#8217;t have an iOS Developer ticket, there&#8217;s not much point in even trying to get an Apple mobile app working.  However, if you are considering the program but not want to fork over $99 just yet, you can wait until the next post to see how it&#8217;s done and whether it&#8217;s worth it.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2011%2F08%2Fmobile-banner-factory-for-ios%2F&amp;title=Mobile%20Banner%20Factory%20for%20iOS" id="wpa2a_16"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/08/mobile-banner-factory-for-ios/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Flash Builder Mobile ActionScript 3.0 Design Pattern</title>
		<link>http://www.as3dp.com/2011/07/flash-builder-mobile-actionscript-3-0-design-pattern/</link>
		<comments>http://www.as3dp.com/2011/07/flash-builder-mobile-actionscript-3-0-design-pattern/#comments</comments>
		<pubDate>Wed, 27 Jul 2011 16:05:57 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Strategy Pattern]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6065</guid>
		<description><![CDATA[Easy Update A couple of days ago, we had an example for beginners creating a mobile application using Flash Professional. We used an Android example because everyone can get the tools they need for emulating an Android. (By the way, partnering up with Google-Android and Flash was a brilliant move by Android. Being an &#8220;Apple [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6064" class="wp-caption alignleft" style="width: 258px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/phoneFB.png" alt="Flexible Design Pattern Re-used with Flash Builder Development" title="phoneFB" width="248" height="502" class="size-full wp-image-6064" /><p class="wp-caption-text">Flexible Design Pattern Re-used with Flash Builder Development</p></div><strong>Easy Update</strong></p>
<p>A couple of days ago, we had an example for beginners creating a mobile application using <strong>Flash Professional</strong>.  We used an Android example because everyone can get the tools they need for emulating an Android. (By the way, partnering up with Google-Android and Flash was a brilliant move by Android.  Being an &#8220;Apple Guy,&#8221;  I thought I&#8217;d be doing nothing but apps for my iPhone and iPads; however, Android made it a lot easier to develop apps for Android using some kind of ActionScript 3.0 development tool, and Blackberry&#8217;s not far behind. So I am very happy to know that iOS is now available, but they&#8217;ll just have to wait their turn for a beginner&#8217;s post.)  As promised, I wanted to create a beginner&#8217;s example using Flash Builder.  This example uses the same Strategy Pattern as the <a href="http://www.as3dp.com/2011/07/25/beginning-mobile-actionscript-3-0-design-patterns-with-flash/">Flash mobile tutorial</a>. However, as you will see, there are some key differences when dealing with Flash Pro and Flash Builder. Also, I hope you see that a very different logo (FB instead of Sandlight) and text. All that was changed for this difference was swapping the image used for the logo and the text in the external text file. Also, I had to change some settings in the BringText class, but that too was easy and didn&#8217;t upset the program. Why? <em>Because it was a design pattern!</em></p>
<p><strong>Stepping Through Flash Builder Mobile</strong></p>
<p>This is going to be the same walk-through with Flash Builder as with Flash Pro but with a few differences.  The Flash Builder work flow is slightly different, but you will find more similarities than differences. Before we get going download Flash Builder 4.5.1. (You can use the 30-day free trial version.)  You can probably work with FB 4 with all of the updates, including the Android SDK, but everything in this example was done with FB 4.5.1. (If you are a student or faculty, you can get a free educational version of FB if you contact Adobe with your student or faculty ID. Faculty can get it free for their classrooms and labs as well.) Install to the defaults. Go to <a href="http://labs.adobe.com/">Adobe Labs</a> download and install both Adobe AIR 3 (beta) and Adobe Flash Player 11 (beta) and let&#8217;s get started:</p>
<p><strong>First</strong>, open Flash Builder and select File > New > ActionScript Mobile Project. (<strong>Note:</strong> You will find both a <em>Flex Mobile Project</em> and an <em>ActionScript Mobile Project</em>; be sure to select <em><strong>ActionScript Mobile Project</strong></em>.) In the Project name window, name the project <strong>FBmoStrat </strong>as shown in Figure 1 :</p>
<div id="attachment_6072" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/Fig11.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; New ActionScript Mobile Project window&lt;/em&gt;" title="Fig1" width="500" height="476" class="size-full wp-image-6072" /><p class="wp-caption-text"><em><strong>Figure 1:</strong> New ActionScript Mobile Project window</em></p></div>
<p>I used the default location and the default SDK, which was Flex 4.5.1. Click Next.</p>
<p><strong>Second</strong>, when <strong>The Create an ActionScript Mobile AIR Project </strong>window appears with the <strong>Mobile Settings</strong> tab selected, you will see that all Target platforms (Apple iOS, BlackBerry Tablet OS and Google Android) are checked as shown in Figure 2.<br />
<div id="attachment_6075" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/Fig21.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2:&lt;/strong&gt;Initial Create Window&lt;/em&gt;" title="Fig2" width="500" height="476" class="size-full wp-image-6075" /><p class="wp-caption-text"><em><strong>Figure 2:</strong>Initial Create Window</em></p></div></p>
<p><strong>Third</strong>, uncheck <strong>both </strong>the Apple iOS and BlackBerry Tablet OS. When you do that, the Permissions for Android appear as shown in Figure 3:</p>
<div id="attachment_6080" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/Fig31.png" alt="&lt;em&gt;&lt;strong&gt;Figure 3: &lt;/strong&gt; Android Permissions&lt;/em&gt;" title="Fig3" width="500" height="624" class="size-full wp-image-6080" /><p class="wp-caption-text"><em><strong>Figure 3: </strong> Android Permissions</em></p></div>
<p>Be sure that the Platform is Google Android and that the only permission checked is INTERNET. The default is to Automatically reorient the screen in the Application settings. Leave that default checked and click the Next button.</p>
<p><strong>Fourth,</strong> in the Build Paths step, you will see a set of default selections you can leave alone except for the Main application file that you want to change from<strong> FBmoStrat.as</strong> to <strong>Client.as</strong>. Figure 4 shows the step correctly configured:</p>
<div id="attachment_6085" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/Fig4.png" alt="&lt;em&gt;&lt;strong&gt;Figure 4:&lt;/strong&gt; Build Paths step&lt;/em&gt;" title="Fig4" width="500" height="616" class="size-full wp-image-6085" /><p class="wp-caption-text"><em><strong>Figure 4:</strong> Build Paths step</em></p></div>
<p>If your Build Paths steps looks like Figure 4, click Finish. You should see the following script automatically generated:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6065code47'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p606547"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p6065code47"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageAlign</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageScaleMode</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// support autoOrients</span>
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">align</span> = StageAlign.<span style="color: #006600;">TOP_LEFT</span>;
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">scaleMode</span> = StageScaleMode.<span style="color: #006600;">NO_SCALE</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The Stage Align and StageScaleMode built-in ActionScript 3.0 classes will help align the application when it is turned in different directions.  The <strong>super()</strong> method invokes the parent class <strong>Sprite</strong>.  You will be leaving the constructor function as is but add more materials for the Client class. Before doing that, go ahead and test the problem and download all of the files with the buttons below:<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/dp/mobileFB/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignleft size-full wp-image-1257" /></a><a href="http://nemo.mwd.hartford.edu/~wsanders/dp/mobileFB/FBmoStrat.zip"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a></p>
<p>You still will need to test the file from Flash Builder and contend with one last set of settings. So gather up all of the files and read on before testing.</p>
<p><span id="more-6065"></span></p>
<p><strong>Testing from Flash Builder</strong></p>
<p>Once you all of the classes in the project are saved in the Flash Builder src folder, along with the <em>graphic file and text file</em>, click the Run button to test the application. The first time you do this, you will see the Run Configurations window shown in Figure 5.</p>
<div id="attachment_6090" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/Fig5.png" alt="&lt;em&gt;&lt;strong&gt;Figure 5:&lt;/strong&gt; Run Configurations window&lt;/em&gt;" title="Fig5" width="500" height="549" class="size-full wp-image-6090" /><p class="wp-caption-text"><em><strong>Figure 5:</strong> Run Configurations window</em></p></div>
<p>In the Launch method area, select the<strong> On desktop</strong> radio button, and select <strong>Mororola Droid 2</strong> and click the <strong>Apply</strong> button. Finally, click the <strong>Run</strong> button.</p>
<p><strong>The Strategy Design Pattern in a Mobile Context</strong></p>
<p>If you read the post on creating this app using Flash Pro, there&#8217;s not much new here. This post is only meant to show how to add a design pattern into a mobile application using Flash Builder.  If you use apps on mobile devices, you must be aware of the updating that your app goes through. Using the Strategy pattern, adding more concrete strategies is easy, and changing the graphic and text contents is equally easy. In other words, this is a very flexible tool to both create and update mobile applications just as it is with traditional applications.  The following classes are little changed from the previous post using Flash Pro, but they are included for review and the small differences that could impact your understanding.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6065code48'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p606548"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
</pre></td><td class="code" id="p6065code48"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Client</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageAlign</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">StageScaleMode</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> context:Context=<span style="color: #000000; font-weight: bold;">new</span> Context<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> BringGraphic<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> contextTxt:Context=<span style="color: #000000; font-weight: bold;">new</span> Context<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> BringText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> mobileSprite:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> textSprite:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// support autoOrients</span>
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">align</span> = StageAlign.<span style="color: #006600;">TOP_LEFT</span>;
			<span style="color: #0066CC;">stage</span>.<span style="color: #0066CC;">scaleMode</span> = StageScaleMode.<span style="color: #006600;">NO_SCALE</span>;
			setLogo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			setText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> setLogo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			mobileSprite.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">bringLoad</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mobilelogo.png&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			mobileSprite.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">5</span>,mobileSprite.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">10</span>;
			addChild<span style="color: #66cc66;">&#40;</span>mobileSprite<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> setText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			textSprite.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>contextTxt.<span style="color: #006600;">bringLoad</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mobile.txt&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			textSprite.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">5</span>,textSprite.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">150</span>;
			addChild<span style="color: #66cc66;">&#40;</span>textSprite<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Context</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Context <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bring:IStrategy;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bringObj:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Context<span style="color: #66cc66;">&#40;</span>bring:IStrategy<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">bring</span> = bring;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> bringLoad<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			bringObj.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>bring.<span style="color: #006600;">contentLoader</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> bringObj;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//IStrategy interface</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IStrategy
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> contentLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Bring Graphic--loads graphics and SWF files</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> BringGraphic <span style="color: #0066CC;">implements</span> IStrategy
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> urlReq:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loader:Loader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> objNow:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> contentLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			urlReq = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			loader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			loader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>urlReq<span style="color: #66cc66;">&#41;</span>;
			objNow.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>loader<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> objNow;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//BringText -- loads external text and xml files and formats text</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLLoader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BringText <span style="color: #0066CC;">implements</span> IStrategy
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> textContainer:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> urlNow:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtLoader:URLLoader=<span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtFld:<span style="color: #0066CC;">TextField</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtFormat:<span style="color: #0066CC;">TextFormat</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> uLoader:URLLoader;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> contentLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			urlNow=<span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			txtLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, addTxtFld<span style="color: #66cc66;">&#41;</span>;
			txtLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>urlNow<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> textContainer;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> addTxtFld<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			uLoader=URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>;
			txtFormat=<span style="color: #000000; font-weight: bold;">new</span>  <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			txtFormat.<span style="color: #0066CC;">font</span>=<span style="color: #ff0000;">&quot;Verdana&quot;</span>;
			txtFormat.<span style="color: #0066CC;">size</span>=<span style="color: #cc66cc;">24</span>;
			txtFormat.<span style="color: #0066CC;">color</span>=0x000000;
			txtFld.<span style="color: #006600;">defaultTextFormat</span>=txtFormat;
			txtFld.<span style="color: #0066CC;">multiline</span>=<span style="color: #000000; font-weight: bold;">true</span>;
			txtFld.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">300</span>;
			txtFld.<span style="color: #0066CC;">height</span>=<span style="color: #cc66cc;">300</span>;
			txtFld.<span style="color: #0066CC;">wordWrap</span>=<span style="color: #000000; font-weight: bold;">true</span>;
			txtFld.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">10</span>,txtFld.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">50</span>;
			txtFld.<span style="color: #0066CC;">text</span>=uLoader.<span style="color: #0066CC;">data</span>;
			textContainer.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>txtFld<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>If someone would like to add their own logo and text about their company, services or products using this application, we&#8217;d like to show them off. It&#8217;d be a good use of the design pattern, and you&#8217;d get some worldwide notoriety for your company!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2011%2F07%2Fflash-builder-mobile-actionscript-3-0-design-pattern%2F&amp;title=Flash%20Builder%20Mobile%20ActionScript%203.0%20Design%20Pattern" id="wpa2a_18"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/07/flash-builder-mobile-actionscript-3-0-design-pattern/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Beginning Mobile ActionScript 3.0 Design Patterns with Flash</title>
		<link>http://www.as3dp.com/2011/07/beginning-mobile-actionscript-3-0-design-patterns-with-flash/</link>
		<comments>http://www.as3dp.com/2011/07/beginning-mobile-actionscript-3-0-design-patterns-with-flash/#comments</comments>
		<pubDate>Mon, 25 Jul 2011 14:54:35 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Strategy Pattern]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6018</guid>
		<description><![CDATA[Get Your App Up! The very first mobile app I want to build is one that I can give away to promote my company or my clients&#8217; companies. Yes, I&#8217;d like to make lots of money with a mobile action game like Angry Birds, but in order to get started, I want something that I [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6017" class="wp-caption alignleft" style="width: 258px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/phone1.png" alt="ActionScript 3.0 Design Patterns in Mobile Apps" title="phone" width="248" height="502" class="size-full wp-image-6017" /><p class="wp-caption-text">ActionScript 3.0 Design Patterns in Mobile Apps</p></div><strong>Get Your App Up!</strong></p>
<p>The very first mobile app I want to build is one that I can give away to promote my company or my clients&#8217; companies. Yes, I&#8217;d like to make lots of money with a mobile action game like <em>Angry Birds</em>, but in order to get started, I want something that I can easily update and change. It seems like every time I check my phone apps, I&#8217;ve got updates. What is one of the main features of Design Patterns? Easy updates! So, if you want to create mobile applications in ActionScirpt 3.0 that are easy to update, you&#8217;ll find design patterns your BFF!</p>
<p>This first one is built with Flash CS5.5. If you do not have Flash CS5.5; download the 30-day trial version. If you&#8217;re using Flash Builder, use FB 4.5.1.  or download the trial version. (I hope to have a FB tutorial using this same example later in the week. In the meantime, take a look at <a href="http://www.as3dp.com/2011/06/28/puremvc-goes-mobile-1-mvc-driven-ready-to-compile-mobile-application-with-the-flex-framework-by-christian-peters/">Christian Peters&#8217; mobile example</a> for building an app using design patterns with Flash Builder.)</p>
<p>This first example uses a <strong>Strategy</strong> pattern with algorithms for loading external images (or .SWF files) and text.  That is exactly what I need for a mobile app promoting my company. With the image loading capability, I can load my logo and any images that show off products and services. The concrete text strategy loads up data from external text files. So this ought to be the easiest app to update imaginable!</p>
<p><strong>Easy Android</strong></p>
<p>I was going to start off with an iOS example, but I realized that most of you probably didn&#8217;t want to shell out the $99 developer fee required by Apple for iOS development. So, instead I decided to use an Android example that you could test using an Android emulator. To get started, go ahead and download the source code and see what the app displays:<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/dp/mobile1/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignleft size-full wp-image-1257" /></a><a href="http://www.mwd.hartford.edu/mwd11/downloads/MWDdownloads.php"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a></p>
<blockquote><p>All of the Downloads have been moved to the University of Hartford&#8217;s  <strong>Multimedia Web Design and Development Program</strong> page. The last entry under &#8220;ActionScript 3.0/ Flash&#8221; should have what you want. (I&#8217;m still working on it to include a short description of each entry&#8211;it&#8217;s in the database; just not on the screen).</p></blockquote>
<p>When you run it, you will quickly see that a logo and block of text pops up. That&#8217;s it. Swap out your own logo and texts, and you&#8217;re in business. You can change any of the parameters in the <strong>BringText</strong> concrete strategy class to alter the text appearance, it&#8217;s placement and other features to suite your own tastes or business brand.</p>
<p><strong>Starting from Scratch</strong></p>
<p>Since this is a first time experience for beginners and mobile (which may include experienced ActionScript 3.0 programmers), we&#8217;ll step through the set-up beginning with the FLA file. So read on through the following steps:</p>
<p><span id="more-6018"></span><br />
<strong>First</strong>, download and install the <a href="http://developer.android.com/index.html">Android SDK</a>. If your system is not automatically selected, choose the correct OS. Install the SDK.</p>
<p><strong>Second</strong>, install the <strong>Adobe AIR 3 beta</strong> and <strong>Adobe Flash Player 11 beta</strong>, both at <a href="http://labs.adobe.com/">Adobe Labs</a>.</p>
<p><strong>Third</strong>, download and install Flash CS5.5 trial if you don&#8217;t have it. (If you have Flash CS5 and have installed all of the updates, you ought to be ok.)</p>
<p><strong>Fourth</strong>, in your root directory, create a folder named &#8220;Developer.&#8221; For example, on a Mac, you&#8217;d open up your  Macintosh HD and add the Developer folder.( A hammer icon may automatically appear on the folder.)</p>
<p><strong>Fifth</strong>, open Flash CS5.5 and select  <strong>AIR for Android</strong>. Configure the screen for 320 x 470. (If you have an Android phone with a different screen size, configure it for your phone size.) Create a folder named <strong>MobileApp</strong> inside of the<strong> Developer</strong> folder, and save the file as <strong>AndroidStrategy.fla.</strong></p>
<p><strong>Sixth</strong>, with the stage selected, open the Properties panel. In the text window labeled &#8220;Class&#8221; enter the Class name &#8220;Client.&#8221;</p>
<p><strong>Seventh</strong>, in the Properties panel in the Publish grouping, you should see <strong>AIR for Android</strong> next to &#8220;Player.&#8221; You will see a <strong>wrench icon</strong> to the right. Click the icon to open the AIR for Android Settings window. Figure 1 shows what you should see. If anything looks different, check to make sure you followed the previous steps.<br />
<div id="attachment_6037" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/Fig1.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt;General AIR for Android Settings&lt;/em&gt;" title="Fig1" width="500" height="694" class="size-full wp-image-6037" /><p class="wp-caption-text"><em><strong>Figure 1:</strong>General AIR for Android Settings</em></p></div></p>
<p><strong>Eighth</strong>, in the AIR for Android window, click the <strong>Deployment</strong> tab (near the top of the  window next to the General tab.) Figure 2 shows the Deployment selections. You will need to create a Certificate and Password. Click the Create button to create a certificate. You can use any names you want; something simple works fine. You can use the same certificate for other projects. I had placed the Android SDK in the Development folder and once I created the certificate, the full URL is:</p>
<p> <strong>/Developer/android-sdk-mac_x86/platform-tools/adb.p12</strong>.</p>
<p>If you do not see a &#8220;platform-tools&#8221; folder in your SDK folder, you&#8217;ll need to update your Android SDK by downloading the most recent release. (The earlier ones had the needed files in a different folder.)<br />
<div id="attachment_6042" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/07/Fig2.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2:&lt;/strong&gt; Deployment Tab in AIR for Android Settings Window&lt;/em&gt;" title="Fig2" width="500" height="694" class="size-full wp-image-6042" /><p class="wp-caption-text"><em><strong>Figure 2:</strong> Deployment Tab in AIR for Android Settings Window</em></p></div></p>
<p>Be sure to remember your password, and it helps to click the Remember password for this session so you don&#8217;t have to keep re-entering it. Set the Android deployment to <strong>Emulator release</strong> and don&#8217;t select any of the <strong>After publishing</strong> checkboxes. When you&#8217;re all finished  click OK. You&#8217;re all set for a mobile app using design patterns!</p>
<p><strong>Adding the Design Pattern</strong></p>
<p>This final section uses a simple Strategy design pattern. (If you&#8217;re not familiar with the Strategy pattern, we have a <a href="http://www.as3dp.com/2010/09/28/saturated-strategy-1-a-look-at-the-participants/">saturated series</a> on it as well as a <a href="http://www.as3dp.com/2009/03/08/actionscript-30-easy-and-practical-strategy-design-pattern/">simple introduction</a>.) As noted at the outset, this implementation of the Strategy pattern pulls in graphics and text through Client requests. However, because of the pattern design, there&#8217;s minimal object binding, and any updates or changes will not unravel the whole program. Also, because it&#8217;s simple, it&#8217;s a good pattern to use to understand the basics of the Strategy pattern.</p>
<p><strong>The Client</strong></p>
<p>Without a UI, the pattern is quite easy. The Client simply requests what it wants using literal names for graphic images and the text in an external text file. The following shows the Client code:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6018code54'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p601854"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p6018code54"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> context:Context=<span style="color: #000000; font-weight: bold;">new</span> Context<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> BringGraphic<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> contextTxt:Context=<span style="color: #000000; font-weight: bold;">new</span> Context<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> BringText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> mobileSprite:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> textSprite:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setLogo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			setText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> setLogo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			mobileSprite.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>context.<span style="color: #006600;">bringLoad</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mobilelogo.png&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			mobileSprite.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">5</span>,mobileSprite.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">10</span>;
			addChild<span style="color: #66cc66;">&#40;</span>mobileSprite<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> setText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			textSprite.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>contextTxt.<span style="color: #006600;">bringLoad</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mobile.txt&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			textSprite.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">5</span>,textSprite.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">150</span>;
			addChild<span style="color: #66cc66;">&#40;</span>textSprite<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The protected accessors have been employed so that when the app is developed in Flash Builder in the Flex element, it&#8217;ll feel right at home.  However, in this Client, private access would have worked fine.  This particular implementation of the Strategy returns Sprite objects containing both Text and Graphics, including other Sprite objects. As a result, even the text is added to the stage inside a Sprite. Notice that all requests go through the Context class.</p>
<p><strong>The Context Class and Strategy Interface</strong></p>
<p>Even though the pattern diagrams for the State and Strategy patterns are identical, the intent is quite different. Most important is the Context class in the Strategy design.<em> It does not keep track of a current state </em>as does the State pattern.</p>

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

<p>In some respects the Context class in the Strategy resembles a factory because the request for strategies is made through the Context, but it does not really instantiate a concrete strategy as a factory would. The Sprite instantiated in the Context (bringObj) acts as a carrying container to return the requested strategy to the requesting class (Client in this case.) However, no concrete strategies are created.</p>
<p>The IStrategy interface has a single method to be implemented. The method is used to pass requests back to the requesting class.</p>

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

<p>The IStrategy interface leaves a lot of flexibility for adding more concrete strategies for returning Sprite (or Sprite encased) materials to the client.</p>
<p><strong>Concrete Strategies</strong></p>
<p>Finally, the two concrete strategies both load external content. The first is an image/SWF loader and the latter is a text/xml file loader.  It is simple and flexible:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6018code57'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p601857"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code" id="p6018code57"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Graphic and SWF Loader</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> BringGraphic <span style="color: #0066CC;">implements</span> IStrategy
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> urlReq:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loader:Loader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> objNow:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> contentLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			urlReq = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			loader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			loader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>urlReq<span style="color: #66cc66;">&#41;</span>;
			objNow.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>loader<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> objNow;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The BringText class required an event handler function for insuring that the text was completely loaded. The event handler method is a convenient place to put the TextFormat object and properties. Once the text is pulled out of the text or xml file and placed into the formatted text field, it was packed up and put into a Sprite object ready to return to the Client.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6018code58'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p601858"><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="p6018code58"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Text and XML Loader</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLLoader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BringText <span style="color: #0066CC;">implements</span> IStrategy
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> textContainer:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> urlNow:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtLoader:URLLoader=<span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtFld:<span style="color: #0066CC;">TextField</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtFormat:<span style="color: #0066CC;">TextFormat</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> uLoader:URLLoader;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> contentLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			urlNow=<span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			txtLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, addTxtFld<span style="color: #66cc66;">&#41;</span>;
			txtLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>urlNow<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> textContainer;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> addTxtFld<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			uLoader=URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>;
			txtFormat=<span style="color: #000000; font-weight: bold;">new</span>  <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			txtFormat.<span style="color: #0066CC;">font</span>=<span style="color: #ff0000;">&quot;Verdana&quot;</span>;
			txtFormat.<span style="color: #0066CC;">size</span>=<span style="color: #cc66cc;">24</span>;
			txtFormat.<span style="color: #0066CC;">color</span>=0x000000;
			txtFld.<span style="color: #006600;">defaultTextFormat</span>=txtFormat;
			txtFld.<span style="color: #0066CC;">multiline</span>=<span style="color: #000000; font-weight: bold;">true</span>;
			txtFld.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">300</span>;
			txtFld.<span style="color: #0066CC;">height</span>=<span style="color: #cc66cc;">250</span>;
			txtFld.<span style="color: #0066CC;">wordWrap</span>=<span style="color: #000000; font-weight: bold;">true</span>;
			txtFld.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">10</span>,txtFld.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">90</span>;
			txtFld.<span style="color: #0066CC;">text</span>=uLoader.<span style="color: #0066CC;">data</span>;
			textContainer.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>txtFld<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As noted, this pattern is simple, but very handy. The classes are reusable and simple to update and change. Further, adding more classes is just as easy, and that means updating the mobile application is simple.</p>
<p><strong>On to Flex and Flash Builder</strong></p>
<p>The next step is to take the ActionScript 3.0 in this mobile application and do the same thing in Flash Builder.  The Client class will have to reside in an MXML file instead of an AS file. Next up, we&#8217;ll see how to take this app and bundle it up using Flash builder. I&#8217;d also like to see other simple Flex/Flash Builder mobile design pattern examples that might help our readers—even implementations of this code in the Flash Builder IDE.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2011%2F07%2Fbeginning-mobile-actionscript-3-0-design-patterns-with-flash%2F&amp;title=Beginning%20Mobile%20ActionScript%203.0%20Design%20Patterns%20with%20Flash" id="wpa2a_20"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/07/beginning-mobile-actionscript-3-0-design-patterns-with-flash/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

