<?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; Principles</title>
	<atom:link href="http://www.as3dp.com/category/principles/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>Beginner&#039;s First Design Pattern: The Template Method</title>
		<link>http://www.as3dp.com/2011/05/beginners-first-design-pattern-the-template-method/</link>
		<comments>http://www.as3dp.com/2011/05/beginners-first-design-pattern-the-template-method/#comments</comments>
		<pubDate>Sun, 15 May 2011 19:17:29 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Abstract Classes]]></category>
		<category><![CDATA[Getting Started in Design Patterns]]></category>
		<category><![CDATA[Learning Design Patterns]]></category>
		<category><![CDATA[Principles]]></category>
		<category><![CDATA[Template Method Pattern]]></category>

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

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p5555code6'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p55556"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p5555code6"><pre class="actionscript" style="font-family:monospace;">protected <span style="color: #000000; font-weight: bold;">function</span> DoUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Must override&quot;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Because ActionScript 3.0 has no abstract classes or methods, you have insure that the abstract methods will not be directly instantiated, and one way to do that is to add the illegal operation line. Further to have the proper signature, the method must include some kind of <strong>return</strong> statement, and a <strong>null</strong> value does the trick. In the concrete implementation, each primitive operation is overridden and provided concrete operations.</p>
<p>Within the abstract class, you can have both concrete and abstract methods. One of the advantages of an abstract class (compared with an interface) is that abstract classes can have <em>some</em> concrete methods. For example the following template method uses two primitive operations and a concrete one. It&#8217;s just is to order the steps of the algorithm, which in this case has  three:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p5555code7'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p55557"><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="p5555code7"><pre class="actionscript" style="font-family:monospace;">...<span style="color: #006600;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span>..
&nbsp;
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> templateMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	DoLogo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	DoUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	DoInfo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
protected <span style="color: #000000; font-weight: bold;">function</span> DoUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Must override&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
protected <span style="color: #000000; font-weight: bold;">function</span> DoInfo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">TextField</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Must override&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
protected <span style="color: #000000; font-weight: bold;">function</span> DoLogo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> logo:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">return</span> logo;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
...<span style="color: #006600;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span></pre></td></tr></table></div>

<p>So let&#8217;s stop a second and consider why and how we&#8217;d use a Template Method design based on the following:</p>
<ol>
<li>An abstract class</li>
<li>A method that orders the steps in an algorithm</li>
<li>At least one abstract method that is overridden by a concrete class that implements the abstract class.</li>
</ol>
<p> The abstract class affords the programmer flexibility when similar algorithms are used again and again but with slightly different implementations. For example, you may find yourself using a &#8220;page algorithm.&#8221; The page algorithm loads your logo, creates a UI and then creates content. The logo loading part is going to be the same as you use your logo over and over again. However, the UI and content can vary in several different ways. For that, you need flexibility.</p>
<p><strong>Page Arranger</strong></p>
<p>To get started on the algorithm for a page arranger, we&#8217;ll start with the abstract class which is named <strong>IServices</strong>. One convention you will find in OOP is using an &#8220;I&#8221; prefacing all <strong>Interfaces</strong>. I like to do the same thing with abstract classes because other than the concrete properties and methods, you should think of them as a type of interface. (If you&#8217;re just getting started in OOP and Design Patterns, you might want to check out <a href="http://www.as3dp.com/2011/04/12/actionscript-3-0-oop-design-patterns-beginner-start/">Beginners Start</a> to see where the files go in Flash and Flash Builder.)</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p5555code8'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p55558"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
</pre></td><td class="code" id="p5555code8"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Abstract class. Do not instantiate directly</span>
	<span style="color: #808080; font-style: italic;">//Extend the class for concrete child classes</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> IServices <span style="color: #0066CC;">extends</span> Sprite;
	<span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> logoPix:URLRequest=<span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;logo.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> uiPix:URLRequest;
		protected <span style="color: #000000; font-weight: bold;">var</span> infoPix:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> logoLoader:Loader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> uiLoader:Loader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> infoLoader:Loader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> page:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> logoSprite:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> uiSprite:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		protected <span style="color: #000000; font-weight: bold;">var</span> infoSprite:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> templateMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			page.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>DoLogo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			page.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>DoUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			page.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>DoInfo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> page;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> DoUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Must override&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> DoInfo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Must override&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> DoLogo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			logoLoader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			logoLoader.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">0</span>;
			logoLoader.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">0</span>;
			logoLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>logoPix<span style="color: #66cc66;">&#41;</span>;
			logoSprite.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>logoLoader<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> logoSprite;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In the abstract class IServices, you can see two abstract methods (primitive operations), and two other methods—DoLogo and templateMethod. The DoLogo is going to be the same assuming that the company keeps the same logo; so you might as well set it up and let the child classes reuse it when they extend the abstract class. However, the UI and information may change, and so both of the methods for what they may include are left up to the concrete implementation.</p>
