<?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; Getting Started in Design Patterns</title>
	<atom:link href="http://www.as3dp.com/category/getting-started-in-design-patterns/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>(Update!) ActionScript 3.0 and PHP Design Patterns: January 13-14 New York City</title>
		<link>http://www.as3dp.com/2010/12/actionscript-3-0-and-php-design-patterns-january-6-7-new-york-city/</link>
		<comments>http://www.as3dp.com/2010/12/actionscript-3-0-and-php-design-patterns-january-6-7-new-york-city/#comments</comments>
		<pubDate>Wed, 29 Dec 2010 09:50:47 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Getting Started in Design Patterns]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=4248</guid>
		<description><![CDATA[With partial funding from the National Science Foundation, CUNY (CCNY campus) invited me to do two workshops on design patterns: one on ActionScript 3.0 and the other on PHP. The workshops cover a wide range of basic OOP topics to Design Patterns. Check it out here. The last talk I gave was also in NYC, [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_4256" class="wp-caption alignleft" style="width: 260px"><a href="http://clientbucket.com/training/index.php?p=workshops&#038;wid=1"><img src="http://www.as3dp.com/wp-content/uploads/2010/12/ccny1.png" alt="ccny" title="ccny" width="250" height="163" class="alignleft size-full wp-image-4271" /></a><p class="wp-caption-text">Two-day Design Pattern Workshop on CCNY campus of CUNY</p></div>With partial funding from the National Science Foundation, CUNY (CCNY campus) invited me to do two workshops on design patterns: one on ActionScript 3.0 and the other on PHP. The workshops cover a wide range of basic OOP topics to Design Patterns. <a href="http://clientbucket.com/training/index.php?p=workshops&#038;wid=1">Check it out here.</a></p>
<p>The last talk I gave was also in NYC, and I went over the Strategy pattern in ActionScript 3.0. This time, I&#8217;ll be looking at using the State Design pattern in creating an AS3 state machine. Also, since the class diagrams of the Strategy and State look identical, I&#8217;ll be differentiating between the two. There&#8217;s a world of difference once you get to know them. On the PHP side, I&#8217;ll be looking at setting up a Strategy design pattern for use issuing SQL commands as unique algorithms and explore the little known fact that in PHP interfaces can have constants and how to program to an interface in a language that is weakly typed.</p>
<p><strong>Schedule</strong></p>
<p>The workshop will take place in the Amsterdam Room of the students cafeteria in the North Academic Center (NAC) Building of the beautiful campus of the City College of New York at 160th Convent Avenue, New York, NY 10031. The school campus is located a short walk from the A, B, C, and D trains at the 145th street station, and the #1 train at the 137th street station.</p>
<p><strong>Day 1 ~ 13thth January, 2011</strong><br />
Breakfast: 8:30AM &#8211; 9:00AM</p>
<p>Object Oriented Programming in PHP: 9:00AM &#8211; 12:00PM<br />
Reviewing PHP<br />
OOP in PHP<br />
Classes in PHP<br />
Abstract classes and Interfaces<br />
Programming to an interface<br />
Lunch: 12:00PM &#8211; 12:35PM</p>
<p>SQL commands in PHP using mysquli structures: 12:35PM- 2:45PM<br />
Re-thinking programs with Design Patterns<br />
The Strategy Design Pattern: Encapsulating Algorithms<br />
Starting with Data Entry and Data Read Algorithms<br />
Adding more Algorithms by implementing more Strategy classes<br />
The ease of change<br />
Coffee Break: 2:45PM &#8211; 3:00PM</p>
<p>Using type hinting in a weakly typed language: 3:00PM &#8211; 4:00PM</p>
<p><strong>Day 2 ~ 14th January, 2011</strong><br />
Breakfast: 8:30AM &#8211; 9:00AM</p>
<p>Client-side programming in ActionScript 3.0 with Video objects: 9:00AM &#8211; 12:00PM</p>
<p>Using NetConnection and NetStream objects<br />
Streaming video and progressive download<br />
Object Oriented Programming with ActionScript 3.0<br />
Lunch: 12:00PM &#8211; 12:35PM</p>
<p>OOP in ActionScript 3.0: 12:35PM &#8211; 2:45PM<br />
Basic OOP concepts<br />
The concept of a State Machine (State Engine):  Statecharts<br />
Creating a video player as a State Machine<br />
Coffee Break: 2:45PM &#8211; 3:00PM</p>
<p>State Design Pattern: 3:00PM &#8211; 4:00PM<br />
Context class<br />
State Interface<br />
Concrete States<br />
Adding states and changing states</p>
<p><strong>The Coders Algonquin Round Table</strong></p>
<p>In addition to the regular schedule, I&#8217;m hoping to bend an elbow at the Algonquin Hotel lounge. We can have an informal OOP/DP roundtable and pretend we&#8217;re the coder version the original Algonquin Round Table&#8211;Dorothy Parker, Robert Benchley, Edna Ferber, George S. Kaufman, Robert Sherwood and Tallulah Bankhead. The Algonquin is located at 59-63  44th Street between Fifth and Sixth Avenues. Right around six o&#8217;clock on the evening of January 13. At the FCNY meeting last summer, I didn&#8217;t get a chance for an informal chat with many of you, and I don&#8217;t want to make that mistake again.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2010%2F12%2Factionscript-3-0-and-php-design-patterns-january-6-7-new-york-city%2F&amp;title=%28Update%21%29%20ActionScript%203.0%20and%20PHP%20Design%20Patterns%3A%20January%2013-14%20New%20York%20City" 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/2010/12/actionscript-3-0-and-php-design-patterns-january-6-7-new-york-city/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Design Pattern Starter Kit</title>
		<link>http://www.as3dp.com/2009/11/actionscript-30-design-pattern-starter-kit/</link>
		<comments>http://www.as3dp.com/2009/11/actionscript-30-design-pattern-starter-kit/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 02:48:43 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Getting Started in Design Patterns]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=2075</guid>
		<description><![CDATA[Over the years, Chandima and I have created several tools for helping ActionScript 3.0 developers create and use Design Patterns. For those who have recently decided to take the leap into coding patterns, this post provides one-stop shopping for getting started. For the old-timers, it represents a place where you can find key elements and [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_2083" class="wp-caption alignleft" style="width: 250px"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/dpu.png" alt="Getting Started is Design Patterns Can be Daunting" title="dpu" width="240" height="279" class="size-full wp-image-2083" /><p class="wp-caption-text">Getting Started in Design Patterns Can be Daunting</p></div>Over the years, Chandima and I have created several tools for helping ActionScript 3.0 developers create and use Design Patterns. For those who have recently decided to take the leap into coding patterns, this post provides one-stop shopping for getting started. For the old-timers, it represents a place where you can find key elements and refresh your understanding of design pattern basics.</p>
<p><strong>What are Design Patterns?</strong></p>
<p>So as not to create false expectations, <strong>Design Patterns</strong> will <em>not</em> make your programs run faster or better. They were developed to aid in creating object oriented programs that can be reused and easily changed and updated. One of the side benefits is that they aid in understanding Object Oriented Programming (OOP); so, if you leap into design patterns, you&#8217;re leaping into OOP.</p>
<p><em>They&#8217;re not easy</em>, but don&#8217;t let that scare you. Some are easier than others, and by starting with the easy ones, the more challenging ones will come in time.</p>
<p><em>They payoffs are huge</em>. If you&#8217;re reading this line, it means you&#8217;re not afraid of expanding your mind beyond what you thought you could do in programming. Once you&#8217;ve figured out &#8216;Hello World&#8217; level programming and discovered the beauty of a good algorithm, it&#8217;s time to look at the bigger picture. So let&#8217;s get started!<br />
<span id="more-2075"></span></p>
<p><strong>Always Start and End with OOP Principles</strong></p>
<p>OOP Principles and Design Pattern Principles are virtually the same; so to help you get started with OOP Principles, we&#8217;ve organized all of the links to the principles in one place-> <a href="http://www.as3dp.com/2009/05/13/index-principles-and-work/">Here!</a></p>
<p>Also, for your desktop, you can use the AIR applications that contain a summary of critical information:<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/02/airimg.png" alt="airimg" title="airimg" width="104" height="69" class="alignnone size-full wp-image-736" /></p>
<ul>
<li>Principles </li>
<li>Program Variation and Design Pattern</li>
<li>Design Pattern Summary (still under construction but useable)</li>
</ul>
<p>You can download all of the AIR applications <a href="http://poobah.mwd.hartford.edu/wsanders/starterKit/">here</a>.</p>
<p><strong>Books</strong></p>
<p>Learning Design Patterns requires patience, and books are about the most patient tools around. Here are three we find helpful:</p>
<ol>
<li> <a href="http://oreilly.com/catalog/9780596528461/">ActionScript 3.0 Design Patterns</a>. Of course we&#8217;re going to recommend our own book. We wrote it with ActionScript programmers in mind. The best part is that you don&#8217;t have to convert the examples into ActionScript 3.0—that&#8217;s already been done. (Also available for iPhone.)</li>
<li> <a href="http://oreilly.com/catalog/9780596007126/">Head First Design Patterns</a>. This book takes your head on a fun tour of design pattern thinking. The examples are all in Java, but it&#8217;s not too difficult to work through with ActionScript 3.0.</li>
<li> <a href="http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1257564388&#038;sr=1-1">Design Patterns: Elements of Reusable Object-Oriented Software</a>. This is the book that launched programming design patterns. The examples in SmallTalk are difficult to convert into ActionScript 3.0, but the other materials are rich enough in detail so that you can create your own examples.</li>
</ol>
<p>In addition to those books, look for good books on OOP and Design Patterns. However, if we had to recommend a core Design Pattern/OOP Library, those three books serve as a core.</p>
<p><strong>Magazines and Online Sources</strong></p>
<p>While we&#8217;re on the topic print material, you will find a good online magazine <em><a href="http://ffdmag.com">Flash &#038; Flex Developer</a></em>. (http://ffdmag.com/) This magazine used to be a pay-to-subscribe paper publication. However, they changed to free digital format (PDF). The quality has remained high as they have switched to this new format. In a couple of issues I&#8217;ve written articles on Design Patterns that are designed for beginners. (5/2009 and 6/2009).</p>
<p>Occasionally, you can find an ActionScript 3.0 Design Pattern article at the Adobe.com site, but not as many as we&#8217;d like to see. Also, we&#8217;d like to see more blogs dedicated to ActionScript 3.0 Design Patterns. If you find any, we&#8217;d like to hear about them.</p>
<p><strong>Finding What You Need on This Blog</strong></p>
<p>With well over 100 posts and over 1000 comments (at this writing), we have to revise our categories and add indexes occasionally. Also, we have posts like this one to try and gather up the collected wisdom and offer starting places for new readers. Please add any suggestions you have in the suggestion box on the right side of this blog -> if you have any ideas for finding materials or topics you&#8217;d like to see. Finally, at the bottom of every post you will see a <strong>list of related posts</strong>. Usually, you can find what you need, but if you still cannot, use the <strong>search window</strong> at the top of the blog. Your comments are always welcomed!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2009%2F11%2Factionscript-30-design-pattern-starter-kit%2F&amp;title=ActionScript%203.0%20Design%20Pattern%20Starter%20Kit" id="wpa2a_6"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/11/actionscript-30-design-pattern-starter-kit/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

