<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
	>
<channel>
	<title>Comments on: ActionScript 3.0 Easy and Practical Strategy Design Pattern</title>
	<atom:link href="http://www.as3dp.com/2009/03/08/actionscript-30-easy-and-practical-strategy-design-pattern/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.as3dp.com/2009/03/08/actionscript-30-easy-and-practical-strategy-design-pattern/</link>
	<description>OOP Techniques for Flash and Flex Developers</description>
	<lastBuildDate>Sat, 28 Aug 2010 21:46:42 -0700</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: William B. Sanders</title>
		<link>http://www.as3dp.com/2009/03/08/actionscript-30-easy-and-practical-strategy-design-pattern/comment-page-1/#comment-5324</link>
		<dc:creator>William B. Sanders</dc:creator>
		<pubDate>Fri, 09 Jul 2010 08:57:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/?p=749#comment-5324</guid>
		<description>Olá! Henrique,

¡Agradecimentos outra vez para seus comentários!

Cordialmente,
Bill</description>
		<content:encoded><![CDATA[<p>Olá! Henrique,</p>
<p>¡Agradecimentos outra vez para seus comentários!</p>
<p>Cordialmente,<br />
Bill</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Henrique</title>
		<link>http://www.as3dp.com/2009/03/08/actionscript-30-easy-and-practical-strategy-design-pattern/comment-page-1/#comment-5316</link>
		<dc:creator>Henrique</dc:creator>
		<pubDate>Thu, 08 Jul 2010 23:14:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/?p=749#comment-5316</guid>
		<description>Thank you for the reply. And it&#039;s portuguese, Brazil. =)

Best Regards,
Henrique</description>
		<content:encoded><![CDATA[<p>Thank you for the reply. And it&#8217;s portuguese, Brazil. =)</p>
<p>Best Regards,<br />
Henrique</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: William B. Sanders</title>
		<link>http://www.as3dp.com/2009/03/08/actionscript-30-easy-and-practical-strategy-design-pattern/comment-page-1/#comment-5314</link>
		<dc:creator>William B. Sanders</dc:creator>
		<pubDate>Thu, 08 Jul 2010 21:08:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/?p=749#comment-5314</guid>
		<description>Bonjour Henrique,
Olá! Henrique,
Hola Henrique,

You English is much better than my French, Spanish or Portuguese--or whatever language it is. No apologies necessary.

The Freemans (Head First) used abstract examples (system.out.println) with their ducks&#039; quacking and flying behaviors and so they didn&#039;t have to dip into a library to pull out something that would provide a concrete operation. Basically, if I had used &lt;b&gt;trace()&lt;/b&gt; I would not have had to extend Sprite. The Duck class is first a concrete Context and then an Abstract Context and the different types of Ducks (real, rubber, decoy) are subclasses of the Context class. The different behaviors are all separate Strategy interfaces (one for quacking and one for flying) and the implementations of those two strategies are used by different types of ducks.

Figure 1 (in the article) is what the Gang of Four used in their book, and while I only used a single strategy implementation of a strategy and had no child class for the Context, I didn&#039;t need it. However, as you and others have demonstrated, it&#039;s not too difficult to add more concrete strategies.

As for &quot;family of algorithms&quot; providing different loading algorithms certainly falls within a &quot;family.&quot; (GoF are pretty vague about what they mean about &quot;family&quot;, but I do believe that the Freemans &quot;flying&quot; and &quot;quacking&quot; are as good as any.) By the same token, so is &quot;loading.&quot;

In your example, Context class seems to be abstract, but in the original GoF class diagram, Context is concrete (one of the few places where you&#039;ll find a concrete class that is subclassed in their book), but the Duck class the Freemans use is also abstract as well (the second one on pg. 18). However, I followed the Gang of Four&#039;s class diagram and used a concrete Context class.(Their example also used a concrete Context participant.) Here&#039;s what you need in the Context:

-configuration with a ConcreteStrategy object.
-maintains a reference to a Strategy object.

As you can see, those requirements are met. &lt;em&gt;In addition&lt;/em&gt; you may:

- define an interface that lets Strategy access its (the Context&#039;s) data.

I chose not to do that because I had no reason to do so. However, look how easy it is to add another concrete Strategy to load something other than a SWF (or graphic) file. For example, adding a concrete text loading strategy implementing the ILoader (Strategy) -- as Curro did in the first comment.

In any event, I want to thank you for your comments. They add a good deal to the discussion.

Kindest regards,
Bill</description>
		<content:encoded><![CDATA[<p>Bonjour Henrique,<br />
Olá! Henrique,<br />
Hola Henrique,</p>
<p>You English is much better than my French, Spanish or Portuguese&#8211;or whatever language it is. No apologies necessary.</p>
<p>The Freemans (Head First) used abstract examples (system.out.println) with their ducks&#8217; quacking and flying behaviors and so they didn&#8217;t have to dip into a library to pull out something that would provide a concrete operation. Basically, if I had used <b>trace()</b> I would not have had to extend Sprite. The Duck class is first a concrete Context and then an Abstract Context and the different types of Ducks (real, rubber, decoy) are subclasses of the Context class. The different behaviors are all separate Strategy interfaces (one for quacking and one for flying) and the implementations of those two strategies are used by different types of ducks.</p>
<p>Figure 1 (in the article) is what the Gang of Four used in their book, and while I only used a single strategy implementation of a strategy and had no child class for the Context, I didn&#8217;t need it. However, as you and others have demonstrated, it&#8217;s not too difficult to add more concrete strategies.</p>
<p>As for &#8220;family of algorithms&#8221; providing different loading algorithms certainly falls within a &#8220;family.&#8221; (GoF are pretty vague about what they mean about &#8220;family&#8221;, but I do believe that the Freemans &#8220;flying&#8221; and &#8220;quacking&#8221; are as good as any.) By the same token, so is &#8220;loading.&#8221;</p>
<p>In your example, Context class seems to be abstract, but in the original GoF class diagram, Context is concrete (one of the few places where you&#8217;ll find a concrete class that is subclassed in their book), but the Duck class the Freemans use is also abstract as well (the second one on pg. 18). However, I followed the Gang of Four&#8217;s class diagram and used a concrete Context class.(Their example also used a concrete Context participant.) Here&#8217;s what you need in the Context:</p>
<p>-configuration with a ConcreteStrategy object.<br />
-maintains a reference to a Strategy object.</p>
<p>As you can see, those requirements are met. <em>In addition</em> you may:</p>
<p>- define an interface that lets Strategy access its (the Context&#8217;s) data.</p>
<p>I chose not to do that because I had no reason to do so. However, look how easy it is to add another concrete Strategy to load something other than a SWF (or graphic) file. For example, adding a concrete text loading strategy implementing the ILoader (Strategy) &#8212; as Curro did in the first comment.</p>
<p>In any event, I want to thank you for your comments. They add a good deal to the discussion.</p>
<p>Kindest regards,<br />
Bill</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Henrique</title>
		<link>http://www.as3dp.com/2009/03/08/actionscript-30-easy-and-practical-strategy-design-pattern/comment-page-1/#comment-5309</link>
		<dc:creator>Henrique</dc:creator>
		<pubDate>Thu, 08 Jul 2010 01:33:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/?p=749#comment-5309</guid>
		<description>Hi Bill,

Really nice tutorial! Incredible site too, great resource for the subject of Design Patterns and good practices on actionscript.
[I&#039;m NOT a english speaker... so if I sound wrong or arrogant... just say it! Ok?]

Well, I have some complains, questions and maybe suggestions.

I really don&#039;t like the &quot;extend Sprite&quot; on model classes. I know that in flash (almost)  everything translates to display objects. The Sprite object is quite heavy and slow (to add and remove from the display). I also agree with Jiri that PixLoader should not render itself, just load stuff (single responsibility). But I understand your point of view also. Anyway, that is just me complaining and mumbling around.

Interesting observations were made by Jiri and Curro.But my question is: is correct to place another public method or property in the strategy (getContext or whatever)? All the strategy patterns that I read on books and on the web uses just one method. Head First Design Patterns talks about &quot;family of algorithms&quot; (like sort algorithms, tax algoritms, loader algorithms, ...). Therefore the strategy should have only the  &#039;load&#039; method and not the &#039;getContainer&#039; or unload, ..... But again, I&#039;m not sure about that, just saying what I read about it. Would be good to know different opinions.

Another important point to think, in this example, is how flash and actionscript handles loading (for simplicity, consider only Loader and URLLoader, just ignore the fancy stuff like web services, sockets, etc, at least for now). Loading in flash, like all of you know, uses the event mechanism. So if your designer wants to do some nice loading animations, you really need to take that into consideration. You NEED to support that event mechanism on your strategy!

All said..., let&#039;s code!

Starts with a simple interface. Note that &#039;load&#039; returns something.

&lt;pre lang=&quot;actionscript&quot;&gt;
package
{
	public interface ILoadMaster
	{
		function load(url:String):*;
	}
}
&lt;/pre&gt;

Now the concrete class.

&lt;pre lang=&quot;actionscript&quot;&gt;
package
{
	import flash.display.Loader;
	import flash.net.URLRequest;

	public final class PixLoader implements ILoadMaster
	{
		private var request:URLRequest;
		private var loader:Loader;
		
		public function load(url:String):*
		{
			request = new URLRequest(url);
			loader = new Loader();
			loader.load(request);
			
			return loader;
		}
		
	}
}
&lt;/pre&gt;

And the other.

&lt;pre lang=&quot;actionscript&quot;&gt;
package
{
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public final class TextLoader implements ILoadMaster
	{
		private var request:URLRequest;
		private var loader:URLLoader;
		
		public function load(url:String):*
		{
			request = new URLRequest(url);
			loader = new URLLoader();
			loader.load(request);
			
			return loader;
		}
		
	}
}
&lt;/pre&gt;

The contexts...

&lt;pre lang=&quot;actionscript&quot;&gt;
package
{
	import flash.display.Sprite;
	import flash.errors.IllegalOperationError;

	public class Context extends Sprite
	{
		protected var loadMaster:ILoadMaster;
		
		public function Context(loadMaster:ILoadMaster)
		{
			this.loadMaster = loadMaster;
		}
		
		public function load(url:String):void
		{
			throw new IllegalOperationError(&quot;Abstract method: must be overridden in a subclass&quot;);
		}
		
	}
}



package
{
	public final class PixContext extends Context
	{
		public function PixContext(loadMaster:ILoadMaster)
		{
			super(loadMaster);
		}
		
		public override function load(url:String):void
		{
			addChild(super.loadMaster.load(url)); // Pay attention! addChild!!
		}
		
	}
}



package
{
	public final class TextContext extends Context
	{
		public function TextContext(loadMaster:ILoadMaster)
		{
			super(loadMaster);
		}
		
		public override function load(url:String):void
		{
			super.loadMaster.load(url);
		}
		
	}
}
&lt;/pre&gt;

As you are returning Loader object from PixLoader and URLLoader from TextLoader you can add an event in the context class.

You could go further extending IEventDispatcher. Like so:

&lt;pre lang=&quot;actionscript&quot;&gt;
package
{
	import flash.events.IEventDispatcher;

	public interface ILoadMaster extends IEventDispatcher
	{
		function load(url:String):*;
	}
}



package
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.net.URLRequest;

	public final class PixLoader extends EventDispatcher implements ILoadMaster
	{
		private var request:URLRequest;
		private var loader:Loader;
		
		public function load(url:String):*
		{
			request = new URLRequest(url);
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
			loader.load(request);
			
			return loader;
		}
		
		private function loaded(event:Event):void
		{
			//dispatchEvent(new Event(Event.COMPLETE));
			dispatchEvent(event);
		}
		
	}
}
&lt;/pre&gt;

Now you can directly dispatch events from your strategy. Not sure if its a good thing.</description>
		<content:encoded><![CDATA[<p>Hi Bill,</p>
<p>Really nice tutorial! Incredible site too, great resource for the subject of Design Patterns and good practices on actionscript.<br />
[I'm NOT a english speaker... so if I sound wrong or arrogant... just say it! Ok?]</p>
<p>Well, I have some complains, questions and maybe suggestions.</p>
<p>I really don&#8217;t like the &#8220;extend Sprite&#8221; on model classes. I know that in flash (almost)  everything translates to display objects. The Sprite object is quite heavy and slow (to add and remove from the display). I also agree with Jiri that PixLoader should not render itself, just load stuff (single responsibility). But I understand your point of view also. Anyway, that is just me complaining and mumbling around.</p>
<p>Interesting observations were made by Jiri and Curro.But my question is: is correct to place another public method or property in the strategy (getContext or whatever)? All the strategy patterns that I read on books and on the web uses just one method. Head First Design Patterns talks about &#8220;family of algorithms&#8221; (like sort algorithms, tax algoritms, loader algorithms, &#8230;). Therefore the strategy should have only the  &#8216;load&#8217; method and not the &#8216;getContainer&#8217; or unload, &#8230;.. But again, I&#8217;m not sure about that, just saying what I read about it. Would be good to know different opinions.</p>
<p>Another important point to think, in this example, is how flash and actionscript handles loading (for simplicity, consider only Loader and URLLoader, just ignore the fancy stuff like web services, sockets, etc, at least for now). Loading in flash, like all of you know, uses the event mechanism. So if your designer wants to do some nice loading animations, you really need to take that into consideration. You NEED to support that event mechanism on your strategy!</p>
<p>All said&#8230;, let&#8217;s code!</p>
<p>Starts with a simple interface. Note that &#8216;load&#8217; returns something.</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('p749code1'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7491"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p749code1"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> ILoadMaster
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #66cc66;">*</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Now the 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('p749code2'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7492"><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
</pre></td><td class="code" id="p749code2"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">class</span> PixLoader <span style="color: #0066CC;">implements</span> ILoadMaster
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> request:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loader:Loader;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #66cc66;">*</span>
		<span style="color: #66cc66;">&#123;</span>
			request = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			loader = <span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			loader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>request<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">return</span> loader;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>And the other.</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('p749code3'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7493"><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
</pre></td><td class="code" id="p749code3"><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;">net</span>.<span style="color: #006600;">URLLoader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">class</span> TextLoader <span style="color: #0066CC;">implements</span> ILoadMaster
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> request:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loader:URLLoader;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #66cc66;">*</span>
		<span style="color: #66cc66;">&#123;</span>
			request = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			loader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			loader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>request<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">return</span> loader;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The contexts&#8230;</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('p749code4'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7494"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
</pre></td><td class="code" id="p749code4"><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>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Context <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> loadMaster:ILoadMaster;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Context<span style="color: #66cc66;">&#40;</span>loadMaster:ILoadMaster<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">loadMaster</span> = loadMaster;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Abstract method: must be overridden in a subclass&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">class</span> PixContext <span style="color: #0066CC;">extends</span> Context
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> PixContext<span style="color: #66cc66;">&#40;</span>loadMaster:ILoadMaster<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span>loadMaster<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			addChild<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">super</span>.<span style="color: #006600;">loadMaster</span>.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Pay attention! addChild!!</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">class</span> TextContext <span style="color: #0066CC;">extends</span> Context
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> TextContext<span style="color: #66cc66;">&#40;</span>loadMaster:ILoadMaster<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span>loadMaster<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> override <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span>.<span style="color: #006600;">loadMaster</span>.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you are returning Loader object from PixLoader and URLLoader from TextLoader you can add an event in the context class.</p>
<p>You could go further extending IEventDispatcher. Like so:</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('p749code5'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p7495"><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
</pre></td><td class="code" id="p749code5"><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;">events</span>.<span style="color: #006600;">IEventDispatcher</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> ILoadMaster <span style="color: #0066CC;">extends</span> IEventDispatcher
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #66cc66;">*</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Loader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">EventDispatcher</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">IEventDispatcher</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> final <span style="color: #000000; font-weight: bold;">class</span> PixLoader <span style="color: #0066CC;">extends</span> EventDispatcher <span style="color: #0066CC;">implements</span> ILoadMaster
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> request:URLRequest;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loader:Loader;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #66cc66;">*</span>
		<span style="color: #66cc66;">&#123;</span>
			request = <span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">url</span><span style="color: #66cc66;">&#41;</span>;
			loader = <span style="color: #000000; font-weight: bold;">new</span> Loader<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			loader.<span style="color: #006600;">contentLoaderInfo</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #006600;">COMPLETE</span>, <span style="color: #0066CC;">loaded</span><span style="color: #66cc66;">&#41;</span>;
			loader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span>request<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">return</span> loader;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">loaded</span><span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//dispatchEvent(new Event(Event.COMPLETE));</span>
			dispatchEvent<span style="color: #66cc66;">&#40;</span>event<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Now you can directly dispatch events from your strategy. Not sure if its a good thing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: William B. Sanders</title>
		<link>http://www.as3dp.com/2009/03/08/actionscript-30-easy-and-practical-strategy-design-pattern/comment-page-1/#comment-4051</link>
		<dc:creator>William B. Sanders</dc:creator>
		<pubDate>Fri, 19 Feb 2010 20:39:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/?p=749#comment-4051</guid>
		<description>Hi Tyler,

Polymorphism can be found in the Strategy classes themselves—they are written so that depending on the context of their use, they will have different meanings. I believe that we did type to the interface and not the implementation.

Kindest regards,
Bill</description>
		<content:encoded><![CDATA[<p>Hi Tyler,</p>
<p>Polymorphism can be found in the Strategy classes themselves—they are written so that depending on the context of their use, they will have different meanings. I believe that we did type to the interface and not the implementation.</p>
<p>Kindest regards,<br />
Bill</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tyler</title>
		<link>http://www.as3dp.com/2009/03/08/actionscript-30-easy-and-practical-strategy-design-pattern/comment-page-1/#comment-4050</link>
		<dc:creator>tyler</dc:creator>
		<pubDate>Fri, 19 Feb 2010 19:43:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/?p=749#comment-4050</guid>
		<description>Would it be appropriate to write a context interface, so that clients could also utilize polymorphism instead of typing to the concrete implementation of that context? 

For example in the client...

protected var _context:IContext;

protected function init():void{
     _context = new ConcreteContext(&quot;someUrl.swf&quot;);
}</description>
		<content:encoded><![CDATA[<p>Would it be appropriate to write a context interface, so that clients could also utilize polymorphism instead of typing to the concrete implementation of that context? </p>
<p>For example in the client&#8230;</p>
<p>protected var _context:IContext;</p>
<p>protected function init():void{<br />
     _context = new ConcreteContext(&#8221;someUrl.swf&#8221;);<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: William B. Sanders</title>
		<link>http://www.as3dp.com/2009/03/08/actionscript-30-easy-and-practical-strategy-design-pattern/comment-page-1/#comment-1823</link>
		<dc:creator>William B. Sanders</dc:creator>
		<pubDate>Wed, 18 Mar 2009 09:48:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/?p=749#comment-1823</guid>
		<description>Hi Jiri,

Wow! You sure can pick the topics! Object pooling in design patterns is fairly controversial, but I agree it would be a good topic--just to see what the pros and cons are.

In some respects, we all want more speed, but with the speed of current memory and CPUs, it&#039;s not the issue it once was with the old 8-bit processors and severely limited memory. Instead, the issue of speed has shifted to design and re-design. However, I&#039;m a sucker for squeezing more speed from a program (as the Flyweight patterns does) and it is something that I think is worth a post. However, here&#039;s a list of what I have to cover first:

Deal with my client&#039;s project
Finish re-writing a video player
Posts on the following topics:
&#160;&#160;&#160;&#160;Open/Closed Principle
&#160;&#160;&#160;&#160;Dependency Inversion Principle
&#160;&#160;&#160;&#160;Least Knowledge Principle
&#160;&#160;&#160;&#160;Hollywood Principle
&#160;&#160;&#160;&#160;One reason to change principle
&#160;&#160;&#160;&#160;Part II of Builder Pattern

Possibly Chandima might be interested and we could have something sooner, but until I get these other projects off my desk, I can&#039;t put anything new there. However, it is a perfect topic for this blog. (Maybe you&#039;d like to write a post on it?)

Thanks for the great idea,
Bill</description>
		<content:encoded><![CDATA[<p>Hi Jiri,</p>
<p>Wow! You sure can pick the topics! Object pooling in design patterns is fairly controversial, but I agree it would be a good topic&#8211;just to see what the pros and cons are.</p>
<p>In some respects, we all want more speed, but with the speed of current memory and CPUs, it&#8217;s not the issue it once was with the old 8-bit processors and severely limited memory. Instead, the issue of speed has shifted to design and re-design. However, I&#8217;m a sucker for squeezing more speed from a program (as the Flyweight patterns does) and it is something that I think is worth a post. However, here&#8217;s a list of what I have to cover first:</p>
<p>Deal with my client&#8217;s project<br />
Finish re-writing a video player<br />
Posts on the following topics:<br />
&nbsp;&nbsp;&nbsp;&nbsp;Open/Closed Principle<br />
&nbsp;&nbsp;&nbsp;&nbsp;Dependency Inversion Principle<br />
&nbsp;&nbsp;&nbsp;&nbsp;Least Knowledge Principle<br />
&nbsp;&nbsp;&nbsp;&nbsp;Hollywood Principle<br />
&nbsp;&nbsp;&nbsp;&nbsp;One reason to change principle<br />
&nbsp;&nbsp;&nbsp;&nbsp;Part II of Builder Pattern</p>
<p>Possibly Chandima might be interested and we could have something sooner, but until I get these other projects off my desk, I can&#8217;t put anything new there. However, it is a perfect topic for this blog. (Maybe you&#8217;d like to write a post on it?)</p>
<p>Thanks for the great idea,<br />
Bill</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jiri</title>
		<link>http://www.as3dp.com/2009/03/08/actionscript-30-easy-and-practical-strategy-design-pattern/comment-page-1/#comment-1822</link>
		<dc:creator>Jiri</dc:creator>
		<pubDate>Wed, 18 Mar 2009 09:26:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/?p=749#comment-1822</guid>
		<description>Bill, one more question. I think it would be a very interesting topic to write about object pooling. There are allready some nice classes out there. I recently developed in interest in it, because using them can mean a huge speed performance.</description>
		<content:encoded><![CDATA[<p>Bill, one more question. I think it would be a very interesting topic to write about object pooling. There are allready some nice classes out there. I recently developed in interest in it, because using them can mean a huge speed performance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jiri</title>
		<link>http://www.as3dp.com/2009/03/08/actionscript-30-easy-and-practical-strategy-design-pattern/comment-page-1/#comment-1821</link>
		<dc:creator>Jiri</dc:creator>
		<pubDate>Wed, 18 Mar 2009 09:23:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/?p=749#comment-1821</guid>
		<description>@Curro You are right with the wildcard as a return type. I was thinking about it myself, but was thinking you would always have to case the return type.
Thinking about the question &quot;what will happen if I make changes&quot; I would go for the solution of Curro, becuase it is the most flexible one.
It would be nice to encapuslate the creation of the Context to and get rid of the  switch/case, but I guess that would become a over designed thing :)

Jiri</description>
		<content:encoded><![CDATA[<p>@Curro You are right with the wildcard as a return type. I was thinking about it myself, but was thinking you would always have to case the return type.<br />
Thinking about the question &#8220;what will happen if I make changes&#8221; I would go for the solution of Curro, becuase it is the most flexible one.<br />
It would be nice to encapuslate the creation of the Context to and get rid of the  switch/case, but I guess that would become a over designed thing :)</p>
<p>Jiri</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: William B. Sanders</title>
		<link>http://www.as3dp.com/2009/03/08/actionscript-30-easy-and-practical-strategy-design-pattern/comment-page-1/#comment-1818</link>
		<dc:creator>William B. Sanders</dc:creator>
		<pubDate>Wed, 18 Mar 2009 01:24:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.as3dp.com/?p=749#comment-1818</guid>
		<description>Hi Curro,

In your code just type in the symbols as you always do. Your code was fine. I just added the &lt;pre&gt; tags to format it--never use the UTF-8 code in your programs.

A lot of developers (myself included) have used multiple Context classes--an abstract base class and multiple concrete implementations. However, I&#039;ve been going the other way--towards a single concrete Context class.

I suppose either way works. Anyway, the question to ask with design patterns is, &lt;em&gt;what will happen if I make changes&lt;/em&gt;? As long as you can keep the relations loose and functional, you will find many variations that you can work with.

Thanks for all your interest and very nice work.

Kindest regards,
Bill</description>
		<content:encoded><![CDATA[<p>Hi Curro,</p>
<p>In your code just type in the symbols as you always do. Your code was fine. I just added the &#60;pre&#62; tags to format it&#8211;never use the UTF-8 code in your programs.</p>
<p>A lot of developers (myself included) have used multiple Context classes&#8211;an abstract base class and multiple concrete implementations. However, I&#8217;ve been going the other way&#8211;towards a single concrete Context class.</p>
<p>I suppose either way works. Anyway, the question to ask with design patterns is, <em>what will happen if I make changes</em>? As long as you can keep the relations loose and functional, you will find many variations that you can work with.</p>
<p>Thanks for all your interest and very nice work.</p>
<p>Kindest regards,<br />
Bill</p>
]]></content:encoded>
	</item>
</channel>
</rss>
