<?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; Decorator</title>
	<atom:link href="http://www.as3dp.com/category/design-patterns/decorator/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>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('p6228code7'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p62287"><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="p6228code7"><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('p6228code8'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p62288"><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="p6228code8"><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('p6228code9'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p62289"><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="p6228code9"><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('p6228code10'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p622810"><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="p6228code10"><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('p6228code11'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p622811"><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="p6228code11"><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('p6228code12'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p622812"><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="p6228code12"><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_2"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/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('p6182code16'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p618216"><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="p6182code16"><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('p6182code17'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p618217"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p6182code17"><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('p6182code18'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p618218"><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="p6182code18"><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_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/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_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/08/actionscript-3-0-design-patterns-and-ios-a-new-home-for-apps/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The Warrior&#039;s New Clothes: Using AS3 in Flash Builder to Create Sprite Graphic Classes</title>
		<link>http://www.as3dp.com/2011/06/the-warriors-new-clothes-using-as3-in-flash-builder-to-create-sprite-graphic-classes/</link>
		<comments>http://www.as3dp.com/2011/06/the-warriors-new-clothes-using-as3-in-flash-builder-to-create-sprite-graphic-classes/#comments</comments>
		<pubDate>Wed, 15 Jun 2011 14:44:00 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Beginner's Guide]]></category>
		<category><![CDATA[Decorator]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=5750</guid>
		<description><![CDATA[Sprite Classes in Code When I&#8217;m working on an example, I&#8217;ll use a combination of Flash Pro and Flash Builder. However, using Flash Pro, all of my drawings are in the Flash Library and may not be accessible to Flash Builder users. Besides, my drawings are pretty simple composed of basic shapes—rectangles, circles and triangles. [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_5749" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/06/armor.png" alt="Code your own Sprite classes" title="armor" width="250" height="250" class="size-full wp-image-5749" /><p class="wp-caption-text">Code your own Sprite classes</p></div><strong>Sprite Classes in Code</strong></p>
<p>When I&#8217;m working on an example, I&#8217;ll use a combination of Flash Pro and Flash Builder. However, using Flash Pro, all of my drawings are in the Flash Library and may not be accessible to Flash Builder users. Besides, my drawings are pretty simple composed of basic shapes—rectangles, circles and triangles. These images are transformed into Sprite classes in the Symbol Editor of Flash Pro, and the type is changed from the default MovieClip to Sprite. If I coded the images as Sprites using ActionScript 3.0, the Flash Builder users would be able to more easily use them and the Flash Pro users could use them with equal ease.</p>
<p>Let&#8217;s face it, my graphics are not going to get any better whether I code them or draw them; so why not code them? So that&#8217;s what I&#8217;ve done. In the <a href="http://www.as3dp.com/2011/06/14/well-armored-warrior-decorator-for-beginners/">previous post on the Decorator design pattern for beginners</a>, I had three graphic &#8220;armor&#8221; pieces that were used with concrete decorator classes. All I&#8217;ve done for this mini-post is to provide the &#8220;armor&#8221; sprites as pure code. From now on when I have a Sprite image, I&#8217;ll just code them up in ActionScript 3.0 as Sprite classes and make it easy for everyone. Here&#8217;s the Shield, BodyArmor and Helmet classes: (Next page.)<br />
<span id="more-5750"></span><br />
<strong>Shield</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('p5750code22'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p575022"><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="p5750code22"><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;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Shield <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> shield: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;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Shield<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			shield.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,0x0066ff<span style="color: #66cc66;">&#41;</span>;
			shield.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x0033ff,.50<span style="color: #66cc66;">&#41;</span>;
			shield.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawCircle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span>, <span style="color: #cc66cc;">30</span>,<span style="color: #cc66cc;">31.25</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>;
			addChild<span style="color: #66cc66;">&#40;</span>shield<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>Body Armor</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('p5750code23'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p575023"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p5750code23"><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;">Shape</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BodyArmor <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> bodyArmor: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;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BodyArmor<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			bodyArmor.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>,0x990000<span style="color: #66cc66;">&#41;</span>;
			bodyArmor.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x999999,.50<span style="color: #66cc66;">&#41;</span>;
			bodyArmor.<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;">0</span>,<span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">116</span>,<span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">58</span>,<span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			bodyArmor.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>bodyArmor<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>Helmet</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('p5750code24'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p575024"><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="p5750code24"><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;">Shape</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Helmet <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> helmet: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;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Helmet<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			helmet.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xcc0000<span style="color: #66cc66;">&#41;</span>;
			helmet.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">moveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>;
			helmet.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">curveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span>, -<span style="color: #cc66cc;">20</span>, <span style="color: #cc66cc;">60</span>, <span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>;
			helmet.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			helmet.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,0x996600<span style="color: #66cc66;">&#41;</span>;
			helmet.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">moveTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span>;
			helmet.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineTo</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>helmet<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>I realize that this is pretty simple stuff, but if it helps beginners working with OOP and classes, not to mention design patterns, it&#8217;s worth it. Now, all that you have to do is to create the Warrior!!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2011%2F06%2Fthe-warriors-new-clothes-using-as3-in-flash-builder-to-create-sprite-graphic-classes%2F&amp;title=The%20Warrior%26%23039%3Bs%20New%20Clothes%3A%20Using%20AS3%20in%20Flash%20Builder%20to%20Create%20Sprite%20Graphic%20Classes" 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/06/the-warriors-new-clothes-using-as3-in-flash-builder-to-create-sprite-graphic-classes/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Well-Armored Warrior:ActionScript 3.0 Decorator for Beginners</title>
		<link>http://www.as3dp.com/2011/06/well-armored-warrior-decorator-for-beginners/</link>
		<comments>http://www.as3dp.com/2011/06/well-armored-warrior-decorator-for-beginners/#comments</comments>
		<pubDate>Tue, 14 Jun 2011 21:05:05 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Beginner's Guide]]></category>
		<category><![CDATA[Decorator]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=5725</guid>
		<description><![CDATA[Decorating an Object In looking over the chapter on the Decorator pattern in our book I&#8217;ve felt that maybe I picked too complex a set of examples. In a Decorator example on this blog I attempted to remedy that with a very easy yet somewhat abstract example. However, I&#8217;ve decided to try and have a [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_5724" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/06/knight.png" alt="Decorator Adds Changes without Changing the Object" title="knight" width="250" height="271" class="size-full wp-image-5724" /><p class="wp-caption-text">Decorator Adds Changes without Changing the Object</p></div><strong>Decorating an Object</strong></p>
<p>In looking over the chapter on the Decorator pattern in our book I&#8217;ve felt that maybe I picked too complex a set of examples. In a<a href="http://www.as3dp.com/2009/04/26/actionscript-30-easy-and-practical-decorator-design-pattern/"> Decorator example on this blog</a> I attempted to remedy that with a very easy yet somewhat abstract example. However, I&#8217;ve decided to try and have a hugely simple example that is both interactive and solves a practical program in games. The knights of yore represent a good metaphor for the Decorator Design Pattern. The Component (knight) is decorated with armor. The same knight may have more than a single suit of armor and yet easily be recognized as the same knight. We could put him in a time machine and trade his steel suit of armor with one decorated with kevlar or some other modern weapon-stopping armor.</p>
<p>In a game context, different characters can be decorated with different weapons, armor and capabilities. If we have to provide our characters with a set of features at the outset, we can get stuck easily if we want to make changes to the character. The Decorator pattern recognizes that in dynamic situations, you need dynamic solutions. You don&#8217;t want to have to create a new character every time the character changes. With the Decorator, it&#8217;s easy to make changes without changing the core component (character). In this example, I used a graphic warrior and three different graphic semi-opaque graphic armor. To optimize the simplicity, each of the four elements—a component (character) and three types of armor (shield, breast plate and helmet) serve to illustrate the pattern. Further, I used a different class for each as well. These were stored in the library as Sprites. Use the Play and Download buttons below to become familiar with the pattern:<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/blog/Decorator/" target= "_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignleft size-full wp-image-1257" /></a><a href="http://nemo.mwd.hartford.edu/~wsanders/blog/Decorator/decorator.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>If you click the <strong>Play</strong> button, the initial figure is the &#8220;Concrete Component&#8221; participant, and as you click each button in turn, a new &#8220;Concrete Decorator&#8221; appears. (If you keep clicking the decorator buttons, the decorators accumulate. You can add a line of code to remove them easy enough, but I didn&#8217;t in an effort to keep it as simple as pie.)</p>
<p><strong>The Classes</strong></p>
<p>The class diagram and the whole pattern always leaves me with a question mark over my head like a befuddled cartoon character. However, after looking at the class diagram for a while, it begins to make sense. The unusual aspect of the pattern is that the abstract class Decorator subclasses the abstract class Component. In other words, one abstract class inherits from another. Further, the Decorator &#8220;wraps&#8221; the Component. Figure 1 shows the Decorator class diagram:<br />
<div id="attachment_893" class="wp-caption alignnone" style="width: 485px"><img src="http://www.as3dp.com/wp-content/uploads/2009/04/decoratorsmall1.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; Decorator class diagram&lt;/em&gt;" title="decoratorsmall1" width="475" height="258" class="size-full wp-image-893" /><p class="wp-caption-text"><em><strong>Figure 1:</strong> Decorator class diagram</em></p></div></p>
<p>The major feature of the design pattern is the curving reference (an aggregate relationship) from the<em><strong> Decorator</strong></em> to the <em><strong>Component</strong></em>. The inheritance of the ConcreteComponent is a straight inheritance from an abstract class. That&#8217;s pretty clear. However, the &#8220;wrapping&#8221; looks a bit different and one way to &#8220;see&#8221; it is through the Client. Assuming that the concrete object (the Warrior) is already there, the following code is involved in wrapping the component in a concrete decorator (a Shield, for example.) The code in the Client looks like the following:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p5725code31'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p572531"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p5725code31"><pre class="actionscript" style="font-family:monospace;">conComponent = <span style="color: #000000; font-weight: bold;">new</span> DecShield<span style="color: #66cc66;">&#40;</span>conComponent<span style="color: #66cc66;">&#41;</span>;
mover.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>conComponent.<span style="color: #006600;">getarmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>In this example you have the following:</p>
<ul>
<li>conComponent is typed as a Component</li>
<li>conComponent instantiates a concrete decorator with itself as a parameter </li>
<li>the conComponent instance invokes the getarmor() method, a Component method,  that returns the armor&#8217;s image</li>
</ul>
<p>The trick is that each of the concrete decorators inherits both the Component and Decorator interfaces. As a result, the armor (or anything else) is able to change the appearance of the component without changing the concrete component itself.<br />
<span id="more-5725"></span><br />
<strong>A Running Start</strong></p>
<p>In looking at the code, I think that it may be helpful to begin with the Client that makes a request through the Component abstract class. It&#8217;s a little busy for a Client because it sets up a  UI and allows users to add one concrete decorator at a time. Yet in all, it&#8217;s pretty simple:</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('p5725code32'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p572532"><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
</pre></td><td class="code" id="p5725code32"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">Button</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> conComponent: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> btnShield:<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> btnBodyArmor:<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> btnHelmet:<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> mover: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>
			btnShield.<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>;
			btnShield.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">10</span>,btnShield.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">40</span>,btnShield.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Shield&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnShield<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			btnBodyArmor.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,addBodyArmor<span style="color: #66cc66;">&#41;</span>;
			btnBodyArmor.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">10</span>,btnBodyArmor.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">65</span>,btnBodyArmor.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Body Armor&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnBodyArmor<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			btnHelmet.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,addHelmet<span style="color: #66cc66;">&#41;</span>;
			btnHelmet.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">10</span>,btnHelmet.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">90</span>,btnHelmet.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Helmet&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnHelmet<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			mover.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>conComponent.<span style="color: #006600;">getarmor</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			mover.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">100</span>,mover.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">100</span>;
			addChild<span style="color: #66cc66;">&#40;</span>mover<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>
			conComponent = <span style="color: #000000; font-weight: bold;">new</span> DecShield<span style="color: #66cc66;">&#40;</span>conComponent<span style="color: #66cc66;">&#41;</span>;
			mover.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>conComponent.<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> addBodyArmor<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>
			conComponent = <span style="color: #000000; font-weight: bold;">new</span> DecBodyArmor<span style="color: #66cc66;">&#40;</span>conComponent<span style="color: #66cc66;">&#41;</span>;
			mover.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>conComponent.<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> addHelmet<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>
			conComponent = <span style="color: #000000; font-weight: bold;">new</span> DecHelmet<span style="color: #66cc66;">&#40;</span>conComponent<span style="color: #66cc66;">&#41;</span>;
			mover.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>conComponent.<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>Automatically places the Warrior (concrete component) on the screen and lets the user add decorators one at a time. In studying the unusual way in which requests are made for each decorator, we will now back up into the abstract Component and Decorator classes.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p5725code33'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p572533"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p5725code33"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//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>;
	<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: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In this particular case, the abstract class isn&#8217;t very abstract. The single method sets up the component interface, as either a concrete component or a concrete decorator to be sent back as a Sprite instance. The details of the getarmor() method are specified in the concrete  decorators.</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('p5725code34'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p572534"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p5725code34"><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;">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>
		protected <span style="color: #000000; font-weight: bold;">var</span> char:Component;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The Decorator, which extends the Component, is made up of a single property that is typed as a Component. However, the property is used as a reference to the object decorated and not a change in the concrete component itself.</p>
<p><strong>The Concrete Classes</strong></p>
<p>The concrete classes are ones that provide an image on the screen. All of them were created in the Flash Pro library, but they could have been done by creating sprites classes with Flash Builder. (In a mini-post later on this week, there&#8217;ll be an example of how to use Flash Builder to create graphic Sprites equivalent to those done in Flash Pro.)</p>
<p>The concrete component has only a single component that will be decorated with the concrete decorators. In this case, the concrete component class is named &#8220;Warrior.&#8221; It provides a warrior to dress with armor.</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('p5725code35'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p572535"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p5725code35"><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: #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: #808080; font-style: italic;">//Delphi is a Sprite class</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=<span style="color: #000000; font-weight: bold;">new</span> Delphi<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>The Warrior uses an instance of the Delphi class that is nothing more than a graphic depiction of the warrior to be decorated. (By the way, &#8220;Delphi&#8221; is a conjunction of the Greek letters Delta Phi or DP or <em>Design Patterns</em>!)</p>
<p>The concrete decorators are all tied into the concrete component through the wrapping of the component. Each of the concrete decorators extends the Decorator. However, since the Decorator extends the Component, each of the concrete component also inherit the Components methods and properties. Because the Component class is abstract (in the way it is treated), each method must be overridden. In this way, unique elements can make up the different concrete decorations. However, to keep everything as simple as possible, the Sprite classes are just ones in the Library.</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('p5725code36'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p572536"><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
</pre></td><td class="code" id="p5725code36"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Shield</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;">100</span>,armor.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">100</span>;
			<span style="color: #b1b100;">return</span> armor;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #808080; font-style: italic;">//Body Armor</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 Body Armor</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DecBodyArmor <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> DecBodyArmor<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;">//BodyArmor 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> BodyArmor<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			armor.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">12</span>,armor.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">57</span>;
			<span style="color: #b1b100;">return</span> armor;
		<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;">//Helmet</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 Helmet</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DecHelmet <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> DecHelmet<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;">//Helmet 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> Helmet<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			armor.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">39</span>,armor.<span style="color: #006600;">y</span>=-<span style="color: #cc66cc;">9</span>;
			<span style="color: #b1b100;">return</span> armor;
		<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>Adding Armor</strong></p>
<p>Let me make a couple of suggestions. First, add a weapon or two or more armor. This will entail creating a new concrete decorator. You can make the armor or weapon as simple or complex as you like. If you&#8217;re using Flash Builder, just create a Sprite class with graphics that depict a weapon. (By the way, later this week, I want to do a short post on creating your own image class library if you&#8217;re using Flash Builder. (This is just for beginners; so it will be simple.) Anyway, if you play with it, you&#8217;ll come away understanding it far more than just looking at what I&#8217;ve done. Besides it&#8217;s fun and we&#8217;d really like to see some of your ideas.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2011%2F06%2Fwell-armored-warrior-decorator-for-beginners%2F&amp;title=Well-Armored%20Warrior%3AActionScript%203.0%20Decorator%20for%20Beginners" 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/06/well-armored-warrior-decorator-for-beginners/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Easy and Practical Decorator Design Pattern</title>
		<link>http://www.as3dp.com/2009/04/actionscript-30-easy-and-practical-decorator-design-pattern/</link>
		<comments>http://www.as3dp.com/2009/04/actionscript-30-easy-and-practical-decorator-design-pattern/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 12:03:44 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Decorator]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Design Patterns at Work]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=886</guid>
		<description><![CDATA[An Enigma Wrapped in a Clear Casing The Decorator Design Pattern is an enigma wrapped in a perfectly clear casing. I can think of fewer design patterns that are clearer in terms of what they do or seem more practical. By using concrete decorations, you can change an object without touching its structure. You can [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.as3dp.com/wp-content/uploads/2009/03/bucket.png" alt="bucket" title="bucket" width="150" height="133" class="alignleft size-full wp-image-750" /> <strong>An Enigma Wrapped in a Clear Casing</strong><br />
The Decorator Design Pattern is an enigma wrapped in a perfectly clear casing. I can think of fewer design patterns that are clearer in terms of what they do or seem more practical. By using concrete decorations, you can change an object without touching its structure. You can pick and choose what features you want to add, and your central object (or Concrete Component) is untouched. You want to add another feature? <em>¡No problema!</em> Just add another decoration. Likewise, if you want to remove an unwanted feature, just remove the decoration you no longer want. All of this changing is accomplished without touching the object all the while the object is effectively changed by the decorations. So updating is simply a matter of changing decorations and/or adding new components to be decorated. (Download all files for program<a href="http://www.sandlight.net/Decorator.zip"> here </a>)</p>
<p>In our book, the most real world practical example was a car dealership where different models of hybrid cars (Components) were set up with different accessories (Decorators) to arrive at a final price. As different models of hybrids became available, they could be added to the components and as different accessories came and went, they could be added to the program without having to disrupt the structure of the program. The pricing of the car was based on both on the base price (Component) and the whistles and bells (Decorators) the buyer selected. Once set up, the site is easy to maintain, update and reuse in a different context.</p>
<p>In this post we want to provide a simple yet usable example that can be modified for your own needs. Many of the simple examples we’ve seen (or created ourselves) have made extensive use of string and numeric variables and/or trace output. Given the fact that practical applications make extensive use of such variables, there’s nothing wrong with that. However, we’ve found that the most difficult part of creating design patterns with ActionScript 3.0 is in making use of graphic materials and movie clips.<br />
<span id="more-886"></span><br />
The enigmatic part of the Decorator is in setting it up. In order to see what I mean, take a look at the class diagram in Figure 1.<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/04/decoratorsmall1.png" alt="decoratorsmall1" title="decoratorsmall1" width="475" height="258" class="alignnone size-full wp-image-893" /><br />
<em><strong>Figure 1:</strong> Decorator Class Diagram</em></p>
<p>Most of the details about the way this design pattern works are in our book, but I think a discussion of some of the features of this pattern would be helpful to point out a couple of things that struck me as a bit enigmatic.</p>
<p>In looking at what varies, we find that the variation is <strong>responsibilities of an object without sub-classing</strong>—in other words, <em>use delegation</em>. Of all of the things that can vary in a program structure, those assigned to the Decorator still need further clarification. As you will see in the next section, the Decorator seems to even contradict its own element of variation because it double-subclasses. However, once everything is straightened out, you will find the Decorator example we use to be very simple to implement and even use in a real live work situation.</p>
<p><strong>An Abstract Class as a Subclass</strong></p>
<p>One of the most unusual elements of the Decorator pattern is the fact that the abstract class, Decorator, is a subclass of the base abstract class, Component. The concrete component classes derived directly from the Component class act in a very similar way as the concrete decorator classes. This is because they have an almost identical interface. However, the concrete decorator classes wrap a Component reference inherited from the Decorator.</p>
<p>In some Decorator designs, the wrapper is actually in the abstract Decorator class. They are set up with a constructor in the abstract class itself, but as you know, ActionScript 3.0 has no abstract classes. While it might be possible in the ersatz abstract classes we use in AS3, a constructor seems to be superfluous. First, all of the decorators are typed as <em>components</em> in our design. You will see:</p>
<blockquote><p>
private var <strong>block:Component</strong>=new ConcreteComponent();<br />
…<br />
<strong>block</strong> = new ConcreteDecorator1(<strong>block</strong>);
</p></blockquote>
<p>That’s possible because the abstract Decorator class extends the abstract Component class. Even though the object is instantiated using Component typing, the same interface is inherited in the concrete decorators because their parent class (Decorator) inherits the interface of the Component class.</p>
<blockquote><p><strong>Time Out:</strong> At this point your brain may be saying, <em> I should have listened to my mother who told me to stay away from those design pattern hoodlums. They’re nothing but trouble, and you’ll rue the day that you had anything to do with them. Why don’t you work for your nice cousin, the drug dealer?</em> Well maybe you’re not thinking that, but that kind of code can jolt your brain. It’s perfectly normal, and it’ll go away in 9-10 years or when you die. See? Your mother was right, just like when she told your sister not to date guys with the nickname, <em> killer</em>.</p></blockquote>
<p>Second, the design suggests that the concrete decorators be instantiated by wrapping the component instance. That would mean that the constructors would have to override the parent class constructor function. I don’t suppose there’s anything wrong with that, but I saw nothing to be gained from it. In their work, the Freemans (<em>Head First Design Patterns</em>) similarly did not include the wrapper in the abstract Decorator, even though Java <em>does have an abstract class modifier</em>. In any event, the decorator wraps the component and that allows the component and decorator elements to share much of the design’s structure.</p>
<p><strong>Creating and Using Shape Components and Decorators</strong></p>
<p>Other than the fact that the Decorator abstract class is a subclass of the Component abstract class and the Decorator wraps the Component class, the rest of the design is pretty straightforward. To understand this pattern, I believe that we’d be best served by starting with the results and then drilling down to see how it was done. Figure 2 shows what you will see with the default settings in the Client class.<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/04/decoutput.png" alt="decoutput" title="decoutput" width="285" height="417" class="alignnone size-full wp-image-891" /><br />
<em><strong>Figure 2:</strong> Three Decorators and one Component</em></p>
<p>To both illustrate what’s happening and separate the decorator and component objects, I used a simple design. The black rounded rectangle is a component instance and the three colored round “filters” are decorators. The black component uses a solid fill and the decorators use 50% alpha fills. With this design, adding additional decorators <em>and</em> components is simple. Just add another <em>concrete</em>  decorator or component class. Any shape can be used as either component or decorator.</p>
<p><strong>The Component and Decorator Abstract Classes</strong></p>
<p>In this example, both the base and sub-base classes have been kept simple. Keep in mind that this Decorator pattern is set up to be easy to use and practical at the same time. We’ll begin with the Component class. All it does is to provide the interface for the pattern. It has a single method, <strong>getFigure()</strong>, that can be used by <em>both</em> the Components and Decorators. It returns a Shape object.</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('p886code44'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p88644"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p886code44"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Component</span>
<span style="color: #808080; font-style: italic;">//Abstract class: Do not instantiate</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;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Component
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> figure: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> getFigure<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> figure;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>That’s not difficult at all. Any reference to a Shape object as a return object requires us to import the Shape class, but otherwise, it’s just a plain vanilla return function.</p>
<p>The base Decorator class is also very simple to build. Because it extends the Component class, it too inherits the single <strong>getFigure()</strong> method; so there’s no need to include it again. However, as you can see in the class diagram, it needs to hold a reference to the Component class. In fact, that’s about all it contains that you need to include in the code as can be seen in the following listing:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p886code45'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p88645"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p886code45"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Decorator</span>
<span style="color: #808080; font-style: italic;">//Abstract class: Do not instantiate</span>
package
<span style="color: #66cc66;">&#123;</span>
	<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>
		protected <span style="color: #000000; font-weight: bold;">var</span> block:Component;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Keep in mind that the Decorator is an abstract class, and like all abstract classes, you do not instantiate it. Just because it is a subclass does not mean it can be instantiated.</p>
<p>Some of you who have read the Freemans’ <em>Head First Design Patterns</em> chapter on the Decorator (the URL to which is at the end of this post) may have noticed that they <strong>did</strong> include or re-implement the main operation (<strong> getDescription()</strong>) in the Decorator. However, they did so as an abstract method, which is available in Java but not in ActionScript 3.0. Further, they concatenated another element in their concrete decorators. Nothing is being concatenated to the Shape object in this example, and it wouldn’t be possible anyway—as a concatenation at least.</p>
<p><strong>The Concrete Component and Decorator Classes</strong></p>
<p>Given that this Decorator example is very simple and has only a single method, you will not see a lot differences between the concrete component and the decorators. However, that makes it all the easier to implement and better understand what’s going on. First of all, the <strong> ConcreteComponent</strong> class extends the Component class. The constructor function creates a simple Shape object. Because it is a subclass of the Component class, it has the <strong> getFigure()</strong> method  and the Shape typed figure property. This may seem to be an unnecessary redundancy because the constructor should take care of creating the shape; however, it still needs the <strong> getFigure()</strong> method to return it to a request by the Client 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('p886code46'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p88646"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p886code46"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//ConcreteComponent</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteComponent <span style="color: #0066CC;">extends</span> Component
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ConcreteComponent<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			figure.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x000000,<span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>;
			figure.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRoundRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">250</span>,<span style="color: #cc66cc;">200</span>,<span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>;
			figure.<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: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>You can create as many concrete component classes as you want. Here we’ve just created one to keep it simple. Further on in this post, you will be given an opportunity to add more concrete component classes in a contest involving the Decorator design pattern.<br />
Next, we will create the three decorator classes. You will note that instead of drawing a rounded rectangle, each draws a circle and each uses 50% alpha instead of 100%. Also, the decorator constructors wrap a Component instance. The Shape is created in the getFigure() operation which overrides the same operation in the Constructor class. We could have placed the Shape construction in the constructor function like we did with the ConcreteComponent class but chose not to in order to suggest more flexibility. Besides, the constructor function really should not be used for too much serious work. (See Miško Hevery’s <a href="http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/"> article </a> on making the constructor doing real work.) The following three class are identical except for their names and the fill color of the circles:</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('p886code47'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p88647"><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="p886code47"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Concrete 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;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteDecorator1 <span style="color: #0066CC;">extends</span> Decorator
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ConcreteDecorator1<span style="color: #66cc66;">&#40;</span>block:Component<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">block</span> = block;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> getFigure<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			figure.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xff0000,.5 <span style="color: #66cc66;">&#41;</span>;
			figure.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawCircle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">225</span>,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;
			figure.<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> figure;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>


<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p886code48'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p88648"><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="p886code48"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Concrete 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;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteDecorator2 <span style="color: #0066CC;">extends</span> Decorator
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ConcreteDecorator2<span style="color: #66cc66;">&#40;</span>block:Component<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">block</span> = block;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> getFigure<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			figure.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x00ff00,.5 <span style="color: #66cc66;">&#41;</span>;
			figure.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawCircle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">225</span>,<span style="color: #cc66cc;">200</span>,<span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;
			figure.<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> figure;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>


<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p886code49'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p88649"><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="p886code49"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Concrete 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;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteDecorator3 <span style="color: #0066CC;">extends</span> Decorator
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ConcreteDecorator3<span style="color: #66cc66;">&#40;</span>block:Component<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">block</span> = block;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> getFigure<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			figure.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x0000ff,.5 <span style="color: #66cc66;">&#41;</span>;
			figure.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawCircle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">225</span>,<span style="color: #cc66cc;">300</span>,<span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;
			figure.<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> figure;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>First, note the fact that literals are used for the colors, location and size. You may be thinking that doesn’t provide a lot of runtime flexibility with the shapes. Actually, it’s not that big of a deal. If you want, you can add parameters to the getFigure() operation without losing stride. However, the focus is on what the concrete decorators do and how they can be used as objects to change a concrete component; so for the time being don’t worry about adding parameters. (You can add parameters later.)</p>
<p><strong>The Client</strong></p>
<p>In many of the more recent examples, we’ve been adding user interfaces (UI) to the Client class for making requests from a patterned program in both Flex and Flash. In this example, we do not for two reasons. First, by using a few requests in the client not associated with a UI, you can clearly see how to make requests from the Decorator. Secondly, we’re having a contest where you will be providing your own UI in either Flash or Flex using ActionScript 3.0. (See The ActionScript 3.0 Decorator Design Pattern Contest for rules). For now, though, here’s the Client 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('p886code50'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p88650"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code" id="p886code50"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Client for Decorator Design Pattern</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> block:Component=<span style="color: #000000; font-weight: bold;">new</span> ConcreteComponent<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>
			addChild<span style="color: #66cc66;">&#40;</span>block.<span style="color: #006600;">getFigure</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			block = <span style="color: #000000; font-weight: bold;">new</span> ConcreteDecorator1<span style="color: #66cc66;">&#40;</span>block<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>block.<span style="color: #006600;">getFigure</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			block = <span style="color: #000000; font-weight: bold;">new</span> ConcreteDecorator2<span style="color: #66cc66;">&#40;</span>block<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>block.<span style="color: #006600;">getFigure</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			block = <span style="color: #000000; font-weight: bold;">new</span> ConcreteDecorator3<span style="color: #66cc66;">&#40;</span>block<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>block.<span style="color: #006600;">getFigure</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>In looking at the requests in the Client everything is centered on the <strong>block</strong> Component object.  The object is first typed as a Component and then instantiated as a <strong>ConcreteComponent</strong>. The rest of the Client class does little more than make requests through the <strong>Component.getFigure()</strong> method. Each of the decorators is created by the desired concrete decorator wrapping the <strong>block</strong> object. Of course each concrete decorator is used just once and added to the <strong>DisplayObjectContainer</strong> and then destroyed as the <strong>block</strong> instance is re-instantiated with a new concrete decorator.</p>
<p><strong>Into the Lunch Bucket</strong></p>
<p>Now then, some of you may be thinking, <em>that’s not so bad</em>, and you’d be right. Sometimes the more daunting design patterns turn out to be pretty simple and sensible after all. You can add more concrete component classes by copying, pasting and renaming the ConcreteComponent class to <strong>ConcreteComponentA</strong>, etc. Change the Shape object in the new concrete component class, and Bob’s your uncle, it’s done! Likewise, you can do the same with concrete decorators. Add and change them all you want, and you’ll see that the Decorator design pattern structure handles them all with aplomb. Nothing falls apart or goes <em>boom!</em> It just keeps on truckin’.</p>
<p>You can see the Least Knowledge principle at work here. In part, you can see it in how the changes in the Component are delegated to the Decorator. Also, note that you need not drill down to access the different decorators. One dot does the trick (e.g., <strong>block.getFigure()</strong>) to bring up both the concrete component and decorators.</p>
<p>Finally, you can get a <a href="http://oreilly.com/catalog/hfdesignpat/chapter/ch03.pdf ">Free Chapter</a> on the Decorator Design pattern from the Freeman’s <em>Head First Design Patterns</em> book. All of the examples are in Java, but it’s full of good stuff about using the Decorator, and all of it can be extrapolated for using with ActionScript 3.0. (Did I mention it’s free?)</p>
<p>So between our chapter on the Decorator in our book, this post about the Decorator, and the Freeman’s Decorator chapter, this is one design pattern you ought to be able to use in your work. To get started, why not enter our first <a href="http://www.as3dp.com/2009/04/25/actionscript-30-design-pattern-contest-the-golden-lunch-bucket-awards/">Golden Lunch Bucket Contest</a>? To practice, here’s what you can do (and see how freakin’ easy the pattern is to use):</p>
<ol>
<li>Create a new concrete decorator with an entirely different Shape and add it to the program. </li>
<li>Change the existing Shape in the concrete component and add a new concrete component with yet another shape. </li>
<li>Change the Client to execute the program with the new concrete decorator and new and changed concrete component</li>
</ol>
<p>That ought to do it for this Lunch Bucket Design Pattern.  Be sure to take a look at the first <a href="http://www.as3dp.com/2009/04/25/actionscript-30-design-pattern-contest-the-golden-lunch-bucket-awards/">Golden Lunch Bucket Contest</a> that uses the design pattern in this post!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2009%2F04%2Factionscript-30-easy-and-practical-decorator-design-pattern%2F&amp;title=ActionScript%203.0%20Easy%20and%20Practical%20Decorator%20Design%20Pattern" id="wpa2a_12"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/04/actionscript-30-easy-and-practical-decorator-design-pattern/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Design Pattern Contest: The Golden Lunch Bucket Awards!</title>
		<link>http://www.as3dp.com/2009/04/actionscript-30-design-pattern-contest-the-golden-lunch-bucket-awards/</link>
		<comments>http://www.as3dp.com/2009/04/actionscript-30-design-pattern-contest-the-golden-lunch-bucket-awards/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 21:42:38 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Contests]]></category>
		<category><![CDATA[Decorator]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=880</guid>
		<description><![CDATA[The Contest Okay, here’s a chance for Fame and Fortune. (We’ll supply the former; you supply the latter.) On Monday, April 27 you’ll find a new ActionScript 3.0 Easy and Practical Decorator Lunch Bucket Pattern on this blog. The contest will begin on Monday, April 27 and end Friday May, 22&#8211;2009. (That’s not a lot [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.as3dp.com/wp-content/uploads/2009/04/goldenbucket.png" alt="goldenbucket" title="goldenbucket" width="150" height="150" class="alignleft size-full wp-image-879" /><br />
<strong>The Contest</strong></p>
<p>Okay, here’s a chance for Fame and Fortune. (We’ll supply the former; you supply the latter.) On Monday, April 27 you’ll find a new <a href="http://www.as3dp.com/2009/04/26/actionscript-30-easy-and-practical-decorator-design-pattern/"><strong>ActionScript 3.0 Easy and Practical Decorator Lunch Bucket Pattern</strong></a> on this blog. The contest will begin on <strong>Monday, April 27</strong> and end <strong>Friday May, 22</strong>&#8211;2009. (That’s not a lot of time! But it should not take a lot of time to make the changes.) You can download the Decorator files <a href=http://www.sandlight.net/Decorator.zip> here </a>.</p>
<p>The contest is to see if you can change the Decorator design pattern in the new post, <strong>ActionScript 3.0 Easy and Practical Decorator Design Pattern</strong>.<br />
Using the provided Component class (no changes allowed) and the provided Decorator class (no changes allowed) add new or changed Concrete Components and/or Concrete Decorators to make the most interesting program using the Decorator Design Pattern.</p>
<p>We have four age categories, and each category will have 1st, 2nd, and 3rd prizes. The <strong>Grand Prize</strong> will be given to the very best entry, regardless of age. Here are the categories:</p>
<ul>
<li>Under 18</li>
<li>18-25</li>
<li>26-40</li>
<li>Over 40</li>
</ul>
<p>All winners will be displayed on this blog along with their entries and photos. Read on to learn the rules.<br />
<span id="more-880"></span><br />
<strong>The Rules</strong></p>
<ol>
<li>You cannot change the code in the Component Class</li>
<li> You cannot change the code in the Decorator Class </li>
<li>You can only request concrete decorators or components from the Client class</li>
<li>You cannot change the interfaces or signatures in the concrete decorator or component classes</li>
<li>You cannot add bitmapped graphics or load external bitmapped or SWF files.</li>
<li>You can add UI components to the Client class only and only for making requests for concrete decorators or components.</li>
<li>All entries must be compacted in .zip format</li>
<li>Either Adobe Flex Builder or Flash may be used—if it’s all code you could use Note Pad or TextEdit I suppose.</li>
<li>All ActionScript must be version 3.0</li>
</ol>
<p>If any of the rules are broken, the entry will be disqualified.</p>
<p>All inquiries about the Contest must be made to the Comments Section of this blog entry. The reason is that if one person has a query, there’s usually a dozen more with the same question, and it saves time to respond to a single query than many. (No email inquires will be answered).</p>
<p><strong>All entries must have the following:</strong></p>
<ul>
Name<br />
Age<br />
Subject Line in email: Golden Lunch Bucket Contest<br />
Attached .zip file with your entry</ul>
<p>Email entries to: Bill Sanders: <strong>wdsanders@comcast.net</strong></p>
<p><strong>No entries will be accepted after May 22, 2009</strong></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2009%2F04%2Factionscript-30-design-pattern-contest-the-golden-lunch-bucket-awards%2F&amp;title=ActionScript%203.0%20Design%20Pattern%20Contest%3A%20The%20Golden%20Lunch%20Bucket%20Awards%21" id="wpa2a_14"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/04/actionscript-30-design-pattern-contest-the-golden-lunch-bucket-awards/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>