<p>Next, enter the concrete implementation of the abstract class. In the following you will see that both of the abstract methods are not implemented.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p5555code9'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p55559"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p5555code9"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteService <span style="color: #0066CC;">extends</span> IServices
	<span style="color: #66cc66;">&#123;</span>
		override protected <span style="color: #000000; font-weight: bold;">function</span> DoUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			uiPix=<span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;UI.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
			uiLoader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			uiLoader.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">0</span>;
			uiLoader.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">110</span>;
			uiLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>uiPix<span style="color: #66cc66;">&#41;</span>;
			uiSprite.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>uiLoader<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> uiSprite;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> DoInfo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			infoPix=<span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;info.swf&quot;</span><span style="color: #66cc66;">&#41;</span>;
			infoLoader=<span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			infoLoader.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">150</span>;
			infoLoader.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">110</span>;
			infoLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>infoPix<span style="color: #66cc66;">&#41;</span>;
			infoSprite.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>infoLoader<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> infoSprite;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Keeping in mind that you <em>neve</em>r implement an abstract class, but you want to program to the abstract class (think <em>interface</em>), you will need a client class to request the use of the concrete implementation. However, by declaring the requesting instantiation through the abstract class, you can keep the binding loose. So the variable <strong>tm</strong> is typed to IServices but instantiates ConcreteService.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p5555code10'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p555510"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p5555code10"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> tm:IServices=<span style="color: #000000; font-weight: bold;">new</span> ConcreteService<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			addChild<span style="color: #66cc66;">&#40;</span>tm.<span style="color: #006600;">templateMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

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

		<guid isPermaLink="false">http://www.as3dp.com/?p=4393</guid>
		<description><![CDATA[At a workshop not long ago I found myself saying, I&#8217;ve got nothing against 4th Graders. It was in relation to the MVC, which is the precursor to the Gang of Four&#8217;s catalog of patterns, but it&#8217;s appropriate in this discussion as well. Originally, I was asked what I had against MVC, and I said, [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.as3dp.com/wp-content/uploads/2011/01/evolution.png" alt="evolution" title="evolution" width="500" height="313" class="alignnone size-full wp-image-4396" /><br />
At a workshop not long ago I found myself saying,</p>
<blockquote><p>I&#8217;ve got nothing against 4th Graders.</p></blockquote>
<p>It was in relation to the MVC, which is the precursor to the Gang of Four&#8217;s catalog of patterns, but it&#8217;s appropriate in this discussion as well. Originally, I was asked what I had against MVC, and I said, <em>Nothing, and I&#8217;ve got nothing against 4th graders either</em>. The reference was to getting stuck in the MVC in a continuous loop—just like getting stuck in the 4th grade. The MVC is brilliant because it helped free programmers from monolithic conceptualizations in programming. However, it&#8217;s a starting point; not an ending point. The Pure MVC advocates certainly agree with that idea, and that&#8217;s fine, but pure or (impure?) MVC, it&#8217;s still an MVC. The GoF make the point that different patterns can be developed around what varies in an application, and the MVC is a way to deal with UIs and has much in common with the Observer pattern; which leaves some 20 other patterns that focus on different variations. (Later developments of the MVC have elevated it to an architecture.)</p>
<p>In my own case, I got stuck on the State pattern for about a year and decided that it was time to move on after realizing that other patterns would serve me better when something other than state varies. Redoing my Web site now, I wish I had used design patterns in more than the video player I made with a State pattern.</p>
<p><strong>Why OOP? What&#8217;s Wrong with Sequential Programming?</strong></p>
<p>First off, let&#8217;s get straight what we mean by sequential programming. In one context, it means <em>Not Parallel Programming or Not Concurrent Programming</em>. Just ask anyone at <strong>Intel</strong>. They think that everyone should learn concurrent programming based on the fact that their processors are all multi-core, and single core processors have gone the way of the Dodo bird. They&#8217;ve got a point. In looking at my collection of computers my old Win XT may be single core, but I&#8217;m not even sure that&#8217;s it&#8217;s not a dual core. My Macs and Win 7 (64-bit) systems are all dual or quad core. (See Joe Duffy&#8217;s book, <a href="http://xtragfx.com/ebooks/70606-concurrent-programming-on-windows.html">Concurrent Programming on Windows</a> for about 1,000 pages of info on this kind of programming.) Just to be straight about the point, that&#8217;s not the kind of sequential programming I&#8217;m talking about.</p>
<p>In this post, I&#8217;m referring to non-structured programming where you just write one line after another. When I used to write assembly language programs (especially using the assembler I wrote myself) everything was non-structured. Likewise, a lot of the Basic programming I wrote was sequential along with Fortran and non-Pascal stuff. If I had to put a date on it, I&#8217;d guess such programming is about 60-70 years old. It&#8217;s not old school; it&#8217;s just old.</p>
<p>What&#8217;s wrong with sequential programming? Is it slower? Does it not execute correctly? Are there algorithms I cannot write with it?</p>
<blockquote><p>No to all of the above.</p></blockquote>
<p> It&#8217;s main shortcomings center around the <em>spaghetti factor</em>. After not too many lines of code, it&#8217;s easy to get tangled up. If your programs are short, you&#8217;ve got little to worry about with good ole&#8217; sequential programming. Otherwise, sequential programming is difficult (near impossible) to maintain, change and keep straight.</p>
<p>However, you&#8217;d be amazed by the number of programming books that still present coding in nothing but sequential structure—which is non-structured. (In the past I&#8217;ve been guilty of the same thing; so I&#8217;ll be throwing no stones.) Some authors who have attempted to do otherwise have been snarled at&#8230;<em>What about the beginners? What about the non-developers?!</em> Grrrr! So it&#8217;s not just complacent code-writers who create programs in nothing but sequential structures; some authors do the same.</p>
<p>To be fair, a lot of the code in computer books is presented in <em>snippets</em>—little bits of sequential code to explain the syntax or to provide a working example. That&#8217;s fine and not what I&#8217;m talking about. In good books all of the bigger examples are in OOP or at least procedural programming. What I&#8217;m talking about are books where everything in the book is presented as sequential programming. One I have in mind showed a three-tiered <strong>if</strong> statement that was part of a bigger sequential monstrosity. How that mess helps beginners is beyond me. (By the way, the particular book I have in mind was published in 2009 by one of the most reputable computer book publishers in one of the most respected series.)</p>
<p>Oddly I found that by working with design patterns I end up doing simple sequential snippets—none of the messy spaghetti stuff. Read on to see why.</p>
<p><span id="more-4393"></span></p>
<p><strong>Procedural Programming to the Rescue!</strong></p>
<p>If I had to summarize procedural programming in one word, it would be <em><strong>functions</strong></em>. If I see the keyword &#8220;function&#8221; in a program, I can assume that the programmer is at least using procedural programming. The great thing about procedural programming is that you can re-use the same function all you want and it&#8217;s a whole lot easier to organize a task.</p>
<p>The problem that comes up in procedural programming centers around <em>scope</em>. Global variables can be used with many different procedures (functions), but they are more likely to creep in where they&#8217;re unwanted and screw up a program. Local variables are tethered to functions, and you may have occasion to use one of their values, but since they&#8217;re scoped to the function, they have a whole other set of issues. So while a huge improvement over sequential programming, programmers still had many challenges to face with procedural programming.</p>
<p><strong>OOP Emerges: Encapsulation</strong></p>
<p>Like Procedural Programming that can be summarized by the use of <strong>functions</strong>, Object Oriented Programming (OOP) can be summarized by <strong><em>encapsulation</em></strong>. To prevent the scoping problems and aid in debugging, the concept of the <em>object</em> emerged as a fully contained set of properties (something like variables) and methods (encapsulated functions). The objects were built in classes and a set of rules regulated communication between objects using accessors—private, protected and public. Classes represent abstractions of different entities that can be used to get something done in a program. The key to getting something done is through communication between the objects and implementing concrete instances of abstract classes. That&#8217;s an oversimplification, but clearly the focus shifts from sequences to communication.</p>
<p><strong>Design Patterns: Organized but Loose</strong></p>
<p>While OOP went a long way to create a programming model that could handle larger programs, some of the communication between objects resulted in a kind of binding that made re-use and change very difficult. In fact, some large and complex programs requiring even a little change had to be re-built from the ground up.</p>
<p>In 1979 the Model View Controller (MVC) offered a way to build interfaces for SmallTalk using the MVC triad of classes. It was key in de-coupling classes that had previously been grouped together.</p>
<p>In 1995,  Gamma, Helm, Johnson and Vlissides came out with a catalog of 23 design patterns organized around the purpose of the pattern (Creational, Structural and Behavioral) and the pattern scope (Class or Object). They offered six different criterion for selecting a pattern, key among them examining what you want to vary to avoid redesign and encapsulate what varies.</p>
<p>Professional programmers, in both industrial and higher educational settings, adopted or professed the use of design patterns. The purpose then as now is to aid in the development of software that is characterized by change and revision. In some respects they are OOP solutions to common programming structures. They are not templates as design templates in graphic design are. Rather they are broad yet precise ways to organize classes around  frameworks with a general and common programming goal.</p>
<p><strong>Do Design Patterns Add to or Reduce Complexity?</strong></p>
<p>For some, all that design patterns accomplish is to create a more complex system for both thinking about programming and creating a program. True, they do take some time to fully grasp, but so does programming. However, design patterns provide a fairly complete set of structures for dealing with programming problems. Further, once adopted, they save a great deal of time in re-structuring and making program changes.</p>
<p>Oddly, using design patterns, I find myself back writing simple sequential code. Consider the fact that classes should only take on a single responsibility and are organized around an abstract interface. I know the required signatures for the method, and I know the classes and their relationship to other classes. So what&#8217;s left for me to do? I just write snippets for the methods—little sequences of code that are fired when the method is called. Because objects are organized around a known communication system (a framework), I don&#8217;t have to worry about how they get along with the other objects. All of my requests are from a Client class, and the requests are nothing more than little sequences that instantiate a class and use the object&#8217;s methods and properties.</p>
<p>Now, I go back and look at the &#8220;easy&#8221; and &#8220;fun&#8221; book that has nothing but sequential programming (the word &#8220;function&#8221; is not even in the Index). I see a river of nested conditional statements in loops. That&#8217;s easy? Revision? Forget about it! I&#8217;m happy writing my little snippets inside well organized methods inside classes that I can easily re-use and revise.</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%2F01%2Factionscript-oop-and-design-patterns-why-bother%2F&amp;title=ActionScript%20OOP%20and%20Design%20Patterns%3A%20Why%20Bother%3F" id="wpa2a_4"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/01/actionscript-oop-and-design-patterns-why-bother/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Saturated Abstract Factory 3: What is a Single Responsibility?</title>
		<link>http://www.as3dp.com/2010/11/actionscript-3-0-saturated-abstract-factory-3-what-is-a-single-responsibility/</link>
		<comments>http://www.as3dp.com/2010/11/actionscript-3-0-saturated-abstract-factory-3-what-is-a-single-responsibility/#comments</comments>
		<pubDate>Mon, 29 Nov 2010 12:46:50 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Principles]]></category>
		<category><![CDATA[Saturated Abstract Factory]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=4058</guid>
		<description><![CDATA[Parameters of Responsibility While waiting around for someone to offer up a dynamic client for the Abstract Factory, I started thinking about the whole concept of single responsibility. This is nothing new, and we have a post on the topic, but it&#8217;s one of those things that keeps nagging at me, and I thought it&#8217;d [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_4066" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2010/11/AbFactory3.png" alt="Moving from Inheritance to Composition" title="AbFactory3" width="250" height="250" class="size-full wp-image-4066" /><p class="wp-caption-text">Moving from Inheritance to Composition</p></div><strong>Parameters of Responsibility</strong></p>
<p>While waiting around for someone to offer up a dynamic client for the Abstract Factory, I started thinking about the whole concept of single responsibility. This is nothing new, and <a href="http://www.as3dp.com/2009/05/12/design-pattern-principles-for-actionscript-30-single-responsibility-principle/">we have a post on the topic</a>, but it&#8217;s one of those things that keeps nagging at me, and I thought it&#8217;d be a good topic of discussion. What keeps buzzing around my mind is the concept of <em>single</em>. In team sports, different positions are assigned different responsibilities. One of the most unique positions is that of the goalkeeper (or goalie) who is the only member of the team who can use his hands. So I thought that it&#8217;d be a good idea to begin with the goalkeeper.</p>
<p><strong>The Duties of a Goalkeeper</strong></p>
<p><div id="attachment_4057" class="wp-caption alignright" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2010/11/singleRespon.png" alt="Classes should have only a single responsibility" title="singleRespon" width="250" height="180" class="size-full wp-image-4057" /><p class="wp-caption-text">Classes should have only a single responsibility</p></div>In the game of soccer (football or fútbol to the rest of the world) what are the responsibilities of the goalkeeper? Any fútbol fan can list several, but in the context of a soccer team, the goalkeeper has certain focal responsibilities and not others. For example, the goalkeeper is not expected to leave his post and attempt to score a goal. That&#8217;s the responsibility of the striker (forward or even midfielder), but not the goalkeeper.<br />
<span id="more-4058"></span><br />
In one sense the goalkeeper has a single responsibility, but in just about every other sense, he has several. This is what was flittering about my mind when deciding what to do with the three drawing functions from the second installment of the Saturated Abstract Factory. Since I want to get the three methods out of an inheritance bind, I pondered whether to create a single &#8220;drawing&#8221; class or set up a &#8220;drawing&#8221; interface and have three different implementations—one each for rectangles, ellipses and triangles. A possible alternative would be to have an abstract class and three child concrete child classes.</p>
<p><strong>Going with Granular</strong></p>
<p>After pondering this for a bit, I decided to set up a drawing interface and treat each drawing as a different object—rectangles, ellipses and triangles. A single interface provides a degree of looseness and cohesion at the same time. All calls from the Client can be made to the interface and the same method is used for each object. Figure 1 shows the class diagram for this segment.<br />
<div id="attachment_4076" class="wp-caption alignnone" style="width: 384px"><img src="http://www.as3dp.com/wp-content/uploads/2010/11/fig1.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; Drawing interface and concrete implementations&lt;/em&gt;" title="fig1" width="374" height="186" class="size-full wp-image-4076" /><p class="wp-caption-text"><em><strong>Figure 1:</strong> Drawing interface and concrete implementations</em></p></div></p>
<p>The interface needs enough parameters so that all of the implementations have enough parameters for each argument that Client needs to create the desired shape. All of them need a color parameter for the fill operation, and both the ellipse and rectangle need four other parameters. However, the triangle requires six parameters; so the interface will have to have a total of seven parameters—one for the color, and then six more for drawing. The following listing shows the interface:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p4058code14'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p405814"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p4058code14"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IDrawMaster
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> drawNow<span style="color: #66cc66;">&#40;</span>fill:uint,
					p1:<span style="color: #0066CC;">Number</span>,p2:<span style="color: #0066CC;">Number</span>,
					p3:<span style="color: #0066CC;">Number</span>,p4:<span style="color: #0066CC;">Number</span>,
					p5:<span style="color: #0066CC;">Number</span>,p6:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Shape;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Other than using a descriptive parameter name for the color parameters, the others are named p1-p6. Also, rather than returning a Sprite, this returns a Shape object.</p>
<p>The implementations for each of the shapes is fairly simple and involves a bit of cut and paste—more to avoid typos than to save time. The following three classes are implementations of the interface:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p4058code15'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p405815"><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
</pre></td><td class="code" id="p4058code15"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Rectangle</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BoxMaker <span style="color: #0066CC;">implements</span> IDrawMaster
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> archGraphic:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> drawNow<span style="color: #66cc66;">&#40;</span>fill:uint,
						p1:<span style="color: #0066CC;">Number</span>,p2:<span style="color: #0066CC;">Number</span>,
						p3:<span style="color: #0066CC;">Number</span>,p4:<span style="color: #0066CC;">Number</span>,
						p5:<span style="color: #0066CC;">Number</span>,p6:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
            archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>fill<span style="color: #66cc66;">&#41;</span>;
            archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,0x82877A<span style="color: #66cc66;">&#41;</span>;
            archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span>p1, p2, p3, p4<span style="color: #66cc66;">&#41;</span>;
            archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #b1b100;">return</span> archGraphic;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Ellipse</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EllipseMaker <span style="color: #0066CC;">implements</span> IDrawMaster
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> archGraphic:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> drawNow<span style="color: #66cc66;">&#40;</span>fill:uint,
						p1:<span style="color: #0066CC;">Number</span>,p2:<span style="color: #0066CC;">Number</span>,
						p3:<span style="color: #0066CC;">Number</span>,p4:<span style="color: #0066CC;">Number</span>,
						p5:<span style="color: #0066CC;">Number</span>,p6:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>fill<span style="color: #66cc66;">&#41;</span>;
			archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span>,0x7C389E<span style="color: #66cc66;">&#41;</span>;
			archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawEllipse</span><span style="color: #66cc66;">&#40;</span>p1, p2, p3, p4<span style="color: #66cc66;">&#41;</span>;
			archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">return</span> archGraphic;
		<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;">//Triangle</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TriangleMaker <span style="color: #0066CC;">implements</span> IDrawMaster
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> archGraphic:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> drawNow<span style="color: #66cc66;">&#40;</span>fill:uint,
						p1:<span style="color: #0066CC;">Number</span>,p2:<span style="color: #0066CC;">Number</span>,
						p3:<span style="color: #0066CC;">Number</span>,p4:<span style="color: #0066CC;">Number</span>,
						p5:<span style="color: #0066CC;">Number</span>,p6:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>fill<span style="color: #66cc66;">&#41;</span>;
			archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawTriangles</span><span style="color: #66cc66;">&#40;</span>Vector.<span style="color: #66cc66;">&lt;</span>number<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span> p1,p2,  p3,p4,  p5,p6<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			archGraphic.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">return</span> archGraphic;
		<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, we have a nice loose set of drawing objects and a single interface we can use to whistle up what shape we want.</p>
<p><strong>The Composer Client</strong></p>
<p>The final step in this installation is to create a client that uses the different objects. In effect, the actual composition is done in the Client. Ultimately, that&#8217;s not what we want, but for now, you can clearly see how composition is done.</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('p4058code16'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p405816"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code" id="p4058code16"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Colors: B2D1C2,389E6B,356B31,765478,7C389E</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> drawToolBox:IDrawMaster=<span style="color: #000000; font-weight: bold;">new</span> BoxMaker<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> drawToolEllipse:IDrawMaster=<span style="color: #000000; font-weight: bold;">new</span> EllipseMaker<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> drawToolTriangle:IDrawMaster=<span style="color: #000000; font-weight: bold;">new</span> TriangleMaker<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> dummy:uint=<span style="color: #cc66cc;">0</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			addChild<span style="color: #66cc66;">&#40;</span>drawToolBox.<span style="color: #006600;">drawNow</span><span style="color: #66cc66;">&#40;</span>0xB2D1C2,<span style="color: #cc66cc;">50</span>,<span style="color: #cc66cc;">50</span>,<span style="color: #cc66cc;">400</span>,<span style="color: #cc66cc;">300</span>,dummy,dummy<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>drawToolEllipse.<span style="color: #006600;">drawNow</span><span style="color: #66cc66;">&#40;</span>0x356B31,<span style="color: #cc66cc;">300</span>,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">60</span>,dummy,dummy<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>drawToolTriangle.<span style="color: #006600;">drawNow</span><span style="color: #66cc66;">&#40;</span>0x765478,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">220</span>,<span style="color: #cc66cc;">80</span>,<span style="color: #cc66cc;">200</span>,<span style="color: #cc66cc;">300</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>So now, when we look at the Client, it creates three objects, all typed as the IDrawMaster interface. Then each object&#8217;s <strong>drawNow()</strong> method is called to draw some kind of object. (By the way, the <strong>drawNow()</strong> method is a perfect example of <em>polymorphism</em>.) With both the ellipse and rectangle classes, I added a <strong>dummy</strong> variable so that it wouldn&#8217;t throw a &#8216;wrong number of arguments&#8217; error. It reduces confusion (I hope.)</p>
<p>At this stage, the coupling between the Client and the object it composes is still tighter than we want. So in the next installment, we&#8217;ll see about adding a factory to loosen things up. However, you still have a great opportunity to create a Client with an interactive interface in either Flash or Flex. What are you waiting for?! Hope to hear from 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%2F2010%2F11%2Factionscript-3-0-saturated-abstract-factory-3-what-is-a-single-responsibility%2F&amp;title=ActionScript%203.0%20Saturated%20Abstract%20Factory%203%3A%20What%20is%20a%20Single%20Responsibility%3F" 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/2010/11/actionscript-3-0-saturated-abstract-factory-3-what-is-a-single-responsibility/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Class Constructor Function Should Do No Real Work: Getting Agile Part I</title>
		<link>http://www.as3dp.com/2010/01/class-constructor-function-should-do-no-real-work-getting-agile-part-i/</link>
		<comments>http://www.as3dp.com/2010/01/class-constructor-function-should-do-no-real-work-getting-agile-part-i/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 17:22:51 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Principles]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=2322</guid>
		<description><![CDATA[A while back I read an article by Miško Hevery that was part of a Guide for Writing Testable Code. Granted the stuff that Miško writes about is for the software engineers at Google, and my best guess is that it is primarily for Java programmers. (The examples are in Java and unfortunately [for us [...]]]></description>
			<content:encoded><![CDATA[<p>A while back I read an <a href="http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/">article by Miško Hevery</a> that was part of a <a href="http://misko.hevery.com/code-reviewers-guide/">Guide for Writing Testable Code</a>. Granted the stuff that Miško writes about is for the software engineers at Google, and my best guess is that it is primarily for Java programmers. (The examples are in Java and unfortunately [for us ActionScript 3.0 programmers] many of the solutions are through Java libraries.) Nevertheless, we can learn from the larger concepts and apply them to our projects.</p>
<p><strong>Agile, Design Patterns and Unit Testing</strong></p>
<p>If the senior programmers at Google (and I would imagine Adobe, Apple, Microsoft and elsewhere) have a mantra in programming, it&#8217;s probably something like this:</p>
<blockquote><p>Keep it agile!</p></blockquote>
<p>As most of you know (or suspect), Design Patterns are a type of Agile programming, and the development of both (DPs and Agile) in the 1990&#8242;s overlap in content and developers. They were all reading each other&#8217;s articles and contributing to one another through venues like OOPSLA and ACM (Association of Computing Machines). The agility movement was in part a reaction to heavy-handed, micro-managed systems of software development processes that were par for the course up to that time. This was not a rebellious overthrow so much as a realization that in order to get things done the processes had to change. In other words, Agile programming and Design Patterns were children of necessity as much as innovation.</p>
<p><strong>The Agile Manifesto</strong></p>
<p>In 2001, a number of developers put forth what they called the <a href="http://www.AgileManifesto.org/">Agile Manifesto</a>. It is a concise list of what they have come to value—here it is:</p>
<ul>
<li><strong>Individuals and interactions</strong> over processes and tools </li>
<li><strong>Working software</strong> over comprehensive documentation</li>
<li><strong>Customer collaboration</strong> over contract negotiation </li>
<li><strong>Responding to change</strong> over following a plan</li>
</ul>
<p>The forgers of the Manefesto go on to state that while there is value in the items on the right (normal), we value the items on the left (<strong>bold</strong>) <em>more</em>.</p>
<p>For those of you expecting something dramatic or revolutionary, you might be disappointed by this tepid manifesto. (These guys aren&#8217;t Marx &#038; Engels even though they have PhotoShopped a picture to look like an impressionist painting of anarchists gathered in an ill-lighted London basement as they plan to overthrow the hoary capitalists). Nevertheless, you can see how such a value system is tied into a movement to get out from under the sludge dictated by a focus on protocols rather than results.<br />
<span id="more-2322"></span><br />
<strong>No Real Work</strong></p>
<p>As part of unit testing, Miško Hevery argues that the constructor functions should do no real work. By real work, Hevery means,</p>
<blockquote><p>When your constructor has to instantiate and initialize its collaborators, the result tends to be an inflexible and prematurely coupled design.</p></blockquote>
<p>For developing design patterns, you can see where this idea is crucial. If each participant (participating class) in a design pattern instantiates/initializes the other participants, you&#8217;d get instant lock-jaw. The flexibility and reusability that design patterns afford are lost.</p>
<p>So what should Constructor Functions look like? How on earth can we create agile or any other kind of software if we cannot use collaborating classes through instantiation? The answer to these questions can be seen in good design patterns. The patterns themselves are set up so that you do not make the mistake of having constructor functions, outside of the Client, doing real work. In Part II of this post, we&#8217;ll look at some examples. Until then, take a look at some Agile programming to loosen up.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2010%2F01%2Fclass-constructor-function-should-do-no-real-work-getting-agile-part-i%2F&amp;title=Class%20Constructor%20Function%20Should%20Do%20No%20Real%20Work%3A%20Getting%20Agile%20Part%20I" 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/2010/01/class-constructor-function-should-do-no-real-work-getting-agile-part-i/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>More OOP Principle Analogies</title>
		<link>http://www.as3dp.com/2009/10/more-oop-principle-analogies/</link>
		<comments>http://www.as3dp.com/2009/10/more-oop-principle-analogies/#comments</comments>
		<pubDate>Fri, 30 Oct 2009 09:12:16 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Principles]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1945</guid>
		<description><![CDATA[At this last OOPSLA conference I was talking with one of the European participants about remembering the different principles, and he showed me this series of images based of SOLID principles. The SOLID principles are from the acronym: S: Single responsibility principle O: Open closed principle L: Liskov substitution principle I: Interface segregation principle D: [...]]]></description>
			<content:encoded><![CDATA[<p>At this last OOPSLA conference I was talking with one of the European participants about remembering the different principles, and he showed me  <a href="http://www.lostechies.com/blogs/derickbailey/archive/2009/02/11/solid-development-principles-in-motivational-pictures.aspx"> this series of images based of SOLID principles</a>. The SOLID principles are from the acronym:</p>
<p>S: Single responsibility principle<br />
O: Open closed principle<br />
L: Liskov substitution principle<br />
I: Interface segregation principle<br />
D: Dependency inversion principle</p>
<p>My favorite is the giant Swiss Army Knife in the Single responsibility principle:</p>
<p><img src="http://www.as3dp.com/wp-content/uploads/2009/10/single.jpg" alt="single" title="single" width="500" height="400" class="alignnone size-full wp-image-1946" /></p>
<p>Now, whenever I start loading up a class with more functions than the giant Swiss Army Knife, I&#8217;m reminded to throttle back! You&#8217;ll have a lot of fun at <a href="http://www.lostechies.com/">LosTechies.com</a> with other articles on design patterns and OOP. They&#8217;re not ActionScript, but they&#8217;re informative.</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%2F10%2Fmore-oop-principle-analogies%2F&amp;title=More%20OOP%20Principle%20Analogies" 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/10/more-oop-principle-analogies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OOPSLA 2009: Hope to See Some ActionScripters!</title>
		<link>http://www.as3dp.com/2009/10/oopsla-2009-hope-to-see-some-actionscripters/</link>
		<comments>http://www.as3dp.com/2009/10/oopsla-2009-hope-to-see-some-actionscripters/#comments</comments>
		<pubDate>Fri, 23 Oct 2009 19:18:27 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Principles]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1936</guid>
		<description><![CDATA[Tomorrow morning bright and early I&#8217;m off to Orlando, Florida for the 2009 Object Oriented Programming and System Languages Association (OOPSLA) annual meetings. I&#8217;ll be making an ActionScript 3.0 presentation in a workshop on Sunday. Our workshop is named &#8220;Good Examples for Exposing Bad Practice&#8221; and meets in Pastoral 3 from 8:30-5:00 (Oct 25) Mine [...]]]></description>
			<content:encoded><![CDATA[<p>Tomorrow morning bright and early I&#8217;m off to Orlando, Florida for the 2009 Object Oriented Programming and System Languages Association (OOPSLA) annual meetings. I&#8217;ll be making an ActionScript 3.0 presentation in a workshop on Sunday. Our workshop is named &#8220;Good Examples for Exposing Bad Practice&#8221; and meets in Pastoral 3 from 8:30-5:00 (Oct 25) Mine is based on the &#8216;Wrong Way Warrior&#8221;; so it should be familiar.  On Monday, I&#8217;ll be at the Educators Symposium all day, and I would really like to meet other ActionScripters who might be at the conference</p>
<p>Here are some other speakers you might want to hear/meet:<br />
<strong>1. Miško Hevery</strong><br />
 <em>Automatic Dependency Injection In The Land Of Dynamic Languages</em></p>
<p><strong>2. Barbara Liskov</strong> (Liskov Principle)<br />
 <em>Keynote Speaker</em></p>
<p><strong>3. Ralph Johnson</strong> (GoF)<br />
 <em>Regrowing a Language: Refactoring Tools Allow Programming Languages to Evolve</em></p>
<p>Anyway, just in case any of you will be there, drop by one of the sessions. Everyone&#8217;s been very accepting of ActionScript 3.0, and it&#8217;d be fun to chat with some fellow ActionScripters!</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%2F10%2Foopsla-2009-hope-to-see-some-actionscripters%2F&amp;title=OOPSLA%202009%3A%20Hope%20to%20See%20Some%20ActionScripters%21" 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/10/oopsla-2009-hope-to-see-some-actionscripters/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Integrating design patterns &quot;just before they become useful&quot;</title>
		<link>http://www.as3dp.com/2009/09/integrating-design-patterns-just-before-they-become-useful/</link>
		<comments>http://www.as3dp.com/2009/09/integrating-design-patterns-just-before-they-become-useful/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 22:53:51 +0000</pubDate>
		<dc:creator>Chandima Cumaranatunge</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Principles]]></category>
		<category><![CDATA[Quotes]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1315</guid>
		<description><![CDATA[I recently commented on a provocative post by Jovan Sranojlovic called &#8220;Do Not Use Design Patterns Upfront&#8221; He made some thoughtful observations on why design patterns are important to software engineering and how they can a abused in that context as well. However, I disagreed with his basic thesis that design pattern should only be [...]]]></description>
			<content:encoded><![CDATA[<p>I recently commented on a provocative post by Jovan Sranojlovic called &#8220;<a href="http://blog.jovan-s.com/2009/07/29/do-not-use-design-patterns-upfront/" target="_blank">Do Not Use Design Patterns Upfront</a>&#8221; He made some thoughtful observations on why design patterns are important to software engineering and how they can a abused in that context as well. However, I disagreed with his basic thesis that design pattern should <strong>only</strong> be applied in a <em>code refactoring</em> context and not before. Based on his argument, <em>thinking in design patterns</em> upfront, based on the intent and purpose of code in not good, and even harmful. I tweeted my disagreement with a link to <a href="http://junit.sourceforge.net/doc/cookstour/cookstour.htm" target="_blank">JUnit A Cook&#8217;s Tour</a>, an example of, what I thought was, <em>thinking in design patterns</em> at the application design stage. The resulting tweet stream follows:</p>
<blockquote><p>&#8220;Do Not Use Design Patterns Upfront&#8221; <a href="http://tr.im/uI3a" target="_blank">http://tr.im/uI3a</a> &#8211; I don&#8217;t agree. Beck &amp; Gamma did just that with JUnit <a href="http://tr.im/uI3z" target="_blank">http://tr.im/uI3z</a> #asdp<br />
<em>1:09 AM Jul 30th by </em><a href="http://twitter.com/chandimac" target="_blank">@chandimac</a></p>
<p><a href="http://twitter.com/chandimac" target="_blank">@chandimac</a> no we didn&#8217;t. we explained junit with patterns, but we started with one tiny test and grew the whole thing from there.<br />
<em>6:33 AM Jul 30th by <a href="http://twitter.com/kentbeck" target="_blank">@kentbeck</a> in reply to </em><a href="http://twitter.com/chandimac" target="_blank">@chandimac</a></p>
<p><em><a href="http://twitter.com/kentbeck" target="_blank">@kentbeck</a></em> TX for the clarification. Do you recommend &#8220;thinking in patterns&#8221; early or should they be added at a later refactoring stage?<br />
<em>6:30 AM Jul 31st by </em><a href="http://twitter.com/chandimac" target="_blank">@chandimac</a><em> in reply to </em><em><a href="http://twitter.com/kentbeck" target="_blank">@kentbeck</a></em></p>
<p><a href="http://twitter.com/chandimac" target="_blank">@chandimac</a> i prefer to put patterns in place just before they become useful. if i can&#8217;t see how to do that, then soon after.<br />
<em>6:51 AM Jul 31st by </em><em><a href="http://twitter.com/kentbeck" target="_blank">@kentbeck</a></em><em> in reply to </em><a href="http://twitter.com/chandimac" target="_blank">@chandimac</a></p></blockquote>
<p><a href="http://junit.sourceforge.net/doc/cookstour/cookstour.htm" target="_blank">JUnit A Cook&#8217;s Tour</a> explains the <span>design of a system by starting with nothing and applying patterns, one after another, until you have the architecture of the system. As Kent pointed out, the article was written that way, and </span><span>I had incorrectly assumed that JUnit was developed that way as well.<br />
</span></p>
<h4>Design Pattern wisdom in 140 characters</h4>
<blockquote style="background-color: #99FF33;border: thick double #0066FF;padding: 10px;font-size: 150%;"><p>I put patterns in place just before they become useful. If I can&#8217;t see how to do that, then soon after.</p>
<p>&#8211; Kent Beck</p></blockquote>
<p>I was quite intrigued by Kent&#8217;s reply to my follow up question. It got me thinking about a bunch of issues. What does it mean for a design pattern to be &#8220;useful?&#8221; How much before is &#8220;just before?&#8221; What does it mean to &#8220;put patterns in place?&#8221;</p>
<p>I was so intrigued that I re-read a bunch of stuff on refactoring and design patterns by Beck, Gamma, Kerievsky and Alexander. I try my best to answer some of the questions raised in the remainder of this post &#8211; or more appropriately, reconcile some of my cognitive dissonance.<span id="more-1315"></span></p>
<h4>In which ways are design patterns &#8220;useful?&#8221;</h4>
<p>One of the oft suggested reasons for using design patterns is that they &#8220;make object-oriented design more flexible, elegant, and ultimately reusable.&#8221; That is a direct quote from the GoF Design Patterns book. Although elegance will bring immediate gratification, the fruits of flexibility and re-use are realized in the future, most often during subsequent refactoring. So, in the context of Kent Beck&#8217;s preference to apply patterns just before they become useful; is the only useful reward for thinking in patterns upfront, the personal satisfaction of doing something elegantly?</p>
<p>I think we get a little hung up on the flexibility and re-usability that design patterns bring to software development and forget about another aspect that is equally important.</p>
<p>I  found it helpful to go back to Christopher Alexander&#8217;s  books on architectural patterns and his argument for design patterns in architecture. Good architecture communicates both form and function. Alexander concentrated on deriving the basic functional attributes of different architectural elements. For example, in <a href="http://www.amazon.com/gp/product/0195019199?ie=UTF8&amp;tag=chandimcumara-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0195019199" target="_blank">A Pattern Language</a>, he said the following about the <em>main entrance</em>.</p>
<blockquote><p><span class="h3class">Place the main entrance of the building at a point where it can be seen immediately from the main avenues of approach and give it a bold, visible shape which stands out in front of the building.</span></p></blockquote>
<p>The main entrance can take many forms but its function is common across all main entrances. It has to unambiguously communicate to a person that it is the main entrance. The <em>position</em> of the entrance relative to avenues of approach and its <em>shape</em> compared to other entrances communicates this function. How did Alexander come to this conclusion? By studying main entrances in different buildings designed by different architects and deriving their most salient characteristics.</p>
<p><a href="http://www.flickr.com/photos/deymos/906896797/in/set-72157605867372165/" target="_blank"><img class="alignnone" src="http://farm2.static.flickr.com/1014/906896797_0b6e166549.jpg" alt="" width="200" height="133" /></a> <a href="http://www.flickr.com/photos/25695066@N00/3781968317/" target="_blank"><img class="alignleft" src="http://farm3.static.flickr.com/2650/3781968317_a0b0aefcd2.jpg" alt="" width="200" height="133" /></a></p>
<p>The architectural pattern for the main entrance does not say anything about how the entrance should be built, what materials to use, dimensions and similar minutia. All it does is communicate its functional attributes at a high level.</p>
<p>Software design patterns <em>communicate</em> intent and function at a high level of abstraction as well. This happens at a much higher conceptual level than lines of code, methods, and individual classes. Patterns communicate in the voice of the collective intelligence of thousands of expert developers who tried, tested, modified and otherwise fine-tuned code in response to a common programming problem until a generic pattern could be identified in the solution.</p>
<p>Just like architectural patterns, software design patterns communicate meaning. They not only tell you that there are relationships between classes, but provides reasons for why those relationships exist. Erich Gamma described this very well in <a href="http://www.artima.com/lejava/articles/gammadp.html" target="_blank">How to Use Design Patterns</a> in <em>Leading-Edge Java Magazine</em>. Although JUnit was developed in a test-driven way, Kent and Erich had highly abstract conversations about the design of JUnit using design patterns. Design patterns provided a &#8220;design vocabulary&#8221; to expedite development -  Gamma calls this &#8220;high-velocity design.&#8221;</p>
<p>So, design patterns provide developers with a &#8220;design vocabulary&#8221; to discuss application design. But what about the individual developer? Is the meaning conveyed by a &#8220;design vocabulary&#8221; as useful to an individual developer even if he/she doesn&#8217;t need to communicate it to someone else?</p>
<h4>Is a design vocabulary useful to <em>you</em>?</h4>
<p>Design patterns provide a set of concepts describing how and why a set of classes relate to each other, and the order in which they interact with each other. Forget about flexibility, reusability and elegance for a minute; Is the advantage of being able to visualize code at a higher conceptual level reason enough to think in terms of design patterns upfront? Do a simple test, think of a design pattern that you know and understand well. What comes to mind? Do you see code or something else?</p>
<p>ActionScript 3 developers use design patterns every time we sit down to code. How? because AS3 is rife with native implementations of design patterns. For example, the Flash <em>display list </em>implements a <em>Composite Pattern</em>.  Another example is the AS3 Event model, which follows the <em>Chain of Responsibility</em> <em>Pattern</em> by bubbling events up the display hierarchy.</p>
<p>When I think about nested movie clips, I see hierarchical trees with events shooting up and down the hierarchy like lightning bolts. I find this type of visualization incredibly useful to internalize the relationships between objects. Design patterns provide the scaffolding for me to visualize these relationships.</p>
<p>Jesse Freeman wrote a good article recently called <a href="http://www.insideria.com/2009/08/3-design-patterns-built-into-a.html" target="_blank">3 Design Patterns Built Into AS3</a>. Can you spot others?</p>
<h4>How much before is &#8220;just before?&#8221;</h4>
<p>This is an interesting question as well. If we take the literal meaning of &#8220;just before&#8221; we will miss the point. We must take &#8220;just before&#8221; in the full context of Kent&#8217;s statement. He says &#8220;just before they become useful.&#8221; So, the larger question is when do patterns become useful?</p>
<p>Design patterns allow code to be &#8220;flexible&#8221; allowing  developers to handle change elegantly. Flexible programs are no doubt useful, but are the fruits of flexibility immediately useful? We tend to think  that &#8220;change&#8221; in code as something that happens way down the road, such as the release of a new version of an application. But does &#8220;change&#8221; happen on a day-to-day basis? When you consider that most programming projects are not coded in a day, we realize that programming is a continuous exercise in &#8220;change.&#8221; You may write some code today, but tomorrow you might think that it really &#8220;stinks&#8221; and modify it or scrap it altogether and start afresh.</p>
<p>This is very well described by Kent Beck himself in a callout in the excellent book, <em><a href="https://www.amazon.com/dp/0201485672?tag=chandimcumara-20&amp;camp=213381&amp;creative=390973&amp;linkCode=as4&amp;creativeASIN=0201485672&amp;adid=0MPG8DN0YMP72QCGPE46&amp;" target="_blank">Refactoring: Improving the Design of Existing Code</a></em>.</p>
<blockquote><p>Programs have two kinds of value: what they can do for you today and what they can do for you tomorrow&#8230;.If you can get today&#8217;s work done today, but you do it in such a way that you can&#8217;t possibly get tomorrow&#8217;s work done tomorrow, then you lose.</p></blockquote>
<p>For example, you may have an inkling that the algorithm you are using today may not be optimal, and decide to integrate a strategy pattern so that it gives you the flexibility to integrate a different algorithm tomorrow without disrupting existing code.</p>
<p>I do realize that Kent&#8217;s quote was made in the context of refactoring code. Refactoring to design patterns is also refactoring. So, is putting a pattern in place today to  ease tomorrows work considered &#8220;just before it becomes useful?&#8221; What do you think?</p>
<h4>What does it mean to &#8220;put patterns in place?&#8221;</h4>
<p>The original question that prompted me to write this article was whether design patterns should be put in place only in a refactoring context or whether it is useful to think in terms of design patterns upfront as well. One argument can be that it depends on the development methodology.</p>
<p>For example, if you follow an <em>agile</em> development approach, design emerges from iterative refactoring. In most cases, development relies on <em>feedback</em> provided by tests to drive the design. In this scenario there is not much option but to put design patterns in place during refactoring.</p>
<p>On the other hand, if you follow a more <em>prescriptive</em> approach ( for want of a better term ), there is an emphasis on <em>planning</em> and <em>modeling </em>before coding. In this scenario, there is an opportunity to put patterns in place up front.</p>
<p>One could also argue that it really doesn&#8217;t matter because in most projects we use a combination of both development methods.</p>
<p>Ultimately my driving question will probably transform into an argument between the merits of different development methodologies. There is no doubt as to the &#8220;usefulness&#8221; of design patterns and it may not matter whether you put them into place up front or during refactoring &#8211; either way, you will get the benefits.</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%2F09%2Fintegrating-design-patterns-just-before-they-become-useful%2F&amp;title=Integrating%20design%20patterns%20%26quot%3Bjust%20before%20they%20become%20useful%26quot%3B" 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/09/integrating-design-patterns-just-before-they-become-useful/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Tight Coding and Loose Coupling</title>
		<link>http://www.as3dp.com/2009/08/tight-coding-and-loose-coupling/</link>
		<comments>http://www.as3dp.com/2009/08/tight-coding-and-loose-coupling/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 15:43:20 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Principles]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1334</guid>
		<description><![CDATA[Tight Code and Tighter Programmers The other day I was thinking about programs with tight code and loose coupling. Having been raised on tight coding I started looking around for a decent definition of tight code and came across one of my programming heroes, Charles H. Moore, inventor of FORTH programming language. For Moore, as [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.as3dp.com/wp-content/uploads/2009/08/tightprogrammer.png" alt="tightprogrammer" title="tightprogrammer" width="224" height="350" class="alignleft size-full wp-image-1335" /><strong>Tight Code and Tighter Programmers</strong></p>
<p>The other day I was thinking about programs with <em>tight code</em> and <em>loose coupling</em>. Having been raised on <strong>tight coding</strong> I started looking around for a decent definition of tight code and came across one of my programming heroes, Charles H. Moore,  inventor of FORTH programming language. For Moore, as well as others discussing tight code, the term refers to</p>
<blockquote><p>… a program that is written very efficiently. </p></blockquote>
<p>That doesn’t say a whole lot, but it says everything. In FORTH, not a single electron is wasted in the code, but not a lot of people use it because you have to think like a processing stack—last in first off. In other words, you have to think exactly backwards from normal sequencing.  Ergo, most programmers have migrated to a high level languages with a C++ type of format such as ECMAScript and ActionScript 3.0. To some extent, <em>efficient code</em> is code without superfluous elements that gets a job done. (That’s why I don’t like to use conditional statements where I can help it—there’s generally a more direct option.)</p>
<p>In the context of OOP, I think that <em>tight code</em> can be equated with <em>tightly woven objects</em>. Such objects are good at what they do and nothing else. We can apply the single responsibility rule:<br />
<blockquote> A class should only have a single responsibility.</p></blockquote>
<p> However, they can work with other objects.<br />
<span id="more-1334"></span><br />
Where we run into trouble with <em>tightness</em> in programming is with <strong>Tight Programmers</strong>. Their structures are so tight that their objects cannot interact with other objects. Updates? Forget about it! They don’t want updates. In fact if a customer wants an update she’ll be told that she should have thought about that before he wrote all of the code. Is the code tight? Sure, it’s tight. Unfortunately, it is also inflexible. We need to differentiate between <em>tight coding</em> and <em>inflexible coding</em>. Tight coding is lean and purposeful. Inflexible coding resists change and updates. Design Patterns embrace loose coupling and tight coding.</p>
<p><strong>Loose Coupling and Loose Practices</strong></p>
<p><img src="http://www.as3dp.com/wp-content/uploads/2009/08/looseprogrammer.png" alt="looseprogrammer" title="looseprogrammer" width="224" height="350" class="alignright size-full wp-image-1336" /></p>
<p>When we used to throw a program together, we called it  <em>strong-arming</em>. We basically threw code at a problem until it did what we wanted. Good practices? What practices? We just beat the crap out of it until something happened. That kind of programming is what some consider (politely) as <em>loose coding</em>. In some respects, it’s like <em>programming by committee</em>—anything that works is included in the program. However, the committee is a committee of one—the programmer.<br />
<blockquote>You want change? Hand me that baseball bat and let me fix it right up!</p></blockquote>
<p>Oddly, loose coupling in programming requires more structure than tight coupling. With tight coupling, you can wrap up a program in a single class that has more lines of code than the national debt. With loose coupling you need to have a well-planned structure so that you can easily know what classes to add and what kind of interface will accomplish a particular task or family of tasks.</p>
<p><strong>The OOP and Design Pattern Programmer has a Split Personality</strong></p>
<p>To be a good programmer in OOP and design patterns, I think you need to have two very different sides of the tight and loose programmer. On the one hand, you need to know your code to write clear, efficient, and effective algorithms. On the other hand, you need to know how to write code that interacts with other code but is not made up of that code.</p>
<p>Perhaps one of the most important elements of Class Diagrams is the set of lines that show the nature of the <em>relationships between classes</em>. These lines represent code within one or both classes that allow them to work together to accomplish a task. More importantly, though, they represent the ability of the structure to encompass change and update without having to rebuild the application. This is accomplished with <em>tight coding</em> of objects and <em>loose coupling</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%2F08%2Ftight-coding-and-loose-coupling%2F&amp;title=Tight%20Coding%20and%20Loose%20Coupling" 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/08/tight-coding-and-loose-coupling/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Wrong Way Warrior: Getting Flexibility with Design Patterns—Part II</title>
		<link>http://www.as3dp.com/2009/08/wrong-way-warrior-getting-flexibility-with-design-patterns-part-ii/</link>
		<comments>http://www.as3dp.com/2009/08/wrong-way-warrior-getting-flexibility-with-design-patterns-part-ii/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 21:41:53 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[Principles]]></category>
		<category><![CDATA[Strategy Pattern]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1304</guid>
		<description><![CDATA[Gentle Reader: This is the second part of a two-part set of posts. For this one to be useful, please take a look at Part I. Also, I’m not an expert on military operations or organizations; so if there’s any error in a basic infantry platoon, feel free to correct me. I am aware, however, [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Gentle Reader:</strong> This is the second part of a two-part set of posts. For this one to be useful, please take a look at <a href="http://www.as3dp.com/2009/08/15/the-wrong-way-warrior-where-oop-alone-is-not-enough—part-i/">Part I</a>. Also, I’m not an expert on military operations or organizations; so if there’s any error in a basic infantry platoon, feel free to correct me. I am aware, however, of the 7-1 ratio of Service to Combat units in the modern military, and that this is only a simple component of a far more sophisticated structure—that’s why I selected it!</em></p>
<p>In the first installment of the <em>Wrong Way Warrior</em>, we saw how an OOP developer put together a simple proof-of-concept using what he thought was a prudent approach to a battle simulation. He’d provide the Warrior with certain characteristics and then subclass those characteristics to concrete warriors that would share the capabilities of the parent class. In addition, the concrete warriors would be given a movie clip representation of the warrior.</p>
<p>After the first design was sent to the customer, the response was less than favorable. It was described as “a children’s game” at best. The military advisor described it as a <em>caveman battle plan</em> where all of the combatants are similarly armed with a club to attack adversaries. The problem was that it was bound to a fairly static design, and it would be impossible to be used for a simulation that had more complex behaviors. However, the other submissions were not much better, and so the customer provided a simple organization within the military to simulate—the basic infantry platoon. After all, they’re paying your company $1.5 million to develop the simulation. (This was news to the developer!) Figure 1 shows the organization in terms of a new set of movie clips:<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/08/platoon.png" alt="platoon" title="platoon" width="496" height="599" class="alignnone size-full wp-image-1305" /><br />
<em><strong>Figure 1:</strong> Movie clip representation of platoon</em><br />
<span id="more-1304"></span><br />
As you can see, far from being a herd of cavemen with clubs, each of the members of the platoon has a specific role. A lieutenant with the aid of an experienced platoon sergeant runs the show. A radio operator keeps in contact with the higher-level organization (company) and other assets than can be brought to bear (e.g. artillery and air support). Broken down into four squads, 3 rifle squads and a weapons squad, the organization is fairly generic of those in the US Army and is instructive for some more complex (and realistic) applications in Flex and Flash.</p>
<p><strong>What Varies?</strong></p>
<p>To get started, it helps to have some kind of perspective on the problem. If we look at the basic military hierarchy, we can get an idea:<br />
<nl></p>
<li>Platoon (Smallest) </li>
<li>Company (3-5 platoons) </li>
<li>Battalion (4-6 companies) </li>
<li>Brigade (2-5 battalions) </li>
<li>Division (3 brigades) </li>
<li>Corps (2-5 divisions) </li>
<li>Army (2 or more corps) </li>
<p></nl></p>
<p>Can the simulation be expanded even to company size? The sample platoon is set up for 38 soldiers, and the Company for between 62-190, depending on specialized assignments. These are only the combat units and do not include such service and support units as medics, transport, military police and other service units.</p>
<p>In looking at Figure 1, we can immediately see that roles vary. Put another way, <em>their behaviors vary</em>. Initially, a single Warrior class was used and subclasses of Red and Blue classes made up two opposing sides.  However, other than the appearance of their movie clip icons, they were identical. If we added more and more functions, we’d have to override virtually ever other method. So, we need another way.</p>
<p>For this round of development, we need to think about one of the first principles of design patterns,</p>
<blockquote><p>
Separate what varies from what stays the same and encapsulate what varies.
</p></blockquote>
<p>Since we’ve identified that behaviors vary because of the different roles (<em>not a bunch of undifferentiated cavemen with clubs</em>), we’ll start with the weapons and firing behavior. We’ll put the behaviors into an interface and set up implementations of the different weapons. At the same time, we’ll set up the Warrior class so that they can use the different weapons through <em>composition</em>. That is, each instance of each subclass will <em>have a</em> different weapon to use instead of the weapon and its related behavior as an integral part of the particular Warrior class.</p>
<p><strong>Sound On!</strong></p>
<p>To see where all of this is leading <strong>turn up your sound</strong> and click the Play button. You will see different organizational options when you press the buttons along the top. When you click on the individual “soldiers”, they will play a sound representing their weapons. In the case of the officer and NCO’s, the entire unit’s weapons fire simultaneously representing commanded fire. Because the anti-armor and machine gun units require two people to use the weapons, clicking either of the two sprites serve to fire the weapon. After you’ve had a look, download the files—only selected few were placed in this post.<br />
<a href="http://www.sandlight.net/dpBlog/dpwarrior/"><br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignnone size-full wp-image-1257" /></a><br />
<a href="http://www.sandlight.net/dpBlog/dpwarrior/PatternWarrior.zip"><br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/07/download.png" alt="download" title="download" width="99" height="47" class="alignnone size-full wp-image-1153" /></a></p>
<p>To kick things off we’ll be looking at a very simplified Warrior class and an interface for firing weapons shown in the following code:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1304code21'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p130421"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code" id="p1304code21"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Warrior abstract class</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//Abstract class. Do not instantiate</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Warrior <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> fire:Fire;
		<span style="color: #808080; font-style: italic;">//protected var movement:Movement;</span>
		<span style="color: #808080; font-style: italic;">//protected var comm:Communicate;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Encapsulating behaviors</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">interface</span> Fire
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> fireWeapon<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Those two elements should be viewed as <em>structuring elements</em> that help guide the development of the program. The Warrior abstract class includes a reference to the Fire interface and the set of behaviors that will be derived from implementations of that interface. The concrete Warrior classes will <em>not</em> derive their behaviors from the parent class but from the aggregate relationship between the Warrior interface (abstract class) and the Fire interface. (You can see two commented out variables, <em>movement</em> and <em>comm</em> that I’ll be referring to in a later post, but they can be ignored for the rest of this post.)</p>
<p><strong>Different Characters and Behaviors</strong></p>
<p>If you’ve ever been admonished to differentiate between a <em>bad action</em> and a <em>bad person</em> you’ll understand what’s going on here. The <em>person</em> belongs to the <strong>Warrior</strong> class and the <em>action</em> belongs to the <strong>Fire</strong> interface. They have the relationship shown in Figure 2:<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/08/warriordesign.png" alt="warriordesign" title="warriordesign" width="422" height="161" class="alignnone size-full wp-image-1306" /></p>
<p><em><strong>Figure 2:</strong> Class diagram of Warrior and Fire </em></p>
<p>As you can see in Figure 2, all of the algorithms for weapons actions are encapsulated in the Fire implementations. The Warrior subclass objects get their behaviors through the Warrior interface through the Fire interface.</p>
<p><strong>Marrying the Warrior to the Action</strong></p>
<p>In bringing together the behavior and character, you’ll be glad to know there’s no <em>bridezilla</em> in the coupling. In fact, to quote another fundamental OOP principle,</p>
<blockquote><p>
Objects should be loosely coupled.
</p></blockquote>
<p>So even better, by having loosely coupled objects, when something changes, there won’t be a collapse in the entire system just in case a particular coupling doesn’t work as planned.</p>
<p>In Part I of <em>The Wrong Way Warrior</em>, the developer used movement with buttons to illustrate how each instance inherited the ability to move on the screen. This time around, the developer is demonstrating how the different types of soldiers carry out a behavior—firing a weapon—that is composed into his object through aggregation. So, instead of shooting up the screen, this time the developer decided to use sound effects of the different weapons. Also, to fire the weapons, instead of having another set of buttons, clicking on individual concrete Warrior objects launches the appropriate sound effects.</p>
<p>As noted above, the concrete Warrior objects are coupled with a Fire implementation, and as you will see, they’re very similar. In that respect they’re interchangeable; and that’s just what we want. Starting off, the following listings show the Rifleman (<strong>RifleMn</strong>) character and the <strong>FireRifle </strong>behavior.</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('p1304code22'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p130422"><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
</pre></td><td class="code" id="p1304code22"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Creates concrete Warrior instance</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;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RifleMn <span style="color: #0066CC;">extends</span> Warrior
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Rifleman is movie clip in Library</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> rm1:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Rifleman<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> RifleMn<span style="color: #66cc66;">&#40;</span>fx:uint,fy:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			rm1.<span style="color: #006600;">x</span> = fx,rm1.<span style="color: #006600;">y</span> = fy;
			rm1.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, fireNow<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rm1<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> fireNow<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>
			fire = <span style="color: #000000; font-weight: bold;">new</span> FireRifle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Implements Fire behavior</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FireRifle <span style="color: #0066CC;">implements</span> Fire
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">media</span>.<span style="color: #0066CC;">Sound</span>;
		<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">media</span>.<span style="color: #006600;">SoundChannel</span>;
		<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;WeaponSounds/M16.mp3&quot;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> weapon:SoundChannel;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> fireWeapon<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: #000000; font-weight: bold;">var</span> urlReq:URLRequest = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> gun:<span style="color: #0066CC;">Sound</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Sound</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			gun.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>urlReq<span style="color: #66cc66;">&#41;</span>;
			weapon = gun.<span style="color: #0066CC;">play</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Firing M16--1 shot burst&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 concrete Warrior selects a movie clip that represents its character first. Typed as a Sprite, it is used as a clickable event generator. In this case, the MovieClip object is named, <strong>Rifleman</strong>. The Rifleman instance is event-linked to a function <strong>fireNow()</strong> which then uses the <strong>fire</strong> object <em>inherited</em> from the parent class—Warrior. Using the <strong>fire</strong> object, the program instantiates the particular implementation of the behavior. In this case, it wants an instance of <strong>FireRifle</strong>. Keep in mind that <strong>fire</strong> inherited from Warrior is typed as a Fire object—only committing to the interface. This allows any implementation of the interface and its methods.</p>
<p>In looking at the Fire implementation of <strong>FireRifle</strong>, it basically sets up the sound file that is played. However, it only represents <em>any</em> algorithm that could be placed there instead. For example, it could be used in an algorithm to measure the expenditure of force against a target and keep track of the amount of ammunition a concrete Warrior instance expends and has remaining.</p>
<p>When you download and look at the different classes, you’ll see they’re pretty similar. However, some characters, like the Lieutenant, have a much more robust use of the Fire implementations as you can see in the following listing:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1304code23'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p130423"><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
</pre></td><td class="code" id="p1304code23"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Lieutenant <span style="color: #0066CC;">extends</span> Warrior
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Lieut is movie clip in Library</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> lt1:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Lieut<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> Lieutenant<span style="color: #66cc66;">&#40;</span>fx:uint,fy:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			lt1.<span style="color: #006600;">x</span> = fx,lt1.<span style="color: #006600;">y</span> = fy;
			lt1.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, fireNow<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>lt1<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> fireNow<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//Fire Personal Weapon</span>
			fire = <span style="color: #000000; font-weight: bold;">new</span> FireRifle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//PlatoonFire</span>
			squadFire<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			weaponsSquadFire<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			squadFire<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			squadFire<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> squadFire<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//Fire Team 1</span>
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire = <span style="color: #000000; font-weight: bold;">new</span> FireSAW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire = <span style="color: #000000; font-weight: bold;">new</span> FireGrenade<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//Fire Team 2</span>
			fire = <span style="color: #000000; font-weight: bold;">new</span> FireRifle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire = <span style="color: #000000; font-weight: bold;">new</span> FireSAW<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire = <span style="color: #000000; font-weight: bold;">new</span> FireGrenade<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> weaponsSquadFire<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			fire=<span style="color: #000000; font-weight: bold;">new</span> FireMG<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire=<span style="color: #000000; font-weight: bold;">new</span> FireRocket<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			fire.<span style="color: #006600;">fireWeapon</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, the Lieutenant fired every weapon in the platoon, as did the Platoon Sergeant. This illustrates how flexible this design is. Because the behaviors are separated from the objects that use them, you can put them in any combination you want.</p>
<p><strong>The Client</strong></p>
<p>If you look at the Client carefully, you’ll see no reference at all to the Fire operations. That’s because the concrete Warrior objects all access Fire behaviors themselves. There’s no reason (nor would you want one!) to implement a concrete Fire object from the Client directly. Client constructor creates buttons used to display different arrangements of the troops. The bigger problem was getting rid of one configuration when another had been placed on the stage. Keith Peters provided a handy algorithm for clearing all children, and so I put it in the piedPiper() function that clears everything—including the buttons! So the piedPiper() method replaces the buttons after blowing them and the rest of the children off the stage.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1304code24'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p130424"><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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
</pre></td><td class="code" id="p1304code24"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">Button</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> rocketBtn:<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> mgBtn:<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> ftBtn:<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> sqBtn:<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> hqBtn:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> lieut:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> platoonSgt:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> radio:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sqLdr:Warrior;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> tmLdr:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sawMan:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grenMan:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> rifMan:Warrior;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> tmLdr2:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sawMan2:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> grenMan2:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> rifMan2:Warrior;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> mgunner:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> asstMgunner:Warrior;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> rocket:Warrior;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> asstRocket:Warrior;
&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>
			ftBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, fireTeam<span style="color: #66cc66;">&#41;</span>;
			ftBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;FireTeam&quot;</span>;
			ftBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">10</span>,ftBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">10</span>;
			addChild<span style="color: #66cc66;">&#40;</span>ftBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			sqBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, squad<span style="color: #66cc66;">&#41;</span>;
			sqBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Squad&quot;</span>;
			sqBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">120</span>,sqBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">10</span>;
			addChild<span style="color: #66cc66;">&#40;</span>sqBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			mgBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, mgCrew<span style="color: #66cc66;">&#41;</span>;
			mgBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Machine Gun&quot;</span>;
			mgBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">230</span>,mgBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">10</span>;
			addChild<span style="color: #66cc66;">&#40;</span>mgBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			rocketBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, rocketCrew<span style="color: #66cc66;">&#41;</span>;
			rocketBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Anti Armor&quot;</span>;
			rocketBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">340</span>,rocketBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">10</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rocketBtn<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			hqBtn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, hq<span style="color: #66cc66;">&#41;</span>;
			hqBtn.<span style="color: #006600;">label</span> = <span style="color: #ff0000;">&quot;Headquarters&quot;</span>;
			hqBtn.<span style="color: #006600;">x</span> = <span style="color: #cc66cc;">450</span>,hqBtn.<span style="color: #006600;">y</span> = <span style="color: #cc66cc;">10</span>;
			addChild<span style="color: #66cc66;">&#40;</span>hqBtn<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> fireTeam<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>
			piedPiper<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			tmLdr = <span style="color: #000000; font-weight: bold;">new</span> TeamLdr<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">300</span>,<span style="color: #cc66cc;">424</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>tmLdr<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			rifMan = <span style="color: #000000; font-weight: bold;">new</span> RifleMn<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">428</span>,<span style="color: #cc66cc;">338</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rifMan<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			sawMan = <span style="color: #000000; font-weight: bold;">new</span> SAW<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">480</span>,<span style="color: #cc66cc;">424</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>sawMan<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			grenMan = <span style="color: #000000; font-weight: bold;">new</span> Grenadier<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">428</span>,<span style="color: #cc66cc;">512</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>grenMan<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> squad<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>
			piedPiper<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			sqLdr = <span style="color: #000000; font-weight: bold;">new</span> SquadLdr<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">82</span>,<span style="color: #cc66cc;">300</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>sqLdr<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			tmLdr = <span style="color: #000000; font-weight: bold;">new</span> TeamLdr<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">300</span>,<span style="color: #cc66cc;">424</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>tmLdr<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			rifMan = <span style="color: #000000; font-weight: bold;">new</span> RifleMn<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">428</span>,<span style="color: #cc66cc;">338</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rifMan<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			sawMan = <span style="color: #000000; font-weight: bold;">new</span> SAW<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">480</span>,<span style="color: #cc66cc;">424</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>sawMan<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			grenMan = <span style="color: #000000; font-weight: bold;">new</span> Grenadier<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">428</span>,<span style="color: #cc66cc;">512</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>grenMan<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			tmLdr2 = <span style="color: #000000; font-weight: bold;">new</span> TeamLdr<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">420</span>,<span style="color: #cc66cc;">162</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>tmLdr2<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			rifMan2 = <span style="color: #000000; font-weight: bold;">new</span> RifleMn<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">548</span>,<span style="color: #cc66cc;">76</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rifMan2<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			sawMan2 = <span style="color: #000000; font-weight: bold;">new</span> SAW<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">600</span>,<span style="color: #cc66cc;">162</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>sawMan2<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			grenMan2 = <span style="color: #000000; font-weight: bold;">new</span> Grenadier<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">548</span>,<span style="color: #cc66cc;">250</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>grenMan2<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> mgCrew<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>
			mgunner = <span style="color: #000000; font-weight: bold;">new</span> MG<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">520</span>,<span style="color: #cc66cc;">500</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>mgunner<span style="color: #66cc66;">&#41;</span>;
			asstMgunner = <span style="color: #000000; font-weight: bold;">new</span> AssistantMG<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">550</span>,<span style="color: #cc66cc;">570</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>asstMgunner<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> rocketCrew<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>
			rocket = <span style="color: #000000; font-weight: bold;">new</span> RocketMan<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">650</span>,<span style="color: #cc66cc;">250</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rocket<span style="color: #66cc66;">&#41;</span>;
			asstRocket = <span style="color: #000000; font-weight: bold;">new</span> AssistRocket<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">700</span>,<span style="color: #cc66cc;">300</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>asstRocket<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> hq<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>
			lieut = <span style="color: #000000; font-weight: bold;">new</span> Lieutenant<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">132</span>,<span style="color: #cc66cc;">98</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>lieut<span style="color: #66cc66;">&#41;</span>;
			platoonSgt = <span style="color: #000000; font-weight: bold;">new</span> PltSgt<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">66</span>,<span style="color: #cc66cc;">188</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>platoonSgt<span style="color: #66cc66;">&#41;</span>;
			radio = <span style="color: #000000; font-weight: bold;">new</span> RadioMan<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">194</span>,<span style="color: #cc66cc;">188</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>radio<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> clearFireTeam<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			removeChild<span style="color: #66cc66;">&#40;</span>tmLdr<span style="color: #66cc66;">&#41;</span>;
			removeChild<span style="color: #66cc66;">&#40;</span>sawMan<span style="color: #66cc66;">&#41;</span>;
			removeChild<span style="color: #66cc66;">&#40;</span>grenMan<span style="color: #66cc66;">&#41;</span>;
			removeChild<span style="color: #66cc66;">&#40;</span>rifMan<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> clearSquad<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			clearFireTeam<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			removeChild<span style="color: #66cc66;">&#40;</span>sqLdr<span style="color: #66cc66;">&#41;</span>;
			removeChild<span style="color: #66cc66;">&#40;</span>tmLdr2<span style="color: #66cc66;">&#41;</span>;
			removeChild<span style="color: #66cc66;">&#40;</span>sawMan2<span style="color: #66cc66;">&#41;</span>;
			removeChild<span style="color: #66cc66;">&#40;</span>grenMan2<span style="color: #66cc66;">&#41;</span>;
			removeChild<span style="color: #66cc66;">&#40;</span>rifMan2<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> piedPiper<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>numChildren <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				removeChildAt<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			addChild<span style="color: #66cc66;">&#40;</span>ftBtn<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>sqBtn<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>mgBtn<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>rocketBtn<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>hqBtn<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>If you run the program from within the Flash/Flex IDE, you should be able to see trace statements in the Output window as well to tell you what’s going on—in addition to the sound effects.</p>
<p><strong>The Next Steps</strong></p>
<p>I doubt there’s anyone who’s read this blog who doesn’t recognize the design pattern in use; so I won’t go into that. However, this project is yet to be completed. That will come in future posts. Now that we’re on the right track, we (that means you as well) can envision what other behaviors—or class of behaviors and their accompanying algorithms—could be added to the current set.</p>
<p>If you’re interested in game development, I hope that this little demonstration has shown the practicality of using design patterns and how easy they can make it. This is especially true as your projects become more complex. You might also want to review Chapter 13 of our book where we cover the <strong>Symmetric Proxy Pattern</strong>. There you will find a solution to the problem of simultaneous movement and multi-person play over the Internet using Flash Media Server. Setting up a platoon or even squad level game using simultaneous movement would be a lot of fun, and you could use several of the design patterns we discussed on this blog and in our book.</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%2F08%2Fwrong-way-warrior-getting-flexibility-with-design-patterns-part-ii%2F&amp;title=Wrong%20Way%20Warrior%3A%20Getting%20Flexibility%20with%20Design%20Patterns%E2%80%94Part%20II" 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/08/wrong-way-warrior-getting-flexibility-with-design-patterns-part-ii/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Design Patterns/OOP/Principles and Algorithms: The Forest and Trees of Programming</title>
		<link>http://www.as3dp.com/2009/07/actionscript-30-design-patternsoopprinciples-and-algorithms-the-forest-and-trees-of-programming/</link>
		<comments>http://www.as3dp.com/2009/07/actionscript-30-design-patternsoopprinciples-and-algorithms-the-forest-and-trees-of-programming/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 09:56:15 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Principles]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1255</guid>
		<description><![CDATA[I’ve been working on documentation, and I began thinking about the concepts of design patterns/OOP/principles and algorithms as the forest and trees, respectively. The documentation involves Server Side ActionScript (SSAS, which is ActionScript 1.0) and good old ActionScript 3.0 on the client side. (In this context, client refers to the client making requests from the [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve been working on documentation, and I began thinking about the concepts of <em>design patterns/OOP/principles</em> and <em>algorithms</em> as the <em>forest</em> and <em>trees</em>, respectively. The documentation involves Server Side ActionScript (SSAS, which is ActionScript 1.0) and good old ActionScript 3.0 on the client side. (In this context, <em>client</em> refers to the client making requests from the server, and not <em>Client</em> as a class that makes requests from other classes in a design pattern.)</p>
<p>In previous posts I’ve admitted to being an algorithm junkie, and for the last several days that’s exactly the habit I’ve been feeding. Since I was documenting SSAS—ActionScript 1.0— which has no user classes or typed data, I can say with a straight face that I really didn’t have a lot of choice. However, the client side work with AS 3.0 could have been more OOP-like and maybe a little Strategy pattern could have been used just to get everyone off on the right foot.<br />
<span id="more-1255"></span><br />
Let’s face it, though, if someone is trying to learn how to work with a class, no matter what version of ActionScript, they don’t need it to be couched in a design pattern or OOP. I’ll often see some convoluted program that the documenter hopes to show off his/her programming acumen while explaining how to use a particular class with its properties, methods and events. Eventually, the long-suffering reader may finally (if ever) get what the documentation is trying to get across, but an easier route would have been to create a straightforward example the user can <em>use</em>. The developer is either going to use it in an OOP/DP context or not regardless of an example’s structure.</p>
<p><strong>Clear and Simple are not the Same</strong></p>
<p>In working with the documentation, I created a big fat string that contained data from a Web service and was sent from the server to a callback function on the client side. Once the client got the data, it had to be taken apart and broken into elements to be placed into an array. Since I was making the example for documentation, I wanted it to be fairly simple. The Web service delivered data in the form of a series of strings, and these small strings were concatenated into a single large string. The concatenated string kept the chunks of information from the Web service separated with tildes (~). The server-side program sent the concatenated string to the client. So all I had to do was to split up the string into array elements and run out the data to a text object using the tile (~) to re-establish the original categories on the client. I used the following ActionScript 3.0 method:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1255code26'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p125526"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p1255code26"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//The webser parameter is a concatenated string from</span>
<span style="color: #808080; font-style: italic;">//Web service sent from Flash Media Interactive Server</span>
&nbsp;
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> serviceReport<span style="color: #66cc66;">&#40;</span>webser:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> serviceArray:<span style="color: #0066CC;">Array</span>=webser.<span style="color: #0066CC;">split</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">/</span>~<span style="color: #66cc66;">/</span><span style="color: #66cc66;">&#41;</span>;
	txtArea.<span style="color: #0066CC;">text</span> = <span style="color: #ff0000;">&quot;&quot;</span>;
	<span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> serInfo <span style="color: #b1b100;">in</span> serviceArray<span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		txtArea.<span style="color: #006600;">appendText</span><span style="color: #66cc66;">&#40;</span>serviceArray<span style="color: #66cc66;">&#91;</span>serInfo<span style="color: #66cc66;">&#93;</span>+<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In and of itself, the method and accompanying algorithm are no great shakes, but since I don’t spend a lot of time breaking down big strings into array elements, I had to re-learn how to do it. Obviously I could have <em>strong-armed</em> it by iterating through the string and pushing chunks of it onto the array. That would have gotten it done, but I remember when I whined loudly to Adobe to include Regular Expressions in ActionScript. Well, they did and it wouldn’t be right of me to not use them. (Perl programmers write symphonies in Regular Expressions, but a lot of us mere mortals have to re-educate ourselves in the damned things every time we use them.) Then I dug up the <strong>split</strong> method which I had “remembered” as having something to do with arrays; however, it turns out the method is actually a string method to break up strings into array elements. What’s more, they use Regular Expressions in their parameters; so all I had to do was to check out <strong>String.split()</strong>in the String class and use a Regular Expression to crunch the big string into array elements.</p>
<p>For some reason, the person writing the documentation decided to show off some slick tricks using Regular Expressions. None of the “tricks” did anything other than generate confusion because the results of the Regular Expressions <em>did absolutely nothing of use</em>. So, I decided on a hunch to use the regular expression, “/~/” as the <strong>split()</strong> parameter, and it worked fine—no thanks to the documentation.</p>
<p>My intention was not to create an example of how to turn a string into array elements but rather how to use the SSAS WebService class in ActionScript 1.0 to get information from a web service and display it in a client-side object. Passing data from an ActionScript 1.0 object to an ActionScript 3.0 object involves a series of steps that must be handled in a certain way, and sometimes these steps are a little tricky—especially when using web services from remote servers and then passing them through an open socket server such as Flash Media Interactive Server and then back to a display object generated by Flex or Flash.</p>
<p>In explaining the process if it is so simple that the developer cannot use it in a program, it’s useless.<br />
<blockquote>If I describe a nail and show a picture of a nail I’ve provided a clear and simple idea of a nail, but I have not explained carpentry.</p></blockquote>
<p> This is what I mean when I say that sometimes <em>simple is not the same as clear</em>. Likewise, a pithy set of examples can be as confusing as something long and convoluted—<a href= "http://www.anl.gov/Careers/Education/rube/rubenapkin.html" > check out Rube Goldberg</a> to visualize convolution. (Oddly, Goldberg’s creations have a clean water clarity from the starting point to the end result—convoluted as they may be.)</p>
<p>The examples of working with class procedures are the <em>trees</em> of programming in that they are algorithms encapsulated in methods. However, the procedures for these algorithms sound more like forests—or even dense jungles—in some cases. With the busy programmer or student of programming (which to some extent we all are and will continue to be), how do we ever get to OOP or Design Patterns when dealing with some fairly complex algorithms in the first place?</p>
<p><strong>Levels of Abstractions</strong></p>
<p>One image of separating levels of abstractions is with a single head of cattle and a herd. Further arranging the cattle, we can imagine the cattle organized in corrals. From space, the corrals look like specks, and several corrals organized into a ranch may appear as a dot. Figure 1 gives you an idea—the picture from space shows part of Argentina’s cattle country, and some of those pixels <em>might be</em> corrals or ranches but the dark areas are lakes.</p>
<p><img src="http://www.as3dp.com/wp-content/uploads/2009/07/levels.png" alt="levels" title="levels" width="507" height="130" class="alignnone size-full wp-image-1256" /></p>
<p><em><strong>Figure 1:</strong> All Levels of Abstractions have Concrete Elements</em></p>
<p>A single cow (named <em>Evita Guevara</em>) is different from all other cows in the herd, but she is part of the herd. We can differentiate Herd A from Herd B by size, color, location or any other criteria we want. However, Cow A from Herd A may also sometimes hang out with Herd B; so Cow A in Herd A is identical to Cow A in Herd B. Or is it? When Cow A is in Herd B we can say that the character of Herd B changes but so too does Cow A because of a different Herd? Of course we can. In the same way that the same algorithm can be used in a variety of different classes, classes within a design pattern or a design patterns within a framework. Because of the context it’s in, it is transformed, but at the same time it transforms and defines the context.</p>
<p>Going the other way, I find that quantum computing to be informative. Some years ago I was reading about a French engineer who had successfully created a computing unit in nanotechnology where each bit was made up of electrons in three states—<strong>on</strong> (excited), <strong>off</strong> (unexcited) and <strong>both</strong> (simultaneously excited and unexcited). Thus, <a href="http://nsf.gov/discoveries/disc_summ.jsp?cntn_id=114269&#038;org=NSF">quantum bits (called <em>qubits</em>) can be 0 or 1, or both 0 and 1 at the same time</a>—superposition. Ternary computer systems with the extra state have an exponential effect on computing capacity, which can further enhance computing, but in order for them to be useful to programmers, the organization of states needs to be codified in such a way that they can be employed. To compare the Ternary (sometimes called Trinary) numbers with Binary numbers in a four-bit register, click the Play button, and the Download button to download the code.</p>
<p><a href="http://www.sandlight.net/ternary/"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignnone size-full wp-image-1257" /></a><a href="http://www.sandlight.net/ternary/Ternary.as"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/download.png" alt="download" title="download" width="99" height="47" class="alignnone size-full wp-image-1153" /></a></p>
<p>Currently, the “tree” of computing is the binary numbering system using the base 16 hexadecimal as the following set of numbers illustrates what can be placed in a 4-bit unit of computing space.</p>
<p>01-0000-0<br />
02-0001-1<br />
03-0010-2<br />
04-0011-3<br />
05-0100-4<br />
06-0101-5<br />
07-0110-6<br />
08-0111-7<br />
09-1000-8<br />
10-1001-9<br />
11-1010-A<br />
12-1011-B<br />
13-1100-C<br />
14-1101-D<br />
15-1110-E<br />
16-1111 –F</p>
<p>Using 8-bit bytes, 256 different combinations (00 to FF) are possible. (Obviously, the more bytes used, the more combinations.) In a simple 8-bit computing environment, such as the 6502 processor (used in the early Apple computers), the next step up from the bit combination of 1s and 0s, was machine language that substituted hexadecimal values for operations. For example the following machine code stores the contents of the accumulator into an absolute address 7A4B.</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>8D 4B 7A</strong></p>
<p>The 8D value is more easily understood as the opcode, <strong>STA</strong>, which stands for “Store the contents of the accumulator in an address.” So now, you have,</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>STA $7A4B</strong></p>
<p>Suppose the contents of the accumulator create the string—“Hello.” The string is then stored in the address 7A4B. Just to make it simpler, suppose that we give a name to the address 7A4B and call it “Greeting.” So now we can say,</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<em>Store the string “Hello” in the address named “Greeting.”</em></p>
<p>Another way we could state it would be,</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>Greeting=”Hello”; </strong></p>
<p>The engineering and programming behind the process is far more complex than explained here, but what we’re looking at is programming being built from one level of abstraction to another. (If you haven’t read the Pulitzer Prize winning book, <em>The Soul of a New Machine</em> by Tracy Kidder, you’re in for a real treat on creating a new miniprocessor chip (before microprocessor chips, they had the miniprocessor chips). After the project was over, one of the developers moved to Vermont where he wanted to<br />
<blockquote>…deal with no unit of time shorter than a season.</p></blockquote>
<p><strong>Move Up to OOP</strong></p>
<p>Having done a project where I microcoded a 4-bit robot microprocessor to interface with an 8-bit computer, I can tell you that coding in 0s and 1s is tedious. The machine language using a base-16 numbering system, while a lot better, was nowhere near as easy as using an assembler—really &#8220;high level&#8221; abstraction using opcodes. Of course, a higher level assembly language like Forth was even better, but involved far more coding than languages like C++ and certainly BASIC. ActionScript 3.0 seems to be about the same level of abstraction as Java and is empowered to work with more abstract concepts like classes and namespaces.</p>
<p>Having started on the binary level, which is one abstraction up from logic gates and electrical impulses, we’ve come to ActionScript 3.0 where we can use a single statement like,</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>String.split(/~/);</strong></p>
<p>to parse a giant string and turn the chunks into array elements. Granted you have to know a thing or two about Regular Expressions and the right method to employ along with the class that the method is associated with. However, it’s a lot easier than even thinking about doing that in a lower level language such as assembly language. Furthermore, by understanding ActionScript 3.0 (or any other language), the more you know about it, the more you can use the built-in operations to code even less.</p>
<p>When we talk of <em>classes</em> we’ve really talking about <em>objects</em>. As an object, we can make the class do a lot of things for us, but in order to use it we need to understand its properties and methods and any associated events. To some extent, there’s a tradeoff between the amount of time we need to study or use a class or a namespace and how useful it becomes. However, the fact that <em>we can work with classes and their properties, methods and events,</em> is a big clue that the language has moved to a higher level of abstraction.</p>
<p><strong>Principles Are The Highest Level Of Abstraction</strong></p>
<p>Keeping in mind that one level of abstraction is nothing more than <em>an abstraction</em> we can think of any level as abstract or concrete as any other. Thus, what are the trees and what are the forests changes with the level of abstraction. To be successful in programming is to be able to relate to different levels of programming abstractions. For the most part, we’d think that if we stuck with the IDE we’d be on a pretty firm base. That’s true until the IDE takes a drastic turn. The utility of the old <em>Normal Mode</em> in the Flash IDE drastically changed when ActionScript files were introduced. Likewise, with the advent of Flex, the IDE changed yet again from Flash to Flex for many developers.</p>
<p>Further, I don’t think that any developer who sticks with a single language is going to be too successful overall. Most ActionScript 3.0 developers I know are functionally knowledgeable in at least one middleware language like PHP, ASP.NET (C# or VB), ColdFusion or even Perl. Likewise, a lot of ActionScript developers work with Flash Media Server, and more and more are coming to ActionScript 3.0 via Java and C++.</p>
<p>By the same token that wrapping oneself in a single language is <em>not</em> a good career plan, I don’t think that holding on to a single design pattern is going to serve one too well either. I admit that I tend to do most of my work with the <em>State, Strategy</em> or <em>Factory Method</em> patterns, but as I begin to run into different kind of programming challenges, I start seriously reviewing the whole list of patterns. Further as <a href="http://codebetter.com/blogs/ian_cooper/archive/2009/06/11/should-you-learn-frameworks-or-principles.aspx"> Ian Cooper of CodeBetter.com</a> points out, marrying a single framework isn’t such a hot idea either. While some frameworks, such as MVC, seem to be more stable than others, they too change and mature as witnessed in PureMCV. However, as Cooper notes, companies like Microsoft seem to have weekly frameworks and trying to keep up with them can be an exercise in futility not to mention taking time from better uses of your programming time.</p>
<p>Cooper suggests that the key to being a good programmer for any company, regardless of the <em>framework du jour</em>, is a solid understanding of <a href= "http://www.as3dp.com/2009/05/13/index-principles-and-work/" ><strong>OOP principles</strong></a>. Whether you should spend your time learning more about Cairngorm or Mate is pertinent insofar as a specific company is concerned (you’re up for a job interview and one of the requirements is knowing Cairngorm or Mate); however, overall, it’s less important than knowing the principles on which both are based—good OOP.  Assuming you have a good handle on your development IDE and its toolkit, picking up a framework is certainly important, and a good foundation framework is MVC and derivations like PureMVC. However, where you see <em>familiarity with Xframework</em> in a job description, if you have a good sense of what the general principles are, you can familiarize yourself with a framework without having to memorize it.</p>
<p><strong>Sliding Up and Down the Abstraction Pole: The Music Scan</strong></p>
<p>During my preparation for an instrument rating in flying, one of the techniques we learned was the <em>music scan</em>. In instrument conditions (i.e., weather conditions that prevent visibility), pilots have to rely on their flight instruments. One of the dangers that pilots encounter while flying by Instrument Flight Rules (IFR) is fixating on a single instrument. For example, if you focus on your air speed indicator, you may be heading straight into the ground if you don’t check your altimeter, climb indicator and artificial horizon. To help pilots scan all of their instruments, they taught us to think of scanning the instruments as conducting an orchestra, humming <em>ah-1 and ah-2</em> and scan all of the relevant instruments in an inverted “V” pattern. This may sound silly, but if you’ve ever encountered turbulence on a commercial flight, your attention can easily be fixated like a deer caught in headlights (AKA, “scared stupid”). For pilots of small planes that turbulence is multiplied several times and they’ll often fixate on one instrument while being shaken wildly. (Think of a coffee can containing a single marble being rattled by a crazed giant.)</p>
<p>In programming, we can be in danger of doing the same thing—fixating on a single level of abstraction. Rarely do we have to dip down to the level of assembly programming or even think about the binary logic that underlies our programs. The very best programmers I know will look at a problem from several different perspectives and on different levels of abstraction. OOP Principles are relevant to all layers of abstraction, but in particular from the level of operations on up to frameworks. In many ways, good programmers seem to live in a <em>principle sandwich</em>—principles surround them on all levels of programming.</p>
<ul>
<li><strong>OOP Principles</strong> </li>
<li>Frameworks</li>
<li>Design Patterns </li>
<li>Operations</li>
<li><strong>OOP Principles</strong> </li>
</ul>
<p>For me, the biggest trap is getting stuck in an operational level—especially when I’m in a hurry. True, I’ll think of some principles (like encapsulate everything I can), but all too often I’ll crank out what amount to <em>operational objects</em>. However, if I go to the next level of abstraction, Design Patterns, all too often I’ll start with a Strategy pattern because its <em>variation</em> is an algorithm—too big of a temptation for an algorithm junkie. I suppose that’s an improvement since everything used to be State patterns. Likewise, I’m employing more Factory Method patterns, and so while I’m unstuck on a single pattern, I’ve still got to keep a music scan going—scanning design patterns in addition to levels of abstraction.</p>
<p>My next big leap is to a Framework. True, by combining design patterns, I’ve created <a href="http://www.as3dp.com/2009/06/24/friends-with-benefits-state-and-factory-method-together-at-last—part-ii/"> little frameworks</a> but I want to start with a nice simple MVC that can go into the Lunch Bucket series—a simple framework that can be taken to work. (Readers might also think about taking some of the PureMVC designs done by Chandima to work as well.) However, for the most part I’ll be sliding between the operational and design pattern levels. It’s not that frameworks of too abstract, but often they’re a bit too purpose oriented, and I like the granularity of design patterns. However, if I can find the right framework for the kinds of things that I typically do, I’ll adopt it—or develop a framework of my own.</p>
<p><strong>Keep on Humming</strong></p>
<p>As long as you force yourself to think about different levels of abstraction—a music scan of programming levels—you’ll be able to escape fixation on a single level. Abstractions are as concrete or abstract as you make them because ultimately they constitute perceptions. The OOP Principles seem to work pretty well from an operational level to a framework level, and that pretty much describes what we actually do in programming. Back in the day when I spent about 10 years with engineers from General Dynamics at weekly Forth meetings, I’m reminded of their insistence that C++ was a sham because it really wasn’t reusable code as promised. It worked differently with different processors, and only a language on the level of Forth could be trusted to do the right thing because with Forth you could program right down to the metal. They felt that if you could not take advantage of the different characteristics of the different CPUs you weren’t allowing yourself the full range of possibilities of your computer.</p>
<p>Unfortunately (or fortunately) very few programmers ever warmed up to Forth because of its reverse Polish notation (or Postfix notation) and low-level abstraction—as noted, it was really a higher-level assembly language. I learned a lot from Forth, and some of my own programming idiosyncrasies reflect that. However, I’m far more productive and comfortable in the higher-level languages such as ActionScript 3.0. Likewise, higher-level concepts such as OOP are vital to reusable and team-built projects. When OOP concepts can be cast in design patterns, it not only provides a common language on another level of abstraction, it increases re-usability and change of the program. Frameworks, organize operations and class relations on a still higher level of abstraction.</p>
<p>However, getting stuck on one level of abstraction, especially on an operational level where you’re trying to get the next algorithm to work is definitely a rut. Moving up to design patterns and OOP is a small but critical step, and it becomes as concrete as the operational level once you have practiced for a while. The trick is, though, to keep moving through the levels using OOP principles as the grease on the pole that allows you to slide to different levels and adapt to different levels. So if you ever find yourself in an abstraction rut on some level, just remember, <em>ah-1 and ah-2</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%2F07%2Factionscript-30-design-patternsoopprinciples-and-algorithms-the-forest-and-trees-of-programming%2F&amp;title=ActionScript%203.0%20Design%20Patterns%2FOOP%2FPrinciples%20and%20Algorithms%3A%20The%20Forest%20and%20Trees%20of%20Programming" 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/07/actionscript-30-design-patternsoopprinciples-and-algorithms-the-forest-and-trees-of-programming/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

