<?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; Builder</title>
	<atom:link href="http://www.as3dp.com/category/design-patterns/builder/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 Builder Design Pattern Part II: Multiple Builds</title>
		<link>http://www.as3dp.com/2009/12/actionscript-3-0-builder-design-pattern-part-ii-multiple-builds/</link>
		<comments>http://www.as3dp.com/2009/12/actionscript-3-0-builder-design-pattern-part-ii-multiple-builds/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 15:45:18 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Builder]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=2212</guid>
		<description><![CDATA[As a concrete follow-up to Part I of this two-part post, I thought that once I decided on which graphics, text and video to use the development would go pretty quickly. It took a lot longer than I thought, but now that the concrete example is ready it’s pretty easy to see how to put [...]]]></description>
			<content:encoded><![CDATA[<p>As a concrete follow-up to <a href="http://www.as3dp.com/2009/03/17/actionscript-30-builder-design-pattern-part-i-controlling-creation/">Part I</a> of this two-part post, I thought that once I decided on which graphics, text and video to use the development would go pretty quickly. It took a lot longer than I thought, but now that the concrete example is ready it’s pretty easy to see how to put a practical Builder together and re-use and extend it. Because this second part builds on the first part, if you’re only vaguely familiar with the Builder Design Pattern, you might want to review Part I.</p>
<p><strong>What to Build?</strong></p>
<p>In <a href="http://www.as3dp.com/2009/03/17/actionscript-30-builder-design-pattern-part-i-controlling-creation/">ActionScript 3.0 Builder Design Pattern Part I</a>, we set up a vacation-building site using trace statements to display different characteristics. To make these characteristics come alive and work in some practical matter, the goal is to create a builder that does the following:</p>
<ul>
<li>Load an external graphic </li>
<li>Play a video that can be displayed in any format used by Flash Media Server or Progressive Download</li>
<li>Display a different background for each vacation destination. The background is made of an external .swf file</li>
<li>Present text write-up using external text source</li>
</ul>
<p>In looking at those four actions, you might be thinking, that’s not exactly rocket science. You’d be right. However, when you start adding more and more vacation destinations, keeping track of everything can dampen one’s resolution. With the Builder, though, I found it very easy to add, change, and delete. In fact, I encourage you to add your own favorite vacation place and add it using the builder described in this post.</p>
<p><strong>The Design</strong></p>
<p>In Part I, I talked about different variations in the Builder’s design. The one we’re using closely follows the first as far as the Product class is concerned. All of the concrete builder classes use the same Product class. The Product class  assembles the different parts under construction. However, in order to keep the Product class manageable, I added several Helper classes to take care of the details for the different parts that need to be built for the final product. Figure 1 shows the file pattern for the design:<br />
<div id="attachment_2177" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2009/12/builderFilesFit.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1: &lt;/strong&gt; File Class Diagram of Builder Design Pattern&lt;/em&gt;" title="builderFilesFit" width="500" height="447" class="size-full wp-image-2177" /><p class="wp-caption-text"><em><strong>Figure 1: </strong> File Class Diagram of Builder Design Pattern</em></p></div></p>
<p>As you can see, several classes use the Product class, and it uses all of the helper classes. Even the Client holds a reference to the Product class.  As far as design is concerned, though, this one is identical to the one we used in Part I with the added helper classes.<br />
<span id="more-2212"></span><br />
<strong>The Client Class</strong></p>
<p>One of the interesting features of the Client class in this design is that it initiates the request through the Director and then holds a reference to the Product whose creation it initiated. (It’s sort of like throwing a ball and then running to catch it.) To see the path initiated by the Client’s request consider the following sequence:</p>
<ol>
<li>private var director:<strong>Director</strong>=new Director();<br />
Client instantiates instance of the Director class. The Director has a single method, construct, and instantiating an instance of Director makes the construct method available.</li>
<li>private var <strong>trip1</strong>:IBuilder=new ConcreteBuilder1();<br />
A variable specifies which concrete product it wants constructed. </li>
<li>private var srilanka:<strong>Product</strong>;<br />
A reference is established in a variable to the Product.</li>
<li>director.<strong>construct(trip1)</strong>;<br />
The Director instance constructs the specified concrete builder</li>
<li>srilanka=trip1.<strong>getResult()</strong>;<br />
The Builder instance passes the constructed elements to the Product reference</li>
<li>srilanka.<strong>disclose()</strong>;<br />
The Product variable creates the details of the concrete builder using the helper classes to handle the details of each element. It takes care of housekeeping chores by removing the previous build from the display list.</li>
</ol>
<p>The following listing shows the entire Client class. The constructor function brings up a background and places the buttons on the display list. I suppose there&#8217;s some irony in throwing together a &#8220;home page&#8221; with no information other than three buttons and the background pattern. I could have added all of the elements that the individual vacation destinations include, but instead I kept it open for anyone who wants to add a more substantial home page.</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('p2212code10'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221210"><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
</pre></td><td class="code" id="p2212code10"><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> magicCarpet: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> navContainer: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> il:ImageLoader;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> director:Director=<span style="color: #000000; font-weight: bold;">new</span> Director<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">//Concrete builders</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> trip1:IBuilder=<span style="color: #000000; font-weight: bold;">new</span> ConcreteBuilder1<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> trip2:IBuilder=<span style="color: #000000; font-weight: bold;">new</span> ConcreteBuilder2<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> trip3:IBuilder=<span style="color: #000000; font-weight: bold;">new</span> ConcreteBuilder3<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #808080; font-style: italic;">//Products</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> srilanka:Product;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sanfrancisco:Product;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> prague:Product;
		<span style="color: #808080; font-style: italic;">//Buttons</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btn1:<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> btn2:<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> btn3:<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>;
&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>
			il=<span style="color: #000000; font-weight: bold;">new</span> ImageLoader<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;vacationBG.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>magicCarpet<span style="color: #66cc66;">&#41;</span>;
			magicCarpet.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>il<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>navContainer<span style="color: #66cc66;">&#41;</span>;
			setButtons<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setButtons<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			btn1.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">16</span>;
			btn1.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">64</span>;
			btn1.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Sri Lanka&quot;</span>;
			btn1.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,goSriLanka<span style="color: #66cc66;">&#41;</span>;
			navContainer.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>btn1<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			btn2.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">16</span>;
			btn2.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">96</span>;
			btn2.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;San Francisco&quot;</span>;
			btn2.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,goSanFrancisco<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btn2<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			btn3.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">16</span>;
			btn3.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">128</span>;
			btn3.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Prague&quot;</span>;
			btn3.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,goPrague<span style="color: #66cc66;">&#41;</span>;
			navContainer.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>btn3<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> goSriLanka<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>
			carpetCleaner<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			director.<span style="color: #006600;">construct</span><span style="color: #66cc66;">&#40;</span>trip1<span style="color: #66cc66;">&#41;</span>;
			srilanka=trip1.<span style="color: #006600;">getResult</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			srilanka.<span style="color: #006600;">disclose</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			magicCarpet.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>srilanka<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> goSanFrancisco<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>
			carpetCleaner<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			director.<span style="color: #006600;">construct</span><span style="color: #66cc66;">&#40;</span>trip2<span style="color: #66cc66;">&#41;</span>;
			sanfrancisco=trip2.<span style="color: #006600;">getResult</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			sanfrancisco.<span style="color: #006600;">disclose</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			magicCarpet.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>sanfrancisco<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> goPrague<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>
			carpetCleaner<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			director.<span style="color: #006600;">construct</span><span style="color: #66cc66;">&#40;</span>trip3<span style="color: #66cc66;">&#41;</span>;
			prague=trip3.<span style="color: #006600;">getResult</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			prague.<span style="color: #006600;">disclose</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			magicCarpet.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>prague<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> carpetCleaner<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span>magicCarpet.<span style="color: #006600;">numChildren</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				magicCarpet.<span style="color: #006600;">removeChildAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, the Client is pretty busy, and as the number of vacation destinations grows the Client will need the assistance of helper classes to generate the necessary UIs to handle a wider variety of requests.</p>
<p><strong>The Director Class and IBuilder Interface</strong></p>
<p>In some respects the Director is like a template method. It lines up the operations required to build a product. However, the operations all use the builder interface (IBuilder) to set up the steps to put everything together. What’s more, both the Director and IBuilder are identical to the same class and interface used in the abstract Builder used in Part I. The following listing shows the two unchanged design participants:</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('p2212code11'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221211"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code" id="p2212code11"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Director</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Director
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> construct<span style="color: #66cc66;">&#40;</span>builder:IBuilder<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			builder.<span style="color: #006600;">buildHotel</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			builder.<span style="color: #006600;">buildSight</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			builder.<span style="color: #006600;">buildEvent</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			builder.<span style="color: #006600;">buildWriteUp</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Builder interface</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">interface</span> IBuilder
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> buildHotel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> buildSight<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> buildEvent<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> buildWriteUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> getResult<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Product;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The IBuilder interface holds a reference to the Product in the <strong>getResult()</strong> method. In this particular design, I added it because all of the concrete builder classes use the method. By doing so, I was able to type the individual concrete builders to the interface (keeping things loose) and then call the <strong>getResult()</strong> method from a builder instance. (See the Client listing above.)</p>
<p><strong>The Concrete Builders</strong></p>
<p>Even the concrete builders seem fairly abstract in this design. All they do is pass strings to the product for construction via the helper classes. However, the concrete builders, like just about everything else in a design pattern, are communication nodes. Remember, in OO,</p>
<blockquote><p>a process is expressed as a succession of messages across objects</p></blockquote>
<p>So what we&#8217;re seeing is the process of  messages passed through the classes (objects). In the concrete builders, the content of the messages is carved in the stone of literal values. This may bring up some short with the query, <em>Why didn&#8217;t you put them in variables?</em> First, keep in mind that literals are assigned <em>somewhere</em>, and all that&#8217;s being done is placing the assignment in the concrete builders. Second, adding variables would only back up the assignment of the literals and add unnecessary code.</p>
<p>Other than assembling the product, the concrete builders are also returning a completed product. Given the complex nature of the building process, one might wonder whether the job gets done by the time its ready to return something. The process returns the Product object in some shape, but not necessarily the one you&#8217;re expecting. After all, this builder is accessing video, loading text, graphics, and SWF files. What if the FMS server isn&#8217;t quite awake? All kinds of things have to happen, and I&#8217;ve found that on initial tests some parts get left behind or are a bit late in getting in place. Preloaders give me the creeps because they imply you haven&#8217;t paid attention to the size of your files and the user is going to be kept waiting. For a prime time version of this application, you might want to think about some kind of notification that the product is fully assembled and perhaps a<em>&#8221; be with you in a jiffy&#8221;</em> note to the user who&#8217;s waiting for the product.</p>
<p>I placed the finished product on three different servers and got three different reactions. On my LAN, the San Francisco video (plain vanilla FLV running on Flash Media Server) works perfectly, but when I posted it on a different remote Flash Media Server, it did a quick entrance and exit. One server did not play the progressive download, while another played the progressive download but did not put in the background. Click the Play button and see what you get.<br />
<a href="http://www.sandlight.net/context/"><img src="http://www.as3dp.com/wp-content/uploads/2009/12/play.jpg" alt="play" title="play" width="106" height="46" class="alignnone size-full wp-image-2255" /></a></p>
<p>The following three listings show the concrete builders in the design:</p>
<p>Sri Lanka is the first destination. The video uses a progressive download, and in case you can&#8217;t work with Flash Media Server 3.5.2 (or later), you can change all of the concrete builders to work with progressive downloads as well. All you have to do is the use the &#8220;.flv&#8221; suffix. (See how easy it is to change things using a Builder pattern?)</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2212code12'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221212"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code" id="p2212code12"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteBuilder1 <span style="color: #0066CC;">implements</span> IBuilder
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> product:Product = <span style="color: #000000; font-weight: bold;">new</span> Product<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> buildHotel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;srilanka/hotel.jpg&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildSight<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;srilanka/sriLankaBG.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildEvent<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;srilanka/sriLanka.flv&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildWriteUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;srilanka/sriLanka.txt&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getResult<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Product
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> product;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Figure 2 shows what the builder generates for this concrete builder:</p>
<div id="attachment_2201" class="wp-caption alignnone" style="width: 495px"><img src="http://www.as3dp.com/wp-content/uploads/2009/12/rlEx.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2:&lt;/strong&gt; Sri Lanka is colorful, and the background reflects this character &lt;/em&gt;" title="rlEx" width="485" height="365" class="size-full wp-image-2201" /><p class="wp-caption-text"><em><strong>Figure 2:</strong> Sri Lanka is colorful, and the background reflects this character </em></p></div>
<p>The second destination is San Francisco. Everything is pretty much the same except the video file is being streamed from Flash Media Server. If you want to run an FLV file named sanFrancisco using progressive download, just change the value to &#8220;sanFrancisco.flv&#8221; and any local folder in use.</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('p2212code13'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221213"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p2212code13"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteBuilder2 <span style="color: #0066CC;">implements</span> IBuilder
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> product:Product = <span style="color: #000000; font-weight: bold;">new</span> Product<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> buildHotel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;sf/hotel.jpg&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildSight<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;sf/sfBG.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildEvent<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;sanFrancisco&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildWriteUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;sf/sf.txt&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getResult<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Product
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> product;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Coming up with a color combination for San Francisco was a little tricky, and I finally settled on one I found at <a href="http://kuler.adobe.com/#themes/newest?time=0">Kuler</a>. (In fact I got all of my color combinations there.) Figure 3 shows the results:</p>
<div id="attachment_2202" class="wp-caption alignnone" style="width: 495px"><img src="http://www.as3dp.com/wp-content/uploads/2009/12/sflEx.png" alt="&lt;em&gt;&lt;strong&gt;Figure 3:&lt;/strong&gt; San Francisco&#039;s Fairmont Hotel&#039;s lobby is featured&lt;/em&gt;" title="sflEx" width="485" height="365" class="size-full wp-image-2202" /><p class="wp-caption-text"><em><strong>Figure 3:</strong> San Francisco's Fairmont Hotel's lobby is featured</em></p></div>
<p>The third concrete builder is built around a visit to Prague, Czech Republic I took. The color combination for the background is named on Kuler, <em>Mala Strana</em> (little district) where I happened to stay on my visit. The video is an HD one taken with a Mino HD Flip camera and rendered into H.264 format—f4v. You will notice that the <strong>buildEvent()</strong> method passes a literal with a long reference—&#8221;mp4:pragueFolk.f4v&#8221;. Unfortunately, H.264 format is not available for progressive download, but you can substitute any .flv file you want.</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('p2212code14'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221214"><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="p2212code14"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteBuilder3 <span style="color: #0066CC;">implements</span> IBuilder
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> product:Product = <span style="color: #000000; font-weight: bold;">new</span> Product<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> buildHotel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//graphic</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;prague/hotel.jpg&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildSight<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//swf file</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;prague/pragueBG.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildEvent<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//video</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mp4:pragueFolk.f4v&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildWriteUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//import text</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;prague/prague.txt&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getResult<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Product
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> product;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Figure 4 shows the combined elements for this third rendered build:<br />
<div id="attachment_2206" class="wp-caption alignnone" style="width: 495px"><img src="http://www.as3dp.com/wp-content/uploads/2009/12/prEx.png" alt="&lt;em&gt;&lt;strong&gt;Figure 4: &lt;/strong&gt; Prague&#039;s flavor is captured in text, image and video.&lt;/em&gt;" title="prEx" width="485" height="365" class="size-full wp-image-2206" /><p class="wp-caption-text"><em><strong>Figure 4: </strong> Prague's flavor is captured in text, image and video.</em></p></div></p>
<p>While the classes that make up the concrete builders are very similar, you can see that the exact nature of the builds and their content is quite different.</p>
<p><strong>The Product Class and Helper Classes</strong></p>
<p>All of the heavy lifting is done by the Product class and its helpers. (Think of the Product class as Santa Claus and the helpers as his elves.)  As you can see in the Product class listing, the object uses an array to assemble the product. Then it goes through the array using a switch statement to create the requested element using the helper classes (ImageLoader, EventPlayer and TextLoader). Once it is finished, it empties the array (features.length=0).</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('p2212code15'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221215"><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
</pre></td><td class="code" id="p2212code15"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Product <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> features:<span style="color: #0066CC;">Array</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> ep:EventPlayer;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> tl:TextLoader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> il:ImageLoader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bg:ImageLoader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> productHolder:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> assemble<span style="color: #66cc66;">&#40;</span>...<span style="color: #006600;">rest</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			features.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>rest<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> disclose<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			cleanProduct<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>productHolder<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #000000; font-weight: bold;">var</span> counter:uint=<span style="color: #cc66cc;">0</span>;
&nbsp;
			<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> facet:<span style="color: #0066CC;">Array</span> <span style="color: #b1b100;">in</span> features<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>counter<span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					<span style="color: #808080; font-style: italic;">//Graphic</span>
					<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">0</span> :
						il=<span style="color: #000000; font-weight: bold;">new</span> ImageLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>facet<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
						productHolder.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>il<span style="color: #66cc66;">&#41;</span>;
						il.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">410</span>,il.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">10</span>;
						<span style="color: #b1b100;">break</span>;
					<span style="color: #808080; font-style: italic;">//Swf</span>
					<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">1</span> :
						bg=<span style="color: #000000; font-weight: bold;">new</span> ImageLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>facet<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
						productHolder.<span style="color: #006600;">addChildAt</span><span style="color: #66cc66;">&#40;</span>bg,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
						<span style="color: #b1b100;">break</span>;
					<span style="color: #808080; font-style: italic;">//Video</span>
					<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">2</span> :
						ep=<span style="color: #000000; font-weight: bold;">new</span> EventPlayer<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>facet<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
						productHolder.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>ep<span style="color: #66cc66;">&#41;</span>;
						<span style="color: #b1b100;">break</span>;
					<span style="color: #808080; font-style: italic;">//Text write-up</span>
					<span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">3</span> :
						tl=<span style="color: #000000; font-weight: bold;">new</span> TextLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>facet<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
						productHolder.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>tl<span style="color: #66cc66;">&#41;</span>;
						<span style="color: #b1b100;">break</span>;
				<span style="color: #66cc66;">&#125;</span>
				counter++;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			features.<span style="color: #0066CC;">length</span>=<span style="color: #cc66cc;">0</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> cleanProduct<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>productHolder.<span style="color: #006600;">numChildren</span><span style="color: #66cc66;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				productHolder.<span style="color: #006600;">removeChildAt</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</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 Product class has to deal with placing materials on the display list and removing them once its finished. I found that the easiest way to handle the DisplayObject was to create a sprite to act as a holder. Before adding new materials to the display list, everything in the holder is removed using the code:</p>
<p><strong><code>while (productHolder.numChildren>0)<br />
{<br />
	productHolder.removeChildAt(0);<br />
}</code></strong></p>
<p>I learned that if I used that code on the root display, it&#8217;d wipe out everything; however, it is a handy tool for cleaning up. So you can use it to clean up the just the holder sprite.</p>
<p>Loading images (jpg, png, gif) and compiled Flash (.swf) files use identical formats. So the ImageLoader helper could be used for both loading the image of the hotel and the background. Note, however, that the background has to be slipped beneath everything else, and so it was placed at the lowest level (0) using <strong>addChildAt()</strong>. The following script shows the code for this hard working helper:</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('p2212code16'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221216"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code" id="p2212code16"><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;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ImageLoader <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> imgNow:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loader:Loader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ImageLoader<span style="color: #66cc66;">&#40;</span>img:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			imgNow=img;
			<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">url</span>:URLRequest=<span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>imgNow<span style="color: #66cc66;">&#41;</span>;
			loader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>loader<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, the ImageLoader class doesn&#8217;t take much code. (You may have noticed that the Client uses it to load the initial background.) It&#8217;s small but mighty!</p>
<p>The EventPlayer class is essentially a video player class. It accommodates both streaming via Flash Media Player and progressive downloads. Basically, if it is passed a url with the &#8220;.flv&#8221; extension, it plays the video as a progressive download; otherwise, it plays the video using Flash Media Server.</p>
<p>As for placing the video object on the display list, the video object is removed from the list by itself. When the video ends, it tidies up after itself. The following listing shows this somewhat complex helper class.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2212code17'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221217"><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
</pre></td><td class="code" id="p2212code17"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetConnection</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #0066CC;">NetStream</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">media</span>.<span style="color: #0066CC;">Video</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">NetStatusEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EventPlayer <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> vid:<span style="color: #0066CC;">Video</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Video</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> nc:<span style="color: #0066CC;">NetConnection</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">NetConnection</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> ns:<span style="color: #0066CC;">NetStream</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> rtmpNow:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;rtmpe://192.168.0.11/context/events&quot;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> goodConnect:<span style="color: #0066CC;">Boolean</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> theEnd:<span style="color: #0066CC;">Boolean</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> metaSniffer:<span style="color: #0066CC;">Object</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> flv:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> vidBed:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> EventPlayer<span style="color: #66cc66;">&#40;</span>flvNow:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			addChild<span style="color: #66cc66;">&#40;</span>vidBed<span style="color: #66cc66;">&#41;</span>;
			vidBed.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>vid<span style="color: #66cc66;">&#41;</span>;
			flv=flvNow;
&nbsp;
			nc.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>NetStatusEvent.<span style="color: #006600;">NET_STATUS</span>,checkConnection<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>flv.<span style="color: #0066CC;">substring</span><span style="color: #66cc66;">&#40;</span>flv.<span style="color: #006600;">length</span>-<span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">!</span>=<span style="color: #ff0000;">&quot;.flv&quot;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				nc.<span style="color: #0066CC;">connect</span><span style="color: #66cc66;">&#40;</span>rtmpNow<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span>
			<span style="color: #66cc66;">&#123;</span>
				nc.<span style="color: #0066CC;">connect</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
				ns=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#40;</span>nc<span style="color: #66cc66;">&#41;</span>;
				vid.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">60</span>,vid.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">320</span>;
				ns.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span>flv<span style="color: #66cc66;">&#41;</span>;
				vid.<span style="color: #006600;">attachNetStream</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
				ns.<span style="color: #006600;">client</span>=metaSniffer;
				metaSniffer.<span style="color: #006600;">onMetaData</span>=getMeta;
				ns.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>NetStatusEvent.<span style="color: #006600;">NET_STATUS</span>,checkComplete<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> checkConnection<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:NetStatusEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			goodConnect=<span style="color: #0066CC;">e</span>.<span style="color: #006600;">info</span>.<span style="color: #006600;">code</span>==<span style="color: #ff0000;">&quot;NetConnection.Connect.Success&quot;</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>goodConnect<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				ns=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">NetStream</span><span style="color: #66cc66;">&#40;</span>nc<span style="color: #66cc66;">&#41;</span>;
				vid.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">60</span>,vid.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">320</span>;
				ns.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span>flv<span style="color: #66cc66;">&#41;</span>;
				vid.<span style="color: #006600;">attachNetStream</span><span style="color: #66cc66;">&#40;</span>ns<span style="color: #66cc66;">&#41;</span>;
&nbsp;
				ns.<span style="color: #006600;">client</span>=metaSniffer;
				metaSniffer.<span style="color: #006600;">onMetaData</span>=getMeta;
&nbsp;
				ns.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>NetStatusEvent.<span style="color: #006600;">NET_STATUS</span>,checkComplete<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">//Add Error Handler</span>
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getMeta<span style="color: #66cc66;">&#40;</span>mdata:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>mdata.<span style="color: #006600;">width</span><span style="color: #66cc66;">&lt;</span>=<span style="color: #cc66cc;">720</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				vid.<span style="color: #0066CC;">width</span>=mdata.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>;
				vid.<span style="color: #0066CC;">height</span>=mdata.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span>
			<span style="color: #66cc66;">&#123;</span>
				vid.<span style="color: #0066CC;">width</span>=mdata.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">3</span>;
				vid.<span style="color: #0066CC;">height</span>=mdata.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">3</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> checkComplete<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:NetStatusEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			theEnd=<span style="color: #0066CC;">e</span>.<span style="color: #006600;">info</span>.<span style="color: #006600;">code</span>==<span style="color: #ff0000;">&quot;NetStream.Play.Stop&quot;</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>theEnd<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				ns.<span style="color: #0066CC;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				vid.<span style="color: #0066CC;">clear</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				removeChild<span style="color: #66cc66;">&#40;</span>vidBed<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 EventPlayer class also deals with videos of different sizes using the file&#8217;s own metadata. It looks at the width and height of the video and then decides whether to divide the width and height by 2 or 3. (Anyone who&#8217;d like to add a more sophisticated algorithm for this process is welcomed.)</p>
<p>The final helper class loads the text stored in an external text file. The operations are fairly straightforward for text loading. However, it does have a load-checker to seen when all of the text from the file has been passed to the URLLoader object (txtLoader) which is then passed to the text property of the TextField instance.</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('p2212code18'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p221218"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
</pre></td><td class="code" id="p2212code18"><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: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLLoader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TextLoader <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> textContainer:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> urlNow:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtLoader:URLLoader=<span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtFld:<span style="color: #0066CC;">TextField</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtFormat:<span style="color: #0066CC;">TextFormat</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> TextLoader<span style="color: #66cc66;">&#40;</span>textNow:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			addChild<span style="color: #66cc66;">&#40;</span>textContainer<span style="color: #66cc66;">&#41;</span>;
			urlNow=<span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>textNow<span style="color: #66cc66;">&#41;</span>;
			txtLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, addTxtFld<span style="color: #66cc66;">&#41;</span>;
			txtLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>urlNow<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> addTxtFld<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> loader:URLLoader=URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span>;
			txtFormat=<span style="color: #000000; font-weight: bold;">new</span>  <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			txtFormat.<span style="color: #0066CC;">font</span>=<span style="color: #ff0000;">&quot;Verdana&quot;</span>;
			txtFormat.<span style="color: #0066CC;">size</span>=<span style="color: #cc66cc;">11</span>;
			txtFormat.<span style="color: #0066CC;">color</span>=0x000000;
			txtFld.<span style="color: #006600;">defaultTextFormat</span>=txtFormat;
			txtFld.<span style="color: #0066CC;">multiline</span>=<span style="color: #000000; font-weight: bold;">true</span>;
			txtFld.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">210</span>;
			txtFld.<span style="color: #0066CC;">height</span>=<span style="color: #cc66cc;">250</span>;
			txtFld.<span style="color: #0066CC;">wordWrap</span>=<span style="color: #000000; font-weight: bold;">true</span>;
			txtFld.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">163</span>,txtFld.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">40</span>;
			txtFld.<span style="color: #0066CC;">text</span>=loader.<span style="color: #0066CC;">data</span>;
			textContainer.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>txtFld<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>When a new vacation destination is called, the text is removed by the Product class. In this way, you do not get text stacking on top of itself.</p>
<p><strong>Do Something</strong></p>
<p>I am hoping that some readers will add another vacation destination to better understand how the Builder pattern works and to see how easy it is to build a relatively complex object. All adding a new destination requires is building another concrete builder by copying and pasting one of the existing ones and adding the required text literals. Create a .swf file with a background for your new vacation and place it where it can be accessed from a Web server. Add a button to the Client and the sequence of steps used for the first three; and you&#8217;re done.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2009%2F12%2Factionscript-3-0-builder-design-pattern-part-ii-multiple-builds%2F&amp;title=ActionScript%203.0%20Builder%20Design%20Pattern%20Part%20II%3A%20Multiple%20Builds" 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/2009/12/actionscript-3-0-builder-design-pattern-part-ii-multiple-builds/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Builder Design Pattern Part I: Controlling Creation</title>
		<link>http://www.as3dp.com/2009/03/actionscript-30-builder-design-pattern-part-i-controlling-creation/</link>
		<comments>http://www.as3dp.com/2009/03/actionscript-30-builder-design-pattern-part-i-controlling-creation/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 14:05:19 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Builder]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=816</guid>
		<description><![CDATA[Changing How a Composite Object Gets Created In both our book and on this blog, Chandima and I have tried to clarify and simplify ActionScript 3.0 Design Patterns and yet keep them accurate and, whenever possible, practical. After reviewing many, many Builder examples, I was surprised to find pieces missing in several instances, and while [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Changing How a Composite Object Gets Created</strong></p>
<p>In both our book and on this blog, Chandima and I have tried to clarify and simplify ActionScript 3.0 Design Patterns and yet keep them accurate and, whenever possible, practical. After reviewing many, many Builder examples, I was surprised to find pieces missing in several instances, and while the examples may have combined elements (such as the Client and Director); others simply left out crucial components. (Who could forget bad, bad Leroy Brown who looked like a jigsaw puzzle with a couple of pieces gone?)</p>
<p>Given this state of affairs, it occurred to me that this pattern might be tricky; however, after working up an example, it turns out to be relatively simple and useful. Nevertheless, it’s not quite as simple as some, such as the Strategy or Factory Method, and so the discussion of the Builder is in two parts. This first part provides a clear overview so that you can easily see how the different components work together and starts a practical project., The second part (in a separate post) removes the abstract <strong>trace</strong> statements and displays actual content on the stage in a useful way. This will be to see if video, SWF, text, and images can be usefully integrated using the Builder.</p>
<p><strong> Builder Overview</strong></p>
<p>When you first look at the Builder class diagram shown in Figure 1, you might think it looks a lot like the <a href="http://www.as3dp.com/2009/03/08/actionscript-30-easy-and-practical-strategy-design-pattern/">Strategy pattern</a>. Well, the class diagrams are similar, but the intent is very different. Also, you’ll find that the little Product class is hugely important.</p>
<p><img src="http://www.as3dp.com/wp-content/uploads/2009/03/builder.png" alt="builder" title="builder" width="451" height="231" class="alignnone size-full wp-image-817" /><br />
<em><strong>Figure 1:</strong> Builder Class Diagram</em><br />
<span id="more-816"></span><br />
The GoF summarize the intent of the Builder class as, <em>[to] separate the construction of a complex object from its representation so that the same construction process can create different representations.</em> Therein lies the problem. You can imagine the different ways this may play out. One way, which we’ve used in this initial example, is to use the Product class as a general clearinghouse for products with everything going to the same Product class. It “assembles” the results of the different operations from the concrete Builder classes and displays them. Another way to use the Product class is to create a different product class for each concrete Builder class. This latter technique is good when you want to use the Builder design pattern for building products based on the same interface but generating very different results.</p>
<p>In one creative Builder written in Java, the developer had a single Product class and then delegated the production to different concrete classes using aggregation from the Product class to the “helper” classes. It was an interesting design in that the differences between the concrete Builder classes was minor but each had a common task that required a hefty amount of work. The solution, at first glance, seemed to be to create a separate Product class for each of the concrete Builder classes (as did GoF in one of their examples) but since the building work is not done in the Product class, he needed to add the helper classes.</p>
<p>To get a better picture of the Builder design pattern, the following shows the task performed by each participant in a project that builds different vacations. Each vacation shows a hotel, a major sight (e.g., Eiffel Tower), an event (e.g., Carnival) worth seeing in the selected location, and a general write-up.</p>
<p><strong>Builder:</strong> The Builder is the abstract interface. In this first example, I used an interface rather than an abstract class. The operations in the Builder are the required tasks for building the different parts of the Product object. In this example, the main tasks are:</p>
<ul>
<li>buildHotel</li>
<li> buildSight</li>
<li> buildEvent</li>
<li> buildWriteUp</li>
</ul>
<p>I used the <strong>getResult()</strong> name that GoF show in the Class diagram as part of a ConcreteBuilder, but since the requests in the Client are typed to the interface (IBuilder), I needed to place it in the Builder itself.</p>
<p><strong>ConcreteBuilder:</strong> Each ConcreteBuilder class implements the operations in the Builder interface, builds the product and delivers the assembled results to the Product. As noted, the <strong>getResult()</strong> retrieves the product. Because in this example, all concrete builders use the same product, it uses the same retrieval operation. However, if each built significantly different products, each of the retrieval operations would have to be unique.</p>
<p><strong>Director:</strong> Using the Builder interface, the Director constructs the desired object. For example, the following are two used in our example:<br />
<code>&nbsp; &nbsp; &nbsp; &nbsp;builder.buildHotel();<br />
	&nbsp; &nbsp; &nbsp; &nbsp;builder.buildSight();</code><br />
The Director holds an aggregate relation to the IBuilder using an IBuilder instance in a function’s parameter.</p>
<p><strong>Product:</strong> The Product class is used to represent the building process. In this example, it does so by pushing elements into a Vector (or Array) object. <em>It may</em> include classes that define the constituent parts for assembling the parts into the final result. So you can either have multiple ConcreteBuilders instantiate the same Product object or different ones.</p>
<p><strong>Composing Video, SWF, Images, and Text</strong></p>
<p>In developing this application, I was thinking of a travel site that would include the following:</p>
<ul>
<li>An SWF file showing a hotel</li>
<li> A graphic file showing a building or other sight</li>
<li> A video of an event</li>
<li> Text write-up describing the vacation destination</li>
</ul>
<p>In Part II of this post I plan to fill in the current trace statements with the above objects. Because every vacation location will have each of the objects, I cannot see using separate Product classes for each of the vacation sites; therefore, it will have a similar structure as this current example.</p>
<p><strong>A Builder Vacation Site</strong></p>
<p>In order to provide a visual image of what this particular design pattern does, Figure 2 shows the user interface along with the output via trace statements. (If you use Kuler, either the one built into Flash CS4 or at the <a href="http://kuler.adobe.com">Adobe Kuler</a> site, you will be happy to know that they have a color theme named <em>Builder</em> that has been employed in the example.)</p>
<p><img src="http://www.as3dp.com/wp-content/uploads/2009/03/buildermin.png" alt="buildermin" title="buildermin" width="580" height="745" class="alignnone size-full wp-image-818" /></p>
<p><em><strong>Figure 2:</strong> Builder example with UI, background and output</em></p>
<p>The four output lines generated by selecting one of the buttons conforms to the four Builder functions (less the getResult() method). With Figure 2 in mind, we can now dissect the program.</p>
<p><strong>The Client</strong></p>
<p>The Client class is not shown in the class diagram, and so we need to consider where it directs its requests. According to GoF, <em>the client creates the Director object and configures it with the desired Builder object</em>. You will see this operation in the following format:<br />
<code><strong><br />
director.construct(trip1);<br />
srilanka = trip1.getResult(); //srilanka = Product instance<br />
srilanka.disclose();<br />
</strong></code><br />
The <strong>director</strong> is a Director instance and <strong>trip1</strong> is a Builder instance typed to the IBuilder interface. In this implementation, each “trip” refers to one of the three vacation destinations—Sri Lanka, San Francisco or Prague. When one of the UI buttons is pressed, an event handler simply generates the necessary output through the Director’s <strong>construct()</strong> method. Then, a Product instance, named after the vacation destination, is assigned the return value of the particular ConcreteBuilder and then the Product <strong>disclose()</strong> method displays the information as a trace output.</p>
<p>The following code shows the entire Client 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('p816code26'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81626"><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
</pre></td><td class="code" id="p816code26"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Client</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> 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> director:Director = <span style="color: #000000; font-weight: bold;">new</span> Director<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #808080; font-style: italic;">//Concrete builders</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> trip1:IBuilder = <span style="color: #000000; font-weight: bold;">new</span> ConcreteBuilder1<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> trip2:IBuilder = <span style="color: #000000; font-weight: bold;">new</span> ConcreteBuilder2<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> trip3:IBuilder = <span style="color: #000000; font-weight: bold;">new</span> ConcreteBuilder3<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #808080; font-style: italic;">//Products</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> srilanka:Product;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sanfrancisco:Product;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> prague:Product;
		<span style="color: #808080; font-style: italic;">//Buttons</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btn1:<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> btn2:<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> btn3:<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>;
&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>
			setButtons<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setButtons<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			btn1.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">16</span>;
			btn1.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">64</span>;
			btn1.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Sri Lanka&quot;</span>;
			btn1.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, goSriLanka<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btn1<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			btn2.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">16</span>;
			btn2.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">96</span>;
			btn2.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;San Francisco&quot;</span>;
			btn2.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, goSanFrancisco<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btn2<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			btn3.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">16</span>;
			btn3.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">128</span>;
			btn3.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Prague&quot;</span>;
			btn3.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, goPrague<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btn3<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> goSriLanka<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>
			director.<span style="color: #006600;">construct</span><span style="color: #66cc66;">&#40;</span>trip1<span style="color: #66cc66;">&#41;</span>;
			srilanka = trip1.<span style="color: #006600;">getResult</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			srilanka.<span style="color: #006600;">disclose</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> goSanFrancisco<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>
			director.<span style="color: #006600;">construct</span><span style="color: #66cc66;">&#40;</span>trip2<span style="color: #66cc66;">&#41;</span>;
			sanfrancisco = trip2.<span style="color: #006600;">getResult</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			sanfrancisco.<span style="color: #006600;">disclose</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> goPrague<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>
			director.<span style="color: #006600;">construct</span><span style="color: #66cc66;">&#40;</span>trip3<span style="color: #66cc66;">&#41;</span>;
			prague = trip3.<span style="color: #006600;">getResult</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			prague.<span style="color: #006600;">disclose</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>Now that you can see the request structure, we can look at the main participants in the pattern.</p>
<p><strong>Director</strong></p>
<p>The Director class is a concrete class with the responsibility of constructing an object. Using its sole method <strong>construct()</strong> it uses a Builder (IBuilder) reference to create the necessary parts that make up the product.</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('p816code27'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81627"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p816code27"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Director</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Director
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> construct<span style="color: #66cc66;">&#40;</span>builder:IBuilder<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			builder.<span style="color: #006600;">buildHotel</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			builder.<span style="color: #006600;">buildSight</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			builder.<span style="color: #006600;">buildEvent</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			builder.<span style="color: #006600;">buildWriteUp</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>The Director acts to create an aggregate relationship with the Builder interface (IBuilder) and allows the Client to request a product be built. In this way it maintains a loose relationship between its request and the concrete builders. This reduces dependencies thereby providing less problems changing or adding concrete builders and the products they build.</p>
<p><strong>Builder Interface</strong></p>
<p>I always feel more comfortable using ActionScript 3.0 interfaces than the ersatz abstract classes even though I sometimes miss the advantages of abstract classes. However, for this example, the ActionScript 3.0 <strong>interface</strong> statement is just what I need. Like most interfaces, all the IBuilder interface does is to provide abstract operations to be used in implementations. In the case of the Builder design pattern, these operations specify which parts of the Product will be built.</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('p816code28'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81628"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p816code28"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Builder Interface</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">interface</span> IBuilder
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> buildHotel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> buildSight<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> buildEvent<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> buildWriteUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
		<span style="color: #000000; font-weight: bold;">function</span> getResult<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Product;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As noted, the pattern’s class diagram does not show the getResult() method in the Builder interface , but it is necessary if the Client  types to the Builder (IBuilder) interface.</p>
<p><strong>Concrete Builders: The Patterns Payoff</strong></p>
<p>Once the Builder interface has been established, adding or changing the concrete part builders is a piece of cake. For example, I could add as many concrete builders for different vacation destinations as I want.  Likewise, I could change the parts to be “assembled” in any of the concrete builder classes. In this particular example, it’s even easier because the only thing that has to be changed is the content in the  <strong>Product.assemble()</strong> operation. The following make up the three concrete builders:</p>
<p><em>Sri Lanka</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('p816code29'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81629"><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
</pre></td><td class="code" id="p816code29"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//ConcreteBuilder1</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteBuilder1 <span style="color: #0066CC;">implements</span> IBuilder
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> product:Product = <span style="color: #000000; font-weight: bold;">new</span> Product<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> buildHotel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Mt. Lavinia Hotel is right on the beach&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildSight<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;The Temple of Buddha's tooth in Kandy is beautiful&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildEvent<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Esala Perahera fesitval features elephants&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildWriteUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Let me tell you about Sri Lanka...&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getResult<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Product
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> product;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><em>San Francisco</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('p816code30'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81630"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code" id="p816code30"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//ConcreteBuilder2</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteBuilder2 <span style="color: #0066CC;">implements</span> IBuilder
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> product:Product = <span style="color: #000000; font-weight: bold;">new</span> Product<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> buildHotel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Fairmont Hotel is a landmark in San Francisco&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildSight<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Take a drive across Golden Gate Bridge&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildEvent<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Don't miss the Carnival in May&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildWriteUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Let me tell you about San Francisco...&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getResult<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Product
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> product;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><em>Prague</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('p816code31'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81631"><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
</pre></td><td class="code" id="p816code31"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//ConcreteBuilder3</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteBuilder3 <span style="color: #0066CC;">implements</span> IBuilder
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> product:Product = <span style="color: #000000; font-weight: bold;">new</span> Product<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> buildHotel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Hotel Tyl -- small but nice&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildSight<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Visit the medieval Prague Castle&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildEvent<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;The Prague International Jazz Festival is a treat&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> buildWriteUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			product.<span style="color: #006600;">assemble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Let me tell you about Prague...&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getResult<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Product
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> product;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>With the parts assembly completed, we turn next to the final product—the Product Class.</p>
<p><strong>Product</strong></p>
<p>As we’ve discussed, the Product class represents the assembled parts created by the builders. In this example, all of the parts are pushed as strings onto a Vector object (or Array). Then the final product is displayed through a <strong>disclose()</strong> method that sends the output using a trace statement.</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('p816code32'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81632"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code" id="p816code32"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Product</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> features:Vector.<span style="color: #66cc66;">&lt;</span>string<span style="color: #66cc66;">&gt;</span>  = <span style="color: #000000; font-weight: bold;">new</span> Vector.<span style="color: #66cc66;">&lt;</span>string<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #808080; font-style: italic;">//If your version of Flex or Flash does not have Vector uncomment</span>
		<span style="color: #808080; font-style: italic;">//the following line and comment out the Vector line</span>
		<span style="color: #808080; font-style: italic;">//private var features:Array=new Array();</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> assemble<span style="color: #66cc66;">&#40;</span>aspect:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			features.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>aspect<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> disclose<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> facet:<span style="color: #0066CC;">String</span> <span style="color: #b1b100;">in</span> features<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>facet<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			features.<span style="color: #0066CC;">length</span>=<span style="color: #cc66cc;">0</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>Keeping in mind that the purpose of this first example is to see how the participants in a Builder work together, think of ways that this pattern could be applied to some of your own projects.</p>
<p><strong>The Test is in Changing and Adding</strong></p>
<p>This sample application ought to be a good one for changing and adding classes. Try adding another class for another city or country. You’ll have to make adjustments to the Client as well to select anything new that you add. Changing content in this particular application is simply a matter of changing the content of the text literals in the ConcreteBuilder classes.</p>
<p>As always, your comments and ideas are welcomed.</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%2F03%2Factionscript-30-builder-design-pattern-part-i-controlling-creation%2F&amp;title=ActionScript%203.0%20Builder%20Design%20Pattern%20Part%20I%3A%20Controlling%20Creation" 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/2009/03/actionscript-30-builder-design-pattern-part-i-controlling-creation/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

