<?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; Abstract Classes</title>
	<atom:link href="http://www.as3dp.com/category/abstract-classes/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>Beginning ActionScript 3.0 Abstraction : The Factory Method at Work</title>
		<link>http://www.as3dp.com/2011/05/beginning-actionscript-3-0-abstraction-the-factory-method-at-work/</link>
		<comments>http://www.as3dp.com/2011/05/beginning-actionscript-3-0-abstraction-the-factory-method-at-work/#comments</comments>
		<pubDate>Tue, 31 May 2011 10:35:42 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Abstract Classes]]></category>
		<category><![CDATA[Beginner's Guide]]></category>
		<category><![CDATA[Factory Method]]></category>
		<category><![CDATA[Learning Design Patterns]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=5638</guid>
		<description><![CDATA[Join the Campaign to Stamp Out Ugly! Gentle Readers: When you click the Play button in this post, the UI that appears isn&#8217;t very pretty. That&#8217;s because I have no graphic design skills. By changing the requests in the Client class, you could easily make a much nicer UI. Why not give it a shot [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.as3dp.com/wp-content/uploads/2011/05/abstraction1.png" alt="abstraction" title="abstraction" width="250" height="251" class="alignleft size-full wp-image-5639" /><em><strong>Join the Campaign to Stamp Out Ugly!</strong></em></p>
<p><em><strong>Gentle Readers:</strong> When you click the Play button in this post, the UI that appears isn&#8217;t very pretty. That&#8217;s because I have no graphic design skills. By changing the requests in the Client class, you could easily make a much nicer UI. Why not give it a shot and send us your revised Client request set? You can even add new Concrete Creators and Products. Send in your refinements to our comment section. Either that or click Play and see Ugly!</em></p>
<p><strong>The Concreteness of Abstraction</strong></p>
<p>The first time I went to London, I picked up a Tube Map (Map of the London Underground) and was able to get where I wanted to go. The map of London&#8217;s tube is a masterpiece of clarity and abstraction. Based on electrical circuitry, it&#8217;s very easy to find one&#8217;s way around London even on a first visit. Likewise, Paris&#8217; Metro has an almost identical type of map for it&#8217;s underground system, and it too is easy to find where you want to go. The secret is having just enough detail to use the system and not so much detail to make its use confusing. Further the coding is clear: different colors depict different named lines. Bakerloo is brown, Circle is yellow, Central is red and District is green. At a glance you see that arriving at Heathrow Airport, you take the Piccadilly line into town (purple) and switch lines wherever you see a white circle off the Piccadilly line. The details are not provided, and the Thames river does not run along the neat geometrical angles shown on the Tube Map. It&#8217;s just a reference point and lets you know whether you&#8217;re north or south of the Thames. The river&#8217;s or tube&#8217;s exact shape doesn&#8217;t matter; the whole thing is an abstraction of London&#8217;s subway system and a major feature of the city. The details would just get in the way of clarity.</p>
<p>Abstraction in OOP and Design Patterns works the same way. An object only exposes as much information as the client (requesting object) requires. The perfect abstract structure in a design pattern is an Interface because it is nothing but abstract methods. The only way to really appreciate abstraction is to see it in action; so this post concentrates on demonstrating abstractions in a fundamental design pattern—the Factory Method.</p>
<p><strong>Two Interfaces and some Concrete Classes</strong></p>
<p>The Factory Method requires a factory interface called Creator and a &#8220;product&#8221; interface called Product. The Creator interface provides a factory method function to create concrete instances of products through the abstract Product interface. In this implementation of the Factory Method Design Pattern, both abstract participants are interfaces (Creator and Product). Figure 1 shows the relationship between the participants in the design pattern:<br />
 <div id="attachment_5646" class="wp-caption alignnone" style="width: 495px"><img src="http://www.as3dp.com/wp-content/uploads/2011/05/FactoryMethodDP.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1: &lt;/strong&gt; Factory Method Design Pattern&lt;/em&gt;" title="FactoryMethodDP" width="485" height="193" class="size-full wp-image-5646" /><p class="wp-caption-text"><em><strong>Figure 1: </strong> Factory Method Design Pattern</em></p></div></p>
<p>In a step-through of the pattern you see the following:</p>
<ol>
<li>Client wants a certain product. Makes <a href="http://www.as3dp.com/2010/01/16/actionsscript-3-0-design-pattern-relations-part-i-acquaintances/">request</a> through the Creator (factory)</li>
<li>Concrete creator specifies the concrete  product and <a href="http://www.as3dp.com/2010/02/18/actionsscript-3-0-design-pattern-relations-part-iv-creation/">instantiates an instance</a>, returning it to the Client</li>
</ol>
<p>It&#8217;s a very straightforward pattern, but often beginners will puzzle over why all the work simply to get a class instance? The answer to that question lies more in the larger implementations and re-use of the pattern than it does in the small example used here. However, a good general answer is that it loosely binds the product to the request. If you change the product&#8217;s contents, you should not have to change anything else in the pattern. The Client makes the request through an abstraction and really doesn&#8217;t care about the details of the product. If the product is changed, the exact same request brings up a different object. However, nothing has to be changed to access the new object. Use the buttons below to download and play the example:<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/dp/beginners/b2/FactoryMethAb.zip" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignleft size-full wp-image-2020" /></a><a href="http://nemo.mwd.hartford.edu/~wsanders/dp/beginners/b2/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignnone size-full wp-image-1257" /></a></p>
<p>Chapter 2 in our book, and <a href="http://www.as3dp.com/category/design-patterns/factory-method/">other posts on this blog</a> go into detail about the Factory Method. Here the focus is on <em>abstraction</em>, and the Factory Method is merely used to illustrate the power of abstraction. In order to show the range of products that can be generated from the same abstract interfaces, I used both component UIs and graphic background Shape objects as products.</p>
<p><strong>Using Abstractions and Programming to the Interface</strong></p>
<p>One of the key principles of design pattern programming is,</p>
<blockquote><p>Program to the Interface and not the implementation&#8230;.</p></blockquote>
<p>In order to see both how abstractions are used  and  how relations work in the n the Factory Method pattern, Figure 2 shows code snippets related to each of the participants (classes and relationships).<br />
<div id="attachment_5664" class="wp-caption alignnone" style="width: 495px"><img src="http://www.as3dp.com/wp-content/uploads/2011/05/FactoryMethodCode.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2: &lt;/strong&gt; Coded Elements of the Factory Method pattern&lt;/em&gt;" title="FactoryMethodCode" width="485" height="400" class="size-full wp-image-5664" /><p class="wp-caption-text"><em><strong>Figure 2: </strong> Coded Elements of the Factory Method pattern</em></p></div><br />
In looking at Figure 2 code snippets, notice how many times that Creator or Product are used. Both reflect the wholly abstract interfaces in the program. By making references to the interfaces (or abstract classes), the binding between the Client and the requests is very loose. By having loose binding through abstractions, the developer can better update and change the program. The only part of the objects exposed are those that are required. If the concrete products change, the Client is not tightly bound to the concrete elements because the requests hold references to the abstract interfaces and not the concrete participants. (Read on to see how these are all put together!)<br />
<span id="more-5638"></span></p>
<p><strong>The Client, Creator and Product</strong></p>
<p>This section shows the code for all of the participants. (You can download it all if you&#8217;d like to follow this post using Flash or Flash Builder by clicking the Download button above.) I&#8217;d like to start with the Client and then examine the abstract interfaces. We&#8217;ll start with the Client:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p5638code6'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p56386"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code" id="p5638code6"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> 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> bkdp:Creator=<span style="color: #000000; font-weight: bold;">new</span> MakeBackdrop<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> btn1:Creator=<span style="color: #000000; font-weight: bold;">new</span> MakeButton<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:Creator=<span style="color: #000000; font-weight: bold;">new</span> MakeButton<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> txtA:Creator=<span style="color: #000000; font-weight: bold;">new</span> MakeTextArea<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> txtI:Creator=<span style="color: #000000; font-weight: bold;">new</span> MakeTextInput<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			addChild<span style="color: #66cc66;">&#40;</span>bkdp.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">12</span>,<span style="color: #cc66cc;">15</span>,<span style="color: #ff0000;">&quot;0xcc0000&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btn1.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">30</span>,<span style="color: #ff0000;">&quot;Top&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btn2.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">60</span>,<span style="color: #ff0000;">&quot;Bottom&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>txtA.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">130</span>,<span style="color: #cc66cc;">30</span>,<span style="color: #ff0000;">&quot;Lots of text can go here!&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>txtI.<span style="color: #006600;">factoryMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span>,<span style="color: #cc66cc;">100</span>,<span style="color: #ff0000;">&quot;Input here&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In looking at the Client, you can see that all requests are through the Creator interface. Because all of the concrete creators (factories) implement the Creator, the objects can be typed to the interface and not the concrete factories. Note that while the Client is requesting concrete Products, products are not even mentioned once. The <strong>factoryMethod()</strong> takes care of making the request that delivers the product. Think of it as requesting a cup of coffee from a barista. You put in your order for a <em>Café Latte Uruguay Negro</em> (<em>my coffee house is more pretentious than yours</em>). You don&#8217;t care about how the barista makes the cup of coffee you ordered; you just know to ask him for the specific product you want, and he&#8217;ll take care of it. The Client is doing the same thing.</p>
<p>The Creator (factory) interface is simple because it is abstract. You provide the framework, and the implementations will take care of the concrete elements.</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('p5638code7'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p56387"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p5638code7"><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;">interface</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,why:<span style="color: #0066CC;">Number</span>,lbl:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you will see in the concrete implementations of the Creator, you can do a lot of different things with the three parameters.</p>
<p>Next, the Product interface looks very similar to the Creator. In fact, other than different names for the interface and method, they&#8217;re the same. The don&#8217;t have to be, but in this case they just happen to be. However, as you will see, the implementations of each are different individually and as a group.s</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('p5638code8'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p56388"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p5638code8"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">interface</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> uiProduct<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,why:<span style="color: #0066CC;">Number</span>,lbl:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Going back to the example of the London Tube Map, you can see that the abstractions have very little detail. One &#8220;sounds like&#8221; it makes something (<strong>factoryMethod()</strong>) and the other &#8220;sounds like&#8221; a product (<strong>uiProduct()</strong>). However, you have enough information to know that each does something different.</p>
<p><strong>The Concrete Implementations: Factories and Products</strong></p>
<p>The final step in working with the abstractions in the interfaces is to make them do something concrete. First, we&#8217;ll deal with the factories. The following code creates factories for each of the products:</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('p5638code9'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p56389"><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
</pre></td><td class="code" id="p5638code9"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Make buttons</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MakeButton <span style="color: #0066CC;">implements</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btn:Product = <span style="color: #000000; font-weight: bold;">new</span> UiButton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,why:<span style="color: #0066CC;">Number</span>,lbl:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> btn.<span style="color: #006600;">uiProduct</span><span style="color: #66cc66;">&#40;</span>ex,why,lbl<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;">//Make backdrop</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MakeBackdrop <span style="color: #0066CC;">implements</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bkDrop:Product = <span style="color: #000000; font-weight: bold;">new</span> UiBackdrop<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,why:<span style="color: #0066CC;">Number</span>,lbl:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> bkDrop.<span style="color: #006600;">uiProduct</span><span style="color: #66cc66;">&#40;</span>ex,why,lbl<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;">//Make TextAreas</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MakeTextArea <span style="color: #0066CC;">implements</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtArea:Product = <span style="color: #000000; font-weight: bold;">new</span> UiTxtArea<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,why:<span style="color: #0066CC;">Number</span>,lbl:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> txtArea.<span style="color: #006600;">uiProduct</span><span style="color: #66cc66;">&#40;</span>ex,why,lbl<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;">//Make TextInput</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MakeTextInput <span style="color: #0066CC;">implements</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtInput:Product = <span style="color: #000000; font-weight: bold;">new</span> UiTxtInput<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,why:<span style="color: #0066CC;">Number</span>,lbl:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> txtInput.<span style="color: #006600;">uiProduct</span><span style="color: #66cc66;">&#40;</span>ex,why,lbl<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, they&#8217;re all pretty much the same. They declare instances as a Product but instantiate them as concrete product implementations. There&#8217;s not a lot of variety; just the name of the instantiated concrete product.</p>
<p>The concrete products show a great deal of variation, though. This demonstrates the flexibility of abstractions and the loose bindings between a client object and the requested products.</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('p5638code10'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p563810"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
</pre></td><td class="code" id="p5638code10"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Button</span>
package
<span style="color: #66cc66;">&#123;</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;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UiButton <span style="color: #0066CC;">implements</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnPack: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> <span style="color: #0066CC;">button</span>:<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> uiProduct<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,why:<span style="color: #0066CC;">Number</span>,lbl:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">button</span>.<span style="color: #006600;">label</span> = lbl;
			<span style="color: #0066CC;">button</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,btnBusiness<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">button</span>.<span style="color: #006600;">x</span> = ex;
			<span style="color: #0066CC;">button</span>.<span style="color: #006600;">y</span> = why;
			btnPack.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">button</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> btnPack;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> btnBusiness<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Whatever business you have in mind....&quot;</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;">//Backdrop</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UiBackdrop <span style="color: #0066CC;">implements</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> backdropHolder: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> backdrop:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> uiProduct<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,why:<span style="color: #0066CC;">Number</span>,lbl:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			backdrop.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#40;</span>lbl<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			backdrop.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,0x0000cc<span style="color: #66cc66;">&#41;</span>;
			backdrop.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span>ex, why, ex+<span style="color: #cc66cc;">100</span>, why+<span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;
			backdrop.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			backdropHolder.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>backdrop<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> backdropHolder;
		<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;">//Text Area</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #006600;">TextArea</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UiTxtArea <span style="color: #0066CC;">implements</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> taPack: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> txtArea:TextArea=<span style="color: #000000; font-weight: bold;">new</span> TextArea<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> uiProduct<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,why:<span style="color: #0066CC;">Number</span>,lbl:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			txtArea.<span style="color: #0066CC;">text</span> = lbl;
			txtArea.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">200</span>,txtArea.<span style="color: #0066CC;">height</span> = <span style="color: #cc66cc;">200</span>;
			txtArea.<span style="color: #006600;">x</span> = ex;
			txtArea.<span style="color: #006600;">y</span> = why;
			taPack.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>txtArea<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> taPack;
		<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;">//Text Input</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #006600;">TextInput</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UiTxtInput <span style="color: #0066CC;">implements</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> tiPack: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> txtInput:TextInput=<span style="color: #000000; font-weight: bold;">new</span> TextInput<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> uiProduct<span style="color: #66cc66;">&#40;</span>ex:<span style="color: #0066CC;">Number</span>,why:<span style="color: #0066CC;">Number</span>,lbl:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			txtInput.<span style="color: #0066CC;">text</span> = lbl;
			txtInput.<span style="color: #006600;">x</span> = ex;
			txtInput.<span style="color: #006600;">y</span> = why;
			tiPack.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>txtInput<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> tiPack;
		<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 in the different implementations of the Product object, there&#8217;s an immense variation. The Factory Method uses abstraction to make it very easy for the Client to request what&#8217;s needed, provide loose binding, and do it while allowing enough room for variation to do whatever the developer wants.</p>
<p>The best part comes when I decide to change something. Suppose I decided I want to make my own TextArea object using a TextField, TextFormat and other coded objects so I don&#8217;t have to worry too much about component differences between Flash Professional and Flash Builder. I can simply change my implementation in the concrete product without affecting anything else. In other words, I can make huge programs without having to start all over from scratch.</p>
<p>Thanks abstraction!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2011%2F05%2Fbeginning-actionscript-3-0-abstraction-the-factory-method-at-work%2F&amp;title=Beginning%20ActionScript%203.0%20Abstraction%20%3A%20The%20Factory%20Method%20at%20Work" id="wpa2a_2"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/05/beginning-actionscript-3-0-abstraction-the-factory-method-at-work/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Beginner&#039;s First Design Pattern: The Template Method</title>
		<link>http://www.as3dp.com/2011/05/beginners-first-design-pattern-the-template-method/</link>
		<comments>http://www.as3dp.com/2011/05/beginners-first-design-pattern-the-template-method/#comments</comments>
		<pubDate>Sun, 15 May 2011 19:17:29 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Abstract Classes]]></category>
		<category><![CDATA[Getting Started in Design Patterns]]></category>
		<category><![CDATA[Learning Design Patterns]]></category>
		<category><![CDATA[Principles]]></category>
		<category><![CDATA[Template Method Pattern]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=5555</guid>
		<description><![CDATA[The Template Method: A Design Pattern with a Principle In learning OOP and Design Patterns, what you&#8217;re learning is a set of principles with associated programming patterns. The Template Method is a valuable starting place because it is used to illustrate the OOP/DP principle called The Hollywood Principle. Be sure to go over the more [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_5560" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/05/dpVirgin.jpg" alt="I&#039;m a Design Pattern Virgin too!" title="dpVirgin" width="250" height="225" class="size-full wp-image-5560" /><p class="wp-caption-text">It's my first time too!</p></div><strong>The Template Method: A Design Pattern with a Principle</strong></p>
<p>In learning OOP and Design Patterns, what you&#8217;re learning is a set of principles with associated programming patterns. The Template Method is a valuable starting place because it is used to illustrate the OOP/DP principle called <a href="http://www.as3dp.com/2009/05/07/hollywood-principle-don’t-call-us-we’ll-call-you—actionscript-30-template-design-pattern-goes-hollywood/">The Hollywood Principle</a>. Be sure to go over the more detailed post on the Hollywood Principle, but for here, you just need to become familiar with the idea that higher level components (like parent classes and interfaces) should call lower level components (like concrete classes), but lower level components, should not call higher level components. The principle is called <strong><em>Hollywood</em></strong>, because the casting director tells the budding actor,</p>
<blockquote><p>Don&#8217;t call us, we&#8217;ll call you.</p></blockquote>
<p> The parent class tells the child class the same thing.</p>
<p><strong>Class Diagram</strong></p>
<p>In order to understand a class diagram, you need to understand class relations. If you understand how to work with an UML, you&#8217;re ahead of the game; however, if not, we&#8217;re prepared a <a href="http://www.as3dp.com/2010/01/16/actionsscript-3-0-design-pattern-relations-part-i-acquaintances/">4-part series</a> to help you understand the UML and the whole concept of relations between pattern participants. Figure 1 shows the class diagram for the Template Method:<br />
<div id="attachment_904" class="wp-caption alignnone" style="width: 401px"><img src="http://www.as3dp.com/wp-content/uploads/2009/05/templatemethod.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1: &lt;/strong&gt;Template Method" title="templatemethod" width="391" height="270" class="size-full wp-image-904" /><p class="wp-caption-text"><em><strong>Figure 1: </strong>Template Method Class Diagram</em></p></div></p>
<p>After you&#8217;ve studied the class diagram for a bit, you can see that it&#8217;s made up of one abstract class (all italicized bold lettering indicates an abstract class or interface), and one subclass. One of the operations in the abstract class is concrete (Template Method) and the others are abstract (primitive operations). The abstract methods are italicized.</p>
<p>The template method is made up of the steps in an algorithm, some of which are abstract and some that can be concrete. The template method orders the steps in the algorithm, but the concrete subclass provides concrete implementations of the abstract methods using override. The following two buttons provide a preview of Play and all of the files are available for download:<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/blog/TemplateMethodBegin/"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignleft size-full wp-image-1257" /></a><a href="http://nemo.mwd.hartford.edu/~wsanders/blog/TemplateMethodBegin/TemplateMethod.zip"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a></p>
<p><strong>Abstract Classes and Primitives</strong></p>
<p>One of the more poorly defined concepts in computer programming is that of <em>primitives</em>. Usually when we think of primitives, we think of basic data types like numbers, strings and Booleans. However, the Gang of Four parenthetically note that primitive operations are nothing more than abstract methods.  For example, the following is an abstract operation (or method):</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('p5555code16'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p555516"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p5555code16"><pre class="actionscript" style="font-family:monospace;">protected <span style="color: #000000; font-weight: bold;">function</span> DoUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Must override&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Because ActionScript 3.0 has no abstract classes or methods, you have insure that the abstract methods will not be directly instantiated, and one way to do that is to add the illegal operation line. Further to have the proper signature, the method must include some kind of <strong>return</strong> statement, and a <strong>null</strong> value does the trick. In the concrete implementation, each primitive operation is overridden and provided concrete operations.</p>
<p>Within the abstract class, you can have both concrete and abstract methods. One of the advantages of an abstract class (compared with an interface) is that abstract classes can have <em>some</em> concrete methods. For example the following template method uses two primitive operations and a concrete one. It&#8217;s just is to order the steps of the algorithm, which in this case has  three:</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('p5555code17'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p555517"><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
</pre></td><td class="code" id="p5555code17"><pre class="actionscript" style="font-family:monospace;">...<span style="color: #006600;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span>..
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> templateMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	DoLogo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	DoUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	DoInfo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
protected <span style="color: #000000; font-weight: bold;">function</span> DoUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Must override&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
protected <span style="color: #000000; font-weight: bold;">function</span> DoInfo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">TextField</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Must override&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
protected <span style="color: #000000; font-weight: bold;">function</span> DoLogo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> logo: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: #b1b100;">return</span> logo;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
...<span style="color: #006600;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span></pre></td></tr></table></div>

<p>So let&#8217;s stop a second and consider why and how we&#8217;d use a Template Method design based on the following:</p>
<ol>
<li>An abstract class</li>
<li>A method that orders the steps in an algorithm</li>
<li>At least one abstract method that is overridden by a concrete class that implements the abstract class.</li>
</ol>
<p> The abstract class affords the programmer flexibility when similar algorithms are used again and again but with slightly different implementations. For example, you may find yourself using a &#8220;page algorithm.&#8221; The page algorithm loads your logo, creates a UI and then creates content. The logo loading part is going to be the same as you use your logo over and over again. However, the UI and content can vary in several different ways. For that, you need flexibility.</p>
<p><strong>Page Arranger</strong></p>
<p>To get started on the algorithm for a page arranger, we&#8217;ll start with the abstract class which is named <strong>IServices</strong>. One convention you will find in OOP is using an &#8220;I&#8221; prefacing all <strong>Interfaces</strong>. I like to do the same thing with abstract classes because other than the concrete properties and methods, you should think of them as a type of interface. (If you&#8217;re just getting started in OOP and Design Patterns, you might want to check out <a href="http://www.as3dp.com/2011/04/12/actionscript-3-0-oop-design-patterns-beginner-start/">Beginners Start</a> to see where the files go in Flash and Flash Builder.)</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('p5555code18'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p555518"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
</pre></td><td class="code" id="p5555code18"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Abstract class. Do not instantiate directly</span>
	<span style="color: #808080; font-style: italic;">//Extend the class for concrete child classes</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> IServices <span style="color: #0066CC;">extends</span> Sprite;
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> logoPix:URLRequest=<span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;logo.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> uiPix:URLRequest;
		protected <span style="color: #000000; font-weight: bold;">var</span> infoPix:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> logoLoader:Loader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> uiLoader:Loader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> infoLoader:Loader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> page: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> logoSprite:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> uiSprite:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> infoSprite: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> templateMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			page.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>DoLogo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			page.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>DoUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			page.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>DoInfo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> page;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> DoUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Must override&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> DoInfo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Must override&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> DoLogo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			logoLoader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			logoLoader.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">0</span>;
			logoLoader.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">0</span>;
			logoLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>logoPix<span style="color: #66cc66;">&#41;</span>;
			logoSprite.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>logoLoader<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> logoSprite;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In the abstract class IServices, you can see two abstract methods (primitive operations), and two other methods—DoLogo and templateMethod. The DoLogo is going to be the same assuming that the company keeps the same logo; so you might as well set it up and let the child classes reuse it when they extend the abstract class. However, the UI and information may change, and so both of the methods for what they may include are left up to the concrete implementation.</p>
<p>Next, enter the concrete implementation of the abstract class. In the following you will see that both of the abstract methods are not implemented.</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('p5555code19'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p555519"><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="p5555code19"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteService <span style="color: #0066CC;">extends</span> IServices
	<span style="color: #66cc66;">&#123;</span>
		override protected <span style="color: #000000; font-weight: bold;">function</span> DoUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			uiPix=<span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;UI.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
			uiLoader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			uiLoader.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">0</span>;
			uiLoader.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">110</span>;
			uiLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>uiPix<span style="color: #66cc66;">&#41;</span>;
			uiSprite.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>uiLoader<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> uiSprite;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> DoInfo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			infoPix=<span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;info.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
			infoLoader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			infoLoader.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">150</span>;
			infoLoader.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">110</span>;
			infoLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>infoPix<span style="color: #66cc66;">&#41;</span>;
			infoSprite.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>infoLoader<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> infoSprite;
		<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 you <em>neve</em>r implement an abstract class, but you want to program to the abstract class (think <em>interface</em>), you will need a client class to request the use of the concrete implementation. However, by declaring the requesting instantiation through the abstract class, you can keep the binding loose. So the variable <strong>tm</strong> is typed to IServices but instantiates ConcreteService.</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('p5555code20'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p555520"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p5555code20"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> tm:IServices=<span style="color: #000000; font-weight: bold;">new</span> ConcreteService<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			addChild<span style="color: #66cc66;">&#40;</span>tm.<span style="color: #006600;">templateMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, there&#8217;s not much work for the Client. All it has to do is to use addChild to place the returned Sprite object to the display area. The <strong>templateMethod()</strong> operation is wholly inherited from the IServices class, but at the same time the ConcreteService class allows flexibility of what goes into the DoInfo and DoUI methods.</p>
<p>Play around with this example. The best way is to create some .swf files that you can use instead of the ones provided for download on this post. You&#8217;ll begin to see how flexible this design pattern is and how easy it is to use.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2011%2F05%2Fbeginners-first-design-pattern-the-template-method%2F&amp;title=Beginner%26%23039%3Bs%20First%20Design%20Pattern%3A%20The%20Template%20Method" id="wpa2a_4"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/05/beginners-first-design-pattern-the-template-method/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Saturated Bridge Design Pattern 2: Bridge Clock Implementation</title>
		<link>http://www.as3dp.com/2011/03/actionscript-3-0-saturated-bridge-design-pattern-2-bridge-clock-implementation/</link>
		<comments>http://www.as3dp.com/2011/03/actionscript-3-0-saturated-bridge-design-pattern-2-bridge-clock-implementation/#comments</comments>
		<pubDate>Mon, 14 Mar 2011 00:32:50 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Abstract Classes]]></category>
		<category><![CDATA[Aggregation]]></category>
		<category><![CDATA[Bridge]]></category>
		<category><![CDATA[Saturated Bridge]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=4947</guid>
		<description><![CDATA[Aggregation Magic In discussing aggregation, the Gang of Four left a lot of loose ends, and the relationship can be difficult to grasp. It&#8217;s something like an acquaintance but is stronger. Nevertheless, acquaintances can be just as strong but not be aggregations. So is it really that important to distinguish between aggregation and acquaintances? In [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_4858" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/03/MoonBridge.png" alt="Independent Variationby Decoupling Abstractions and Implementations" title="MoonBridge" width="250" height="213" class="size-full wp-image-4858" /><p class="wp-caption-text">Make an Orthogonally Designed Alarm Clock</p></div><strong>Aggregation Magic</strong></p>
<p>In discussing <strong>aggregation</strong>, the Gang of Four left a lot of loose ends, and the relationship can be difficult to grasp. It&#8217;s something like an <strong>acquaintance</strong> but is stronger. Nevertheless, acquaintances can be just as strong but not be aggregations. So is it really that important to distinguish between aggregation and acquaintances? In most cases the distinction is important  because while many similarities exist between them (just as with men and women), the differences change the character of the relationship.  Gamma <em>et al</em> point out,</p>
<blockquote><p>Aggregation implies that an aggregate object and its owner have identical lifetimes, (p. 22)</p></blockquote>
<p>but that description brings up other questions. We can assume that an aggregate object is the combination of two objects bound through aggregation. However, what is its <em>owner</em>? In <a href="http://www.as3dp.com/2011/03/06/actionscript-3-0-saturated-abstract-bridge-intention/">Part I</a> of this series, you saw that the Bridge pattern is made up in part of an orthogonal component bound by aggregation. That&#8217;s pretty clear, but how is that component <em>owned</em>? Does that mean the parts that make up the component are the owners? If one or the other of the aggregated objects (classes) ceases to be; so will the other one. Thus, the parts are the owners. However, another way of looking at ownership in an aggregate relationship is by examining it parts.</p>
<p>The aggregation relationship is characterized by the Aggregator and Aggregatee as shown in Figure 1.<br />
<div id="attachment_2486" class="wp-caption alignnone" style="width: 449px"><img src="http://www.as3dp.com/wp-content/uploads/2010/01/aggregation.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1: &lt;/strong&gt;Aggregator and Aggregatee&lt;/em&gt;" title="aggregation" width="439" height="61" class="size-full wp-image-2486" /><p class="wp-caption-text"><em><strong>Figure 1: </strong>Does the Aggregator own Aggregatee?</em></p></div></p>
<p>In this model, it would appear that the aggregator is the &#8220;owner.&#8221; It defines the relationship by holding a reference to the aggregatee.</p>
<p>The Orthogonal Bridge</p>
<p>The &#8220;aggregateinstance&#8221; in Figure 1 is the &#8220;bridge&#8221; in the Bridge pattern, and it is the orthogonal component discussed in detail in Part I of this series. The next step in understanding the Bridge design pattern is to implement it. The alarm clock provides a clear way to see the relationship between objects linked by an aggregation that makes up an orthogonal component. Figure 2 show the files for the implementation arranged in a File Diagram:<br />
<div id="attachment_4949" class="wp-caption alignnone" style="width: 494px"><img src="http://www.as3dp.com/wp-content/uploads/2011/03/BridgeFileDiagram.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2: &lt;/strong&gt;Bridge File Class Diagram&lt;/em&gt;" title="BridgeFileDiagram" width="484" height="221" class="size-full wp-image-4949" /><p class="wp-caption-text"><em><strong>Figure 2: </strong>Bridge File Class Diagram</em></p></div><br />
This Bridge example is quite simple and easy to understand as a Bridge pattern. The two interfaces are made up of an abstract class (IAlarmClock) and an interface (IAlarm.) You can think of the physical alarm clock as an instrument that keeps time and the alarm as a sound it makes to generate an alert. The clock and its alarm can vary independently but work in conjunction. The clock has a time-keeping function that can be used to call the alarm when needed. In the example, the clock implementation is that of an analog clock that rings a bell and a clock radio that plays Ride of the Valkyrires (Wagner&#8217;s Walkürenritt). It&#8217;s easy to add a different clock implementation (a Cuckoo clock, for example) or a different alarm—a rocket ship blasting off if you wish. The following two buttons will download and Play the example for you:<br />
<a href="http://www.sandlight.com/bridge/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignleft size-full wp-image-1257" /></a><a href="http://poobah.mwd.hartford.edu/wsanders/bridge/BridgeClock.zip"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a></p>
<p>The clocks do not update, but they should display your accurate local time. You can easily put a Timer object in the program to make the clocks work like running clocks.  Likewise, while I did not place an alarm setting function in the clocks, I placed an alarm testing button.<em> That&#8217;s so you wouldn&#8217;t have to sit around waiting for the alarm to ring.</em> However, it wouldn&#8217;t be difficult to do so. In fact if any of you want to place a running clock in the pattern by extending another implementation from the <strong>IAlarmClock</strong> abstract class, I&#8217;d very much like to see it, and I&#8217;ll display it on the next blog entry in this series—same with implementations to the <strong>IAlarm</strong> interface. Okay so where do we start with implementing this pattern? Read on.<br />
<span id="more-4947"></span></p>
<p><strong>The Reversed Engineered Implementation of the Alarm</strong></p>
<p>We might as well start at the ending. In fact I knew where I was going (driving backwards) because I had a good sense of the Bridge pattern; so it wasn&#8217;t starting without a roadmap. I wanted to start with an <em>alarm</em> implementation. Getting the sounds was easy (check out <a href="http://www.gutenberg.org/ebooks/10177">Project Gutenberg</a> and the old standby, <a href="http://www.flashkit.com/">Flash Kit</a> for mp3 sounds). However, I always have to go back and re-learn the procedure for playing sounds since I don&#8217;t use sounds that much. The following implementation was used for both with the only difference being the MP3 selected:</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('p4947code27'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p494727"><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="p4947code27"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Clock radio IAlarm implementation</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">media</span>.<span style="color: #0066CC;">Sound</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">media</span>.<span style="color: #006600;">SoundChannel</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RideAlarm <span style="color: #0066CC;">implements</span> IAlarm
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> aChannel:SoundChannel;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> aSound:<span style="color: #0066CC;">Sound</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> aRequest:URLRequest;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> alarmPlay<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			aSound=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Sound</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			aChannel=<span style="color: #000000; font-weight: bold;">new</span> SoundChannel<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			aRequest=<span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;alarms/rideOfValkyries.mp3&quot;</span><span style="color: #66cc66;">&#41;</span>;
			aSound.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>aRequest<span style="color: #66cc66;">&#41;</span>;
			aChannel=aSound.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</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> alarmOff<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>aChannel<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				aChannel.<span style="color: #0066CC;">stop</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>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>It&#8217;s pretty simple. All I really needed were methods to play the alarm and to shut it off. From here I backed into the IAlarm interface:</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('p4947code28'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p494728"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p4947code28"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Alarm interface</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IAlarm
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> alarmPlay<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">function</span> alarmOff<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>I don&#8217;t mean to sound like any idiot can get this stuff, but sometimes we make things harder for ourselves than we should—especially when it comes to design patterns. (Yes, I know this is a simple example. It&#8217;s supposed to be, but if you can grasp the pattern concept, then you&#8217;re good to go for more complex implementations of the same pattern.)</p>
<p><strong>Alarm Clocks</strong></p>
<p>Early on I decided that the alarm triggers in the clocks would be buttons rather than a timing trigger. The concepts of triggering the alarm are identical—an event does it. It really doesn&#8217;t matter whether the event is one caused by a Date method or MouseEvent; so I used a MouseEvent so that I could play with the clock and the alarm during development.</p>
<p>For the clocks, I definitely wanted to start with the abstract class from which the clock implementations would come from. First, I had to make sure that the abstract class held a reference to the IAlarm interface. Second, I had a good idea of what methods were needed even though their implementation would vary. The following listing shows the abstract class, IAlarmClock:</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('p4947code29'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p494729"><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="p4947code29"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Abstract class for Alarm Clock</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> IAlarmClock <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> timeNow:<span style="color: #0066CC;">Date</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Date</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> alarm:IAlarm;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> curTime<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Must override&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> doAlarm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Must override&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> offAlarm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Must override&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>I knew that the <strong>Date</strong> object would be needed for the clocks because the class has properties for the hours and minutes needed for the clock hands and &#8220;digital&#8221; display. The <strong>IAlarm</strong> instance, <strong>alarm</strong>, established an aggregate relationship between the abstract class and alarm interface.</p>
<p>The implementations of the <strong>curTime()</strong> method were very close to being constructor functions. The idea of the method originally had been to update the time in hours and minutes from the parent class <strong>timeNow</strong> property. However, I ended up using it for building the clock and setting the hands or &#8220;digital&#8221; values on the clocks as well as getting the time and instantiating the alarm implementations.</p>
<p>Unlike the alarm implementations, the clock implementations were very different. One was used to set up an analog clock and the other a &#8220;clock radio.&#8221; I had drawn the face and the clock hands and saved them as Sprite classes, <strong>ClockFace</strong>,<strong> Hour</strong> and <strong>Minute</strong>. The following two listings show these implementations:</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('p4947code30'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p494730"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code" id="p4947code30"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Analog clock: IAlarmClock implementation</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BellClock <span style="color: #0066CC;">extends</span> IAlarmClock
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> minHand:Minute=<span style="color: #000000; font-weight: bold;">new</span> Minute<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> hrHand:Hour=<span style="color: #000000; font-weight: bold;">new</span> Hour<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> cFace:ClockFace=<span style="color: #000000; font-weight: bold;">new</span> ClockFace<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> clockUp: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> adjust:uint;
&nbsp;
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> curTime<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			alarm=<span style="color: #000000; font-weight: bold;">new</span> DingAlarm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			cFace.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">100</span>;
			hrHand.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">200</span>,hrHand.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">100</span>;
			hrHand.<span style="color: #006600;">rotation</span>=<span style="color: #66cc66;">&#40;</span>timeNow.<span style="color: #006600;">hours</span>+<span style="color: #66cc66;">&#40;</span>timeNow.<span style="color: #006600;">minutes</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">30</span>
			minHand.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">200</span>,minHand.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">100</span>;
			minHand.<span style="color: #006600;">rotation</span>=timeNow.<span style="color: #006600;">minutes</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">6</span>;
			clockUp.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>cFace<span style="color: #66cc66;">&#41;</span>;
			clockUp.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>hrHand<span style="color: #66cc66;">&#41;</span>;
			clockUp.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>minHand<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> clockUp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> doAlarm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			alarm.<span style="color: #006600;">alarmPlay</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> offAlarm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			alarm.<span style="color: #006600;">alarmOff</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 clock radio implementation uses a Sprite backdrop from the Library. The &#8220;digital&#8221; output is a text field with the current time (in hours and minutes). It is set up in a 12-hour loop even though no &#8220;AM&#8221; and &#8220;PM&#8221; indicators are used. (Think of it as another opportunity for improvement.) The important feature is that it uses the same interface (abstract class parent) and instantiates the concrete alarm just like the analog clock.</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('p4947code31'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p494731"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
</pre></td><td class="code" id="p4947code31"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Clock radio implementation of IAlarmClock</span>
package
<span style="color: #66cc66;">&#123;</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: #006600;">TextFieldAutoSize</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;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">motion</span>.<span style="color: #006600;">AdjustColor</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RadioClock <span style="color: #0066CC;">extends</span> IAlarmClock
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> clockUp: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> clockRadio:RadioFace=<span style="color: #000000; font-weight: bold;">new</span> RadioFace<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> timeField:<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> timeFormat:<span style="color: #0066CC;">TextFormat</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> hr:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> mn:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> fullTime:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> adjust:uint;
&nbsp;
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> curTime<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			alarm=<span style="color: #000000; font-weight: bold;">new</span> RideAlarm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			timeNow.<span style="color: #006600;">hours</span><span style="color: #66cc66;">&gt;</span><span style="color: #cc66cc;">12</span> ? adjust=timeNow.<span style="color: #006600;">hours</span>-<span style="color: #cc66cc;">12</span>:adjust=timeNow.<span style="color: #006600;">hours</span>;
			hr=<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>adjust<span style="color: #66cc66;">&#41;</span>;
			mn=<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span>timeNow.<span style="color: #006600;">minutes</span><span style="color: #66cc66;">&#41;</span>;
			mn.<span style="color: #0066CC;">length</span>==<span style="color: #cc66cc;">1</span> ? mn=<span style="color: #ff0000;">&quot;0&quot;</span>+ mn : mn=mn;
			fullTime=hr+<span style="color: #ff0000;">&quot;:&quot;</span>+mn;
			timeField.<span style="color: #0066CC;">autoSize</span>=TextFieldAutoSize.<span style="color: #0066CC;">RIGHT</span>;
			timeFormat.<span style="color: #0066CC;">font</span>=<span style="color: #ff0000;">&quot;OCR A Std&quot;</span>;
			timeFormat.<span style="color: #0066CC;">color</span>=0x00ff00;
			timeFormat.<span style="color: #0066CC;">size</span>=<span style="color: #cc66cc;">72</span>;
			timeField.<span style="color: #006600;">defaultTextFormat</span>=timeFormat;
			timeField.<span style="color: #0066CC;">text</span>=fullTime;
			timeField.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">95</span>,timeField.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">75</span>;
			clockUp.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>clockRadio<span style="color: #66cc66;">&#41;</span>;
			clockUp.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>timeField<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> clockUp;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> doAlarm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			alarm.<span style="color: #006600;">alarmPlay</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> offAlarm<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			alarm.<span style="color: #006600;">alarmOff</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>As you can see from the code, it&#8217;s very flexible but forms a strong orthogonal component. Changing an alarm requires only that the name of the MP3 file is changed. A quick cut-and-paste and you have another alarm sound or tune. Changing clocks requires a new clock face, but adding another can easily fit in with the other interacting participants in the pattern, and <em>that&#8217;s what is important</em>.</p>
<p><strong>Making Requests through the Client</strong></p>
<p>The Bridge includes the Client as an <em>implicit</em> class participant in the pattern. In this example, the Client is more like a sales associate who&#8217;s job it is to display the wares—the clocks and their alarms. Most of the code in the Client is creating the buttons and event handler methods. The methods include a good number conditional statements that check to see if the an extant instance of the clock. In testing, I found that the program would crash faster than a drunk driver if the current usage of an object were not first checked. In a non-demonstrative application, all the client would do would be to instantiated the selected clock and let it take care of the alarm event.</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('p4947code32'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p494732"><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
</pre></td><td class="code" id="p4947code32"><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> aClock:IAlarmClock;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> selectBell:<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> selectRadio:<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> testAlarm:<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> shutOff:<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> clockNow: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> btnWidth:uint=shutOff.<span style="color: #0066CC;">width</span>+<span style="color: #cc66cc;">10</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			addChild<span style="color: #66cc66;">&#40;</span>clockNow<span style="color: #66cc66;">&#41;</span>;
			selectBell.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Analog Clock&quot;</span>;
			selectBell.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">250</span>,selectBell.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">10</span>;
			selectBell.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,getBell<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>selectBell<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			selectRadio.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Radio Clock&quot;</span>;
			selectRadio.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">250</span>,selectRadio.<span style="color: #006600;">x</span>=btnWidth+<span style="color: #cc66cc;">10</span>;
			selectRadio.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,getRadio<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>selectRadio<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			testAlarm.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Test Alarm&quot;</span>;
			testAlarm.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">250</span>,testAlarm.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">*</span>btnWidth+<span style="color: #cc66cc;">10</span>;
			testAlarm.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,alarmTest<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>testAlarm<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			shutOff.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Turn Off Alarm!&quot;</span>;
			shutOff.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">250</span>,shutOff.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">3</span><span style="color: #66cc66;">*</span>btnWidth+<span style="color: #cc66cc;">10</span>;
			shutOff.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,offNow<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>shutOff<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> getClock<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			removeChild<span style="color: #66cc66;">&#40;</span>clockNow<span style="color: #66cc66;">&#41;</span>;
			clockNow=aClock.<span style="color: #006600;">curTime</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			clockNow.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">20</span>,clockNow.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">10</span>;
			addChild<span style="color: #66cc66;">&#40;</span>clockNow<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> getRadio<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>aClock<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				aClock.<span style="color: #006600;">offAlarm</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			aClock=<span style="color: #000000; font-weight: bold;">new</span> RadioClock<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			getClock<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> getBell<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>aClock<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				aClock.<span style="color: #006600;">offAlarm</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			aClock=<span style="color: #000000; font-weight: bold;">new</span> BellClock<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			getClock<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> alarmTest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>aClock<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				aClock.<span style="color: #006600;">doAlarm</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>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> offNow<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>aClock<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				aClock.<span style="color: #006600;">offAlarm</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>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As noted, the clocks and their alarms are set up in what might be called &#8220;frozen&#8221; demo mode. In fact, the Client just uses an IAlarmClock implementation and its methods to select a clock and turn the alarm on and off.</p>
<p>We always welcome your comments, and we hope to hear from you about this pattern. Also, we&#8217;re really hoping that you send in some components that you have created that we can put into this app. In the next post, you&#8217;ll be treated to a discussion (rant?) about using parameters in applications like this one. (Duck!)</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2011%2F03%2Factionscript-3-0-saturated-bridge-design-pattern-2-bridge-clock-implementation%2F&amp;title=ActionScript%203.0%20Saturated%20Bridge%20Design%20Pattern%202%3A%20Bridge%20Clock%20Implementation" id="wpa2a_6"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/03/actionscript-3-0-saturated-bridge-design-pattern-2-bridge-clock-implementation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reuse Where Objects Change: Factory Method at Work with Lazy Instantiation</title>
		<link>http://www.as3dp.com/2011/02/reuse-where-objects-change-factory-method-at-work-with-lazy-instantiation/</link>
		<comments>http://www.as3dp.com/2011/02/reuse-where-objects-change-factory-method-at-work-with-lazy-instantiation/#comments</comments>
		<pubDate>Mon, 21 Feb 2011 01:42:42 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Abstract Classes]]></category>
		<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Factory Method]]></category>
		<category><![CDATA[Lazy instantiation]]></category>
		<category><![CDATA[Loose Coupling]]></category>
		<category><![CDATA[re-use program elements]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=4723</guid>
		<description><![CDATA[Gentle Readers: Chandima and I try different things, and this is the first time (I think) that we&#8217;ve taken an online Adobe Connect presentation and the accompanying PowerPoint slides and translated them into an article (post.) If you were able to attend the online talk, you have all of the slides along with written commentary [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.as3dp.com/wp-content/uploads/2011/02/sleepFactoryB.png" alt="sleepFactoryB" title="sleepFactoryB" width="250" height="173" class="alignleft size-full wp-image-4728" /><em><strong>Gentle Readers:</strong> Chandima and I try different things, and this is the first time (I think) that we&#8217;ve taken an online  Adobe Connect presentation and the accompanying PowerPoint slides and translated them into an article (post.) If you were able to attend the online talk, you have all of the slides along with written commentary and you can take your time to review them. Of course if you are unable to attend because it&#8217;s 3:30 am in Mumbai, this provides an asynchronous means of participating. Let us know what you think. Is it effective? Ineffective? Makes no difference? Your comments are valuable and invited. </em></p>
<p><strong>Lazy Bones</strong></p>
<p>There&#8217;re more ways than one for being lazy with design patterns. In this post we&#8217;ll look at two: First we&#8217;ll see where you can take a  program based on the Factory Method and reuse it for another program. Secondly, we&#8217;ll see where we put lazy instantiation in a Factory Method. That&#8217;s double lazy! So Homer Simpsons of the world unite! This is the design pattern for you. D&#8217;oh! (In the past we&#8217;ve examined <a href="http://www.as3dp.com/2010/05/24/actionscript-3-0-lazy-initialization-and-the-factory-method-design-pattern/">lazy initialization</a>, but in this post we&#8217;ll look at <em>lazy instantiation</em>.)</p>
<p>Last week we had our first online presentation thanks to the Adobe users in Brazil. However, many missed it, and I promised to provide everyone with the materials we covered. Besides, since this is a discussion of a Factory pattern we can reuse our Factory Button! (Even this post is lazy!) Click the two buttons to see the apps with two different implementations. Then you can download the two apps and the PowerPoint file from the presentation by clicking the download button.<br />
<div id="attachment_4179" class="wp-caption alignleft" style="width: 135px"><a href="http://www.sandlight.com/Brasil/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2010/12/factoryBtn.png" alt="Southern Brazil" title="factoryBtn" width="125" height="60" class="size-full wp-image-4179" /></a><p class="wp-caption-text">Southern Brazil</p></div>&nbsp;<div id="attachment_4179" class="wp-caption alignleft" style="width: 135px"><a href="http://www.sandlight.com/NewEngland/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2010/12/factoryBtn.png" alt="New England" title="factoryBtn" width="125" height="60" class="size-full wp-image-4179" /></a><p class="wp-caption-text">New England</p></div><a href="http://nemo.mwd.hartford.edu/~wsanders/Brasil"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a>
<p>So you may be thinking that both of the applications are doing pretty much the same thing—placing objects on the screen. That&#8217;s true. Rocket science it&#8217;s not. However, the nature of the classes is such that we have a wide range of options with the objects. As the applications are currently laid out, it&#8217;s easy to change the objects and the collection of objects without breaking the application. Let&#8217;s see how.<br />
<span id="more-4723"></span></p>
<p><strong>Talk to the Factory</strong></p>
<p>The Factory Method design pattern is made up of a Creator (Factory) interface with concrete creators that instantiate concrete products represening implementations of a Product interface. The Creator contains an abstract <em>FactoryMethod()</em> operation that each concrete creator implements. Figure 1 shows a typical factory pattern with different shapes implied from the concrete creators and products. The shapes are representatives of any objects that the developer wants to request using the <strong>FactoryMethod()</strong> operations.<br />
<div id="attachment_3279" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2010/05/FactoryMethodLazy.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt;Class Diagram Factory Method&lt;/em&gt;" title="FactoryMethodLazy" width="500" height="248" class="size-full wp-image-3279" /><p class="wp-caption-text"><em><strong>Figure 1:</strong> Factory Method Class Diagram</em></p></div></p>
<p>The class diagram accurately portrays the role of the concrete factories—they instantiate the products indicated by the dashed arrows. The factories are more like <em>car dealerships</em> than they are &#8220;factories.&#8221; The dealerships order (instantiate) a completely built car. They don&#8217;t build them—they get them. If you have parameterized products; then perhaps you can think of the instantiation process as one where the objects are &#8220;built&#8221; by the concrete factories that pass the arguments, but in this example, think of the concrete creator (factory) classes as &#8220;auto dealerships&#8221; and not &#8220;auto factories.&#8221;</p>
<p>Figure 2 shows how the client makes its request and the links between the participants:<br />
<div id="attachment_4713" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/02/Slide03.jpg" alt="&lt;em&gt;&lt;strong&gt;Figure 2:&lt;/strong&gt; Client makes request&lt;/em&gt;" title="Slide03" width="500" height="375" class="size-full wp-image-4713" /><p class="wp-caption-text"><em><strong>Figure 2:</strong> Client makes request</em></p></div> By making requests in this manner, the Client is only loosely coupled to the object. If the object changes, the Client has no knowledge of it, but as long as the concrete factory (creator) implements the concrete product correctly, the client&#8217;s request is met.</p>
<p>To get a better idea of the details of the client&#8217;s request, look at Figure 3. The object being requested is a Brazillian state named Paraná (not to be confused with the fish, <em>piranha</em> or <em>paraña</em>). Note that the variable named pirana is typed as a Factory—the interface. Then the request is made by instantiating a concrete factory class, <strong>MakeParana</strong>, an implementation of the Factory abstract class. (<em>Programming to the interface; not the implementation.</em>)<br />
<div id="attachment_4716" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/02/Slide06.jpg" alt="&lt;em&gt;&lt;strong&gt;Figure 3:&lt;/strong&gt; Client makes request using a Factory object&lt;/em&gt;" title="Slide06" width="500" height="375" class="size-full wp-image-4716" /><p class="wp-caption-text"><em><strong>Figure 3:</strong> Client makes request using a Factory object</em></p></div></p>
<p>To help get a better idea of how that request is made in the larger context of a Factory Method design pattern, Figure 4 shows just those classes involved in the request in a file diagram. The request is sent to the MakePirana concrete factory through the Factory interface (an abstract class). The factory instantiates a concrete Product instance named Pirana.<br />
<div id="attachment_4719" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/02/Slide05.png" alt="&lt;em&gt;&lt;strong&gt;Figure 4: Files used in Pirana object request&lt;/strong&gt;&lt;/em&gt;" title="Slide05" width="500" height="375" class="size-full wp-image-4719" /><p class="wp-caption-text"><em><strong>Figure 4: Files used in Pirana object request</strong></em></p></div></p>
<p>If I wanted to change the character of the Pirana object to something else, the Client could care less. It just makes the request to the concrete factory object and lets the factory worry about instantiating the object. Let&#8217;s say that instead of the state of Pirana, I wanted to use the design pattern to get the state of Connecticut. I wouldn&#8217;t change the request to the factory; I&#8217;d change the object that the factory instantiates and returns to the Client.</p>
<p><strong>The Abstract Factory and Product</strong></p>
<p>Both of the interfaces in this implementation of the Factory Method are made up of abstract classes. The Factory class holds a reference to the Product class and an abstract <strong>factoryMethod()</strong> operation. The details of the factoryMethod() are left up to the concrete factories to implement. Figure 5 shows the Factory abstract class:<br />
<div id="attachment_4725" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/02/Slide07.jpg" alt="&lt;em&gt;&lt;strong&gt;Figure 5:&lt;/strong&gt; The Factory Interface (abstract class)&lt;/em&gt;" title="Slide07" width="500" height="375" class="size-full wp-image-4725" /><p class="wp-caption-text"><em><strong>Figure 5:</strong> The Factory Interface (abstract class)</em></p></div></p>
<p>Nothing is declared and the protected variable, <strong>product</strong> is only declared as a Product type. <strong>Product</strong>, is an abstract class and so the product variable will have to be instantiated as a concrete implementation of Product and not Product itself. (You cannot instantiate an abstract class.)</p>
<p>The Product class is similar in that it is abstract and nothing is implemented. Figure 6 shows how it is built:<br />
<div id="attachment_4734" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/02/Slide09.jpg" alt="&lt;em&gt;&lt;strong&gt;Figure 6:&lt;/strong&gt; The abstract class Product&lt;/em&gt;" title="Slide09" width="500" height="375" class="size-full wp-image-4734" /><p class="wp-caption-text"><em><strong>Figure 6:</strong> The abstract class Product</em></p></div></p>
<p>Originally, I had the Product class subclass Sprite, but I realized later that I did not need to do that. Each of the product objects were already children of the Sprite class, and so I had all of the properties and methods in the Sprite class I needed. By not subclassing the Sprite, the Product class is looser because it has no binding through subclassing. Importing the Sprite package is enough to reference the Sprite as the return type of the <strong>getObject()</strong> method and <strong>objectNow</strong> property.</p>
<p><strong>Implementing the Concrete Factories (with Lazy Instantiation) and Products</strong></p>
<p>All that&#8217;s left is to implement are the Factory and Product interfaces (abstract classes). The objects that are used to actually show images on the screen are Library Sprites. I just drew the different states (<em>estados</em>) using Flash tools and converted them into MovieClip symbols. Then, I converted the MovieClip classes into Sprite classes by changing the Base class from <strong>flash.display.MovieClip</strong> to <strong>flash.display.Sprite</strong>.</p>
<p>The concrete factory that calls the requested Parana state, checks to see if the product already exists, and if not, produces one. Then, using the <strong>product</strong> object it uses the <strong>getObject()</strong> method to get the requested product. Figure 7 shows how each of the concrete factories are coded&#8211;the only difference being that they instantiated different concrete products:<br />
<div id="attachment_4756" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/02/Slide08.png" alt="&lt;em&gt;&lt;strong&gt;Figure 7:&lt;/strong&gt; Concrete factory instantiates requested object&lt;/em&gt;" title="Slide08a" width="500" height="375" class="size-full wp-image-4756" /><p class="wp-caption-text"><em><strong>Figure 7:</strong> Concrete factory instantiates requested object</em></p></div></p>
<p>We can see the <strong>lazy instantiation</strong> in the conditional statement that is looking for an uninstantiated product, and if the product does not exist as an instance, it creates one.  The <strong>product</strong> property is inherited from the Factory abstract class. The <strong>product</strong> is initialized in the Factory class <em>as a</em> Product type—that&#8217;s why you don&#8217;t see any data type assigned to the <strong>product</strong> in the concrete factory. So while the product is unimplemented, it has been initialized. This binds the product only to the Product interface and not any single product implementation. As a result, the product object can be used to instantiate any of the concrete properties. This is what makes programming to the interface and loose coupling so valuable.</p>
<p>Finally, we come to the concrete products. Each products extends the Product class and implements the <strong>getObject()</strong> function. The <strong>objNow</strong> property (inherited from the Product abstract class) instantiates a Sprite object from the Library. The Sprite can come from any source where a Sprite can be stored. The important fact is that the object is a Sprite and has key Sprite properties and methods. In this case, all we need are the horizontal (<strong>x</strong>) and vertical (<strong>y</strong>) properties. Figure 8 shows how the Parana product is created;<br />
<div id="attachment_4750" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2011/02/Slide10.jpg" alt="&lt;em&gt;&lt;strong&gt;Figure 8:&lt;/strong&gt; Concrete Product class&lt;/em&gt;" title="Slide10" width="500" height="375" class="size-full wp-image-4750" /><p class="wp-caption-text"><em><strong>Figure 8:</strong> Concrete Product class</em></p></div></p>
<p>The important features of this example lies not what was done initially, but rather how it was reused to create a similar application for a different region. Likewise, the concrete factory and product classes follow the OOP single responsibility principle—each has a single responsibility that is loosely bound and re-useable. For example, the following two classes show how the same application was reused to create the New England version of the same program:</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('p4723code34'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p472334"><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="p4723code34"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Connecticut Factory</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MakeConn <span style="color: #0066CC;">extends</span> Factory
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> factoryMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span> product<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				product=<span style="color: #000000; font-weight: bold;">new</span> ConnProd<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">return</span> product.<span style="color: #006600;">getObject</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;">//Connecticut Product</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConnProd <span style="color: #0066CC;">extends</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> getObject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			objNow=<span style="color: #000000; font-weight: bold;">new</span> Conn<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			objNow.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">188.45</span>, objNow.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">321.75</span>;
			<span style="color: #b1b100;">return</span> objNow;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, the concrete factory and product are the same as those for the Brazilian states. The Factory and Product abstract classes are identical.</p>
<p><strong>One Year of Programming Experience 20 times</strong></p>
<p>You may have heard programmers proclaim,</p>
<blockquote><p>I&#8217;ve been programming for 20 years, blah, blah, blah&#8230;.</p></blockquote>
<p> While such programmers may have a wealth of knowledge from their experience, they may have just been doing the same kind of programming for 20 years. Years ago, I heard a police officer remark about one of the older (and not too effective) officers as a</p>
<blockquote><p>&#8230;guy who had 1 year experience 20 times.</p></blockquote>
<p>Essentially the police officer who made that comment was saying that the old-timer never tried to keep up his skill levels with new knowledge. He just kept doing the same old thing again and again. That can happen at any age, and in computing, it&#8217;s a recipe for disaster.</p>
<p>When I discovered OOP and then Design Patterns, I realized that I had been like that old cop who kept doing the same thing over and over again. To be sure, I learned several new languages ranging from machine/assembly languages to high level languages like BASIC and even PostScript—and just about everything in between. However, while different computer languages have different lexicons, sequential and procedural programming are still S&#038;P programming no matter what language you&#8217;re in.</p>
<p>To break those (bad) habits, design patterns and the OOP that goes with them, forced me to think in different ways approaching programming problems. Instead of thinking in terms of a series of steps, I started to think in terms of loosely bound objects communicating with one another. The only sequences were little ones inside the methods and assigned values to properties—and many of those were just for using other objects. The design patterns themselves became &#8220;cheat sheets&#8221; where I could more easily envision how objects could be arranged to communicate and achieve a goal.</p>
<p>This particular example somehow brought many of the lessons I&#8217;ve learned from design patterns home. It wasn&#8217;t until the last minute that I decided to make another regional map where I could demonstrate re-use of design patterns (Southern Brazil to Northeast US). Because I waited until the last minute, that was about all the time I had—that last minute. However, once I finished the graphic sprites, everything else fell into place. I just re-used the structures and the communication systems from the original app. It may sound funny to voice it, but programming to the interface really saved my bacon. Here I had a whole new set of objects I had to get singing in the same choir, but I knew that as long as I paid attention to the interface, everything would work together—that&#8217;s 27 classes scaled down to 17 for a new application.(That&#8217;s not even counting the Sprite classes with the graphics!) And everything worked out fine.</p>
<p> Try doing that without a design pattern. Spaghetti city!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2011%2F02%2Freuse-where-objects-change-factory-method-at-work-with-lazy-instantiation%2F&amp;title=Reuse%20Where%20Objects%20Change%3A%20Factory%20Method%20at%20Work%20with%20Lazy%20Instantiation" id="wpa2a_8"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/02/reuse-where-objects-change-factory-method-at-work-with-lazy-instantiation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Interface: What are they good for?</title>
		<link>http://www.as3dp.com/2010/12/actionscript-3-0-interface-what-are-they-good-for/</link>
		<comments>http://www.as3dp.com/2010/12/actionscript-3-0-interface-what-are-they-good-for/#comments</comments>
		<pubDate>Sun, 05 Dec 2010 15:47:59 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Abstract Classes]]></category>
		<category><![CDATA[Class Relations]]></category>
		<category><![CDATA[Learning Design Patterns]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=4101</guid>
		<description><![CDATA[Let&#8217;s Be Humble Design Patterns were founded in the context of OOPSLA (Object-Oriented Programming, Systems, Languages &#038; Applications) gatherings sponsored by the ACM (Association for Computing Machinery) and attended by both academic and industry programmers. These are smart folks in the world of computing, but as I&#8217;ve mentioned on other occasions, they are far less [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_4086" class="wp-caption alignleft" style="width: 258px"><img src="http://www.as3dp.com/wp-content/uploads/2010/12/interface.png" alt="What good are interfaces?" title="interface" width="248" height="224" class="size-full wp-image-4086" /><p class="wp-caption-text">What good are interfaces?</p></div> <strong>Let&#8217;s Be Humble</strong></p>
<p>Design Patterns were founded in the context of OOPSLA (Object-Oriented Programming, Systems, Languages &#038; Applications) gatherings sponsored by the ACM (Association for Computing Machinery) and attended by both academic and industry programmers. These are smart folks in the world of computing, but as I&#8217;ve mentioned on other occasions, they are far less arrogant and more willing to listen to others—especially if those others listen to them in turn. Everyone assumes that everyone else is knowledgable and if they have a question, they get a straight answer&#8211;not a <em>I&#8217;m smarter than you</em> answer, but an-answer-to-the-question-asked kind of answer. I appreciate that because I&#8217;ve got a lot of questions.</p>
<p>At the last OOPSLA meeting (now called SPLASH), I got into an argument with a programmer and we both got our backs up. However, we both realized that the other was sincere, and we both shared the common goal of learning more about the topic; and the result was that she and I learned from one another. Mostly, I just ask straight questions and get straight answers—or give them. Sometimes mine or others answers may clash, but that&#8217;s what happens when you share ideas.</p>
<p>Now to the point. The other day I was snooping around the internet for a good distinction between abstract classes and interfaces and I came across a link to a blog that promised such a distinction. In discussing the distinction, the blog&#8217;s host first went on a long-winded detour to tell us how smart he was and how he used a &#8220;test question&#8221; to queries about programming. The &#8220;test question&#8221; was to distinguish between abstract classes and interfaces. His &#8220;answer&#8221; was hopelessly vague and muddled. What a jerk!</p>
<p>So, I am going to try and come up with a clear idea of what a class interface is and why we should care about it. I&#8217;ve included an interface example further on in the post, but you can download the files here:<br />
<a href="http://www.sandlight.com/interface/interface.zip" target="_blank" ><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a></p>
<p><strong>Straight Answer to &#8220;What is an Interface?&#8221;</strong></p>
<p>In our book on page 19, the section, <em>The many meanings of interface</em>, differentiates between user interfaces and object interfaces and notes that the ActionScript 3.0 keyword <strong>interface</strong> that is used to declare an interface structure is yet still another type of interface. Essentially, an interface is an abstract structure with a list of unimplemented methods whose signatures must be used in any implementation of the interface. <em>That&#8217;s about as clear as mud in terms of what an interface is used for</em>. It&#8217;s a straight enough answer, but clarifies nothing, nada, nichts, rien, niente.</p>
<p>In attempting to arrive at a good answer I looked at the Adobe documentation, and while I found nothing inaccurate and lots of examples I found nothing clarifying. So, I decided to back up and look at the <em>more general concept of interface</em>. One that we all seem to know and understand is that of a <em>user interface</em>. An interface that allows users to communicate with a computer program is a user interface. A simple example is a button. If I click a button, I can interact with the program—it is the interface. Click the button below to see what I mean:<br />
<a href="http://www.sandlight.com/interface/" ><img src="http://www.as3dp.com/wp-content/uploads/2010/12/interfaceBtn1.png" alt="interfaceBtn" title="interfaceBtn" width="200" height="58" class="alignnone size-full wp-image-4098" /></a></p>
<p>Essentially, you were able to &#8220;touch&#8221; the program to make it do something. Okay, so we know a user interface is between the user and the computer&#8217;s program. Likewise, a TV set is an interface between a user and television programming across the airwaves, via satellite or from cable.<br />
<span id="more-4101"></span><br />
With that in mind, an ActionScript 3.0 interface must be between <em>something</em> and <em>something</em>. Further, there must be a good reason for having such a relationship. So, with this in mind, I went back to the hunt. What I found was surprisingly simple and yet very clear.</p>
<p><strong>An Abstraction of Service</strong></p>
<p>About five years ago, O&#8217;Reilly published a book  entitled <em>Programming .NET Components</em> by Juval Lowy.  In it Lowy discusses, among other things, the purpose of using interfaces. He uses a model of what he calls <em>component-oriented programming</em> (COP), and while there may be some differences between OOP and COP, they share certain basic assumptions about loose coupling. He calls interfaces, an <em>abstraction of service</em>, in the sense that a service is part of your program that does something—usually found in the class methods. In a nicely worded summary, he notes,</p>
<blockquote><p>When you separate the interface from the implementation in your programming, your client is compatible with far more services.</p></blockquote>
<p>While not putting it in those words, Chandima and I have been making the same point both on this blog and in our book. Again and again we&#8217;ve discussed how a Client class is the one that makes requests. However, what does the Client make the request for? Why services, of course. Because the interface can only state the methods&#8217; signature (see page 19 of our book for a good diagram and explanation of a signature), it can be implemented in many different ways. Thus, a client can create an instance that will make requests through the interface that provides both variety and the ability for the programmer to make later changes without changing the Client.</p>
<p><strong>It&#8217;s All About Communication</strong></p>
<p>OOP is all about communication between objects. Just like the button is a human interface for communication between the user and the service (in the program), an interface is for communication between Client and Implementation or between Class and Class or Object and Object. The following example uses an interface through which the client requests services—one to make an ellipse and the other to make a triangle.(You can download them using the download button at the top of the post.) Click the (patent pending) Acme Interface Button again:<br />
<a href="http://www.sandlight.com/interface/InterfaceWork.html" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2010/12/interfaceBtn1.png" alt="interfaceBtn" title="interfaceBtn" width="200" height="58" class="alignnone size-full wp-image-4098" /></a><br />
 To get started the Client class is the first in the listings here because it best illustrates how the Client requests the services through the interface, (<strong>IGraphic</strong>).</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p4101code37'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p410137"><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
</pre></td><td class="code" id="p4101code37"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
	<span style="color: #0066CC;">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> GraphicClient <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> requesterE:IGraphic=<span style="color: #000000; font-weight: bold;">new</span> EllipseGraphic<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> requesterT:IGraphic=<span style="color: #000000; font-weight: bold;">new</span> TriangleGraphic<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> triBtn:<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> ellipseBtn:<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> holder: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> flexShape:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> GraphicClient<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>;
			holder.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">0</span>,holder.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">55</span>;
			addChild<span style="color: #66cc66;">&#40;</span>holder<span style="color: #66cc66;">&#41;</span>;
			holder.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>flexShape<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> triService<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>
			holder.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span>flexShape<span style="color: #66cc66;">&#41;</span>;
			flexShape=requesterT.<span style="color: #006600;">doDraw</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			holder.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>flexShape<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> ellipseService<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>
			holder.<span style="color: #006600;">removeChild</span><span style="color: #66cc66;">&#40;</span>flexShape<span style="color: #66cc66;">&#41;</span>;
			flexShape=requesterE.<span style="color: #006600;">doDraw</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			holder.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>flexShape<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>
			triBtn.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">100</span>,triBtn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">100</span>;
			triBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, triService<span style="color: #66cc66;">&#41;</span>;
			triBtn.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Request Triangle&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>triBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			ellipseBtn.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">100</span>,ellipseBtn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">130</span>;
			ellipseBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, ellipseService<span style="color: #66cc66;">&#41;</span>;
			ellipseBtn.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Request Ellipse&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>ellipseBtn<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>Originally, I used a single request instance typed to the IGraphic interface, but  each time it was called I had to instantiate a new concrete triangle or ellipse class. That was done to illustrate the flexibility of programming to the interface. However, even an illustration isn&#8217;t worth the bad practice of continually using the <strong>new</strong> statement each time one of the button clicks requests a triangle or ellipse. So, I created two IGraphic types, one for a triangle request and the other for an ellipse request. In this way only a single new statement was used for each object and the new statement is required only once for each object no matter how many times it is used.</p>
<p>The rest of the program is pretty simple. Two classes implement the IGraphic interface and provide public access to call the <strong>doDraw()</strong> method.</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('p4101code38'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p410138"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
</pre></td><td class="code" id="p4101code38"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Interface</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IGraphic
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> doDraw<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Triangle</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TriangleGraphic <span style="color: #0066CC;">implements</span> IGraphic
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> standardTriangle=<span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doDraw<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			standardTriangle.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xcc0000<span style="color: #66cc66;">&#41;</span>;
			standardTriangle.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawTriangles</span><span style="color: #66cc66;">&#40;</span>Vector.<span style="color: #66cc66;">&lt;</span>number<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span> <span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">100</span>,  <span style="color: #cc66cc;">200</span>,<span style="color: #cc66cc;">200</span>,  <span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			standardTriangle.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> standardTriangle;
		<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;">//Ellipse</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EllipseGraphic <span style="color: #0066CC;">implements</span> IGraphic
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> standardEllipse=<span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doDraw<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			standardEllipse.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x00cc00<span style="color: #66cc66;">&#41;</span>;
			standardEllipse.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span>,0x0000cc<span style="color: #66cc66;">&#41;</span>;
			standardEllipse.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawEllipse</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">100</span>, <span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;
			standardEllipse.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> standardEllipse;
		<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 purpose of this example is to show how the interface provides a simple and clear gateway for the client to access services. As the program grows and changes, it is also quite simple to update other services through the same interface.</p>
<p><strong>OOP Interfaces</strong></p>
<p>One of the ironies I discovered in creating this example was found in the Gang of Four&#8217;s discussion of an Object interface (not a framework or program interface). An object&#8217;s interface is the <em>set of all signatures defined by an object&#8217;s operations</em>. They note,</p>
<blockquote><p>An object&#8217;s interface characterizes the complete set of requests that can be sent to the object. Any request that matches a signature in the object&#8217;s interface may be sent to the object. pg. 13</p></blockquote>
<p>Where a program&#8217;s interface (such as the IGraphic one used in the example) is interacting and communicating with objects whose own interfaces are synched, you can imagine both a well-oiled machine through which the different parts communicate as well as one where change and additions can be added seamlessly. So the question may not be, <em>What are interfaces good for?</em>, but instead, <em>How&#8217;d I get along with out them for so long?</em></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%2F2010%2F12%2Factionscript-3-0-interface-what-are-they-good-for%2F&amp;title=ActionScript%203.0%20Interface%3A%20What%20are%20they%20good%20for%3F" id="wpa2a_10"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2010/12/actionscript-3-0-interface-what-are-they-good-for/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Design Pattern Principles for ActionScript 3.0: The Dependency Inversion Principle</title>
		<link>http://www.as3dp.com/2009/04/design-pattern-principles-for-actionscript-30-the-dependency-inversion-principle/</link>
		<comments>http://www.as3dp.com/2009/04/design-pattern-principles-for-actionscript-30-the-dependency-inversion-principle/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 12:51:52 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Abstract Classes]]></category>
		<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Principles]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=862</guid>
		<description><![CDATA[Most of what we mean by the Dependency Inversion Principle has been discussed either in describing what a design pattern does in terms of relating components to one another or implied in other principles. However, I’ve found that the Dependency Inversion Principle acts as a helpful reminder to keep things abstract in working with instances [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-760" title="bucketrule" src="http://www.as3dp.com/wp-content/uploads/2009/02/bucketrule.png" alt="bucketrule" width="150" height="133" />Most of what we mean by the <strong>Dependency Inversion Principle</strong> has been discussed either in describing what a design pattern does in terms of relating components to one another or implied in other principles. However, I’ve found that the Dependency Inversion Principle acts as a helpful reminder to keep things abstract in working with instances that request functionality or extend a base class.</p>
<p>Unfortunately, the concept of <em>inversion</em> is only half the story and is used in reference to programming structures where a higher-level module depends on a lower-level module. One might be led to believe that the inversion occurs where lower level modules depend on higher-level modules <em>only</em>. Actually, the principle holds that<br />
<blockquote>all modules of a program should depend on abstractions</p></blockquote>
<p>Maybe a better name for the principle would be the <strong>Abstraction Dependency Principle</strong>. In any event, program modules should all depend only on abstractions. If you look at Figure 1 you can see a simple example where a Client instantiates a concrete class but depends on the abstract class for typing. Further, the concrete classes depend on the abstract class for implementation. Aggregations and associations would depend on the abstract base class instead of the concrete classes as well.<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/04/depinversion.png" alt="depinversion" title="depinversion" width="408" height="281" class="alignnone size-full wp-image-863" /></p>
<p><em><strong>Figure 1:</strong> Depending on Abstractions</em><br />
As you can see in both the diagram and code snippets, all dependencies are on the Abstract class. That’s what the principle means at its core. It’s not a difficult one, and it overlaps with lots of other principles we’ve discussed on this blog and in our book.<br />
<span id="more-862"></span><br />
<strong>The Devil is in the Details</strong></p>
<p>A corollary to dependency inversion is what might be called <em>detail inversion</em>—not that anyone actually calls it that. The principle is,</p>
<blockquote><p>Details should depend on abstractions, and abstractions <strong>should not</strong> depend on details.</p></blockquote>
<p>As you can see in Figure 1, the details in the two concrete classes depend on the implementations of the abstract class. Likewise, the details of instantiation depend on the nature of the Client’s request. If you’ve been reading about the different design patterns on this blog and in our book, this corollary may appear to be almost too self-obvious to state. However, if this is the first OOP principle you’ve considered, it may not be as clear.</p>
<p>Consider an abstract class called Fruit. As you know, the details of different kinds of fruit are many—from kumquats to kiwi fruit and bananas to blueberries. So, while you can put the general characteristics of fruit in an abstract class (e.g., berry, pome, drupe), the details are in the concrete implementations (e.g., apples and oranges). However, the implementations depend on the general properties in the base abstract class; not the other way around.</p>
<p><strong>Where to Find Dependency Inversion in Design Patterns</strong></p>
<p>Robert C. Martin’s series of articles in his <em>Engineering Notebook</em> written for the <em>C++ Report</em> came out around the same time as the Gang of Four’s <em>Design Patterns</em> book. (Roughly the period of late 1995 to early 1996). However, Martin along with OOP thinkers like Grady Booch and GoF were all thinking along the same lines. Martin makes references to <strong>OOD</strong> for Object Oriented Design—clearly differentiating it from <strong>OOP</strong>, and he references the GoF book, which was brand new at the time he was writing about dependency inversion. What all of these software engineers had in common was that their emphasis had shifted from <strong>just programming</strong>to the broader concept of <strong>programming design</strong>.</p>
<p>So while you’ll not find a reference to <em>dependency inversion</em> in GoF, you will see it applied in design patterns. For example, if you look at the Strategy design pattern, you will see an almost perfect example of dependency inversion. The Client makes requests through the abstract Context, which in turn holds references to the abstract Strategy. Any references to the concrete classes are all through an abstract interface or class. That is, the references are typed as abstract classes or interfaces.  Likewise Martin uses a <strong>Template Method</strong> and <strong>Adapter</strong> design patterns to illustrate dependency inversion. Suffice it to say that the thinking and writing about design patterns in the early 1990s through OOPSLA conference presentations and papers began to coalesce around the mid-1990s in publications focusing on the <em>design</em> aspect of programming. The programming principles that emerged from that era were all looking for ways to express the importance of strong but loose designs for reusability and adding functionality.</p>
<p><strong>Into the Lunch Bucket</strong></p>
<p>Now that you have a sense of dependency inversion, you can put it into your lunch bucket and take it to work. You may be thinking that the other principles pretty much encompass what is included in the Dependency Inversion Principle, and you’d be right. When Robert C. Martin developed the concept, he noted that the principle encompasses both the Open/Closed and Liskov Substitution Principles. So any sense of déjà vu you may have detected is grounded in reality. Also, if you are interested in a related concept that will be discussed in a later post, you might want to look into Martin Fowler’s  <a href="http://martinfowler.com/articles/injection.html">Dependency Injection</a> concept. (Be careful not to get your Martins mixed up!). Likewise, Miško Hevery has a great <a href="http://misko.hevery.com/2008/11/11/clean-code-talks-dependency-injection/">video and article </a> on dependency injection.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2009%2F04%2Fdesign-pattern-principles-for-actionscript-30-the-dependency-inversion-principle%2F&amp;title=Design%20Pattern%20Principles%20for%20ActionScript%203.0%3A%20The%20Dependency%20Inversion%20Principle" id="wpa2a_12"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/04/design-pattern-principles-for-actionscript-30-the-dependency-inversion-principle/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Design Pattern Principles for ActionScript 3.0: Program to an Interface; not an implementation</title>
		<link>http://www.as3dp.com/2009/02/design-pattern-principles-for-actionscript-30-program-to-an-interface-not-an-implementation/</link>
		<comments>http://www.as3dp.com/2009/02/design-pattern-principles-for-actionscript-30-program-to-an-interface-not-an-implementation/#comments</comments>
		<pubDate>Sat, 14 Feb 2009 12:55:24 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Abstract Classes]]></category>
		<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Polymorphism]]></category>
		<category><![CDATA[Principles]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=685</guid>
		<description><![CDATA[The first principle of design patterns is, Program to an interface, not an implementation Simply put, the Gang of Four urges programmers to declare variables only to abstract classes and interfaces and not concrete implementations. You never want to type your instance as a concrete class derived from an interface or abstract class—only to the [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.as3dp.com/wp-content/uploads/2009/02/bucketrule1.png" alt="bucketrule1" title="bucketrule1" width="150" height="133" class="alignleft size-full wp-image-762" />The first principle of design patterns is,</p>
<blockquote><p>Program to an interface, not an implementation</p></blockquote>
<p> Simply put, the Gang of Four urges programmers to declare variables only to abstract classes and interfaces and not concrete implementations. You never want to type your instance as a concrete class derived from an interface or abstract class—only to the interface.</p>
<p>Okay, you may be thinking that we already covered this principle in our book. That’s true. We did in both the introduction (pp. 45-49) and in part of the chapter on the Observer design pattern (pp. 285-288.) We also had a lot of examples, and so why rehash the same principle here? The following reasons seemed compelling enough to warrant additional discussion:</p>
<ul>
<li>Includes dynamic binding</li>
<li>Closely tied to polymorphism</li>
<li> Built into Creation design patterns</li>
<li> Easy to ‘take to work’</li>
</ul>
<p>Before starting, we’ll use the concept of an <strong>interface</strong> to refer to programs that use either abstract classes or ActionScript 3.0 interface. So, if you see a reference to an <em>interface</em>, it could well be an abstract class. It’s the concept of an interface that’s important; not the ActionScript 3.0 interface statement.<br />
<span id="more-685"></span><br />
As you read this, you may hear echoes of the <a href="http://www.as3dp.com/2009/02/08/design-pattern-principles-for-actionscript-30-the-liskov-substitution-principle/">Liskov Substitution Principle</a> (LSP). I’m really not sure what relation there is between the LSP and the first GoF principle. Suffice it to say that LSP is part of the back-story (as they say in Hollywood), but only part. During the 1980s and 1990s when a lot of the OOP and Design Patterns concepts were being developed and presented at OOPSLA, ideas were shared. Further, GoF references <em>Abstraction and Specification in Program Development</em> by Barbara Liskov and John Guttag (McGraw-Hill, New York, 1986) so Gamma and his associates were aware of Liskov’s work. But to say that this principle was <em>derived</em> from LSP is a leap that I’m not willing to take. However, I certainly see no contradiction between LSP and GoF’s first principle.</p>
<p><strong>Dynamic Binding, Polymorphism and Interfaces</strong></p>
<p><a href= "http://www.as3dp.com/2008/12/19/actionscript-30-design-patterns-and-polymorphism-what’s-the-point/">Polymorphism</a> has been discussed elsewhere on this blog, and we all know it is one of the pillars of OOP. This first principle of design patterns is tied into polymorphism. In order to follow the path from interfaces to polymorphism, we need to start with an operation’s <strong>signature</strong>. As you know, a signature is made up of the following:</p>
<ul>
<li>operation’s name</li>
<li>objects it takes as parameters</li>
<li>return value</li>
</ul>
<p><em>In this context</em> we can discuss interfaces as all of the operations in an object. For the most part, an object as used by GoF refers to a <em>class</em> and not an instance of a class as the term <em>object</em> is conventionally used. (I won’t belabor the meaning of signatures and interfaces here, but if you want more details on both concepts, see the discussion beginning on page 19 of our book.)</p>
<p>When we type an object, such as,</p>
<p><code><strong>var myInstance:String; </strong></code></p>
<p>we tend to think of <strong>String</strong> as a <em>class type</em>, but in fact it denotes a particular <em>interface</em>. “Any request that matches a signature in the object’s interface may be sent to the object” (GoF 13).</p>
<p>Bumping this up to user created classes, we type to the interface, which is found in the supertype relative to a subtype. That is, a subclasses’ type resides in the parent class. Because all knowledge of an object is known only through its interface, the request does not address the implementation. As a result, objects with different implementations can have the same interface.</p>
<p>Several different objects can have the same interface (primarily based on the superclass) but different implementations, and since the actual operation that is performed is dependent on both the request and receiving object’s name, the actual outcome occurs at run-time.  Such an association between the a request and its operation at run-time is known as <strong>dynamic binding</strong>. The process of dynamic binding lets you substitute objects with identical interfaces at run-time. You probably know this process by another name, <strong>polymorphism</strong>.</p>
<p>Design patterns help you set up interfaces and use polymorphism in organizing your program. The more you look at the different principles of OOP, the more you can see how these principles are maintained and presented in design patterns. Key are ActionScript 3.0 pure interfaces and abstract classes. All operations in an interface are abstract, but an abstract class can defer some or all implementations to derived classes (subclasses.) Thus, the abstract class is more flexible than the pure interface, but both serve as interfaces</p>
<p><strong>Reducing Implementation Dependencies</strong></p>
<p>Gamma and his associates spell out two key benefits of manipulating objects solely in terms of the interface (of either pure interfaces or abstract classes).</p>
<blockquote><p>1. Clients remain unaware of the specific types of objects they use, as long as the objects adhere to the interface that clients expect. </p></blockquote>
<blockquote><p>2. Clients remain unaware of the classes that implement these objects. Clients only know about the abstract class(es) defining the interface. (GoF, p. 18) </p></blockquote>
<p>The whole purpose is to build functional structures but to keep them loose enough for change and reusability. Obviously you have to instantiate concrete classes, but by using the abstract class interface, (instead of the concrete classes’) you provide flexibility in the implementation. All design patterns in the <strong>Creational</strong> category [Factory Method,  Abstract Factory, Prototype, Builder and Singleton*] essentially <em>force</em> you to create your programs in a way that follows this principle. (*I have serious questions as to the Singleton’s ability to do anything very useful along these lines. See <a href="http://www.as3dp.com/2008/11/26/we-don’t-need-no-stinkin’-singletons-why-to-avoid-the-singleton-pattern-in-actionscript-30-programming/"> this post</a> for details.)</p>
<p><strong>Implementation Lunch Bucket Rule</strong></p>
<p>While GoF’s statement of the principle is very clear once you understand what they mean by an <strong>interface</strong>, we might be able to re-word it for an even simpler rule to take to work.</p>
<blockquote><p>Type all objects to the interface of the parent class and never to the child class</p></blockquote>
<p>Of course the parent and child class share an interface, but the child class (subclass) has concrete implementations.</p>
<p>The best part about this rule is that it is automatically enforced in all creational patterns. So, besides having a simple rule to work with (and bring to work) you have plenty of patterns that automatically set up your program to follow this rule.</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%2F02%2Fdesign-pattern-principles-for-actionscript-30-program-to-an-interface-not-an-implementation%2F&amp;title=Design%20Pattern%20Principles%20for%20ActionScript%203.0%3A%20Program%20to%20an%20Interface%3B%20not%20an%20implementation" id="wpa2a_14"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/02/design-pattern-principles-for-actionscript-30-program-to-an-interface-not-an-implementation/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Design Pattern Principles for ActionScript 3.0: The Liskov Substitution Principle</title>
		<link>http://www.as3dp.com/2009/02/design-pattern-principles-for-actionscript-30-the-liskov-substitution-principle/</link>
		<comments>http://www.as3dp.com/2009/02/design-pattern-principles-for-actionscript-30-the-liskov-substitution-principle/#comments</comments>
		<pubDate>Sun, 08 Feb 2009 12:26:00 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Abstract Classes]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Principles]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=659</guid>
		<description><![CDATA[Gentle Reader: Now that we’ve worked through all of the design patterns in ActionScript 3.0 from GoF (well, Builder is still in the works, but that’ll be available soon), now would be a good time start going through the principles underlying design patterns. This will be the first in that series. The 1987 OOPSLA keynote [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.as3dp.com/wp-content/uploads/2009/02/bucketrule2.png" alt="bucketrule2" title="bucketrule2" width="150" height="133" class="alignleft size-full wp-image-764" /><em><strong>Gentle Reader</strong>: Now that we’ve worked through all of the design patterns in ActionScript 3.0 from GoF (well, <strong>Builder</strong> is still in the works, but that’ll be available soon), now would be a good time start going through the principles underlying design patterns. This will be the first in that series.</em></p>
<p>The 1987 OOPSLA keynote address by Barbara Liskov contained what has become known as the <em>Liskov Substitution Principle (LSP)</em>. Essentially, the principle holds that</p>
<blockquote><p>If a Client is using a reference to a base class it should be able to <strong>substitute</strong> a derived class for it without affecting the program’s operation </p></blockquote>
<p>(Actually Dr. Liskov said something more like:</p>
<blockquote><p>If for each object <strong>o1</strong> of type <strong>S</strong> there is an object <strong>o2</strong> of type <strong>T</strong> such that for all programs <strong>P</strong> defined in terms of <strong>T</strong>, the behavior of <strong>P</strong> is unchanged when <strong>o1</strong> is substituted for <strong>o2</strong> then <strong>S</strong> is a subtype of <strong>T</strong>.</p></blockquote>
<p>but I’m not about to…)</p>
<p>If the Client has an object of a certain type, it should be able to substitute a derived object of the same type. For example, suppose you have an <em>abstract</em> class named <em><strong>Automobile</strong></em> and from that class you have subclasses of <strong>Ford</strong>, <strong>Toyota</strong>, and <strong>Peugeot</strong>. Your Client has an object, <strong>myAuto:Automobile</strong>. The <strong>myAuto</strong> object can be any of the subclasses, and if a substitution is made for any one of them everything keeps on working without a problem and the change is unknown to the Client. So if you substitute a Ford for a Toyota, the Client can work with the objects without having to adjust for the change. What’s more, if you want to add a <strong>Fiat</strong> class as a subclass of <strong>Automobile</strong>, the <strong>myAuto</strong> object handles it with nary a whimper.</p>
<p>The one caveat is that the subclasses must all honor the contractual conditions of the parent class. So, any methods in the parent class must be functioning in the subclass (aka, <em>derived class</em>.)</p>
<p>Now you may be thinking, <em>So what?</em> If you’re at all familiar with other principles of OOP and Design Patterns, this principle may sound vaguely familiar, but what is the importance of this concept/principle/idea? It is this: Because the Client is unaware of the concrete class that the object may implement, the structure is far more resilient. Not only can the same structure be reused, it can be changed, updated and generally fiddled with without easily breaking anything. (Think of adding more car manufacturers to the <strong>Automobile</strong> class.) As far as the Client is concerned, as long as the interface rules are followed with the object, everything is hunky-dory.<br />
<span id="more-659"></span><br />
<strong>A Simple Practice</strong></p>
<p>As a principle, the Liskov Substitution Principle is easy to carry around in your pocket. If you write the following line in your Client (or even your Factory) you’re doing something wrong:</p>
<p><strong><code>myObj:SubClassA = new SubClassA();</code></strong></p>
<p>You know that another subclass of the same base class cannot be substituted for SubClassA. If you attempt to substitute SubClassB for myObj, it will fail. Now, instead, all you have to remember is to write,</p>
<p><strong><code>myObj:ParentClass = new SubClass();</code></strong></p>
<p>That’s (just about) it! Well, one more thing. The parent class needs to be an <em>abstract class or an interface. </em><strong>Now</strong>, that’s it! How difficult is that to remember?</p>
<p><strong>LSP in the Weird World</strong></p>
<p>In looking at examples of the LSP principle, the two that pop up the most are the rectangle/square and ellipse/circle conundrums. We’ll use the rectangle/square example and try to provide two different alternatives to illustrate programming with LSP and without LSP.<br />
Beginning with the geometric observation, <em>all squares are also rectangles</em>, we’re likely to create a structure like that shown in Figure 1:</p>
<p style="text-align: center;">
<img class="size-full wp-image-660 aligncenter" title="rec1" src="http://www.as3dp.com/wp-content/uploads/2009/02/rec1.png" alt="rec1" width="335" height="242" /></p>
<p><em><strong>Figure 1:</strong> Square is a child of Rectangle</em></p>
<p>After all, we can envision a square as nothing more than a rectangle with equal sides. So an operation such as,</p>
<p><strong><code>makeRectangle(w,h); </code></strong></p>
<p>is an effective way to make either a square or rectangle. We add the Square class simply to examine the LS Principle; otherwise creating rectangles or squares would be accomplished using the <strong>makeRectangle(w,h) </strong> method making sure that w and h are equal when making squares. The inheritance path clearly indicates that a Square object <em>IS A</em> Rectangle. (That is, the two have an <em>IS A</em> relationship.) However, because Rectangle is a concrete class if I substitute Rectangle for Square, I’m going to have to change the type. I cannot substitute an instance of Rectangle for an instance of Square. This violates the Liskov Substitution Principle.<br />
Now, consider Figure 2. Instead of having Square as a child of Rectangle, both Square and Rectangle are children of the abstract class <em>Boxes</em>.</p>
<p style="text-align: center;">
<img class="size-full wp-image-661 aligncenter" title="rec2" src="http://www.as3dp.com/wp-content/uploads/2009/02/rec2.png" alt="rec2" width="435" height="243" /></p>
<p><em><br />
<strong>Figure 2: </strong>Abstract Class is Parent of both Squares and Rectangles</em></p>
<p>If I have an instance of Boxes, I can substitute either Square or Rectangle for the instance. In looking at an example, the Client is where we can expect to see the substitution principle at work. In the following example, the Client has a single object (<strong>subThis</strong>) typed as <strong>Boxes</strong>. However, that single object can gingerly create either a square or rectangle without the Client knowing the difference. It is designed to illustrate LSP.</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('p659code46'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65946"><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
</pre></td><td class="code" id="p659code46"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Sprite</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> fl<span style="color: #000066; font-weight: bold;">.</span>controls<span style="color: #000066; font-weight: bold;">.</span>Button<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Client <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">Sprite</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> subThis<span style="color: #000066; font-weight: bold;">:</span>Boxes<span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> rec<span style="color: #000066; font-weight: bold;">:</span>Button=<span style="color: #0033ff; font-weight: bold;">new</span> Button<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> squ<span style="color: #000066; font-weight: bold;">:</span>Button=<span style="color: #0033ff; font-weight: bold;">new</span> Button<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Client<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900; font-style: italic;">//Constructer adds buttons</span>
			rec<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span>rec<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000066; font-weight: bold;">;</span>
			rec<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> = <span style="color: #000000; font-weight:bold;">65</span><span style="color: #000066; font-weight: bold;">;</span>
			rec<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CLICK</span><span style="color: #000066; font-weight: bold;">,</span>doRec<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			rec<span style="color: #000066; font-weight: bold;">.</span>label = <span style="color: #990000;">&quot;Rectangle&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>rec<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			squ<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">170</span><span style="color: #000066; font-weight: bold;">,</span>squ<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">50</span><span style="color: #000066; font-weight: bold;">;</span>
			squ<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">width</span> = <span style="color: #000000; font-weight:bold;">65</span><span style="color: #000066; font-weight: bold;">;</span>
			squ<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">MouseEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CLICK</span><span style="color: #000066; font-weight: bold;">,</span>doSquare<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			squ<span style="color: #000066; font-weight: bold;">.</span>label = <span style="color: #990000;">&quot;Square&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>squ<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> doSquare<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
			subThis=<span style="color: #0033ff; font-weight: bold;">new</span> Square<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">createBox</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">70</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span>subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> doRec<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">MouseEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				<span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
			subThis=<span style="color: #0033ff; font-weight: bold;">new</span> Rect<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">createBox</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">70</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span>subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>The Client has two methods to create either a square or rectangle. However, both methods use the object <strong>subThis</strong> typed as <strong>Boxes</strong>. Using Boxes.createBox() (subThis.createBox()) the Client substitutes the Square and Rectangle for the Boxes object. Everything works smoothly. The way the program is set up when one is used, the box image changes from a square to a rectangle (or vice versa), but if you wanted, you could create as many as you want and splash them all over the stage. Figure 3 shows what you will see in the current setup.<br />
<img class="alignleft size-full wp-image-663" title="squarerec" src="http://www.as3dp.com/wp-content/uploads/2009/02/squarerec.png" alt="squarerec" width="600" height="199" /><br />
<em><strong>Figure 3: </strong> Substituting Square and Rectangle Subclasses for Boxes Object</em></p>
<p>With this simple program, you should be able to see the ramifications for larger programs. In fact, the more your program grows, the more important LSP becomes. If you <em>know</em> that you can substitute <em>any</em> derived class for a base class, you are less likely to have unpleasant surprises. As a program grows and changes, you will have more modules, more objects, and more methods that need to interact in a program. The LSP helps keep those relations functional.</p>
<p><strong>The Base Class</strong></p>
<p>After working with the LS Principle, I wondered,</p>
<blockquote><p>…is there any reason to create a concrete base class? </p></blockquote>
<p>The answer is <strong>no</strong>.</p>
<blockquote><p>Not a single design pattern has a structure with a concrete parent class.</p></blockquote>
<p>What do I ever need a concrete parent class for? I can load up a parent class with functionality that can be used with subclasses, but I need some kind of wiggle room for the subclasses to extend the base class. Following the open/closed principle (“Open for extension. Closed for modification”), I can extend with added functionality from a superclass but I don’t want to modify it.</p>
<p>The following base class (Boxes) has a single method and imports the essential Sprite class needed for the graphics. The concrete classes override the function but they both inherit the Sprite class embedded in the base 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('p659code47'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65947"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p659code47"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #009900; font-style: italic;">//abstract class</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Sprite</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Boxes <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">Sprite</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">createBox</span><span style="color: #000000;">&#40;</span>w<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span>h<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #009900; font-style: italic;">//Abstract content</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>In an odd choice, ActionScript 3.0 has Graphic methods for a circle (drawCircle) and an ellipse (drawEllipse), but not for a square &#8211;only a rectangle (drawRect). In order to create an equivalent square and rectangle pair, I used the new Player 10 ActionScript 3.0 drawPath() method. (If you’re using Flash CS3 or versions of Flex prior to 3.2, alternative Square and Rectangle classes can be found at the end of this post.) In looking at the concrete classes, there’s not much difference other than the second parameter in the Square class is a dummy variable that is happy with number you stick there. The first parameter (h) is passed to a side variable (s) and only that single value determines the width and heights, which, of course, must be equal. First, you have the Rect 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('p659code48'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65948"><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
</pre></td><td class="code" id="p659code48"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Rect <span style="color: #0033ff; font-weight: bold;">extends</span> Boxes
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> recCmds<span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> recCoord<span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> override <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">createBox</span><span style="color: #000000;">&#40;</span>w<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span>h<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			recCmds = <span style="color: #0033ff; font-weight: bold;">new</span> Vector<span style="color: #000066; font-weight: bold;">.</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			recCmds<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
			recCmds<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
			recCmds<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
			recCmds<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
			recCmds<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			recCoord = <span style="color: #0033ff; font-weight: bold;">new</span> Vector<span style="color: #000066; font-weight: bold;">.</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//x</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//y</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span> = w<span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//width</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#93;</span> = w<span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//width</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#93;</span> = h<span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//height</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">6</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">7</span><span style="color: #000000;">&#93;</span> = h<span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//height</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">8</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">9</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0x990000<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span>drawPath<span style="color: #000000;">&#40;</span>recCmds<span style="color: #000066; font-weight: bold;">,</span> recCoord<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Next, the Square class is similar, but notice the “dummy” label for the second parameter.</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('p659code49'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65949"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code" id="p659code49"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Square <span style="color: #0033ff; font-weight: bold;">extends</span> Boxes
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> recCmds<span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> recCoord<span style="color: #000066; font-weight: bold;">:</span>Vector<span style="color: #000066; font-weight: bold;">.;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> s<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> override <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">createBox</span><span style="color: #000000;">&#40;</span>w<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span>dummy<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			s = w<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			recCmds = <span style="color: #0033ff; font-weight: bold;">new</span> Vector<span style="color: #000066; font-weight: bold;">.</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			recCmds<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">;</span>
			recCmds<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
			recCmds<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
			recCmds<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
			recCmds<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			recCoord = <span style="color: #0033ff; font-weight: bold;">new</span> Vector<span style="color: #000066; font-weight: bold;">.</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">10</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//x</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//y</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000000;">&#93;</span> = s<span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//width</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">4</span><span style="color: #000000;">&#93;</span> = s<span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//width</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">5</span><span style="color: #000000;">&#93;</span> = s<span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//height</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">6</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">7</span><span style="color: #000000;">&#93;</span> = s<span style="color: #000066; font-weight: bold;">;</span><span style="color: #009900; font-style: italic;">//height</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">8</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
			recCoord<span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">9</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0x009900<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span>drawPath<span style="color: #000000;">&#40;</span>recCmds<span style="color: #000066; font-weight: bold;">,</span> recCoord<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, no rocket science is involved in either the drawing classes and their base class or the Client class. A single method is used to generate either, and the only real difference between the child classes is the enforcement of the equal width and height in the Square class.</p>
<p><strong>LSP Lunch Bucket Rule</strong></p>
<p>What can we take to work with the Liskov Substitution Principle? It’s nice to know and understand why the principle is in place, but who needs to have all of the implied tentacles of the principle rattling around in your head—especially considering the way Liskov stated it. We know the principle as <em>you should be able to substitute any derived class from a base class where an instance of the base class exists.</em> After further examination, the only way to do that is to use an abstract class or interface for all of your inheritance and implementations. Put more succinctly, the <strong>Lunch Bucket Rule</strong> is,</p>
<blockquote><p>Only subclass from abstract classes. Do not inherit from concrete classes.</p></blockquote>
<p>That’s easy enough to remember. Put that in your head and see if it helps move an OOP and Design Pattern agenda along at work.<br />
\- &#8211; - &#8211; - &#8211; - &#8211; - &#8211; - &#8212; &#8211; - &#8211; - &#8211; - &#8211; - &#8211; - &#8212; &#8211; - &#8211; - &#8211; - &#8211; - &#8211; - &#8212; &#8211; - &#8211; - \</p>
<p><strong>Flex client and Rect and Square Classes for Flash CS3 and Flex prior to 3.2</strong></p>
<p>First, if you&#8217;re using Flex, you might want to use the following mxml program for your client (Ignore the Client.as class if you do):</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('p659code50'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65950"><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="p659code50"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">&lt;?</span>xml <span style="color: #004993;">version</span>=<span style="color: #990000;">&quot;1.0&quot;</span> encoding=<span style="color: #990000;">&quot;utf-8&quot;</span><span style="color: #000066; font-weight: bold;">?&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>Application xmlns<span style="color: #000066; font-weight: bold;">:</span>mx=<span style="color: #990000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> layout=<span style="color: #990000;">&quot;absolute&quot;</span><span style="color: #000066; font-weight: bold;">&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>Script<span style="color: #000066; font-weight: bold;">&gt;</span>
		<span style="color: #000066; font-weight: bold;">&lt;!</span><span style="color: #000000;">&#91;</span>CDATA<span style="color: #000000;">&#91;</span>
		<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Sprite</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> subThis<span style="color: #000066; font-weight: bold;">:</span>Boxes<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> doSquare<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> subThis <span style="color: #000066; font-weight: bold;">&amp;&amp;</span> rawChildren<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">contains</span><span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span> rawChildren<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			subThis=<span style="color: #0033ff; font-weight: bold;">new</span> Square<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">createBox</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">70</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span>subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">;</span>
			rawChildren<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> doRec<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span> subThis <span style="color: #000066; font-weight: bold;">&amp;&amp;</span> rawChildren<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">contains</span><span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span> rawChildren<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeChild</span><span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
			subThis=<span style="color: #0033ff; font-weight: bold;">new</span> Rect<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">createBox</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">70</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">x</span> = <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">,</span>subThis<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">y</span> = <span style="color: #000000; font-weight:bold;">100</span><span style="color: #000066; font-weight: bold;">;</span>
			rawChildren<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>subThis<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;/</span>mx<span style="color: #000066; font-weight: bold;">:</span>Script<span style="color: #000066; font-weight: bold;">&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>Button <span style="color: #004993;">x</span>=<span style="color: #990000;">&quot;100&quot;</span> <span style="color: #004993;">y</span>=<span style="color: #990000;">&quot;76&quot;</span> label=<span style="color: #990000;">&quot;Square&quot;</span> <span style="color: #004993;">click</span>=<span style="color: #990000;">&quot;doSquare()&quot;</span> id=<span style="color: #990000;">&quot;squ&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
	<span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>Button <span style="color: #004993;">x</span>=<span style="color: #990000;">&quot;200&quot;</span> <span style="color: #004993;">y</span>=<span style="color: #990000;">&quot;76&quot;</span> label=<span style="color: #990000;">&quot;Rectangle&quot;</span> id=<span style="color: #990000;">&quot;rec&quot;</span> <span style="color: #004993;">click</span>=<span style="color: #990000;">&quot;doRec()&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;/</span>mx<span style="color: #000066; font-weight: bold;">:</span>Application<span style="color: #000066; font-weight: bold;">&gt;</span></pre></td></tr></table></div>

<p>Here’s the Rect 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('p659code51'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65951"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p659code51"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Rect <span style="color: #0033ff; font-weight: bold;">extends</span> Boxes
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">public</span> override <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">createBox</span><span style="color: #000000;">&#40;</span>w<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span>h<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">beginFill</span> <span style="color: #000000;">&#40;</span>0x990000<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">drawRect</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span>w<span style="color: #000066; font-weight: bold;">,</span>h<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>And now the Square 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('p659code52'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p65952"><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="p659code52"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Square <span style="color: #0033ff; font-weight: bold;">extends</span> Boxes
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> s<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
		<span style="color: #0033ff; font-weight: bold;">public</span> override <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">createBox</span><span style="color: #000000;">&#40;</span>w<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000066; font-weight: bold;">,</span>dummy<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Number</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #000000;">&#123;</span>
			s=w<span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">beginFill</span> <span style="color: #000000;">&#40;</span>0x009900<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #004993;">graphics</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">drawRect</span> <span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span>s<span style="color: #000066; font-weight: bold;">,</span>s<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<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%2F02%2Fdesign-pattern-principles-for-actionscript-30-the-liskov-substitution-principle%2F&amp;title=Design%20Pattern%20Principles%20for%20ActionScript%203.0%3A%20The%20Liskov%20Substitution%20Principle" id="wpa2a_16"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/02/design-pattern-principles-for-actionscript-30-the-liskov-substitution-principle/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Abstract Factory Design Pattern: Multiple Products and Factories</title>
		<link>http://www.as3dp.com/2009/01/actionscript-30-abstract-factory-design-pattern-multiple-products-and-factories/</link>
		<comments>http://www.as3dp.com/2009/01/actionscript-30-abstract-factory-design-pattern-multiple-products-and-factories/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 22:00:24 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Abstract Classes]]></category>
		<category><![CDATA[Abstract Factory]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=575</guid>
		<description><![CDATA[This is one of the few design patterns that I worked up directly from the class diagram and from concepts in GoF. Normally, I like to look at some examples, done in Java or C#, but not this time. As you will see in Figure 1, the pattern appears to be fairly daunting, but I [...]]]></description>
			<content:encoded><![CDATA[<p><!--StartFragment--></p>
<p class="MsoNormal">This is one of the few design patterns that I worked up directly from the class diagram and from concepts in GoF. Normally, I like to look at some examples, done in Java or C#, but not this time. As you will see in Figure 1, the pattern appears to be fairly daunting, but I found it to be eminently practical, and it seemed to be a direct response to questions that I had about the Factory Method design pattern (See Chapter 2 for an in-depth explanation of the Factory Method.) You can download the entire example <a href="http://nemo.mwd.hartford.edu/~wsanders/blog/AbstractFac.zip">here </a>before continuing if you wish.</p>
<p class="MsoNormal"> Let me start with the gist of the example from GoF and provide something more concrete that’s likely to be a typical kind of issue Flash and Flex developers deal with. Imagine a project where your designers have created general templates for a business site and another for a game site. Their templates include a SWF background and a set of buttons for a UI. The buttons are wholly programmed and require nothing in the Library, and so using them for either Flash or Flex is fairly simple.</p>
<p class="MsoNormal"> You want to keep your design loose, and so you decide that a factory will be helpful. However, clearly you will need a factory to create instances of both buttons and the background template. Further, you want your products to derive from an abstract class to give you as much flexibility as possible. In the example here, you will need an abstract product for buttons and another for backgrounds. You also want your factory abstract enough to make requests for sets of objects from the different products. For example, you want your factory to deliver both a set of buttons and a background that are matching pairs. You don’t want a set of buttons for a game site with a background for a business site, but rather you want the buttons to match your background—business buttons with a business background and game buttons with a game background. This is a job for the Abstract Factory.</p>
<p class="MsoNormal"> Figure 1 shows the class diagram. In looking at the “create” lines (dashed lines), think of them as working with matched sets. The Client requests a <strong>business set</strong>; and it gets both a business product for buttons and another product for background. So while the diagram may look busy, it really is doing something that makes sense on a basic level. That is, the design is geared to <strong>sets</strong>; of products with factories that <em>create the requested sets rather than individual objects</em>.</p>
<p class="MsoNormal"><img class="alignleft size-full wp-image-584" title="abfactory66" src="http://www.as3dp.com/wp-content/uploads/2009/01/abfactory66.png" alt="abfactory66" width="513" height="256" /></p>
<p class="MsoNormal"> </p>
<p><em><strong>Figure 1: </strong> Abstract Factory Class Diagram</em><br />
Note that Figure 1 shows that both concrete factories create instances from each of the child classes of the two abstract product classes. You can very quickly see the practicality of this when you substitute some concrete elements for the more general conceptual names.<br />
<span id="more-575"></span><br />
<strong>Buttons and Backgrounds</strong></p>
<p>A simple project to use as an example is a program that loads a buttons-background pair. The buttons are made using the <strong>SimpleButton</strong> class, and the backgrounds are SWF files created using Flash. Figure 2 shows the class diagram for the sample project:<br />
<img class="alignleft size-full wp-image-579" title="abfactoryexample" src="http://www.as3dp.com/wp-content/uploads/2009/01/abfactoryexample.png" alt="abfactoryexample" width="622" height="310" /><br />
<em><strong>Figure 2: </strong> Example of Abstract Factory </em><br />
The two button classes (RoundBtn and RecBtn) dip into a buttons folder where they pick up the buttons. These other classes not in the diagram can be thought of as <em>helper</em> or <em>utility</em> classes. Likewise, the two SWF files might be considered <em>utility objects</em> in the mix. Figure 3 shows the actual files and folders arranged in the class diagram.<br />
<img class="alignleft size-full wp-image-580" title="abfacfile" src="http://www.as3dp.com/wp-content/uploads/2009/01/abfacfile.png" alt="abfacfile" width="618" height="451" /><br />
<em><strong>Figure 3: </strong> File Diagram of Abstract Factory </em></p>
<p>No lines go to either the buttons folder or the two .swf files. They’re really not part of the pattern but rather resources (<em>helpers if you wish</em>) for the different participants. <em>If we did add lines, they’d be dashed ones</em> to indicate that the product subclasses  create instances of the helpers.</p>
<p><strong>The Client</strong></p>
<p>The <strong>Client</strong> is a busy object in this design. It holds references not only to the abstract factory (<strong>AbFactory</strong>) it also holds references to <em>all</em> abstract product classes. The important point here is that by holding references to an interface (the abstract classes), the <strong>Client</strong> doesn’t get bogged down with specific dependencies. If any of the concrete classes change, the client’s references can handle it. Let’s take a look at the code for this key participant in the 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('p575code57'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p57557"><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
</pre></td><td class="code" id="p575code57"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> busFactory:AbFactory;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> gameFactory:AbFactory;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btn1:ProductA;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btn2:ProductA;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btn3:ProductA;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> gameWall:ProductB;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> busWall:ProductB;
&nbsp;
                <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnX:<span style="color: #0066CC;">int</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>
&nbsp;
			busFactory=<span style="color: #000000; font-weight: bold;">new</span> BusinessFactory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			btnX=<span style="color: #cc66cc;">20.9</span>;
			busWall=busFactory.<span style="color: #006600;">createProductB</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>busWall<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			btn1=busFactory.<span style="color: #006600;">createProductA</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Accounts&quot;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btn1<span style="color: #66cc66;">&#41;</span>;
			btn1.<span style="color: #006600;">x</span>=btnX, btn1.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">224</span>;
&nbsp;
			btn2=busFactory.<span style="color: #006600;">createProductA</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Factory&quot;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btn2<span style="color: #66cc66;">&#41;</span>;
			btn2.<span style="color: #006600;">x</span>=btnX, btn2.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">272</span>;
&nbsp;
			btn3=busFactory.<span style="color: #006600;">createProductA</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Production&quot;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btn3<span style="color: #66cc66;">&#41;</span>;
			btn3.<span style="color: #006600;">x</span>=btnX, btn3.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">320</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//Move block comment symbols to change</span>
                       <span style="color: #808080; font-style: italic;">// from business to game</span>
<span style="color: #808080; font-style: italic;">/*
			gameFactory=new GameFactory();
			btnX=18;
			gameWall=gameFactory.createProductB();
			addChild(gameWall);
&nbsp;
			btn1=gameFactory.createProductA(&quot;Fire&quot;);
			addChild(btn1);
			btn1.x=btnX, btn1.y=224;
&nbsp;
			btn2=gameFactory.createProductA(&quot;Move&quot;);
			addChild(btn2);
			btn2.x=btnX, btn2.y=272;
&nbsp;
			btn3=gameFactory.createProductA(&quot;Hide&quot;);
			addChild(btn3);
			btn3.x=btnX, btn3.y=320;
&nbsp;
			// Don’t forget to move close comment block symbol
*/</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 first item to note is how the Client class holds references to both the abstract factory and abstract product:<br />
<code><br />
private var busFactory:<strong>AbFactory</strong>;<br />
private var gameFactory: <strong>AbFactory</strong>;<br />
…<br />
private var btn1: <strong>ProductA</strong>;<br />
private var btn2: <strong>ProductA</strong>;<br />
private var btn3: <strong>ProductA</strong>;<br />
…<br />
private var gameWall: <strong>ProductB</strong>;<br />
private var busWall: <strong>ProductB</strong>;<br />
</code></p>
<p>In this particular example, the choices for a business or game site are separated by commenting out one of the sites. In this case, the business site appears. Of course you can add a UI that would call up one or the other, but this approach seemed a bit clearer. To see one or the other, just change the position of the comment characters (/* and */). So using the default comment characters, when you test the program, Figure 4 shows what you’ll see initially:</p>
<p> <img class="alignleft size-full wp-image-581" title="business" src="http://www.as3dp.com/wp-content/uploads/2009/01/business.png" alt="business" width="550" height="400" /></p>
<p><em><strong>Figure 4: </strong> Business Site and Buttons </em></p>
<p>By moving the comment symbols to block out the business options and opening the game options, you’ll see an entirely different background and button set. Figure 5 shows the dramatic difference.<br />
<img class="alignleft size-full wp-image-582" title="game" src="http://www.as3dp.com/wp-content/uploads/2009/01/game.png" alt="game" width="550" height="400" /></p>
<p><em><strong>Figure 5: </strong> Game Site and Buttons </em></p>
<p>By comparing Figures 4 and 5, you can see an entirely different set of buttons and background. The square in the middle rotates to further distinguish the game products from the business ones. (I know, gratuitous use of animation. Sorry.)</p>
<p><strong>The Abstract and Concrete Factories</strong></p>
<p>The abstract factory allows far more flexibility than a concrete factory. Because the backgrounds and buttons are clearly different kinds of products, it makes perfect sense to have different kinds of factories to generate the products. The utility of the abstract factory can be seen in the different kinds of concrete factories. As you will see, the concrete factories are only slightly different. Each must create an instance of all products classes (<strong>ProductA</strong> and <strong>ProductB</strong> in the example), but because the concrete classes are different they must create different concrete class instances. In this example, The <strong>GameFactory</strong> and <strong>BusinessFactory</strong> classes create different buttons and backgrounds and yet each is a child of the abstract factory class. The following listing shows the factory classes:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p575code58'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p57558"><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
</pre></td><td class="code" id="p575code58"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Abstract Factory</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AbFactory <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createProductA<span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:ProductA
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&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> createProductB<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:ProductB
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</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;">//Game Factory</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GameFactory <span style="color: #0066CC;">extends</span> AbFactory
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> gameBtn:ProductA;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> gameWall:ProductB;
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createProductA<span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:ProductA
		<span style="color: #66cc66;">&#123;</span>
			gameBtn = <span style="color: #000000; font-weight: bold;">new</span> RoundBtn<span style="color: #66cc66;">&#40;</span>txt<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> gameBtn;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createProductB<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:ProductB
		<span style="color: #66cc66;">&#123;</span>
			gameWall=<span style="color: #000000; font-weight: bold;">new</span> GameStyle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> gameWall;
		<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;">//Business Factory</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BusinessFactory <span style="color: #0066CC;">extends</span> AbFactory
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> businessBtn:ProductA;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> businessWall:ProductB;
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createProductA<span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:ProductA
		<span style="color: #66cc66;">&#123;</span>
			businessBtn = <span style="color: #000000; font-weight: bold;">new</span> RecBtn<span style="color: #66cc66;">&#40;</span>txt<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> businessBtn;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createProductB<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:ProductB
		<span style="color: #66cc66;">&#123;</span>
			businessWall=<span style="color: #000000; font-weight: bold;">new</span> BusinessStyle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> businessWall;
		<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, each child factory returns its own unique set of buttons and background as shown in Figures 4 and 5 above.</p>
<p><strong>The Abstract Products and their Children</strong></p>
<p>The design pattern shows two abstract products, but you can think of them as <em>at least</em> two products. You can have as many products as you want, but for each abstract product, you need a concrete factory. To help keep everything clear, the abstract product classes were named <strong>ProductA</strong> and <strong>ProductB</strong>. The “A” products are buttons and the “B” products are backgrounds. As you can see in the following listings, each is different. Both include abstract methods, but <strong>ProductB</strong> has a heavily parameterized method, <strong>setWallName</strong> to add flexibility to the placement of a page header. To help keep everything clear, first you will see the abstract product and then the two concrete children classes for each.</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('p575code59'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p57559"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
</pre></td><td class="code" id="p575code59"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Product A: The buttons</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ProductA <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> setUpA<span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</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;">//Round Button concrete product</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> buttons.<span style="color: #006600;">BtnStateEllipse</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RoundBtn <span style="color: #0066CC;">extends</span> ProductA
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnStateEllipse:BtnStateEllipse;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> RoundBtn<span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setUpA<span style="color: #66cc66;">&#40;</span>txt<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> setUpA<span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			btnStateEllipse = <span style="color: #000000; font-weight: bold;">new</span> BtnStateEllipse<span style="color: #66cc66;">&#40;</span>txt<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnStateEllipse<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;">//Rectangle button concrete product</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> buttons.<span style="color: #006600;">BtnState</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RecBtn <span style="color: #0066CC;">extends</span> ProductA
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnState:BtnState;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> RecBtn<span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setUpA<span style="color: #66cc66;">&#40;</span>txt<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> setUpA<span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			btnState = <span style="color: #000000; font-weight: bold;">new</span> BtnState<span style="color: #66cc66;">&#40;</span>txt<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btnState<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;">//Product B: The Backgrounds</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
	<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: #0066CC;">text</span>.<span style="color: #006600;">TextFieldAutoSize</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ProductB <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> setWallName<span style="color: #66cc66;">&#40;</span>wall:<span style="color: #0066CC;">String</span>,sz:<span style="color: #0066CC;">int</span>,hor:<span style="color: #0066CC;">int</span>,vert:<span style="color: #0066CC;">int</span>,txtField:<span style="color: #0066CC;">TextField</span>,format:<span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			txtField.<span style="color: #0066CC;">text</span>=wall;
			txtField.<span style="color: #006600;">x</span>=hor, txtField.<span style="color: #006600;">y</span>=vert;
			txtField.<span style="color: #0066CC;">autoSize</span>=TextFieldAutoSize.<span style="color: #006600;">CENTER</span>;
			format.<span style="color: #0066CC;">size</span>=sz;
			format.<span style="color: #0066CC;">font</span>=<span style="color: #ff0000;">&quot;Arial Black&quot;</span>;
			txtField.<span style="color: #0066CC;">setTextFormat</span><span style="color: #66cc66;">&#40;</span>format<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>txtField<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> setUpB<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</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;">//Game Background</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<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>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GameStyle <span style="color: #0066CC;">extends</span> ProductB
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loader:Loader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">url</span>:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtField:<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> format:<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> GameStyle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setUpB<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			setWallName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Game Time!&quot;</span>,<span style="color: #cc66cc;">24</span>,<span style="color: #cc66cc;">200</span>,<span style="color: #cc66cc;">30</span>,txtField,format<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> setUpB<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			loader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">url</span>=<span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;GameStyle.swf&quot;</span><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>
&nbsp;
<span style="color: #808080; font-style: italic;">//Business background</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<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>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BusinessStyle <span style="color: #0066CC;">extends</span> ProductB
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loader:Loader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">url</span>:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtField:<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> format:<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> BusinessStyle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setUpB<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			setWallName<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Serious Business&quot;</span>,<span style="color: #cc66cc;">24</span>,<span style="color: #cc66cc;">200</span>,<span style="color: #cc66cc;">30</span>,txtField,format<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> setUpB<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			loader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">url</span>=<span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;BusinessStyle.swf&quot;</span><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>By requesting the game products, the program returns both the rectangle shape buttons and the game background, and likewise with a request to the business products returns the business set. The separate concrete factories keep everything loose and the pattern has very few dependencies.</p>
<p><strong>The Helpers</strong></p>
<p>The RoundBtn class imports an ellipse button from the buttons package:</p>
<p><strong>import buttons.BtnStateEllipse; </strong></p>
<p>and the RecBtn class imports a rectangle button from the buttons package:</p>
<p><strong>import buttons.BtnState; </strong></p>
<p>So in creating the button scripts, the package indicates the buttons folder. The following scripts are found in the buttons folder. These helper classes are reusable just like the participants in a design pattern. However, because literals are used in these for the colors used with the button states, their flexibility is limited to the particular style and color scheme in the buttons.</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('p575code60'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p57560"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
</pre></td><td class="code" id="p575code60"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Button style for games</span>
package buttons
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">//Ellipse shape</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
	<span style="color: #0066CC;">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: #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: #006600;">TextFieldAutoSize</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> BtnBaseOval <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> btnLabel:<span style="color: #0066CC;">TextField</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> format:<span style="color: #0066CC;">TextFormat</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnWidth:<span style="color: #0066CC;">Number</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bkground:Shape;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BtnBaseOval <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">color</span>:uint,color2:uint,btnLabelText:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			btnLabel=<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>;
			btnLabel.<span style="color: #0066CC;">text</span>=btnLabelText;
			btnLabel.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">5</span>;
			btnLabel.<span style="color: #0066CC;">textColor</span>=0x24241c
			btnLabel.<span style="color: #0066CC;">autoSize</span>=TextFieldAutoSize.<span style="color: #006600;">CENTER</span>;
&nbsp;
			format=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Arial Black&quot;</span><span style="color: #66cc66;">&#41;</span>;
			format.<span style="color: #0066CC;">size</span>=<span style="color: #cc66cc;">12</span>;
			btnLabel.<span style="color: #0066CC;">setTextFormat</span> <span style="color: #66cc66;">&#40;</span>format<span style="color: #66cc66;">&#41;</span>;
			btnWidth=<span style="color: #cc66cc;">90</span>;
&nbsp;
			bkground=<span style="color: #000000; font-weight: bold;">new</span> Shape;
			bkground.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">color</span><span style="color: #66cc66;">&#41;</span>;
			bkground.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span> <span style="color: #66cc66;">&#40;</span>.25,color2<span style="color: #66cc66;">&#41;</span>;
			bkground.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawEllipse</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,btnWidth,<span style="color: #cc66cc;">18</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			addChild <span style="color: #66cc66;">&#40;</span>bkground<span style="color: #66cc66;">&#41;</span>;
			addChild <span style="color: #66cc66;">&#40;</span>btnLabel<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;">//Button states and colors game buttons</span>
package buttons
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">SimpleButton</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BtnStateEllipse <span style="color: #0066CC;">extends</span> SimpleButton
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BtnStateEllipse <span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			upState = <span style="color: #000000; font-weight: bold;">new</span> BtnBaseOval<span style="color: #66cc66;">&#40;</span>0x698c3a, 0x218894,txt<span style="color: #66cc66;">&#41;</span>;
			downState = <span style="color: #000000; font-weight: bold;">new</span> BtnBaseOval<span style="color: #66cc66;">&#40;</span>0xc7c68d,0x4d0005, txt<span style="color: #66cc66;">&#41;</span>;
			overState= <span style="color: #000000; font-weight: bold;">new</span> BtnBaseOval<span style="color: #66cc66;">&#40;</span>0x48f23,0x218894,txt<span style="color: #66cc66;">&#41;</span>;
			hitTestState=upState;
		<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;">//Button style for Business</span>
package buttons
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Set up rectangular shape</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
	<span style="color: #0066CC;">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: #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: #006600;">TextFieldAutoSize</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">class</span> BtnBaseRec <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> btnLabel:<span style="color: #0066CC;">TextField</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> format:<span style="color: #0066CC;">TextFormat</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btnWidth:<span style="color: #0066CC;">Number</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bkground:Shape;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BtnBaseRec <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">color</span>:uint,color2:uint,btnLabelText:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			btnLabel=<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> ;
			btnLabel.<span style="color: #0066CC;">text</span>=btnLabelText;
			btnLabel.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">5</span>;
			btnLabel.<span style="color: #0066CC;">textColor</span>=0x24241c;
			btnLabel.<span style="color: #0066CC;">autoSize</span>=TextFieldAutoSize.<span style="color: #0066CC;">LEFT</span>;
&nbsp;
			format=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Verdana&quot;</span><span style="color: #66cc66;">&#41;</span>;
			format.<span style="color: #0066CC;">size</span>=<span style="color: #cc66cc;">12</span>;
			btnLabel.<span style="color: #0066CC;">setTextFormat</span> <span style="color: #66cc66;">&#40;</span>format<span style="color: #66cc66;">&#41;</span>;
			btnWidth=<span style="color: #cc66cc;">90</span>;
&nbsp;
			bkground=<span style="color: #000000; font-weight: bold;">new</span> Shape;
			bkground.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">color</span><span style="color: #66cc66;">&#41;</span>;
			bkground.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span> <span style="color: #66cc66;">&#40;</span>.25,color2<span style="color: #66cc66;">&#41;</span>;
			bkground.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">0</span>,btnWidth,<span style="color: #cc66cc;">18</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			addChild <span style="color: #66cc66;">&#40;</span>bkground<span style="color: #66cc66;">&#41;</span>;
			addChild <span style="color: #66cc66;">&#40;</span>btnLabel<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;">//Button states and colors business buttons</span>
package buttons
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">SimpleButton</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BtnState <span style="color: #0066CC;">extends</span> SimpleButton
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BtnState <span style="color: #66cc66;">&#40;</span>txt:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			upState = <span style="color: #000000; font-weight: bold;">new</span> BtnBaseRec<span style="color: #66cc66;">&#40;</span>0xa9b890, 0x24241c,txt<span style="color: #66cc66;">&#41;</span>;
			downState = <span style="color: #000000; font-weight: bold;">new</span> BtnBaseRec<span style="color: #66cc66;">&#40;</span>0xffff7f8,0xcd1722, txt<span style="color: #66cc66;">&#41;</span>;
			overState= <span style="color: #000000; font-weight: bold;">new</span> BtnBaseRec <span style="color: #66cc66;">&#40;</span>0xcd1722,0xa9b890,txt<span style="color: #66cc66;">&#41;</span>;
			hitTestState=upState;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In looking back over this example, keep in mind that these last set of classes, the helper classes, are not part of the pattern itself. Rather, they are used to make it easy to create buttons without resorting to either the button symbols made in Flash or Flex. However, if you’d prefer the factories to use the built-in button components rather than those in the helper classes, you will find it easy to do so. Just change the targets of creation in the concrete product classes where the buttons are assigned.</p>
<p><strong>When to Use the Abstract Factory</strong></p>
<p>The key element in the <strong>Abstract Factory Design Pattern</strong> is the idea of <em>families of related dependent objects</em>. After working with the pattern for a very short time, it offered up many solutions in my mind. None of us work with single programs—single projects at one time, certainly—but these projects are hardly single programs. They do indeed have certain parts that go together in one context but not another. So instead of having to work up an entire new program to work with families of these objects, this pattern allows you the convenience of working with one group or another depending on the project’s needs.</p>
<p><!--EndFragment--></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%2F01%2Factionscript-30-abstract-factory-design-pattern-multiple-products-and-factories%2F&amp;title=ActionScript%203.0%20Abstract%20Factory%20Design%20Pattern%3A%20Multiple%20Products%20and%20Factories" id="wpa2a_18"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/01/actionscript-30-abstract-factory-design-pattern-multiple-products-and-factories/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Take a Design Pattern to Work Part IV: Establishing a Design Pattern Foundation</title>
		<link>http://www.as3dp.com/2009/01/take-a-design-pattern-to-work-part-iv-establishing-a-design-pattern-foundation/</link>
		<comments>http://www.as3dp.com/2009/01/take-a-design-pattern-to-work-part-iv-establishing-a-design-pattern-foundation/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 00:19:02 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Abstract Classes]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Factory Method]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=544</guid>
		<description><![CDATA[Gentle Reader: This is Part 4 of a four-part series of posts on introducing design patterns and OOP into the work place. Parts 1 through 3 will provide the context for this part. Also, taking a look at No Time for OOP and Design Patterns will give you the background on this series. As always, [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Gentle Reader:</strong> This is Part 4 of a four-part series of posts on introducing design patterns and OOP into the work place. Parts 1 through 3 will provide the context for this part. Also, taking a look at <a href="http://www.as3dp.com/2008/12/07/no-time-for-oop-and-design-patterns/">No Time for OOP and Design Patterns</a> will give you the background on this series. As always, we invite your comments.</em></p>
<p><strong>Note:</strong> Chandima wrote the chapter in our book on the <strong>F</strong><strong>actory Method</strong>, and he gave me invaluable help on the main program in this post as well.</p>
<p><strong>Recap</strong></p>
<p>Up to this point we’ve examined a simple program that loads external text and graphics, a common ActionScript chore. In the most general terms, this is where we’ve been:</p>
<ul>
<li>Part I: Identifying the problem in a current solution. Why ActionScript on the Timeline can cause problems.</li>
<li>Part II: Providing a simple OOP solution: Use of Inheritance</li>
<li>Part III: Loosening Up a programs structure: Adding a design pattern element —a simple factory</li>
</ul>
<p>To conclude the process, we now come to the last part—introducing an actual design pattern to the work place.</p>
<ul>
<li>Part IV: Establishing a Design Pattern Foundation.</li>
</ul>
<p>Given the preceding steps, the context is now in place to add a full design pattern.</p>
<p><strong> From Part to Whole</strong></p>
<p>Part III introduced the Simple Factory method inserted into an existing OOP program. Now it’s time to step back and look at a design pattern <em>en toto</em> and instead of incrementally adding to the existing program, we will refactor the whole kit-n-kaboodle from the perspective of a design pattern.</p>
<p>To get started, if you’re not familiar with the Factory Method pattern, take a look at Chapter 2. In fact Chandima’s <em>Sprite Factory</em> example beginning on page 84 is one of the clearest and most appropriate examples that you can find of the Factory Method pattern in ActionScript 3.0. So before continuing, you might want to do a quick review of the Factory Method and take a look at Figure 1, the class diagram for the pattern. (We’ll wait for you…).</p>
<p><img class="alignleft size-full wp-image-568" title="factorymethoddp852" src="http://www.as3dp.com/wp-content/uploads/2009/01/factorymethoddp852.png" alt="factorymethoddp852" width="520" height="244" /></p>
<p><em><strong>Figure 1</strong>: Factory Method Design Pattern</em></p>
<p>As you can see, the Factory Method (simple factory) is part of the <em>Creator</em> interface and the ConcreteCreator. The interface is an abstract class; so at least one of the methods needs to be abstract—impossible to directly instantiate but easily overridden in a child class.<br />
<span id="more-544"></span><br />
The <strong><em>Product</em></strong> class is an abstract version of what we called the <strong>Staff</strong> class in the previous version of the application. Again, using an abstract class requires abstract methods to be overridden in the child classes. The <strong>ConcreteProduct</strong> classes were previously the <strong>Designer, Developer </strong>and <strong>HCI </strong>classes. We can keep the old designations for clarity, but their parent class is now the <strong><em>Product</em></strong> interface (abstract class). That means that the child classes can now make adjustments to the methods using the override statement. However, those methods not overridden do not need to be revised at all. Figure 2 shows the design pattern (without the pseudo-code blocks.) and with the actual child classes for the <strong><em>Product</em></strong> abstract class.<br />
<img class="alignnone size-full wp-image-548" title="factorymethodstaff" src="http://www.as3dp.com/wp-content/uploads/2009/01/factorymethodstaff.png" alt="factorymethodstaff" width="634" height="298" /></p>
<p><em><strong>Figure 2:</strong> Factory Method Pattern applied to current project</em></p>
<p>As a reminder, the dashed lines with the arrows means that origin of the dashed line <em><strong>creates an instance</strong></em> of the class to which the arrowhead points.</p>
<p><strong>One Day They Came to Work and Found All These Design Patterns</strong></p>
<p>In the process of creating this series of posts and working on a presentation for the Atlanta group with similar aims, I’ve been able to review the process of introducing design patterns at work.  My conclusion is that we need to step both gently and firmly, and by all means do so in a practical way. (When the choice is between being on time and being right, take on time.) However, OOP and design patterns need to be at the back of your mind so that you can begin viewing projects in terms of OOP and design patterns. In turn, this will make your company’s products better, more flexible and less likely to choke up at the wrong time. (They never choke up at the right time—whenever that is.)</p>
<p>So, in looking at this refactored program, keep in mind that we started with a simple program embedded in the Timeline using keyframes to choose graphic and text images. Also, keep in mind the problem, and how the design pattern became a solution for that problem.</p>
<p><strong>The Client Looks the Same</strong></p>
<p>We’ll start with the <strong>Client</strong>, and not surprisingly, it is little changed from last time. However, the request is typed to an abstract class this time (<strong>factory:Creator</strong>).</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p544code66'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54466"><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
</pre></td><td class="code" id="p544code66"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">Button</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Client Class</span>
&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> logo:Sprite = <span style="color: #000000; font-weight: bold;">new</span> Logo<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> splash:Sprite = <span style="color: #000000; font-weight: bold;">new</span> Splash<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> desBtn:<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> devBtn:<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> hciBtn:<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> staffNow:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> staffDisplay:Sprite;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> staff:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> factory:Creator=<span style="color: #000000; font-weight: bold;">new</span> ConcreteCreator<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>
			desBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Designer&quot;</span>;
			desBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,selectStaff<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>desBtn<span style="color: #66cc66;">&#41;</span>;
			desBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">424</span>,desBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">66</span>;
&nbsp;
			devBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Developer&quot;</span>;
			devBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,selectStaff<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>devBtn<span style="color: #66cc66;">&#41;</span>;
			devBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">424</span>,devBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">116</span>;
&nbsp;
			hciBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;HCI&quot;</span>;
			hciBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,selectStaff<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>hciBtn<span style="color: #66cc66;">&#41;</span>;
			hciBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">424</span>,hciBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">166</span>;
&nbsp;
			logo.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">16</span>,logo.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">16</span>;
			logo.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">150</span>,logo.<span style="color: #0066CC;">height</span> = <span style="color: #cc66cc;">121.5</span>;
			addChild<span style="color: #66cc66;">&#40;</span>logo<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			splash.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">32</span>,splash.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">168</span>;
			addChild<span style="color: #66cc66;">&#40;</span>splash<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> selectStaff<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			staff = <span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">label</span>.<span style="color: #0066CC;">toLowerCase</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			splash.<span style="color: #0066CC;">visible</span>=<span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>staffDisplay<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				removeChild<span style="color: #66cc66;">&#40;</span> staffDisplay <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			staffDisplay = factory.<span style="color: #006600;">makeStaff</span><span style="color: #66cc66;">&#40;</span>staff<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span> staffDisplay <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>This next class, the <strong><em>Creator</em></strong>, is an abstract one. The functionality of this class is not appreciated in this particular application because we derive only a single child from it. However, if we wanted to change the program to create other elements that we may wish, such as a <em>button factory</em> we could do so relatively easily, and because of the loose linkage, adding another child will not disrupt the overall program.</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('p544code67'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54467"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code" id="p544code67"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Asbstract class</span>
	<span style="color: #808080; font-style: italic;">//Creator</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> staffNow:Sprite;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> makeStaff<span style="color: #66cc66;">&#40;</span>staff:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			staffNow = createStaff<span style="color: #66cc66;">&#40;</span>staff<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> staffNow;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// ABSTRACT Method (must be overridden in a subclass)</span>
		protected <span style="color: #000000; font-weight: bold;">function</span> createStaff<span style="color: #66cc66;">&#40;</span>staff:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</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 Creator provides the abstract method for the factory in the Factory Method design pattern. The <strong>ConcreteCreator</strong> is where the actual factory work is accomplished.</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('p544code68'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54468"><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
</pre></td><td class="code" id="p544code68"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Factory</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteCreator <span style="color: #0066CC;">extends</span> Creator
	<span style="color: #66cc66;">&#123;</span>
		override protected <span style="color: #000000; font-weight: bold;">function</span> createStaff<span style="color: #66cc66;">&#40;</span>staff:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>staff<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;designer&quot;</span> :
					<span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> Designer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
					<span style="color: #b1b100;">break</span>;
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;developer&quot;</span> :
					<span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> Developer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
					<span style="color: #b1b100;">break</span>;
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;hci&quot;</span> :
					<span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> HCI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
					<span style="color: #b1b100;">break</span>;
				<span style="color: #000000; font-weight: bold;">default</span> :
					<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Error</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Invalid type of staff specified&quot;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</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, that’s almost identical to the Factory class in Part III of this series of posts. The <strong>createStaff()</strong> method is overridden. Because it is a child of the <em><strong>Creator</strong></em> class, it has the <strong>makeStaff()</strong> method, and it uses the <strong>creatStaff()</strong> method in any way that it has been overridden in the concrete creator itself.</p>
<p><strong>The Product and Its Children</strong></p>
<p>A second abstract class, <strong><em>Product</em></strong>, as noted above, is very similar to the Staff class introduced in Part II of this series. The difference is that the Staff class was concrete and the <strong><em>Product</em></strong> class is abstract. Two of its methods are abstract, and both need to be overridden and detailed in the child classes. So instead of inheritance to a concrete class with no flexibility, the child classes of the Product can customize the overridden functions. The following shows how the <em><strong>Product</strong></em> class is set up:</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('p544code69'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54469"><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
</pre></td><td class="code" id="p544code69"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
	<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;">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: #006600;">TextFieldType</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Abstract class</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> loadCheck:URLLoader;
		<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> <span style="color: #0066CC;">textFormat</span>:<span style="color: #0066CC;">TextFormat</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> const WIDE:<span style="color: #0066CC;">int</span>=<span style="color: #cc66cc;">200</span>;
		<span style="color: #0066CC;">private</span> const <span style="color: #0066CC;">TEXTCOLOR</span>:uint=0x007700;
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> loadInfo<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> nowLoaded<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>
			loadCheck = 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>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">font</span> = <span style="color: #ff0000;">&quot;Arial Black&quot;</span>;
			<span style="color: #0066CC;">textFormat</span>.<span style="color: #0066CC;">size</span> = <span style="color: #cc66cc;">16</span>;
			txtFld.<span style="color: #0066CC;">type</span> = TextFieldType.<span style="color: #0066CC;">DYNAMIC</span>;
			txtFld.<span style="color: #0066CC;">width</span> = WIDE;
			txtFld.<span style="color: #0066CC;">wordWrap</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			txtFld.<span style="color: #0066CC;">textColor</span> =<span style="color: #0066CC;">TEXTCOLOR</span>;
			txtFld.<span style="color: #006600;">defaultTextFormat</span> = <span style="color: #0066CC;">textFormat</span>;
			txtFld.<span style="color: #0066CC;">text</span> = loadCheck.<span style="color: #0066CC;">data</span>;
			addChild<span style="color: #66cc66;">&#40;</span>txtFld<span style="color: #66cc66;">&#41;</span>;
			txtFld.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">180</span>,txtFld.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">331</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> loadImg<span style="color: #66cc66;">&#40;</span>img:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The <strong><em>Product</em></strong> has two abstract and one concrete methods. The thinking is that the onLoaded() operation is not going to change, but is needed to have everything laid out for the incoming Sprite object containing the image and text. However, we might come up with positioning ideas for the image that we can change. As for the abstract method for adding the text, it’s probably not going to change, but if it does, we can make the changes in the child classes. If we want to re-position the text, we can still use the concrete method in the Product class itself to change all text.</p>
<p>The result of having abstract methods is that they need to be filled out (populated by code) in the child classes. As a consequence, the concrete <strong><em>Product</em></strong> classes (<strong>Designer, Developer</strong>, and <strong>HCI</strong>) are all larger than the tiny ones in previous posts in this series. They have to override the abstract methods and add the code they want. In this example, they’re virtually identical to one another, but if we wanted to change one, we could do so without interfering with the other concrete child Product classes. The following three classes show how the concrete products are created:</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('p544code70'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54470"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
</pre></td><td class="code" id="p544code70"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Designer</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</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;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Designer <span style="color: #0066CC;">extends</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> staffText:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> staffImg:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">url</span>:URLRequest
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loader:Loader
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> fileNow:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtLoader:URLLoader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loadCheck:URLLoader;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Designer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			staffText = <span style="color: #ff0000;">&quot;text/designer.txt&quot;</span>;
			staffImg = <span style="color: #ff0000;">&quot;images/designer.png&quot;</span>;
&nbsp;
			loadInfo<span style="color: #66cc66;">&#40;</span>staffText<span style="color: #66cc66;">&#41;</span>;
			loadImg<span style="color: #66cc66;">&#40;</span>staffImg<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> loadInfo<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			txtLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			txtLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>,nowLoaded<span style="color: #66cc66;">&#41;</span>;
			fileNow = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>txtInfo<span style="color: #66cc66;">&#41;</span>;
			txtLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>fileNow<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> loadImg<span style="color: #66cc66;">&#40;</span>staffImg:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">url</span> = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>staffImg<span style="color: #66cc66;">&#41;</span>;
			loader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			loader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><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>;
			loader.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">180</span>,loader.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">131</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;">//Developer</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</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;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Developer <span style="color: #0066CC;">extends</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> staffText:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> staffImg:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">url</span>:URLRequest
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loader:Loader
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> fileNow:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtLoader:URLLoader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loadCheck:URLLoader;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Developer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			staffText = <span style="color: #ff0000;">&quot;text/developer.txt&quot;</span>;
			staffImg = <span style="color: #ff0000;">&quot;images/developer.png&quot;</span>;
&nbsp;
			loadInfo<span style="color: #66cc66;">&#40;</span>staffText<span style="color: #66cc66;">&#41;</span>;
			loadImg<span style="color: #66cc66;">&#40;</span>staffImg<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> loadInfo<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			txtLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			txtLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>,nowLoaded<span style="color: #66cc66;">&#41;</span>;
			fileNow = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>txtInfo<span style="color: #66cc66;">&#41;</span>;
			txtLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>fileNow<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> loadImg<span style="color: #66cc66;">&#40;</span>staffImg:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">url</span> = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>staffImg<span style="color: #66cc66;">&#41;</span>;
			loader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			loader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><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>;
			loader.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">180</span>,loader.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">131</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;">//HCI</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</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;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HCI <span style="color: #0066CC;">extends</span> Product
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> staffText:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> staffImg:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">url</span>:URLRequest
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loader:Loader
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> fileNow:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtLoader:URLLoader;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loadCheck:URLLoader;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> HCI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			staffText = <span style="color: #ff0000;">&quot;text/hci.txt&quot;</span>;
			staffImg = <span style="color: #ff0000;">&quot;images/hci.png&quot;</span>;
&nbsp;
			loadInfo<span style="color: #66cc66;">&#40;</span>staffText<span style="color: #66cc66;">&#41;</span>;
			loadImg<span style="color: #66cc66;">&#40;</span>staffImg<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> loadInfo<span style="color: #66cc66;">&#40;</span>txtInfo:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			txtLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			txtLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>,nowLoaded<span style="color: #66cc66;">&#41;</span>;
			fileNow = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>txtInfo<span style="color: #66cc66;">&#41;</span>;
			txtLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>fileNow<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> loadImg<span style="color: #66cc66;">&#40;</span>staffImg:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">url</span> = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>staffImg<span style="color: #66cc66;">&#41;</span>;
			loader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			loader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><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>;
			loader.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">180</span>,loader.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">131</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>This ends the classes that make up the Factory Method pattern used in this post. They&#8217;re available to download. Just click the Download button below and load &#8216;em up in either Flash Builder or Flash.</p>
<p><a href="http://nemo.mwd.hartford.edu/~wsanders/dpWork/FactoryMethod.zip"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a></p>
<p><strong>Back to Work</strong></p>
<p>As this New Year begins with new problems and opportunities for solving problems, we really need to think about the quality of our work. Down the line somewhere, a program that you helped write with good OOP and design pattern practices may solve someone else’s problem. The program may have one feature that because of what you did made it better. Maybe if we just do a little better for ourselves in terms of program practices, something good will come of it. Then at this time next year, we’ll all be a little better. For me, that’s enough.</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%2F01%2Ftake-a-design-pattern-to-work-part-iv-establishing-a-design-pattern-foundation%2F&amp;title=Take%20a%20Design%20Pattern%20to%20Work%20Part%20IV%3A%20Establishing%20a%20Design%20Pattern%20Foundation" id="wpa2a_20"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/01/take-a-design-pattern-to-work-part-iv-establishing-a-design-pattern-foundation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

