<?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; Flash</title>
	<atom:link href="http://www.as3dp.com/category/actionscript/flash/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>The Vote is In!</title>
		<link>http://www.as3dp.com/2011/03/the-vote-is-in/</link>
		<comments>http://www.as3dp.com/2011/03/the-vote-is-in/#comments</comments>
		<pubDate>Fri, 25 Mar 2011 09:28:24 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=5047</guid>
		<description><![CDATA[Monday we&#8217;ll start a new series for beginners in OOP and Design Patterns using ActionScript 3.0. The series will include examples from both Flex (Flash Builder) and Flash (Flash Professional.) The vote wasn&#8217;t even close, but because we believe that concurrent programming (parallel programming, multi-threaded programming) is so crucial to the future of both ActionScript [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.as3dp.com/wp-content/uploads/2011/03/Results.png" alt="Results" title="Results" width="250" height="188" class="alignleft size-full wp-image-5053" />Monday we&#8217;ll start a new series for beginners in OOP and Design Patterns using ActionScript 3.0. The series will include examples from both Flex (Flash Builder) and Flash (Flash Professional.) The vote wasn&#8217;t even close, but because we believe that concurrent programming (parallel programming, multi-threaded programming) is so crucial to the future of both ActionScript 3.0 and programming in general, we&#8217;ll be issuing posts on those as well. In the same polling, there were requests for combo patterns, frameworks, and even one for MVC, but for the time being, we&#8217;ll be developing a series for beginners and I&#8217;d like to make a run at creating a virtual machine for doing concurrent programming in ActionScript.</p>
<p>However, since we agreed to let you decide, our next post will be to get those who are beginning, intermediate and advanced programmers through the worm hole and into the land of OOP and Design Patterns.</p>
<p>In the meantime ponder this: <em>C’est magnifique, mais ce n’est pas la guerre. C’est de la folie.</em> That&#8217;s how the new series begins.</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%2Fthe-vote-is-in%2F&amp;title=The%20Vote%20is%20In%21" 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/03/the-vote-is-in/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Design Patterns at Work Survey</title>
		<link>http://www.as3dp.com/2009/05/actionscript-30-design-patterns-at-work-survey/</link>
		<comments>http://www.as3dp.com/2009/05/actionscript-30-design-patterns-at-work-survey/#comments</comments>
		<pubDate>Fri, 22 May 2009 20:25:47 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Principles]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=993</guid>
		<description><![CDATA[A Three-Question Survey I&#8217;m working on a new post about ActionScript 3.0 Design Patterns at work. To help get an accurate measure of what folks are doing in the ActionScript work world, I put together a simple survey. The survey is made with radio buttons and check boxes; so it only takes a minute to [...]]]></description>
			<content:encoded><![CDATA[<p><strong>A Three-Question Survey</strong></p>
<p>I&#8217;m working on a new post about ActionScript 3.0 Design Patterns at work. To help get an accurate measure of what folks are doing in the ActionScript work world, I put together a simple survey. The survey is made with radio buttons and check boxes; so it only takes a minute to fill out. Click below to take the World&#8217;s Easiest Survey:</p>
<p><a href="http://www.sandlight.com/survey/">ActionScript Design Patterns at Work Survey</a></p>
<p>We thank you in advance, and when we get the post up, you&#8217;ll see the results from the survey.</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%2F05%2Factionscript-30-design-patterns-at-work-survey%2F&amp;title=ActionScript%203.0%20Design%20Patterns%20at%20Work%20Survey" id="wpa2a_4"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/05/actionscript-30-design-patterns-at-work-survey/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Golden Lunch Bucket Contest Winners!</title>
		<link>http://www.as3dp.com/2009/05/golden-bucket-contest/</link>
		<comments>http://www.as3dp.com/2009/05/golden-bucket-contest/#comments</comments>
		<pubDate>Sun, 17 May 2009 20:45:48 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Contests]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=983</guid>
		<description><![CDATA[We finally finished judging the contestants and here are the results. Instead of having several categories for age, we decided to have only one because the ages did not have a great deal of range. Each of the winners happened to be from a different country, and so we can claim a true World Wide [...]]]></description>
			<content:encoded><![CDATA[<p>We finally finished judging the contestants and here are the results. Instead of having several categories for age, we decided to have only one because the ages did not have a great deal of range. Each of the winners happened to be from a different country, and so we can claim a true World Wide Contest. Congratulations to all! <a href="http://nemo.mwd.hartford.edu/~wsanders/winners/"> Download winning entries.</a><center><br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/05/goldenbucketgrand.gif" alt="goldenbucketgrand" title="goldenbucketgrand" width="150" height="150" class="alignnone size-full wp-image-1007" /></center><br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/05/will_wizard.jpg" alt="will_wizard" title="will_wizard" width="100" height="100" class="alignleft size-full wp-image-1008" /><img src="http://www.as3dp.com/wp-content/uploads/2009/05/brazilflag50.gif" alt="brazilflag50" title="brazilflag50" width="69" height="50" class="alignleft size-full wp-image-1185" /><br />
<strong>William Rafael de J. Ribeiro, Brazil</strong><br />Our grand prize winner created an interactive “funny face” using the basic Decorator and adding a second component class for a face and four decorator classes for angry, normal, and sad eyes plus a smile decorator. By moving the mouse around you can see the face change and eyes move.<br />
<center><br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/05/goldenbucketfirst.gif" alt="goldenbucketfirst" title="goldenbucketfirst" width="150" height="150" class="alignnone size-full wp-image-1011" /></center><br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/05/tcpic100.jpg" alt="tcpic100" title="tcpic100" width="100" height="100" class="alignleft size-full wp-image-1022" /><img src="http://www.as3dp.com/wp-content/uploads/2009/05/usflag50.gif" alt="usflag50" title="usflag50" width="95" height="50" class="alignleft size-full wp-image-1186" /><br />
<strong>Todd Coulson, U.S. </strong><br /> Todd’s entry used a creative combination of elements to build a baseball diamond. The field was a simple rectangle, and it was decorated with an infield, the bases, a pitcher’s mound, boundary lines and players scrambling to the field. Todd is interested in adding to this foundation using other design patterns.<br />
<center><br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/05/goldenbucketsecond.gif" alt="goldenbucketsecond" title="goldenbucketsecond" width="150" height="150" class="alignnone size-full wp-image-1013" /></center><br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/05/timo.jpg" alt="timo" title="timo" width="107" height="100" class="alignleft size-full wp-image-1015" /><img src="http://www.as3dp.com/wp-content/uploads/2009/07/finlandflag50.gif" alt="finlandflag50" title="finlandflag50" width="81" height="50" class="alignleft size-full wp-image-1180" /><br />
<strong>Timo Hannelin, Finland</strong><br />
Theoretical background for Timo comes from Katariina Nyberg&#8217;s article about phyllotaxis, fibonacci and the golden ratio. If we had a grand prize for the best algorithm, it would go to Timo. One algorithm makes an amazing amount of shapes—stars, triangles, squares, lines, shells, spirals&#8230;if you are lucky you will see almost all kinds of basic symbols. It also demonstrates that you can change a class in design pattern with a complex algorithm and it doesn’t faze it one bit.</p>
<p><center><img src="http://www.as3dp.com/wp-content/uploads/2009/05/goldenbucketthird.gif" alt="goldenbucketthird" title="goldenbucketthird" width="150" height="150" class="alignnone size-full wp-image-1017" /></center><br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/05/kevo_100.jpg" alt="kevo_100" title="kevo_100" width="100" height="100" class="alignleft size-full wp-image-1018" /><img src="http://www.as3dp.com/wp-content/uploads/2009/07/ukflag50.gif" alt="ukflag50" title="ukflag50" width="100" height="50" class="alignleft size-full wp-image-1182" /><br />
<strong>Kevo X. Thomson, UK</strong><br />
Kevo had a very simple entry. By changing the x and y coordinates of the three concrete decorators, he was able to rearrange the images. Again, the change was made and there was absolutely no negative ripple effect. This is exactly what a good design pattern is supposed to do.</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%2F05%2Fgolden-bucket-contest%2F&amp;title=Golden%20Lunch%20Bucket%20Contest%20Winners%21" 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/2009/05/golden-bucket-contest/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Design Pattern Contest: The Golden Lunch Bucket Awards!</title>
		<link>http://www.as3dp.com/2009/04/actionscript-30-design-pattern-contest-the-golden-lunch-bucket-awards/</link>
		<comments>http://www.as3dp.com/2009/04/actionscript-30-design-pattern-contest-the-golden-lunch-bucket-awards/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 21:42:38 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Contests]]></category>
		<category><![CDATA[Decorator]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>

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

		<guid isPermaLink="false">http://www.as3dp.com/?p=869</guid>
		<description><![CDATA[For those of you who have developed applications where a reference drills down through a series of movie clips, the Least Knowledge Principle may seem a bit harsh. It is very easy and even shows a semblance of foresight and organization to arrange operations in a movie clip set to have each movie clip do [...]]]></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" />For those of you who have developed applications where a reference drills down through a series of movie clips, the <strong>Least Knowledge Principle</strong> may seem a bit harsh. It is very easy and even shows a semblance of foresight and organization to arrange operations in a movie clip set to have each movie clip do one thing in a complex object. To get something working, though, you do not want to rely on drill-down references that visit each of the many movie clips.  Statements such as,</p>
<blockquote><p>myMC_high.myMC_middle.myMC_lower.myMC_lowest.DoSomething()…</p></blockquote>
<p> break the principle of Least Knowledge because it implies that each has some knowledge of the inner workings of the others. Likewise, anyone who has spent much time with JavaScript and the Document Object Model (<strong>DOM</strong>) knows that many of the objects have some hefty drill-downs in references. According to the Least Knowledge Principle, these are all ways to ask for trouble. Let’s see why.<br />
<span id="more-869"></span><br />
<strong>One Dot Over the Line</strong></p>
<p>First of all, few principles have as many names as the Least Knowledge Principle. Here they are—as an aid to remember what the point is and resource to find out more.</p>
<ol>
<li><strong>Law of Demeter: </strong> This term emanated from Northeastern University’s Demeter project. The name <em>Demeter</em> is from the goddess of agriculture which (to me) is more confusing than helpful. The Law of Demeter states:<br />
<blockquote><p>A method &#8220;M&#8221; of an object &#8220;O&#8221; should invoke only the methods of the following kinds of objects:</p>
<ul>
<li>itself</li>
<li>its parameters</li>
<li>any objects it creates/instantiates</li>
<li>its direct component objects</li>
</ul>
</blockquote>
<p>The Law of Demeter is the most specific as to what you should invoke as far as methods are concerned. Remember these four objects—the object itself, its parameters, instantiated objects and direct component objects.
</li>
<li><strong>One Dot Rule: </strong> Of all of the names for the Least Knowledge principle, the One Dot Rule is the most helpful in terms of a simple formula to remember. However, the advice to “use only one dot” is often the subject of heated (is there any other type?) debate among programmers. Generally, they miss the point, which is that any change in between the dots can be catastrophic when making changes. For example a statement like,<br />
<code>object1.object2.object3.methodA();</code><br />
can have dire consequences if one changes either object2 or object 3. </li>
<li><strong>French Mistress Rule (<em>entre nous</em>): </strong>This one doesn’t count since I coined it for our more romantic readers. It’s also known as the <strong>Secret Lover Principle</strong>, with the main idea being that if you carry on <em>une affaire du coeur</em> you had better keep it between you and your <em>amoureux</em>. As you can imagine, this helps remind you that you don’t want to provide anyone with more information than they can handle or need. What usually happens in these situations is that you just communicate with your closest friends—just as you should in programming.</li>
</ol>
<p>Some will conclude that this principle sounds a lot like <em>delegation</em> and why bother with a redundant principle? Well, in a nutshell, it is a lot like delegation—some might even say it’s just another name for delegation. I cannot argue with that point, because delegation is definitely implied in this Least Knowledge Principle. However, the principle is very instructive in that it points out a very specific way to think about coding and building programs. So while it’s a lot like delegation, it focuses on a specific aspect of delegation and organizing a program.</p>
<p><strong>Least Knowledge in Design Patterns</strong></p>
<p>Given all of those different angles on the Least Knowledge Principle, you may wonder what all of this has to do with design patterns. Remembering that the purpose of design patterns is to generate reusable code and programs that are easy to change, the principle keeps us from tangling up our references with too much information that may change in different parts.</p>
<p>Probably the best example of the Least Knowledge Principle at work is the <a href= "http://www.as3dp.com/2008/09/20/actionscript-facade-design-pattern-the-cat-herder/#more-222">Façade Design Pattern</a>. (In fact, in the Façade post, we noted that the Least Knowledge Principle was at work.) In the Façade, instead of making requests through the myriad of classes in another system, all requests are made through the Façade. The requester (Client in this case) just communicates with the Façade and doesn’t know or need to know the details of the other classes. Thus, it can communicate <em>just with its close friends</em> and not become entangled in all of the subsystem classes.</p>
<p>Other Least Knowledge examples can be found in design patterns that use wrappers. In the <a href= "http://www.as3dp.com/2008/08/26/actionscript-proxy-design-pattern-the-virtual-proxy-a-minimal-abstract-example/#more-99"> Proxy </a> pattern, the Client does not communicate with the concrete class, but instead makes requests through a proxy class. Likewise, the Decorator and the Adapter patterns (Chapters 4 and 5 in our book) use wrappers to keep the structure loose and adaptable. In the Decorator, the requests are made through the decorators (wrappers) to the concrete classes that reference the concrete classes as parameters in the instantiation of a decorator. Likewise in the Adapter, requests are through a target interface instance of an adapter object to any adaptee component. The client has no idea of the details and does not need them.</p>
<p><strong>Into the Lunch Bucket</strong><br />
The Least Knowledge principle is easy to remember by at least one of the names it’s called. Using the “use only one dot” rule, you’ll find yourself better off in terms of what might break. Of course we have to live with exceptions, such as the <code>Shape.graphics.method()</code> statements built into ActionScript 3.0 (for the time being at least), but you really need to rethink setting up movie clips within movie clips within movie clips, <em>ad nauseam</em>.</p>
<p>Those who prefer to use the term <em>Law of Demeter</em> seem to have the best horror stories. In one such story I came across, the Law of Demeter was broken in some software that was being used for the Mars pathfinder, and you can imagine the problems that would have caused—your programmer isn’t going to make a house call on that one!</p>
<blockquote><p>We have found, however, that breaking the LoD (Law of Demeter) is very expensive! On Mars Pathfinder, the integration costs of the law breaking parts of the system were at least an order of magnitude higher, and we had to pay that cost on every integration cycle &#8212; not just once! We should have paid the cost once by implementing a more sophisticated protocol that did obey the LoD. (Email from David.E.Smyth at NASA’s JPL.)</p></blockquote>
<p>Also, Miško Hevery’s article on breaking <a href="http://misko.hevery.com/2008/07/18/breaking-the-law-of-demeter-is-like-looking-for-a-needle-in-the-haystack/"> the Law of Demeter </a> is quite good. He uses the analogy of finding a needle in a haystack for the debugging process where the law has been violated. Now put that together with trying to fix a buggy program on the Mars pathfinder!</p>
<p>It’s easier for me to think of the principle in terms of <em> only speaking to your immediate friends</em> or keeping it <em>entre nous</em>. It works best for thinking in terms communicating in shorter links—sort of like a line of people holding hands and just talking to the person on the left or right. It’s not exactly that restrictive, but it helps me to keep the concept in my head. In an upcoming post on the Decorator design pattern on this blog, I hope to show how the Least Knowledge principle is applied in a wrapper application.</p>
<p>In the meantime, take this principle to work in your lunch bucket. If you call a method, remember to keep it close to the vest, and your objects need to know as little as possible about the internal structures of the methods they call outside of their own structure. You should never hear your objects mutter, <em> that’s way more than I needed to know</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%2F2009%2F04%2Fdesign-pattern-principles-for-actionscript-30-the-least-knowledge-principle%2F&amp;title=Design%20Pattern%20Principles%20for%20ActionScript%203.0%3A%20The%20Least%20Knowledge%20Principle" 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/2009/04/design-pattern-principles-for-actionscript-30-the-least-knowledge-principle/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Where&#039;s the Real World?: Design Pattern Examples in ActionScript 3.0</title>
		<link>http://www.as3dp.com/2009/01/wheres-the-real-world-design-pattern-examples-in-actionscript-30/</link>
		<comments>http://www.as3dp.com/2009/01/wheres-the-real-world-design-pattern-examples-in-actionscript-30/#comments</comments>
		<pubDate>Mon, 26 Jan 2009 09:47:05 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=590</guid>
		<description><![CDATA[Gentle Readers: This short post is a request for feedback. The whole issue of appropriate level examples both in our books and this blog is an important one because it speaks to the utility of the writings and posts. So, your thoughts are not only welcomed; they&#8217;re essential. I had a meeting with a computer [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Gentle Readers</strong>: This short post is a request for feedback. The whole issue of appropriate level examples both in our books and this blog is an important one because it speaks to the utility of the writings and posts. So, your thoughts are not only welcomed; they&#8217;re essential.</em></p>
<p>I had a meeting with a computer scientist who was teaching a game class. In our short chat, he must have used the term <strong>real world</strong> a dozen times. Well, I&#8217;m all for the real world (in contrast to the unreal world of unicorns, fairy dust, and honest politicians). However, the real world for one is not the same as it is for another. Recently, I got a comment about a blog entry thanking us for a solution to a practical problem that one of our readers encountered in programming. The same post was criticized by another reader as not being <em>real world</em>. Therein lies the dilemma.</p>
<p><strong>Abstract vs. Concrete</strong></p>
<p>Chandima and I use a range of examples in our book. We start with an abstract minimalist example so that the reader can see the participants in a design pattern and then move on to something more concrete to illustrate a practical application. On this blog, most of the examples start with the more abstract elements and move into a fairly general (somewhat abstract) example that is more practical. The more abstract an example, the more general its applicability—not unlike an abstract class. However, the more concrete an example, the better the reader can use it to model a like problem in an eminently practical way. Each has its benefits. The abstract examples have generalizability and the concrete examples have needed detail.</p>
<p>Were I to do all of my examples using <strong>real world</strong> examples that I deal with, most would involve streaming video and Flash Media Server. My customers usually approach me for just that kind of problem. Obviously,using streaming video and FMS is real world, but its not very generalizable. Likewise, some readers complain that the abstract examples don&#8217;t help because they&#8217;re not practical.</p>
<p>We&#8217;d like your thoughts on this issue. Obviously, the most useful examples would be those that you deal with directly in your work, but like my practical work, it&#8217;s pretty narrow. Keeping these concerns in mind, tell us what&#8217;s most helpful to you.</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%2Fwheres-the-real-world-design-pattern-examples-in-actionscript-30%2F&amp;title=Where%26%23039%3Bs%20the%20Real%20World%3F%3A%20Design%20Pattern%20Examples%20in%20ActionScript%203.0" 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/01/wheres-the-real-world-design-pattern-examples-in-actionscript-30/feed/</wfw:commentRss>
		<slash:comments>18</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('p575code5'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5755"><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="p575code5"><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('p575code6'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5756"><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="p575code6"><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('p575code7'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5757"><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="p575code7"><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('p575code8'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5758"><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="p575code8"><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_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/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('p544code14'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54414"><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="p544code14"><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('p544code15'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54415"><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="p544code15"><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('p544code16'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54416"><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="p544code16"><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('p544code17'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54417"><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="p544code17"><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('p544code18'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p54418"><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="p544code18"><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_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/01/take-a-design-pattern-to-work-part-iv-establishing-a-design-pattern-foundation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Take a Design Pattern to Work Part III : Loosening Up</title>
		<link>http://www.as3dp.com/2009/01/take-a-design-pattern-to-work-part-iii-loosening-up/</link>
		<comments>http://www.as3dp.com/2009/01/take-a-design-pattern-to-work-part-iii-loosening-up/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 17:06:07 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=480</guid>
		<description><![CDATA[Gentle Reader:This is Part 3 of a four-part series of posts on introducing design patterns and OOP into the work place. Reading Parts 1 and 2 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. More so [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Gentle Reader:</strong>This is Part 3 of a four-part series of posts on introducing design patterns and OOP into the work place. Reading Parts 1 and 2 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. More so than most posts, we invite your comments.</em></p>
<p><strong>Doing the Loosen Up</strong></p>
<p>Few of you remember Archie Bell and the Drells, but their song, <em>Tighten Up</em> was a big hit back in the day. With design patterns, we want to do the opposite, <em> Loosen Up</em>. At this point, we’re going to do the <em>Loosen Up</em>. So put on your dancing shoes and look at the post <a href="http://www.as3dp.com/2008/09/19/no-new-is-good-new-using-inheritance-composition-delegation-and-anything-else-other-than-new-in-actionscript-30-design-patterns/">No New is Good New</a>. The purpose of the post is to get us to think about relationships between objects in a programming design. The dictum, <em>program to an interface and not an implementation</em> is invoked, and the post discusses different kinds of relationships found in design pattern diagrams. If you’re not sure about the issues involved, that post will help understand the next step in introducing design patterns to work.</p>
<p>At this stage in the series, we have arrived at one of the pillars of OOP, <strong>inheritance</strong>. Figure 1 shows a simple diagram of where we are now in refactoring the original application that used ActionScript on the Timeline.</p>
<p><img class="alignnone size-full wp-image-486" title="inheritance" src="http://www.as3dp.com/wp-content/uploads/2009/01/inheritance.png" alt="inheritance" width="500" height="195" /></p>
<p><strong><em>Figure 1: </em>Inheritance from a parent class</strong></p>
<p>As you saw, inheritance allows developers to use the properties and methods of existing classes and in so doing saves time and adds consistency. However, a standard inheritance model also requires the client to instantiate all of the child classes that must be used. True, the instantiation can be through the parent class by initially typing the objects as the parent class rather than the child class as the following shows:</p>
<p style="padding-left: 30px;"><strong>private var hci:Staff;<br />
 hci = new HCI();</strong></p>
<p>The purpose of instantiating the parent is to make the link more general and flexible without breaking encapsulation. If the parent class changes, the objects typed to the parent have a better chance of working well without modification.</p>
<p><span id="more-480"></span></p>
<p>As noted in the post about using the <strong>new</strong> statement, each new statement may limit your flexibility. If a change is made to the object, your Client or some other part of your program may have to be modified to accommodate the change. One way to deal with that is to follow the dictum,</p>
<blockquote><p>Identify the aspects that vary and separate them from what stays the same</p>
</blockquote>
<p>So what varies in the program? That’s easy—the staff does. The Client chooses one of three Staff types (children). So how do we separate them? One solution is the use of a simple factory. Basically, a simple factory separates the Client from the instantiation process. Whatever you want, you ask the simple factory instead of instantiating the requested object. The Client instantiates a single factory object, and the factory object instantiates the objects by the Client. In that way, the Client needs to only instantiate a single object rather than any number made up of the child subclasses. Figure 2 shows this new model:</p>
<p><img class="alignnone size-full wp-image-487" title="simplefactory" src="http://www.as3dp.com/wp-content/uploads/2009/01/simplefactory.png" alt="simplefactory" width="497" height="190" /></p>
<p><strong><em>Figure 2: </em>Adding a Simple Factory</strong></p>
<p>At this point, you may be thinking, <em>finally a design pattern!</em> However, the simple factory is not a design pattern but instead a programming operation or mode. As we will see, the simple factory can be used in a Factory Method design pattern, but it’s also used in the <a href="http://www.as3dp.com/2007/06/09/the-actionscript-30-flyweight-saga-part-i/#more-21">Flyweight</a> pattern. Some of the other operations you will find with design pattern names include <em>singleton</em>, <em>observer</em> and <em>template method</em>. (See pp. 66 to 68 for an introduction to the concepts at work here in the factory. Also, Chapter 9 explains the template method as an operation and a design pattern.)</p>
<p>By focusing on the more specific operation instead of the entire design pattern, we’re better able to clearly see what the operation is up to. If you understand the operations, you can more easily introduce them into the workplace as a simple and useful tool. The important task is create a site with as few locked up relations as possible. Do the <em>Loosen Up</em>.</p>
<p><strong>Separating the Parts that Vary from those that Stay the Same</strong></p>
<p>The Client in the example program has the following operation to select among the available choices:</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('p480code23'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p48023"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p480code23"><pre class="actionscript" style="font-family:monospace;">	staffNow = <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>;
	<span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>staffNow<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;developer&quot;</span> :
			develop = <span style="color: #000000; font-weight: bold;">new</span> Developer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>develop<span style="color: #66cc66;">&#41;</span>;
			lastClass = develop;
			<span style="color: #b1b100;">break</span>;
		<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;designer&quot;</span> :
			design = <span style="color: #000000; font-weight: bold;">new</span> Designer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>design<span style="color: #66cc66;">&#41;</span>;
			lastClass = design;
			<span style="color: #b1b100;">break</span>;
		<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;hci&quot;</span> :
			hci = <span style="color: #000000; font-weight: bold;">new</span> HCI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>hci<span style="color: #66cc66;">&#41;</span>;
			lastClass = hci;
			<span style="color: #b1b100;">break</span>;
	<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>By removing this portion of the code from the Client and moving it to a simple factory class, we can extract just the part where change occurs. The following code shows this new Factory 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('p480code24'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p48024"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code" id="p480code24"><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> Factory
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> design:Staff;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> develop:Staff;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> hci:Staff;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> makeStaff<span style="color: #66cc66;">&#40;</span>staffNow:<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>staffNow<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: #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>Note that the private variables with the names of the requested objects are also moved to the Factory. The Client no longer needs them because the Factory is going to be doing the instantiation. Now that the changing aspects were removed from the Client class, we’ll change the Client to make the requests from the Factory class.</p>
<p>Before doing that though, note that the makeStaff() method returns a <em>Sprite</em> instead of the class object. Chandima suggested this because you will be able to place it on the stage. Otherwise, you’d have to use a bit more convoluted approach (which I unfortunately mastered) to insure that the graphics and text appear on the page. Now, in looking at the Client, we see only a single Factory instance instantiated instead of three Staff 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('p480code25'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p48025"><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="p480code25"><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> factory:Factory=<span style="color: #000000; font-weight: bold;">new</span> Factory<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: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			splash.<span style="color: #0066CC;">visible</span>=<span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
			staffNow = <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>;
&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>
			staffDisplay = factory.<span style="color: #006600;">makeStaff</span><span style="color: #66cc66;">&#40;</span>staffNow<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			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>Now suppose that the Staff and the Staff child classes all change. What’s going to happen to the Client?<strong><em> Nothing</em></strong>. All the Client is requesting and the Factory returning is a Sprite. So the Staff and its children classes can change all they want. The Client needs to change nothing. In addition, the Client only instantiates a single instance; not of the Staff class but of the Factory. The following code is the Staff class and its children.</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('p480code26'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p48026"><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
</pre></td><td class="code" id="p480code26"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Staff Class</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">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: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</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: #006600;">TextFieldType</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;">//Parent class</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Staff <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> 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> 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;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">textField</span>:<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>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Staff<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">textField</span>.<span style="color: #0066CC;">type</span> = TextFieldType.<span style="color: #0066CC;">DYNAMIC</span>;
			<span style="color: #0066CC;">textField</span>.<span style="color: #0066CC;">wordWrap</span> = <span style="color: #000000; font-weight: bold;">true</span>;
			<span style="color: #0066CC;">textField</span>.<span style="color: #0066CC;">textColor</span> = 0x007700;
			<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>;
			<span style="color: #0066CC;">textField</span>.<span style="color: #006600;">defaultTextFormat</span> = <span style="color: #0066CC;">textFormat</span>;
		<span style="color: #66cc66;">&#125;</span>
&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>
			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;
		<span style="color: #0066CC;">private</span> <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;">textField</span>.<span style="color: #0066CC;">text</span> = loadCheck.<span style="color: #0066CC;">data</span>;
			<span style="color: #0066CC;">textField</span>.<span style="color: #0066CC;">width</span> = <span style="color: #cc66cc;">200</span>;
			addChild<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">textField</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">textField</span>.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">180</span>,<span style="color: #0066CC;">textField</span>.<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;">url</span> = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>img<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;">150</span>,loader.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">130</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;">//Child classes</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Designer</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Designer <span style="color: #0066CC;">extends</span> Staff
	<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>;
&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>
	<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;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Developer <span style="color: #0066CC;">extends</span> Staff
	<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>;
&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>
	<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;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HCI <span style="color: #0066CC;">extends</span> Staff
	<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>;
&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>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, the Staff class and its children are almost identical to the previous incarnations in Part II of this series. The child classes were changed to include private variables and the invocations of the main operations appear far more polymorphic (they’re all identical), but those changes are just cosmetic to reinforce OOP thinking. Also, a subtle but important change is in changing the typing of the logo and splash page to Sprites type in the Client. This provides an opportunity for you to see if you can work out the operations needed to click the logo and have the splash page appear. Of course, you’ll want to move the request to the Factory class, and if you can do this, you probably are well on your way to sneaking in some good OOP and a pre-design pattern mode into the workplace. Click the button to download the entire application: </p>
<p><a href="http://nemo.mwd.hartford.edu/~wsanders/dpWork/SimpleFactory.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>Playing Nicely in the Sandbox</strong></p>
<p>Several years back a hugely talented artist and I worked together on a project. It began as a mutual admiration relationship and ended in the worse case of angst and misery I can remember. Neither of us liked the other at the end of the project, and the project suffered because of our conflict. To this day I regret the relationship’s destruction. For lack of a better explanation, we had two diametrically opposed ideas of what would best serve the goals of the project and we both assumed that the other was seeking the same goal as we were.</p>
<p>He created animations using Flash and included ActionScript on the Timeline, in buttons, in movie clips and God knows where else. (I don’t think I ever found them all.) Naturally, the Flash app kept growing and the pre-loader I made had to dance the dance of the seven veils to keep the viewer from desperately hammering the Back button. The Library was full of Tween symbols (Tween 1, Tween 2, etc.) that meant something was moved before it was converted to a symbol or some other screw up. Undaunted, the artist soldiered on and eventually created a huge animation that was a wonder to behold. (Of course you could rotate the tires on your car while waiting for the SWF to load.)</p>
<p>The introductory animation was exactly what the clueless project manager had wanted. (I think he said something like, “Dazzle me!”) Unfortunately, after viewing this <em>dazzling</em> behemoth a couple of times, it lost its charm and so as is often the case, the project had a big fat white elephant sitting at the doorstep to be side stepped with a <em>Skip Intro</em> button.</p>
<p>I could go on and on, but the purpose is not to tell tales of woe about non-developers in real world project development. As many of you must know, working in jobs with projects and datelines is more complex than simply putting together good code. For me, the three key elements for a successful Web site are:</p>
<ul>
<li>Good graphics (visible)</li>
<li>Good UI (visible)</li>
<li>Good Programming (invisible)</li>
</ul>
<p>Jennifer Tidwell (who rudely completed degrees from <em>both</em> MIT and Massachusetts College of Art just to make the rest of us look bad) found that good graphics were seen as the most trustworthy element of a Web site. So whether we like it or not, we have to have sites with good graphics, and if you’re a developer, assume you have no graphic or artistic skills. (I know of exceptions—like <em>Jennifer</em>—, and if you’re one of them, you don’t need to tell me.) The fact of the matter is we should do everything we can to use our programming skills to ensure that the graphics can be viewed as intended by the artist. Part of that task may be making sure that the artists keep their mitts off the code.</p>
<p>A few years ago I went to an HCI meeting in London. Two things came out of that meeting. First, HCI is essential to a successful Web site.  Second, the HCI folks feel that they do not get the respect they deserve. In fact, they feel that they get none at all. UI and navigation needs to be well planned and coded. Coding HCI is like anything else, and it can get seriously complex with a big online business site. Given that design patterns and OOP have their greatest value with large-scale projects that are constantly changing, if you find the right pattern with the UI system, you’ll be a happy developer and so will the HCI folks.</p>
<p>The biggest enemies of good HCI are graphic artists. Seriously. A graphic artist gets tied into <em>looking good</em> and the HCI folks into <em>finding stuff</em>. A few years ago MOMA (New York City’s Museum of Modern Art) had a Web site with a navigation system that looked like a Rubik&#8217;s Cube and was about as easy to use. Each block in the cube was an unlabelled trigger and when you clicked on it, you were given a popup telling you what you’ve found. (This occurs shortly before you hit the back button on your browser.) The folks at MOMA probably listened to someone who convinced them that people <em> love to explore</em> Websites. Oddly, the site functioned spiffily which indicated some good code under the hood. So for users who had nothing to do for the rest of their lives, they could explore MOMA’s Rubik navigation system. Since then they discovered that patrons of the arts need to find where they can donate money and art shoppers need to find stuff to buy. If you go to the site now, you’ll see the war between the dancing baloney that makes up the header (unfortunately done in Flash), and the Puritanical UI that allows you to find stuff. (Imagine the discussions that must go on at that site between designers, developers and HCI folks! And you thought you had problems.)</p>
<p><strong>Not Sucking</strong></p>
<p>If you have nothing better to do (and the fact that you’re reading this blog indicates as much), pay a visit to <a href="http://www.webpagesthatsuck.com"> Web Pages that Suck</a>. While the keeper of the site prides himself on being the august voice of good Web sites, his own site suffers from ambiguity. Nevertheless, he has a real knack for identifying bad Web sites, and if you dig around you’ll find some good tips on navigation and making Web sites that work—actually avoiding ones that don’t work. He coined the term <em>Mystery Meat Navigation</em> and his examples of such navigation reinforce the necessity for good HCI people on board your staff. (However, the guy becomes almost apoplectic over Flash, so be forewarned.)</p>
<p>So what does this all have to do bringing a design pattern and good OOP practices to work? Everything! You have to deal realistically with people who have their hands full. Your paycheck and your programming skills are tied to their success. By starting with a few good OOP practices you can work your way to the doorstep of design patterns to improve your code at work. You have to work with designers and HCI folks to create good Website; so be aware of what they must accomplish. By the same token, you need to develop good code to present attractive graphics and for the navigation system to operate smoothly. So while helping others in your workaday mix to accomplish their goals, don’t forget your own.</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-iii-loosening-up%2F&amp;title=Take%20a%20Design%20Pattern%20to%20Work%20Part%20III%20%3A%20Loosening%20Up" 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/take-a-design-pattern-to-work-part-iii-loosening-up/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Take a Design Pattern to Work Part II: A Little OOP</title>
		<link>http://www.as3dp.com/2009/01/take-a-design-pattern-to-work-part-ii-a-little-oop-2/</link>
		<comments>http://www.as3dp.com/2009/01/take-a-design-pattern-to-work-part-ii-a-little-oop-2/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 22:40:15 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Design Patterns at Work]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/2009/01/05/take-a-design-pattern-to-work-part-ii-a-little-oop-2/</guid>
		<description><![CDATA[Gentle Readers: Before reading this post, you’re advised to look at Take a Design Pattern to Work Part I: Identifying the Problem. It provides the necessary context for this post. Also, keep in mind that this series of posts is walking through a process and some of the steps may seem way too simple. We’re [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Gentle Readers:</strong> Before reading this post, you’re advised to look at Take a Design Pattern to Work Part I: Identifying the Problem. It provides the necessary context for this post. Also, keep in mind that this series of posts is walking through a process and some of the steps may seem way too simple. We’re trying to imagine communicating with others who may not be as technically adept as you, and each step is simplified.</em></p>
<p><strong>Getting Off the Timeline</strong></p>
<p>As the readers of this blog are well aware, the Timeline in Flash is a valuable tool for animation. By using the Timeline in conjunction with symbols, IK and tweening, all kinds of wonderful creations are possible. However, the Timeline is not a good place for code, and again, this is nothing new to readers of this blog. So why bring it up?</p>
<p>If you peruse the ActionScript job openings at places like <a href="http://www.dice.com">Dice.com</a> and <a href="http://www.monster.com">Monster.com</a>, you’ll find openings that include the following requirements:</p>
<blockquote><p>The successful candidate should have at least three years experience using ActionScript 2.0 with knowledge of ActionScript 3.0 a plus. He/she should also have the ability to maintain and update legacy Flash sites with ActionScript 1.0. A knowledge of OO programming and design patterns is strongly desirable.</p>
</blockquote>
<p>Work that includes maintaining a site written in ActionScript 1.0 (and even ActionScript 2.0 in some cases) almost certainly means that some or all of the code is written on the Timeline and possibly even embedded in the individual symbols —buttons and movie clips. In other words, a good deal of the work is playing <em>Where’s Waldo?</em> with the ActionScript scattered all over the place. For me, working to maintain such a site is somewhere south of Dante’s 9th Circle of Hell. (You remember Dante’s <em>Divine Comedy</em> from that humanities class you had to take.)</p>
<p>In Part I of this post series, the code is embedded in an ActionScript layer on the Timeline. Maintaining the site is pretty simple both in making changes and adding new materials. The developer makes changes by changing the image file and the text contents in any single keyframe. Additions are made by adding keyframes in the Options layer, code in the ActionScript layer and buttons in the Buttons layer. That’s not a difficult plan. So why change?</p>
<p>With a relatively small staff in the company, there’s probably no reason to change it at all. However, as the staff grows or they want to add more to the site, especially in terms of additional graphics and animated materials, the size of the SWF file grows as well. Pretty soon, you’re making pre-loaders, and that’s a good sign that you’re in trouble.</p>
<p>In addition to the problems in generating a big fat SWF file, a single set of ActionScript statements can only grow into a larger and less manageable set of code. For a small application, it works fine, but as applications grow, unless some of the inherent problems are addressed, an error prone application begins to emerge. Change becomes more problematic and the program begins to lose functionality as well as flexibility.</p>
<p><span id="more-442"></span></p>
<p><strong>The Client and the Request</strong></p>
<p>A good place to start is with the Client class. (I’m going to capitalize <em>Client</em> where I’m referring to the Client as a class. Non-cap  <em>client</em> refers to all other uses of the term such as <em>client-server</em> or <em> Our <strong>client</strong> wanted us to develop a flexible application</em>.) The Client is the <strong><em> gimme class</em></strong>&#8211;“Gimme this! Gimme that!”—in non-idiomatic English, that would be “Give me this! Give me that!”  It makes requests from the set of classes that make up the design pattern. Since we haven’t got a design pattern at this point, think of the Client as the class we need for making requests. Also, as a general concept, think of the Client as making requests from <em>other</em> classes. That is, instead of making requests from itself, while possible from a self-contained function, the Client has a reference to another class or interface.</p>
<p>Some design patterns provide the position of the Client in the class diagram and others do not. For example, on pages 66-67 of <em>ActionScript 3.0 Design Patterns</em>, you can see the Client class in the diagram in both the Simple Factory Method and the Factory Method design pattern, but on page 131, you see no Client class in the Decorator design pattern. That doesn’t mean you’re not going to use a Client class. It simply means your Client class will make its requests from the pattern but is not implied or part of the pattern itself. Patterns with implied Clients such as the <a href="http://www.as3dp.com/category/design-patterns/proxy/"> Proxy</a> pattern display the Client <em>ghosted</em> or faded. Otherwise, a visible Client in a class diagram means that the Client is part of the design pattern. About half the design patterns show the Client as part of the pattern or implied. An implied connection means that means the Client is expected to have a connection between itself and a specific class in the pattern but is not part of the pattern’s structure. Sometimes, a Client is displayed with relations to more than a single other class in the pattern. One example is the <a href="http://www.as3dp.com/2008/12/14/actionscript-30-interpreter-design-pattern-a-postscript-tutor/"> Interpreter </a> pattern.</p>
<p><strong>The Request for an Object</strong></p>
<p>The Client makes a request from another class that has built the desired content. This may be an object or process. A simple example would be to request a graphic image that was created in an image class. In the example from Part I of <em>Bring a Design Pattern to Work</em>, the request is in the form of buttons serving as UIs. Each button requests a different Staff page. In essence the request is to go to and stop at a specified keyframe.  So, let’s take the ActionScript off the Timeline and create a Client class and object classes for the three staff members. The Client requests one of three staff members; so all we need to do is to create three Staff classes. The Client can create its own request UIs using Button components (which in themselves are classes.)
 </p>
<p>We can imagine several different strategies to request the three classes with images and text. First, we could create three SWF files, each with a different staff member with image and text. That’s pretty easy in Flash and Flex. Just slap the graphic and text right on a black background that’s that. Figure 1 shows the setup for a Flex application. After saving and testing the application, you can use the automatically generated SWF file in Flash or Flex. Of course you’d have to create some kind of loader to place the SWF files where you want them, but it would work just fine.</p>
<p><img class="alignnone size-full wp-image-430" title="figure1" src="http://www.as3dp.com/wp-content/uploads/2009/01/figure1.png" alt="figure1" /></p>
<p><em><strong>Figure 1:</strong> Creating SWF file in Flex</em></p>
<p>Using your imagination, you can think of lots more ways that a Client could request an image with a text caption and how to set up the image and text that will be requested. If your customer wants to make changes on her own without having to know a thing about Flash or Flex, then the process for the developer becomes a little tougher. You’d need a way to load image and text files independently and place them on the stage. The customer could just change the contents of the images and text for staff changes. If more staff members are added, the developer would have to go in and add more requests (buttons) to the Client and somehow fix the Object class to accept more images and text captions. Figure 2 diagrams the Client making requests directly to each class.</p>
<p><img class="alignnone size-full wp-image-432" title="figure2" src="http://www.as3dp.com/wp-content/uploads/2009/01/figure2.png" alt="figure2" width="326" height="315" /></p>
<p><em><strong>Figure 2:</strong> Client making requests from each class</em></p>
<p>Each of the Staff classes can then load up the necessary image and text. Adding new staff is simple. Just copy and paste any of the classes and change the image and text targets for the class. The following pseudo-code shows what each staff class looks like:</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('p442code31'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p44231"><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="p442code31"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//NOTE: This is pseudo-code. It does not work.</span>
package
<span style="color: #66cc66;">&#123;</span>
          <span style="color: #0066CC;">import</span>.<span style="color: #006600;">Sprite</span>;
          <span style="color: #0066CC;">import</span>.<span style="color: #006600;">ClassesForLoaders</span>;
          <span style="color: #0066CC;">import</span>.<span style="color: #006600;">ClassesForText</span>;
&nbsp;
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StaffX <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> StaffX<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
                    <span style="color: #66cc66;">&#123;</span>
                              <span style="color: #808080; font-style: italic;">//Text field and its properties</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: #66cc66;">&#125;</span>
&nbsp;
             <span style="color: #808080; font-style: italic;">//Method for loading image</span>
             <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loadImage<span style="color: #66cc66;">&#40;</span>from Client<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
             <span style="color: #66cc66;">&#123;</span>
                         loader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>from Client<span style="color: #66cc66;">&#41;</span>;
              <span style="color: #66cc66;">&#125;</span>
&nbsp;
              <span style="color: #808080; font-style: italic;">//Method for loading text</span>
 	      <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> loadText<span style="color: #66cc66;">&#40;</span>from Client<span style="color: #66cc66;">&#41;</span>
              <span style="color: #66cc66;">&#123;</span>
                         urlLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>from Client<span style="color: #66cc66;">&#41;</span>;
               <span style="color: #66cc66;">&#125;</span>
          <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>Inheritance</strong></p>
<p>If you stop and think about this for a second, you realize that if all of the staff classes are pretty similar in their functionality, why not just create a parent class and instantiate the other classes as inheriting a parent class? As each new child class is added, all you’d need to do is to use a loading method inherited from the parent class. Then each Staff class could be just a pair of loading methods that load the requested materials. Figure 3 shows a diagram of this new arrangement:</p>
<p><img class="alignnone size-full wp-image-433" title="figure3" src="http://www.as3dp.com/wp-content/uploads/2009/01/figure3.png" alt="figure3" width="500" height="286" /></p>
<p><em><strong>Figure 3:</strong> Client making requests through parent class</em></p>
<p>After all, we know that <em>inheritance</em> is one of the cornerstones of OOP, and in looking at Figure 3. It sure looks like good OOP. That’s as good a place to start with introducing OOP concepts (inheritance) as anywhere.</p>
<p><strong>Client Jobs</strong></p>
<p>Because the focal chore of the Client class is to make requests, it may seem either trivial or marginal to give it more than a cursory glance. However, as you will see in this series of posts, it can be crucial. So let’s pay the Client its due.</p>
<p>The request UI in the first post of this series was the Button component, and we’ll continue using that UI. The requests change only slightly. Instead of the requesting the playhead to move to and stop at a particular keyframe, this time the Client requests a class representing one of three staff positions. So the buttons with their attending events and event-handlers are all part of the Client class.</p>
<p>In addition to making requests, the Client has some setup chores. It places a logo and a ‘splash’ page on the stage. Both are made up of movie clip objects in the Library. The logo stays on the stage, but as soon as the photos and text from the staff begin to appear, the splash page disappears. That’s it for the logo and splash page.</p>
<p>Keeping in mind that this application uses subclasses, the first practice to keep in mind is:</p>
<blockquote><p>Program to an interface; not an implementation</p>
</blockquote>
<p>At this point, the parent class (<strong>Staff</strong>) is a pretty static interface, but for the sake of good practices go for a looser typing when possible. The child classes, Designer, Developer, and HCI, are implementations; so we’ll treat the parent class as an interface.*</p>
<p style="padding-left: 60px;">*<strong>Note:</strong> Both interfaces and abstract classes are considered <em>interfaces</em>. I am well aware that ActionScript 3.0 does <em>not</em> have abstract classes, but the concept of an interface here is not the ActionScript statement, <strong>interface</strong>, but instead it refers to the more general idea of a <em>supertype</em>. See pp. 45-49 of our book for more details. Also, the Freemans have a great explanation of the concept on page 12 of <em>Head First Design Patterns</em>.</p>
<p>Thus, in declaring the private variables for the different subclasses of the Staff class, they are typed as Staff. It’s a looser kind of typing even though at this stage of the game, it’s not a whole lot looser given the very static nature of the child classes emerging from the parent class Staff.</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('p442code32'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p44232"><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
</pre></td><td class="code" id="p442code32"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
          <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
          <span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">Button</span>;
          <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
          <span style="color: #808080; font-style: italic;">//Client Class</span>
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
          <span style="color: #66cc66;">&#123;</span>
                    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> logo:Logo=<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:Splash=<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> design:Staff;
                    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> develop:Staff;
                    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> hci:Staff;
                    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> lastClass:Staff;
                    <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>;
&nbsp;
                    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
                    <span style="color: #66cc66;">&#123;</span>
                             <span style="color: #808080; font-style: italic;">//Add the buttons to provide a UI for the requests</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;
                             <span style="color: #808080; font-style: italic;">//Place the logo and splash page on the stage</span>
                              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: #808080; font-style: italic;">//Client request list</span>
                    <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>
                              <span style="color: #808080; font-style: italic;">//Remove the initial text block (in a movie clip)</span>
                              splash.<span style="color: #0066CC;">visible</span>=<span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
                              <span style="color: #808080; font-style: italic;">//Remove previous image and text</span>
                              <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>lastClass<span style="color: #66cc66;">&#41;</span>
                              <span style="color: #66cc66;">&#123;</span>
                                        removeChild<span style="color: #66cc66;">&#40;</span>lastClass<span style="color: #66cc66;">&#41;</span>;
                              <span style="color: #66cc66;">&#125;</span>
&nbsp;
                     <span style="color: #808080; font-style: italic;">//Change the button labels to lowercase</span>
                      staffNow = <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>;
&nbsp;
                      <span style="color: #808080; font-style: italic;">//Use the switch statement to sort our the requests</span>
                              <span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>staffNow<span style="color: #66cc66;">&#41;</span>
                              <span style="color: #66cc66;">&#123;</span>
                                        <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;developer&quot;</span> :
                                                  develop = <span style="color: #000000; font-weight: bold;">new</span> Developer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
                                                  addChild<span style="color: #66cc66;">&#40;</span>develop<span style="color: #66cc66;">&#41;</span>;
                                                  lastClass = develop;
                                                  <span style="color: #b1b100;">break</span>;
                                        <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;designer&quot;</span> :
                                                  design = <span style="color: #000000; font-weight: bold;">new</span> Designer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
                                                  addChild<span style="color: #66cc66;">&#40;</span>design<span style="color: #66cc66;">&#41;</span>;
                                                  lastClass = design;
                                                  <span style="color: #b1b100;">break</span>;
                                        <span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;hci&quot;</span> :
                                                  hci = <span style="color: #000000; font-weight: bold;">new</span> HCI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
                                                  addChild<span style="color: #66cc66;">&#40;</span>hci<span style="color: #66cc66;">&#41;</span>;
                                                  lastClass = hci;
                                                  <span style="color: #b1b100;">break</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>Normally you would not name your Client class, “Client,” but instead you’d provide something more descriptive. However, because this is an exercise to clearly identify the different parts that make up a design pattern, it is helpful to do so here.</p>
<p><strong>The Parent Class</strong></p>
<p>Often in design patterns you’ll have more than a single parent class, but in this OOP pattern, all we’re using the single parent class for is to illustrate inheritance. This particular parent class is very handy because it creates a text field, loads external text in and then load in a graphic or SWF file. (What’s not to like!). Then, we’ll subclass all the child classes that will use it.</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('p442code33'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p44233"><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="p442code33"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
          <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
          <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
          <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">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: #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: #006600;">TextFieldType</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;">//Parent class</span>
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Staff <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> 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> 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;
                    <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">textField</span>:<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>;
&nbsp;
                    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Staff<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
                    <span style="color: #66cc66;">&#123;</span>
                              <span style="color: #808080; font-style: italic;">//Set up text field and text format</span>
                              <span style="color: #0066CC;">textField</span>.<span style="color: #0066CC;">type</span> = TextFieldType.<span style="color: #0066CC;">DYNAMIC</span>;
                              <span style="color: #0066CC;">textField</span>.<span style="color: #0066CC;">wordWrap</span> = <span style="color: #000000; font-weight: bold;">true</span>;
                              <span style="color: #0066CC;">textField</span>.<span style="color: #0066CC;">textColor</span> = 0x007700;
                              <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>;
                              <span style="color: #0066CC;">textField</span>.<span style="color: #006600;">defaultTextFormat</span> = <span style="color: #0066CC;">textFormat</span>;
                    <span style="color: #66cc66;">&#125;</span>
&nbsp;
                    <span style="color: #808080; font-style: italic;">//Load the external text</span>
                    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;
                    <span style="color: #808080; font-style: italic;">//Place loaded text into text field</span>
                    <span style="color: #0066CC;">private</span> <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;">textField</span>.<span style="color: #0066CC;">text</span> = loadCheck.<span style="color: #0066CC;">data</span>;
                              <span style="color: #0066CC;">textField</span>.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">200</span>;
                              addChild<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">textField</span><span style="color: #66cc66;">&#41;</span>;
                              <span style="color: #0066CC;">textField</span>.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">180</span>,<span style="color: #0066CC;">textField</span>.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">331</span>;
                    <span style="color: #66cc66;">&#125;</span>
&nbsp;
                    <span style="color: #808080; font-style: italic;">//Load the graphic image or SWF file</span>
                    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;">url</span> = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>img<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;">150</span>,loader.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">130</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>Note that most of the methods are protected, and one is private. As usual, the constructor is public. The protected methods are such so that they have some encapsulation and yet can be used by children of the parent class.</p>
<p><strong>All the Little Kiddie Classes</strong></p>
<p>Now that the Client and main parent class is ready, the target of the requests can be built. These have been stripped down to illustrate how simple it is to use inheritance to get what you need. At this point the site only needs three children classes, one each for the different departments:</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('p442code34'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p44234"><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="p442code34"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Designers</span>
package
<span style="color: #66cc66;">&#123;</span>
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Designer <span style="color: #0066CC;">extends</span> Staff
          <span style="color: #66cc66;">&#123;</span>
                    <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>
                              loadInfo<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;text/designer.txt&quot;</span><span style="color: #66cc66;">&#41;</span>;
                              loadImg<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;images/designer.png&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;">//Developers</span>
package
<span style="color: #66cc66;">&#123;</span>
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Developer <span style="color: #0066CC;">extends</span> Staff
          <span style="color: #66cc66;">&#123;</span>
                    <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>
                              loadInfo<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;text/developer.txt&quot;</span><span style="color: #66cc66;">&#41;</span>;
                              loadImg<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;images/developer.png&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;">//HCI Folks</span>
package
<span style="color: #66cc66;">&#123;</span>
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HCI <span style="color: #0066CC;">extends</span> Staff
          <span style="color: #66cc66;">&#123;</span>
                    <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>
                              loadInfo<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;text/hci.txt&quot;</span><span style="color: #66cc66;">&#41;</span>;
                              loadImg<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;images/hci.png&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>The only remaining chore is to place the text files and image files in the text and images folders. The original request from the Client class is simplified by using Button components and their labels to identify the desired staff. If you’d rather not have a big conditional switch statement, you can make each button have a different event handler. You can view the application <a href="http://nemo.mwd.hartford.edu/~wsanders/dpWork/Client.html">here </a> and download the application <a href="http://nemo.mwd.hartford.edu/~wsanders/dpWork/Inheritance.zip">here</a>.</p>
<p>When you test your application, you should see pretty much the same thing that you saw with the original application. None of the code is on the Timeline, and all the customer has to change is the content of the text and images in the folders to change what the viewer will see. Figure 4 shows what you can expect to see when you test the application.</p>
<p><img class="alignnone size-full wp-image-437" title="figure4" src="http://www.as3dp.com/wp-content/uploads/2009/01/figure4.png" alt="figure4" width="550" height="400" /></p>
<p><em><strong>Figure 4:</strong> Delivered request</em></p>
<p>If you review the first post of this series you’ll see similarities and differences. The buttons in Figre 4 pick up the background color, and the logo has a better gradient in the sun behind the Sandlight text.</p>
<p><strong>More Help on the Way</strong></p>
<p>At this point, you’ve seen the progression from legacy ActionScript stuck on a Timeline to using OOP. By thinking about loosening up an application, you’re beginning to think about a flexible program where change and update are easier. If you look at your programs at work in this way, you can <em>begin to think</em> of how they can be made stronger and more flexible at the same time. In the next installment, we’ll take the penultimate step on the way to a design pattern for this application.</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-ii-a-little-oop-2%2F&amp;title=Take%20a%20Design%20Pattern%20to%20Work%20Part%20II%3A%20A%20Little%20OOP" 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-ii-a-little-oop-2/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

