<?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; Template Method Pattern</title>
	<atom:link href="http://www.as3dp.com/category/design-patterns/template-method-pattern/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>Hollywood Principle: Don’t Call Us; We’ll Call You—ActionScript 3.0 Template Design Pattern goes Hollywood!</title>
		<link>http://www.as3dp.com/2009/05/hollywood-principle-dont-call-us-well-call-you-actionscript-30-template-design-pattern-goes-hollywood/</link>
		<comments>http://www.as3dp.com/2009/05/hollywood-principle-dont-call-us-well-call-you-actionscript-30-template-design-pattern-goes-hollywood/#comments</comments>
		<pubDate>Thu, 07 May 2009 15:57:38 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Principles]]></category>
		<category><![CDATA[Template Method Pattern]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=900</guid>
		<description><![CDATA[The problem in understanding the Hollywood Principle is that it is too often glossed over as a type of inversion of control. In most respects, the Hollywood principle is really about the direction of calls. Where it gets a little confusing is when you assume the natural order of programming is from bottom to the [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.as3dp.com/wp-content/uploads/2009/05/bucketruleholly.png" alt="bucketruleholly" title="bucketruleholly" width="209" height="184" class="alignleft size-full wp-image-901" />The problem in understanding the <strong>Hollywood Principle</strong> is that it is too often glossed over as a type of inversion of control. In most respects, the Hollywood principle is really about the direction of calls. Where it gets a little confusing is when you assume the <em>natural order</em> of programming is from bottom to the top, with the bottom making calls to the higher levels. At one time, much programming was &#8220;bottom-up&#8221; in this sense, but that&#8217;s been quite a while. Therefore, when talking about <em>inversion</em>, you may be wondering, <em>inversion from what</em>? In fact, the late John Vlissides (one of the Gang of Four) starts off a discussion of the Hollywood Principle, noting,</p>
<blockquote><p>The Template Method pattern leads to an inversion of control known as the &#8220;Hollywood Principle,….&#8221; (1996)</p></blockquote>
<p> As we know, <strong>Dependency Inversion</strong> is better understood as <strong>Abstraction Dependency</strong>—both higher and lower level components come to depend on abstractions. If we think <em>depend on abstractions</em> we have no need to even consider <em>inversion</em>. Doing so only confuses the issue. So, if we examine the Hollywood Principle, let’s do so in terms of what it does in its own right—what is its focus?<br />
<blockquote>The Hollywood Principle holds that higher level components should call lower level components, but lower level components should not call higher level components.</p></blockquote>
<p> So, if we take that focus of the Hollywood Principle we can better discuss its unique features. Is it related to the other principles of OOP? Of course it is, but let’s just focus on the point it makes.<br />
<span id="more-900"></span><br />
<strong>Think Responsibility <em>not</em> Flow of Control</strong></p>
<p>I’ve spent way too much time thinking about <em>flow of control</em> while I should have been thinking about what <em>responsibility</em> each component has. John Vlissides brings up this point in discussing the Hollywood Principle and the <strong>Template Method</strong>. So let’s just take a second to examine the difference between <em>flow of control</em> and <em>responsibility</em>.</p>
<p>In a traditional flow chart, you can see that the focal point is flow of control and not component responsibility. Figure 1 shows a typical flow chart:<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/05/flowchart.png" alt="flowchart" title="flowchart" width="181" height="581" class="alignnone size-full wp-image-902" /><br />
<em><strong>Figure 1: </strong> Flow charts focus on the flow of control</em></p>
<p>Are flow charts really that helpful in OOP? They certainly help to outline different procedures and the sequence of those procedures; however, they were developed for sequential programming and later adopted for procedural programming once it was introduced. By using flow charts, I believe that a developer lays the foundation for sequential or procedural structures.<br />
<blockquote>So, no, we don’t need no stinkin’ flow charts.</p></blockquote>
<p>Now, let’s switch to a very different way of looking at programming. Rather than looking at a class diagram, an object diagram or an interaction diagram (those found in GoF), let’s take a look at a statechart. Figure 2 shows a statechart representing lighting in a darkroom used for developing film.<br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/05/statechart.png" alt="statechart" title="statechart" width="291" height="380" class="alignnone size-full wp-image-903" /><br />
<em><strong>Figure 2: </strong> Statecharts focus on states as objects and the transition between states.</em></p>
<p>Imagine walking into a darkroom and flipping on the light switch. The transition from one state to another is through a trigger. In this case, the trigger is moving the switch from the down to up position. The room lights up. The initial state is one where all lights are off. (Statecharts represent the initial state with a large black dot.) When you get ready to make prints, you will need to turn on a safelight (a red light is used in the example) so that you can see what you’re doing while developing the prints. Full light would ruin the prints in the developing phase; so the white lights must be turned off. Once the developing is complete, you can turn on the white lights again. You can leave on the safelight, but you’ll probably want to turn it off to save both the safelight bulb and electricity.</p>
<p>The thinking is quite different here than with a flowchart. Each state is an object and some kind of event (trigger) moves from one object (state) to another. A state as an object moves the focus from the flow of control to the makeup of the state. The flow is relegated to an event (trigger) that makes transitions from one set of responsibilities (state object) to another.</p>
<p>Now we can look at a class diagram and understand it wholly apart from a flowchart. In Figure 3, we see no flow of control at all, but only responsibilities in the Template Method Design Pattern.</p>
<p><img src="http://www.as3dp.com/wp-content/uploads/2009/05/templatemethod.png" alt="templatemethod" title="templatemethod" width="391" height="270" class="alignnone size-full wp-image-904" /><br />
<em><strong>Figure 3: </strong> Class diagrams show object responsibilities</em></p>
<p>The Template Method (as a design pattern) is different from the template method (an operation). As an operation, the template method simply orders the primitive operations. The actual operations are left to the implementations—concrete classes. The focal point is on organizing primitive operations. Any number of concrete classes can be derived from the abstract class, and their primitive operations can be whatever the developer wants as long as they maintain the abstract class’s interface. John Vlissides notes,<br />
<blockquote> Therefore when you define a new subclass of Node (Abstract class), you have to think <strong>not</strong> in terms of control flow but <em>responsibility</em>—the operations you <em>must</em> override, those you <em>might</em> override, and others you <em>mustn&#8217;t</em> override. Structuring your operations as template methods makes these responsibilities more explicit.</p></blockquote>
<p> The <em>steps of an algorithm</em> vary with the Template Method Design Pattern.</p>
<p>A concrete but simple example of a Template Method would help better understand what Vlissides is talking about.</p>
<p><strong>You Got the Call!</strong></p>
<p>The Template Method Design Pattern is one of the simplest yet most useful and intriguing of all design patterns. (For full details for using it with ActionScript 3.0, see Chapter 9 of our book.) Here, we will look at it in terms of how it reveals the Hollywood Principle, while keeping in mind that the principle itself is a general one that applies to other design patterns and OOP programming in general. I made two versions that you can download; one with the <a href=http://www.sandlight.net/blogDP/HollywoodV.zip> Vector</a> class (requires Flash CS4 or newer or Flex Builder version that supports Vectors) and the other with the <a href= http://www.sandlight.net/blogDP/HollywoodA.zip>Array</a> class (ActionScript 3.0.)</p>
<p>As you can see in Figure 3, the design pattern is very simple. This particular template method employs three graphics. One is a large tan rectangle to be used as a backdrop, and the other two are a red circle and a brown rectangle. The circle and the rectangle overlap, with one or the other on top, depending on what the template method (an operation in the AbstractTM class) decides. The order of the shape objects to be displayed can be changed in the template method. In Figure 4, the image on the left is the initial rendering, and the image on the right shows what happens when the algorithm is reordered. </p>
<p><img src="http://www.as3dp.com/wp-content/uploads/2009/05/hollywoodorder.png" alt="hollywoodorder" title="hollywoodorder" width="401" height="200" class="alignnone size-full wp-image-908" /></p>
<p><em><strong>Figure 4: </strong> Template method determines sequence of an algorithm</em></p>
<p>Essentially, all that’s happening is that the vector (or array) element order is changed. The Client uses the <strong>pop()</strong> method to pull the elements out; so, it first pulls out the tan background and places it in the display list. Then, depending on the order of the steps in the template method, it places the red circle or brown rectangle on top of the tan rectangle and overlapping one another.</p>
<p><strong>Setting Up the Order</strong></p>
<p>Basically, the Template Method establishes the sequence of an algorithm. So the first thing to do is to create the abstract class that will hold the template method that orders the sequence of operations.</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('p900code14'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p90014"><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
</pre></td><td class="code" id="p900code14"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Abstract Template Method</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>;
	<span style="color: #808080; font-style: italic;">//Abstract class do not instantiate</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AbstractTM
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> tmShape=<span style="color: #000000; font-weight: bold;">new</span> Shape<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> tmVector:Vector.<span style="color: #66cc66;">&lt;</span>shape<span style="color: #66cc66;">&gt;</span>=<span style="color: #000000; font-weight: bold;">new</span> Vector.<span style="color: #66cc66;">&lt;</span>shape<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">/*Note: Use Array instead of Vector
		with Flash CS3 or version of Flex
		that does not support Vector class */</span>
&nbsp;
		<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">function</span> templateMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Vector.<span style="color: #66cc66;">&lt;</span>shape<span style="color: #66cc66;">&gt;</span>
		<span style="color: #66cc66;">&#123;</span>
			tmVector.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>roundShape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			tmVector.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>recShape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			tmVector.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>baseShape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span>tmVector<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> roundShape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//Work out details in</span>
			<span style="color: #808080; font-style: italic;">//Concrete class</span>
			<span style="color: #b1b100;">return</span> tmShape;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> recShape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//Work out details in</span>
			<span style="color: #808080; font-style: italic;">//Concrete class</span>
			<span style="color: #b1b100;">return</span> tmShape;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> baseShape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			tmShape.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xF6E497,<span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>;
			tmShape.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">50</span>,<span style="color: #cc66cc;">200</span>,<span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span>;
			tmShape.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> tmShape;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Notice that the <strong>templateMethod()</strong> operation simply takes the three functions (that return shapes) and pushes them into the Vector (or Array) object. The function is locked using the <strong>final</strong> access statement so that it cannot be overridden. Depending on the order, they will be popped out with different consequences. Two of the operations are protected and one is private. The private operation is the one you don’t want to change both in terms of its position in the Vector and its characteristics. It serves as the background for any other Shape objects you may later want to place on top of it.</p>
<p>Next, build a concrete class that extends the abstract class containing the template method. Basically, this class provides the algorithms for creating different shapes. Each of the two protected functions are overridden and filled with code that creates a shape and returns it.</p>
<p>//Concrete Class</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p900code15'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p90015"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code" id="p900code15"><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: #000000; font-weight: bold;">class</span> ConcreteTM <span style="color: #0066CC;">extends</span> AbstractTM
	<span style="color: #66cc66;">&#123;</span>
		override protected <span style="color: #000000; font-weight: bold;">function</span> roundShape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> tmShape=<span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			tmShape.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0xB9121B,<span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>;
			tmShape.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawCircle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">175</span>,<span style="color: #cc66cc;">125</span>,<span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span>;
			tmShape.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> tmShape;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override protected <span style="color: #000000; font-weight: bold;">function</span> recShape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Shape
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> tmShape=<span style="color: #000000; font-weight: bold;">new</span> Shape<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			tmShape.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x4C1B1B,<span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span>;
			tmShape.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">175</span>,<span style="color: #cc66cc;">125</span>,<span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>;
			tmShape.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">return</span> tmShape;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>At this point we need to consider what has been created because the entire design pattern is made up solely of the AbstractTM and ConcreteTM. Had we wanted, we could have added more concrete classes that use the same template method, but for now, this simple example works fine to illustrate the Hollywoood Principle. Basically, the abstract class has the code to generate a background shape and then orders the three functions in any way required by the task in the  <strong>templateMethod</strong>.</p>
<p>In reviewing John Vlissides comment that we <em>should not</em> concern ourselves with the flow of control, but instead make decisions about what must and must not be overridden in our design. Now we’re thinking about the <em>responsibilities of the objects</em> instead of the flow of control.</p>
<p>Finally, we need to make <em><strong>make that call!</strong></em>. Using the Client, we type an object as the abstract class and then instantiate the concrete class. When the Client makes a request, it does so through the <strong>AbstractTM</strong> class, and then to the <strong>ConcreteTM</strong> class. As you will see in the following code, the call is make through the templateMethod() to the specific shapes, and you can clearly see that no reference at all is made to either of the concrete methods in the ConcreteTM class.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p900code16'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p90016"><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
</pre></td><td class="code" id="p900code16"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
&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> shapeTM:AbstractTM = <span style="color: #000000; font-weight: bold;">new</span> ConcreteTM<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> tmShape:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape<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> tmVector:Vector.<span style="color: #66cc66;">&lt;</span>shape<span style="color: #66cc66;">&gt;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">/*Note: Use Array instead of Vector
		with Flash CS3 or version of Flex
		that does not support Vector class */</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>
			tmVector = shapeTM.<span style="color: #006600;">templateMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> vShape:uint=<span style="color: #cc66cc;">0</span>; vShape<span style="color: #66cc66;">&lt;</span><span style="color: #cc66cc;">3</span>; vShape++<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				tmShape = tmVector.<span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				addChild<span style="color: #66cc66;">&#40;</span>tmShape<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Because the example returns the shapes in an vector/array, you have lots of options. As in the example, you can let the template method order them into the display list, but alternatively you can pass them from the vector/array elements</p>
<p><strong>Into the Lunch Bucket</strong></p>
<p>Not only is the Hollywood principle a good one to take to work and use, you can throw in the Template Method Design Pattern to keep it company.  Both are easy to remember, simple to create and very practical. Further, you have a lot of flexibility. As a general rule, I like typed vectors, and so I tend to use the Vector class wherever possible. However, if you use the Array class, you can mix in different types of objects. For example, you might want to set up a template method that uses an array to display text objects for references for graphic object. So you could have a caption or heading and then use it to display a package of a fully described graphic.</p>
<p>In addition to using the Hollywood principle with the Template Method be sure to keep in mind that it is a general principle. If you ever call a higher level object with a lower level object, you’re bound to get in trouble, and as they say,<br />
<blockquote>You’ll never work in this town again!</p></blockquote>
<p> So just remember the Hollywood principle and let the higher level object make <em>the call</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%2F05%2Fhollywood-principle-dont-call-us-well-call-you-actionscript-30-template-design-pattern-goes-hollywood%2F&amp;title=Hollywood%20Principle%3A%20Don%E2%80%99t%20Call%20Us%3B%20We%E2%80%99ll%20Call%20You%E2%80%94ActionScript%203.0%20Template%20Design%20Pattern%20goes%20Hollywood%21" id="wpa2a_4"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/05/hollywood-principle-dont-call-us-well-call-you-actionscript-30-template-design-pattern-goes-hollywood/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Flexibility Pays Off with Template Method</title>
		<link>http://www.as3dp.com/2007/08/flexibility-pays-off-with-template-method/</link>
		<comments>http://www.as3dp.com/2007/08/flexibility-pays-off-with-template-method/#comments</comments>
		<pubDate>Fri, 24 Aug 2007 06:24:49 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Template Method Pattern]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/2007/08/24/flexibility-pays-off-with-template-method/</guid>
		<description><![CDATA[This is a testimonial of sorts for the capacity to easily change an application created with design patterns. Recently Adobe put up a version of a player that streams H.264 formats&#8211;these are files like Apple&#8217;s .mov and MP4, among others. After fumbling around for a while creating the files&#8211;one video .mov using iMovie and a [...]]]></description>
			<content:encoded><![CDATA[<p>This is a testimonial of sorts for the capacity to easily change an application created with <strong>design patterns</strong>. Recently Adobe put up a version of a player that streams H.264 formats&#8211;these are files like Apple&#8217;s .mov and MP4, among others. After fumbling around for a while creating the files&#8211;one video .mov using iMovie and a M4a audio using Garage Band, I went to test them on a progressive download app I had handy that was set up to play audio using the <code>Sound</code> class and video with <code>NetConnection(null)</code> and <code>NetStream</code>. The only problem was that the streaming audio (m4a) file required a <code>NetStream</code> instance and would not work with the <code>Sound </code>class.</p>
<p>Because the classes for handling audio were set up around the <code>Sound</code> class, I thought this would be a major chore, but all I had to do was to make changes in <em>one</em> class. After the changes were made, it worked fine. The best part is that the structure of the <strong>Template Method</strong> did not interfere with any of the other elements in the application. So while the application may be fairly complex for the simple task of playing video and sound, when things change, <em>as they always do</em>, making the changes in the application were simple.</p>
<p>The application can be found at: <a href="http://www.sandlight.com/player9/">http://www.sandlight.com/player9/</a>. Keep in mind that no FLV files are used but that a genuine MOV file being played in Flash using progressive download and the sound is from a M4a file,  the same file format as the ones used for iTunes&#8211;all sitting fat and happy and working. You&#8217;ll need to go to <a href="http://labs.adobe.com/">Adobe Labs</a> and download the Flash 9 player (Beta) to see this at work.</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%2F2007%2F08%2Fflexibility-pays-off-with-template-method%2F&amp;title=Flexibility%20Pays%20Off%20with%20Template%20Method" 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/2007/08/flexibility-pays-off-with-template-method/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

