<?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; Prototype</title>
	<atom:link href="http://www.as3dp.com/category/design-patterns/prototype/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>ActionScript 3.0 Prototype Design Pattern: A Minimalist Example</title>
		<link>http://www.as3dp.com/2008/10/actionscript-30-prototype-design-pattern-a-minimalist-example/</link>
		<comments>http://www.as3dp.com/2008/10/actionscript-30-prototype-design-pattern-a-minimalist-example/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 21:26:07 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Prototype]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=264</guid>
		<description><![CDATA[The Recursive Clone In describing the Prototype design pattern, the Gang of Four indicate that in the collaborations, the client asks a prototype to clone itself. (Go clone yourself!) As innocuous as that sounds, it turns out to be more than a little work, and the pattern, as GoF indicates, is tricky and easy to [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The Recursive Clone</strong></p>
<p>In describing the Prototype design pattern, the Gang of Four indicate that in the collaborations, the client asks a prototype to clone itself. (<em>Go clone yourself!</em>) As innocuous as that sounds, it turns out to be more than a little work, and the pattern, as GoF indicates, is tricky and easy to get wrong. With ActionScript 3.0, my response is, “<em>…you have no idea!</em>”</p>
<p>In a couple of earlier articles relating to the Prototype design pattern in this blog (<em>ActionScript 3.0 Clone</em> and <em>ActionScript 3.0 Recursive Excursion</em>) we looked at some of the issues relating to key elements of the Prototype design pattern, and if you’re thinking of using the Prototype pattern, you might want to take a look at them and some of the comments that readers have made. I’d prefer not to re-hash them here so that we can focus on the design pattern and not its components or underlying processes that we’ve already discussed. So to get started, let’s take a look at the Prototype class diagram in Figure 1:</p>
<p><a href="http://www.as3dp.com/wp-content/uploads/2008/10/prototype.png"><img src="http://www.as3dp.com/wp-content/uploads/2008/10/prototype.png" alt="" title="prototype" width="500" height="278" class="alignnone size-full wp-image-269" /></a></p>
<p><em><strong>Figure 1:</strong> Prototype Design Pattern</em></p>
<p>Because the pattern includes a Client class, you can see right away where the client connects to the other participants in the pattern and something about the nature of the relationship. The key here is that the client holds an object reference to the Prototype class. So the client is going to have some kind of Prototype reference. </p>
<p><span id="more-264"></span></p>
<p>The second key element is the recursive nature of the cloning process. Each concrete class clones itself with a method inherited from the Prototype class. The problem, of course, is where are we going to get a cloning method and how can it be implemented recursively? One solution is simply to build an abstract class that includes a cloning process and then make the Prototype class a subclass to pass on the clone method. That’s what I did, but by keeping the CloneMachine class abstract, no instance of the <code>CloneMachine</code> class could be instantiated in the <code>Prototype</code> class. Nevertheless, I now had access to a cloning method. In some respects that sounds like strong-arming a solution, but I don’t think so. The Java solutions implemented Clonable in the Prototype class and C# solutions used <code>MemberwiseClone</code> and <code>[Serializable]</code> in their Prototype class (among other solutions). The result is that the clone method <code>doClone(obj)</code> from the inherited class is available for creating a clone method in the Prototype class. In the end, I came up with the design shown in Figure 2:</p>
<p><a href="http://www.as3dp.com/wp-content/uploads/2008/10/prototype2.png"><img src="http://www.as3dp.com/wp-content/uploads/2008/10/prototype2.png" alt="" title="prototype2" width="500" height="367" class="alignnone size-full wp-image-278" /></a></p>
<p><em><strong>Figure 2</strong>: ActionScript Prototype Pattern</em></p>
<p>I cannot claim originality in subclassing one abstract class with another. GoF have a few examples, such as an application on page 224 where a <em>Widget</em> class is a subclass of a <em>HelpHandler</em> class—both are abstract classes.</p>
<p><strong>The CloneMachine and Prototype</strong></p>
<p>The first step is to build the class containing the clone method. As described in the previous article on cloning, this methods uses the <code>ByteArray</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('p264code5'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2645"><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="p264code5"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* Do not implement: Treat as Abstract Class */</span>
<span style="color: #808080; font-style: italic;">/* Utility class : Cloning method*/</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">ByteArray</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CloneMachine
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> cloneWork:ByteArray;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doClone<span style="color: #66cc66;">&#40;</span>source:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #66cc66;">*</span>
		<span style="color: #66cc66;">&#123;</span>
			cloneWork = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			cloneWork.<span style="color: #006600;">writeObject</span><span style="color: #66cc66;">&#40;</span>source<span style="color: #66cc66;">&#41;</span>;
			cloneWork.<span style="color: #0066CC;">position</span> = <span style="color: #cc66cc;">0</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span>cloneWork.<span style="color: #006600;">readObject</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>Now that the clone method is available, it will be used to generate the abstract clone method in the Prototype. This makes it a little easier to use the <code>try..catch..finally</code> statement to check for errors and neatly return the cloned 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('p264code6'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2646"><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
</pre></td><td class="code" id="p264code6"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* Prototype class */</span>
<span style="color: #808080; font-style: italic;">/* Do not implement: Treat as Abstract Class */</span>
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: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #0066CC;">Prototype</span> <span style="color: #0066CC;">extends</span> CloneMachine
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> breed:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> description:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> unique:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> clone<span style="color: #66cc66;">&#40;</span>obj:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Object</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> clone:<span style="color: #0066CC;">Object</span> = <span style="color: #000000; font-weight: bold;">null</span>;
			<span style="color: #0066CC;">try</span>
			<span style="color: #66cc66;">&#123;</span>
				clone = <span style="color: #0066CC;">super</span>.<span style="color: #006600;">doClone</span><span style="color: #66cc66;">&#40;</span>obj<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #0066CC;">catch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:ArgumentError<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #0066CC;">finally</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">return</span> clone;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getDescription<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> description;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getBreed<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> breed;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getUnique<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> unique;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setDescription<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">string</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			description = <span style="color: #0066CC;">string</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setBreed<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">string</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			breed = <span style="color: #0066CC;">string</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setUnique<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">string</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			unique = <span style="color: #0066CC;">string</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 clone method is set up by a simple reference to the parent method (<code>clone = super.doClone(obj);</code>). Placed in a <code>try..catch..finally </code>statement, it catches any errors in cloning the object. Otherwise, it returns a clone. Several different get/set functions provide a set of properties for the prototype. The last property, <strong>unique</strong> is overridden in the two concrete Prototype classes for the purpose of example.</p>
<p><strong>Concrete Prototypes</strong></p>
<p>Keeping everything at a minimal level only two concrete prototypes are introduced, Dog and Cat as shown in the following two 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('p264code7'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2647"><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
</pre></td><td class="code" id="p264code7"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// ActionScript file</span>
<span style="color: #808080; font-style: italic;">//Concrete Prototype: Dog</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Dog <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">Prototype</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> doggyTreat:<span style="color: #0066CC;">String</span>;
&nbsp;
		 override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getUnique<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> doggyTreat;
		<span style="color: #66cc66;">&#125;</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setUnique<span style="color: #66cc66;">&#40;</span>yum:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			doggyTreat=yum;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">/</span> ActionScript file
<span style="color: #808080; font-style: italic;">//Concrete Prototype: Cat</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Cat <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">Prototype</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> kittyToy:<span style="color: #0066CC;">String</span>;
&nbsp;
		 override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getUnique<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> kittyToy;
		<span style="color: #66cc66;">&#125;</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setUnique<span style="color: #66cc66;">&#40;</span>playful:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			kittyToy=playful;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, the two concrete classes do nothing more than provide a single setter and getter pair to illustrate that they are different and yet part of the Prototype. </p>
<p><strong>The Client Makes a Clone</strong></p>
<p>The final step in this minimalist Prototype pattern is to build the Client. The client holds a reference to the Prototype class by creating an instance of the Dog and Cat classes, typing both as a Prototype. Then, using a recursive clone, each instance creates a clone of itself. </p>
<p>Lastly, the clone objects are then traced to view the property values of the cloned objects. This last step is to be sure that the cloned objects actually have the properties and property values of the originals.</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('p264code8'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2648"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code" id="p264code8"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Client</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> tDog:<span style="color: #0066CC;">Prototype</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> cDog:<span style="color: #0066CC;">Object</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> tCat:<span style="color: #0066CC;">Prototype</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> cCat:<span style="color: #0066CC;">Object</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>
			tDog = <span style="color: #000000; font-weight: bold;">new</span> Dog<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			tDog.<span style="color: #006600;">setDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Big compulsive brown, black and tan&quot;</span><span style="color: #66cc66;">&#41;</span>;
			tDog.<span style="color: #006600;">setUnique</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Bacon&quot;</span><span style="color: #66cc66;">&#41;</span>;
			tDog.<span style="color: #006600;">setBreed</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Greater Swiss Mountain Dog&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			cDog = tDog.<span style="color: #006600;">clone</span><span style="color: #66cc66;">&#40;</span>tDog<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>cDog.<span style="color: #006600;">breed</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>cDog.<span style="color: #006600;">description</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Treat: &quot;</span>+cDog.<span style="color: #006600;">doggyTreat</span> +<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			tCat = <span style="color: #000000; font-weight: bold;">new</span> Cat  ;
			tCat.<span style="color: #006600;">setDescription</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Sleek, independent, black and tan&quot;</span><span style="color: #66cc66;">&#41;</span>;
			tCat.<span style="color: #006600;">setUnique</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;catnip&quot;</span><span style="color: #66cc66;">&#41;</span>;
			tCat.<span style="color: #006600;">setBreed</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Siamese&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			cCat = tCat.<span style="color: #006600;">clone</span><span style="color: #66cc66;">&#40;</span>tCat<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>cCat.<span style="color: #006600;">breed</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>cCat.<span style="color: #006600;">description</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Favorite toy: &quot;</span>+ cCat.<span style="color: #006600;">kittyToy</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 will see the following output when you test it:<br />
<code><br />
Greater Swiss Mountain Dog<br />
Big compulsive brown, black and tan<br />
Treat: Bacon</p>
<p>Siamese<br />
Sleek, independent, black and tan<br />
Favorite toy: catnip<br />
</code></p>
<p>Before looking at the uses of the Prototype, you should be aware that the cloned objects do not have the methods of the original. For example, the <code>tDog</code> instance can use all of the methods of both the <strong>Prototype</strong> and <strong>Dog</strong> classes. However, the cloned instance, <code>cDog</code> cannot use any of them. We had to have a public property declared in the Prototype and concrete Prototype classes to bring out the properties and their values in the clones. That is, rather than being able to use the getter methods, we had to refer directly to the properties. For example, we had to use</p>
<p><code>    <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cCat.kittyToy;</strong></code></p>
<p>instead of,</p>
<p><code><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cCat. getUnique();</strong></code></p>
<p>That’s a little mystery I’d like to solve.</p>
<p><strong>Uses of the Prototype</strong></p>
<p>As is often the case in looking at design patterns, the best use case I found was in <em>Head First Design Patterns</em>. The Freemans suggest a <strong><em>monster maker</em></strong> where prototypes of monsters would be used to generate clones in a game where monsters were routinely dispatched and re-generated. Cloning would be simpler and less expensive than recreating new monsters in a rapidly changing environment. The idea is to make a prototypical instance and then clone it whenever the system requires a new one. This is especially useful when the classes to instantiate are done so at run-time. In a an environment where any combination of creations are required at unknown points, the Prototype is one pattern to consider.</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%2F2008%2F10%2Factionscript-30-prototype-design-pattern-a-minimalist-example%2F&amp;title=ActionScript%203.0%20Prototype%20Design%20Pattern%3A%20A%20Minimalist%20Example" 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/2008/10/actionscript-30-prototype-design-pattern-a-minimalist-example/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>An ActionScript 3.0 Recursion Excursion</title>
		<link>http://www.as3dp.com/2008/10/an-actionscript-30-recursion-excursion/</link>
		<comments>http://www.as3dp.com/2008/10/an-actionscript-30-recursion-excursion/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 01:23:25 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Prototype]]></category>
		<category><![CDATA[Recursion]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=256</guid>
		<description><![CDATA[Recursion is one of those things I’ve been thinking about lately because of the recursive classes in the Prototype pattern. However, recursion isn’t something that I think about much, but at the OOPSLA conference in Nashville that is just now wrapping up, it came up in as a favored method over loops. A professor from [...]]]></description>
			<content:encoded><![CDATA[<p>Recursion is one of those things I’ve been thinking about lately because of the recursive classes in the Prototype pattern. However, recursion isn’t something that I think about much, but at the OOPSLA conference in Nashville that is just now wrapping up, it came up in as a favored method over loops. A professor from Cornell University was making a presentation at one of the sessions I attended, and he noted that they introduce students to <b>recursion</b> before introducing <b>loops</b> , getting enthusiastic approval from some in the audience. Because I’ve been beating my head against the Prototype pattern, this was more than a little interesting. What’s so special about recursions?</p>
<p><strong>Another Loop?</strong></p>
<p>In some respects, recursion is just another looping structure, but programmers tend to think about them differently, and I thought I’d see what I could find from some of the big brains at OOPSLA. I talked with Dr. Axel Schreiner about recursion and got all I could ever imagine anyone would want to about them. However, like many, he considered recursion to be a loop structure and didn’t seem to be particularly excited whether loops or recursive structures were employed. For Axel, recursion and looping were a matter of looking at repeated actions in different ways.</p>
<p>Defined, <em>a recursion is a method (or function) that can call itself</em>. That doesn’t sound like a loop, but it does have a repeating element in that a recursive function repeats a process by the simple fact of calling itself. The most common examples of recursion is a factorial, but Fibonacci sequences and Towers of Hanoi are other common examples used to illustrate recursion. Since Fibonacci sequences are one of those just plain cool math structures, I thought I’d start with a Fibonacci sequence.</p>
<p><strong>A Fibonacci Example</strong></p>
<p>A Fibonacci sequence is one where each number in a sequence is made up of the sum of the previous two in the sequence. The sequence begins with 0 and 1:</p>
<p><tt>0 1 1 2 3  5  8   13  21</tt></p>
<p>Rather than using a loop to generate the sequence, the recursive function in the following example takes a parameter (r) and returns the next value in the sequence after r. The following class calls the recursive function via a <code>trace() </code>statement. (I was using Flex, but Flash works fine for the same function.)</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('p256code11'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p25611"><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
</pre></td><td class="code" id="p256code11"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*Fibonacci  generated in a recursive function*/</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> FibRecursion <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> FibRecursion<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>fibo<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</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> fibo<span style="color: #66cc66;">&#40;</span>r:uint<span style="color: #66cc66;">&#41;</span>:uint
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>r==<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">0</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>r==<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">return</span> <span style="color: #cc66cc;">1</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">return</span> fibo<span style="color: #66cc66;">&#40;</span>r-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> + fibo<span style="color: #66cc66;">&#40;</span>r-<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The recursion occurs in the third <code>return</code> statement. That is, part of the <code>fibo()</code>  function calls itself.  As you will see, the output is,</p>
<p><code>21</code></p>
<p>The question is, <em>what’s going on inside the cursive function? </em></p>
<p><span id="more-256"></span></p>
<p><strong>A Simple Recursive Function</strong></p>
<p>While the Fibonacci is fun and shows that a recursive function is possible in ActionScript 3.0, it doesn’t really show too much about the inner workings of the recursive method.  So I put together a simple class with a recursive structure.</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('p256code12'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p25612"><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="p256code12"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// Simple Recursion</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> Simple <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Simple<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;End value: &quot;</span>+recursive<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</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> recursive<span style="color: #66cc66;">&#40;</span>n:uint<span style="color: #66cc66;">&#41;</span>:uint
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&lt;</span>=<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #b1b100;">return</span> n +<span style="color: #cc66cc;">100</span>;
			<span style="color: #b1b100;">else</span>
				<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Trace:&quot;</span>+recursive<span style="color: #66cc66;">&#40;</span>n-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #b1b100;">return</span> n;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Keeping in mind that every trace() of the function fires the function, we need to be judicious in where we place our trace statements. In the recursive function, recursively named, <em>recursive</em>, the return in the first condition adds 100 to values of less than or equal to one. (I could not concatenate a string because the return must be an unsigned integer.) The class generates the following output: </p>
<p><code></p>
<p>Trace:101<br />
Trace:2<br />
Trace:3<br />
Trace:4<br />
Trace:5<br />
Trace:6<br />
Trace:7<br />
Trace:8<br />
Trace:9<br />
End value: 10</p>
<p></code></p>
<p>As you can see, the first return value is 1 plus 100. After the first value, the next is 2, and so forth until 9, and then the terminal output occurs as 10, the value of the initial function argument.</p>
<p>This little excursion into recursion is another side trip on the way to a ground up Prototype design pattern that I’m still working on. I’ve looked at a lot of examples from different languages; unfortunately, they have certain built-in features for cloning or abstract classes lacking in ActionScript 3.0—or ECMAScript 264, ref 4. However, like all of the other patterns, eventually this one will get wrestled to the ground.</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%2F2008%2F10%2Fan-actionscript-30-recursion-excursion%2F&amp;title=An%20ActionScript%203.0%20Recursion%20Excursion" 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/2008/10/an-actionscript-30-recursion-excursion/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Clone: A Prelude to the Prototype Design Pattern</title>
		<link>http://www.as3dp.com/2008/09/actionscript-30-clone-a-prelude-to-the-prototype-design-pattern/</link>
		<comments>http://www.as3dp.com/2008/09/actionscript-30-clone-a-prelude-to-the-prototype-design-pattern/#comments</comments>
		<pubDate>Tue, 23 Sep 2008 13:59:52 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Prototype]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=233</guid>
		<description><![CDATA[Where’s the Clone()? The next design pattern I plan to tackle in ActionScript 3.0 is the Prototype Design Pattern. Getting started I ran into the clone() method in other languages like C#. As usual, that wasn’t much help. To my surprise, I found that several classes in ActionScript 3.0 have a clone() method such as [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Where’s the Clone()?</strong></p>
<p>The next design pattern I plan to tackle in ActionScript 3.0 is the Prototype Design Pattern. Getting started I ran into the <strong>clone()</strong> method in other languages like C#. As usual, that wasn’t much help. To my surprise, I found that several classes in ActionScript 3.0 have a <strong>clone()</strong> method such as the <strong>Rectangle</strong> class. However, while perfectly functional, cloning a rectangle with origins in the <strong>flash.geom</strong> namespace isn’t exactly what I had hoped for. (There’s no clone() method in the <strong>Shape</strong> class where I could make lots of clones of rectangle shapes I could put on the stage.) What I wanted was a way to clone objects.</p>
<p><strong>Borrowing from Java</strong></p>
<p>Buried in the <em>Adobe ActionScript 3.0 Live Docs</em> is a little note about cloning arrays. (see <a href="http://livedocs.adobe.com/flex/3/html/10_Lists_of_data_6.html">http://livedocs.adobe.com/flex/3/html/10_Lists_of_data_6.html</a>) Using a method commonly found in Java programs, the note indicates that a <em>deep copy</em> clone is possible using this method. One can use <strong>concat()</strong> or <strong>slice()</strong> with no arguments to make a shallow copy. In most cases, GoF note that shallow copies will work fine, but for more complex structures a deep copy is required.</p>
<p>The docs list the key method as 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('p233code16'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23316"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p233code16"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">utils</span>.<span style="color: #006600;">ByteArray</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> clone<span style="color: #66cc66;">&#40;</span>source:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #66cc66;">*</span>
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">var</span> myBA:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    myBA.<span style="color: #006600;">writeObject</span><span style="color: #66cc66;">&#40;</span>source<span style="color: #66cc66;">&#41;</span>;
    myBA.<span style="color: #0066CC;">position</span> = <span style="color: #cc66cc;">0</span>;
    <span style="color: #b1b100;">return</span><span style="color: #66cc66;">&#40;</span>myBA.<span style="color: #006600;">readObject</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></pre></td></tr></table></div>

<p>That’s a nice little piece of code and simple to understand. The clone() method creates a <strong>ByteArray</strong> that writes the object (an array) and then returns it by reading the object it just wrote. Moreover, it’s a deep copy and as such a true clone in that all of the parts are delivered and not just pointers.</p>
<p><span id="more-233"></span></p>
<p><strong>A Cloning Class</strong></p>
<p>Cloning an array is fine, and I even added a note in <em>Live Docs </em>first asking for an example and then provided one. (I’m not trying to befuddle those who screen comments on <em>Live Docs</em>—it’s just an added bonus.) What I really want to do is to clone an object, and since an array is an object, it seemed to be an easy enough task. So I took the basic algorithm and made it the key method of a <strong>Clone</strong> class. Here’s the code:</p>
<p><em>Clone.as</em></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('p233code17'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23317"><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="p233code17"><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;">utils</span>.<span style="color: #006600;">ByteArray</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> Clone <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> cloneWork:ByteArray;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Clone<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//Constructor</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doClone<span style="color: #66cc66;">&#40;</span>source:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #66cc66;">*</span>
		<span style="color: #66cc66;">&#123;</span>
			cloneWork=<span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			cloneWork.<span style="color: #006600;">writeObject</span><span style="color: #66cc66;">&#40;</span>source<span style="color: #66cc66;">&#41;</span>;
			cloneWork.<span style="color: #0066CC;">position</span> = <span style="color: #cc66cc;">0</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span>cloneWork.<span style="color: #006600;">readObject</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>Next, I created an object with lots of properties of different types—string, number and Boolean. Then, I tried cloning it, and it seemed to work. The following listing shows the client class that used the clone method—<strong>doClone()</strong> from the <strong>Clone</strong> class:</p>
<p><em>ObjectClone.as</em></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('p233code18'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p23318"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code" id="p233code18"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ObjectClone <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> clone:Clone;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> webLanguages:<span style="color: #0066CC;">Object</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> moreWeb:<span style="color: #0066CC;">Object</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ObjectClone<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			webLanguages=<span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>;
			webLanguages.<span style="color: #006600;">flex</span>=<span style="color: #ff0000;">&quot;ActionScript 3.0&quot;</span>;
			webLanguages.<span style="color: #006600;">dotNet</span>=<span style="color: #ff0000;">&quot;C#&quot;</span>;
			webLanguages.<span style="color: #006600;">php</span>=<span style="color: #ff0000;">&quot;PHP&quot;</span>;
			webLanguages.<span style="color: #006600;">inHtml</span>=<span style="color: #ff0000;">&quot;JavaScript&quot;</span>;
			webLanguages.<span style="color: #0066CC;">bandWidth</span>=<span style="color: #cc66cc;">2500000</span>;
			webLanguages.<span style="color: #006600;">usesFMS</span>=<span style="color: #000000; font-weight: bold;">true</span>;
&nbsp;
			<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> w:<span style="color: #66cc66;">*</span> <span style="color: #b1b100;">in</span> webLanguages<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Property &quot;</span>+w+<span style="color: #ff0000;">&quot;=&quot;</span>+webLanguages<span style="color: #66cc66;">&#91;</span>w<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;------------------------&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			moreWeb=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			clone=<span style="color: #000000; font-weight: bold;">new</span> Clone<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			moreWeb = clone.<span style="color: #006600;">doClone</span><span style="color: #66cc66;">&#40;</span>webLanguages<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> k:<span style="color: #66cc66;">*</span> <span style="color: #b1b100;">in</span> moreWeb<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Property &quot;</span>+k+<span style="color: #ff0000;">&quot;=&quot;</span>+moreWeb<span style="color: #66cc66;">&#91;</span>k<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The following output shows that the cloned object has all of the properties and values of the original:<br />
 <code><br />
 Property flex=ActionScript 3.0<br />
 Property dotNet=C#<br />
 Property usesFMS=true<br />
 Property php=PHP<br />
 Property inHtml=JavaScript<br />
 Property bandWidth=2500000<br />
 ------------------------<br />
 Property flex=ActionScript 3.0<br />
 Property dotNet=C#<br />
 Property bandWidth=2500000<br />
 Property usesFMS=true<br />
 Property php=PHP<br />
 Property inHtml=JavaScript<br />
 </code></p>
<p>The mystery is why the output is ordered the way it is. (If anyone knows, please send a comment.) All of the pieces to the objects are there in the clone, and they reflect the correct values of the original. The clone may seem a bit Frankenstein-ish, since the parts are in a different order, but the original seemed to move the parts around as well; so I’m not too concerned.</p>
<p><strong>Next Stop: Prototype Design Pattern</strong></p>
<p>Now that I have a working clone method for cloning objects, the next step will be working it into the <strong>Prototype</strong> pattern. The issue of <em>serialization</em> has not been broached here, but it is a topic I hope to take up in discussing the Prototype. Also, there’s not a lot of difference between an Object <em>as used in this example</em> and an <em>associative array</em> (ActionScript’s hash table equivalent). So, I may make it look more like an associative array rather than a plain vanilla object even though I like the idea of an object better. Also, I believe that using the Prototype Design Pattern beyond a simple example may require more work in the clone department to clone more sophisticated objects like MovieClips and Components.</p>
<p>Comments encouraged!!</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%2F2008%2F09%2Factionscript-30-clone-a-prelude-to-the-prototype-design-pattern%2F&amp;title=ActionScript%203.0%20Clone%3A%20A%20Prelude%20to%20the%20Prototype%20Design%20Pattern" 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/2008/09/actionscript-30-clone-a-prelude-to-the-prototype-design-pattern/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
	</channel>
</rss>

