<?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; Chain of Responsibility</title>
	<atom:link href="http://www.as3dp.com/category/design-patterns/chain-of-responsibility/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>From ActionScript 3.0 to JavaScript Chain of Responsibility: Part II The Help Desk</title>
		<link>http://www.as3dp.com/2012/01/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-ii-the-help-desk/</link>
		<comments>http://www.as3dp.com/2012/01/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-ii-the-help-desk/#comments</comments>
		<pubDate>Wed, 04 Jan 2012 22:29:47 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Chain of Responsibility]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://new.as3dp.com/?p=6352</guid>
		<description><![CDATA[A JavaScript Chain of Responsibility I&#8217;ve always liked JavaScript, but after going through this last project of creating a design pattern with JS, I find that I like ActionScript 3.0 even more than I did before. The biggest problems that I encountered with JavaScript is that different users slipped in different frameworks (e.g., Prototype.js) and [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6360" class="wp-caption alignleft" style="width: 260px"><a href="http://www.as3dp.com/wp-content/uploads/2012/01/jscor250.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/jscor250.png" alt="corjs" title="jscor250" width="250" height="333" class="size-full wp-image-6360" /></a><p class="wp-caption-text">JavaScript can do Design Patterns</p></div><strong>A JavaScript Chain of Responsibility</strong></p>
<p>I&#8217;ve always liked JavaScript, but after going through this last project of creating a design pattern with JS, I find that I like ActionScript 3.0 even more than I did before. The biggest problems that I encountered with JavaScript is that different users slipped in different frameworks (e.g., Prototype.js) and didn&#8217;t seem to mention it. My goal was to create a JS design pattern with pure unadulterated JavaScript—not using JQuery, JSon or some other helpful framework. Now, don&#8217;t get me wrong—I appreciate a good framework as much as the next guy. However, when I&#8217;m trying to create a design pattern for a general language like JavaScript, I&#8217;d rather not have to have readers run out and get one framework or another. I wanted this to work with Plain Vanilla JavaScript. So that&#8217;s what you&#8217;re going to get.</p>
<p>To get started, I took a look at some articles on JavaScript and classes, inheritance and OOP in general. Then I looked at some implementations of JavaScript with a Chain of Responsibility pattern. A lot of them didn&#8217;t make a bit of sense, and that&#8217;s when I realized that they were slipping in different &#8220;helpers,&#8221; and so I went back to the most basic JavaScript OOP I knew of&#8211;the 1999 book, <em>JavaScript Objects</em> by Nakhimovsky and Myers.  Starting there, I pretty much followed that Chain of Responsibility pattern we had in <a href="http://www.as3dp.com/2011/12/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-i/">Part I</a> of this two-part series. However, instead of a mobile browser detector, I created a &#8220;Help Desk.&#8221; </p>
<p>Before continuing, take a look at the &#8220;Help Desk&#8221; app made with the JS CoR:<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/dp/jscor/" target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/play.png" alt="" title="play" width="136" height="48" class="alignnone size-full wp-image-6372" /></a><br />
<div id="attachment_6385" class="wp-caption alignnone" style="width: 510px"><a href="http://www.as3dp.com/wp-content/uploads/2012/01/helpdesk.png"><img src="http://www.as3dp.com/wp-content/uploads/2012/01/helpdesk.png" alt="helpdesk" title="helpdesk" width="500" height="500" class="size-full wp-image-6385" /></a><p class="wp-caption-text">Figure 1: JavaScript Chain of Responsibility Help Desk</p></div></p>
<p><strong>Inheritance is a &#8216;IS-A&#8217;</strong></p>
<p>First I made an abstract Handler class using a single concrete and single abstract method. It seemed to me that classes in JavaScript could be abstract simply by adding a method with no content. I came up with the following 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('p6352code6'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p63526"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p6352code6"><pre class="javascript" style="font-family:monospace;">function Handler()
{ 
	//Handler 'abstract class'
} 
&nbsp;
Handler.prototype.setSuccessor=function(successor)
{ 
	this.successor=successor;
} 
&nbsp;
Handler.prototype.handleRequest=function(req)
{ 
	//Abstract method overridden in concrete implementation
}</pre></td></tr></table></div>

<p>I realize that it looks strange compared to more mature OOP languages like ActionScript 3.0 (not to mention Java and C++), but remember, I&#8217;m using pure native JavaScript. You create the class by the simple expedient of a function. Then, I create the methods for the class by using a <strong>Class.prototype</strong>. The class Handler now has the <strong>setSuccessor</strong> and <strong>handleRequest</strong> methods, just as we had in the ActionScript 3.0 version. So, while it looks a little goofy for creating classes and their methods and properties, that&#8217;s how it&#8217;s done.</p>
<p>So far so good, and it&#8217;s not rocket science. However, the next part gets a little dicey and may pop a few brain nodes. When you extend a class in JavaScript, you first instantiate the parent class. As we all know, you do not instantiate abstract classes or any other interface. They&#8217;re extended or implemented. However, that&#8217;s not the case with JavaScript. Take a look at this first child class of Handler:</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('p6352code7'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p63527"><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="p6352code7"><pre class="javascript" style="font-family:monospace;">//AS3DP inherits handler
AS3DP.prototype= new Handler();
&nbsp;
function AS3DP()
{
	//AS3DP 'class'
}
&nbsp;
AS3DP.prototype.handleRequest=function(req)
{
	this.req=req;
	if(this.req==&quot;as3dp&quot;)
	{
		document.write(&quot;Naturally you want to read &lt;em&gt;ActionScript 3.0 Design Patterns&lt;/em&gt; and visit our blog at as3dp.com.&quot;);
	}
	else if(this.successor != null)
	{
		document.write(&quot;Not AS3DP&lt;br/&gt;&quot;);
		this.successor.handleRequest(this.req);
	}
}</pre></td></tr></table></div>

<p>If you recall, basic design pattern principles, a child class &#8220;Is-A&#8221; parent class. When a JavaScript object (like AS3DP) is instantiated, it is done by declaring itself as a <strong>new Handler()</strong>. So rather than becoming a child of the parent through extension, JavaScript uses the object prototype to declare itself as an instance of the parent. It&#8217;s actually easier to see that a child class indeed &#8220;Is-A&#8221; parent because the prototype declares itself as such.</p>
<p>As we have seen, the methods and properties have been created <em>outside of the class constructors</em>. Instead of being <em>in the class</em>, they are <em>in the prototype</em>. For now, you can think of the class as the &#8220;concept of an object&#8221; and it creates and stores the properties for that concept in prototypes. So, knowing that, we can create the rest of the concrete handlers:</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('p6352code8'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p63528"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
</pre></td><td class="code" id="p6352code8"><pre class="javascript" style="font-family:monospace;">//Game inherits Handler
Game.prototype= new Handler();
&nbsp;
function Game()
{
	//Game 'class'
}
&nbsp;
Game.prototype.handleRequest=function(req)
{
	this.req=req;
	if(this.req==&quot;game&quot;)
	{
		document.write(&quot;As the states change, you want a good state machine like the State Design Pattern.&quot;);
	}
	else if(this.successor != null)
	{
		document.write(&quot;Not State pattern&lt;br/&gt;&quot;);
		this.successor.handleRequest(this.req);
	}
}
&nbsp;
//Algorithm inherits Handler
Algorithm.prototype= new Handler();
&nbsp;
function Algorithm()
{
	//Algorithm 'class'
}
&nbsp;
Algorithm.prototype.handleRequest=function(req)
{
	this.req=req;
	if(this.req==&quot;algorithm&quot;)
	{
		document.write(&quot;The Strategy Design Pattern lets you access different algorithms directly without having to use conditional statements!&quot;);
	}
	else if(this.successor != null)
	{
		document.write(&quot;Not Strategy pattern&lt;br/&gt;&quot;);
		this.successor.handleRequest(this.req);
	}
}
&nbsp;
//Factory inherits Handler
Factory.prototype= new Handler();
&nbsp;
function Factory()
{
	//Factory 'class'
}
&nbsp;
Factory.prototype.handleRequest=function(req)
{
	this.req=req;
	if(this.req==&quot;factory&quot;)
	{
		document.write(&quot;The Factory Method design pattern will unlink the request to build an object from the actual object creation.&quot;);
	}
	else if(this.successor != null)
	{
		document.write(&quot;Not Factory&lt;br/&gt;&quot;);
		this.successor.handleRequest(this.req);
	}
}
&nbsp;
//ToInterface inherits Handler
ToInterface.prototype= new Handler();
&nbsp;
function ToInterface()
{
	//ToInterface 'class'
}
&nbsp;
ToInterface.prototype.handleRequest=function(req)
{
	this.req=req;
	if(this.req==&quot;tointerface&quot;)
	{
		document.write(&quot;The first principle is to &lt;em&gt;Program to the interface and not the implementation.&lt;/em&gt; When you declare a new object, type it to the object's parent class; not the object itself.&quot;);
	}
	else if(this.successor != null)
	{
		document.write(&quot;Not Program to Interface&lt;br/&gt;&quot;);
		this.successor.handleRequest(this.req);
	}
}
&nbsp;
//Truth inherits Handler
Truth.prototype= new Handler();
&nbsp;
function Truth()
{
	//Truth 'class'
}
&nbsp;
Truth.prototype.handleRequest=function(req)
{
&nbsp;
	document.write(&quot;You know I can't handle the truth! Go visit a muse, a swami, or 3-year old.&lt;br/&gt;&quot;);
&nbsp;
}</pre></td></tr></table></div>

<p>As you no doubt have noticed, this Chain of Responsibility implementation is not about mobile devices and their languages. Instead, it&#8217;s a &#8220;Help Desk&#8221; that I think is more typical of what you may actually use a CoR pattern for—either in JavaScript or ActionScript 3.0.</p>
<p>All that&#8217;s left is to do with the JavaScript is to create instances of the different handlers and set the chain of successors. The &#8220;client&#8221; that makes the requests is the HTML5 UI.</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('p6352code9'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p63529"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p6352code9"><pre class="javascript" style="font-family:monospace;">//'Instantiate Concrete Handlers'
var as3dp = new AS3DP();
var game=new Game();
var algorithm=new Algorithm();
var factory=new Factory();
var tointerface=new ToInterface();
var truth=new Truth();
//Set successors
as3dp.setSuccessor(game); 
game.setSuccessor(algorithm); 
algorithm.setSuccessor(factory);
factory.setSuccessor(tointerface);
tointerface.setSuccessor(truth);</pre></td></tr></table></div>

<p>You can set up the successors in any way you want except the last one has to be to the <strong>Truth</strong> class. It has no successor and is meant to be the caboose of the chain. Before going on, place all of the JavaScript into a single file and save it as &#8220;HelpDesk.js&#8221; and put it in the same folder (directory) as the HTML5 program that follows.</p>
<p><strong>The HTML5 Client</strong></p>
<p>Like most work with JavScript, the UI is in HTML. This is no different. All it does is to call the top of the chain (<strong>AS3DP</strong> instance) and make a request. All of the requests look like the following:</p>
<blockquote><p>onclick=&#8221;as3dp.handleRequest(&#8216;algorithm&#8217;);</p></blockquote>
<p>The click handler calls the top of the chain, and the Chain of Responsibility just chugs on through until it finds what you want. Here&#8217;s the whole HTML5 code:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6352code10'); return false;">View Code</a> HTML5</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p635210"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
</pre></td><td class="code" id="p6352code10"><pre class="html5" style="font-family:monospace;">&lt;!DOCTYPE HTML&gt;
&lt;html&gt;
&lt;head&gt;
&lt;style type=&quot;text/css&quot;&gt;
body {
	font-family:Verdana, Geneva, sans-serif;
	color:#1D232;
	background-color:#dddcc5;
	font-size:12px;
}
h2 {
	font-family:&quot;Arial Black&quot;, Gadget, sans-serif;
	font-size:24px;
	text-align:center;
	color:#611427;
}
form {
	color:#6A6A61;
}
h3 {
	background-color:#611427;
	color:#958976;
	font-size:18px;
}
&lt;/style&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;HelpDesk.js&quot;&gt;
&lt;/script&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;Billz Help Desk&lt;/title&gt;
&lt;/head&gt;
&nbsp;
&lt;body&gt;
&lt;header&gt;
  &lt;h2&gt;Billz Help Desk&lt;/h2&gt;
&lt;/header&gt;
&lt;article&gt;
&lt;header&gt;
  &lt;h3&gt;&amp;nbsp;Chain of Responsibility: JavaScript&lt;/h3&gt;
&lt;/header&gt;
&lt;section&gt; This is a simple Chain of Responsibility (CoR) application. Each button represents a different request that the CoR will handle. In this case, the help is in the form of a 'Help Desk' and the CoR finds the appropriate response to the query.&lt;br/&gt;
  &lt;br/&gt;
  &lt;form&gt;
    &lt;input type=button value=&quot;Make Request&quot; onclick=&quot;as3dp.handleRequest('as3dp');&quot; /&gt;
    How do I learn about design patterns for ActionScript 3.0 &lt;br/&gt;
    &lt;input type=button value=&quot;Make Request&quot; onclick=&quot;as3dp.handleRequest('game');&quot; /&gt;
    I'm making a game where states keep changing. What would be a good design pattern for dealing with changing states?&lt;br/&gt;
    &lt;input type=button value=&quot;Make Request&quot; onclick=&quot;as3dp.handleRequest('algorithm');&quot; /&gt;
    My app involves a lot of problem solving with different algorithms? What's a good pattern for dealing with multiple algorithms?&lt;br/&gt;
    &lt;input type=button value=&quot;Make Request&quot; onclick=&quot;as3dp.handleRequest('factory');&quot; /&gt;
    Whenever I create a new object with my Client object, I build up dependencies. Is there a pattern to avoid such dependencies?&lt;br/&gt;
    &lt;input type=button value=&quot;Make Request&quot; onclick=&quot;as3dp.handleRequest('tointerface');&quot; /&gt;
    What is a primary guiding design pattern principle?&lt;br/&gt;
    &lt;input type=button value=&quot;Make Request&quot; onclick=&quot;as3dp.handleRequest('truth');&quot; /&gt;
    I want to know the truth!&lt;br/&gt;
  &lt;/form&gt;
&lt;/section&gt;
&lt;br/&gt;
&lt;section&gt;The CoR pattern works sequentially so that as it moves through the chain, you can see each rejected attempt to find the correct handler. (Normally, you wouldn't see those rejected handlers.) It is a very easy pattern to update and add additional handlers.&lt;/section&gt;
&lt;/article&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>So, now you&#8217;re all set to use JavaScript for design patterns. The plain vanilla JS has limitations doing OOP, but you can harness it and bend it to your OOP will. The many different frameworks that work in conjunction with basic JavaScript will make it look more like an OOP language, but you can get by without. There&#8217;s a lot to be done with even the most unadorned JavaScript, and I tested the app on an iPhone, and it works fine. However, I&#8217;ll bet that several of you can find better ways to handle either the JavaScript or HTML5 (or both) with design patterns, and we&#8217;d like to hear from you. Naturally, if you have a favored framework that makes OOP work more efficient, let us know—even better send a design pattern example.</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%2F2012%2F01%2Ffrom-actionscript-3-0-to-javascript-chain-of-responsibility-part-ii-the-help-desk%2F&amp;title=From%20ActionScript%203.0%20to%20JavaScript%20Chain%20of%20Responsibility%3A%20Part%20II%20The%20Help%20Desk" 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/2012/01/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-ii-the-help-desk/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>From ActionScript 3.0 to JavaScript Chain of Responsibility: Part I</title>
		<link>http://www.as3dp.com/2011/12/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-i/</link>
		<comments>http://www.as3dp.com/2011/12/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-i/#comments</comments>
		<pubDate>Mon, 26 Dec 2011 12:19:30 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Chain of Responsibility]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6348</guid>
		<description><![CDATA[Just Follow the Chain of Responsibility To get started on the quest to see whether useful design patterns can be created with JavaScript, I thought it&#8217;d be a good idea to go from the known to the unknown. In this case, those familiar with this blog know ActionScript 3.0 design patterns, and the unknown (for [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6349" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/12/cordad.png" alt="After Mom, Ask Dad, and then the Dog" title="cordad" width="250" height="201" class="size-full wp-image-6349" /><p class="wp-caption-text">After Mom, Ask Dad, and then the Dog</p></div><strong>Just Follow the Chain of Responsibility</strong></p>
<p>To get started on the quest to see whether useful design patterns can be created with JavaScript, I thought it&#8217;d be a good idea to go from the known to the unknown. In this case, those familiar with this blog know ActionScript 3.0 design patterns, and the unknown (for some at least) is JavaScript. Well, I suppose just about everyone who reads this blog knows something about JavaScript, but several may not have been involved with either JavaScript OOP or design patterns. By creating an ActionScript 3.0 app using a pattern and then going through the app step-by-step, readers may be better able to understand how we might approach design patterns using JavaScript. I decided to use the <strong>Chain of Responsibility</strong> (CoR) pattern to go through the types of mobile operating systems that we examined in the <a href="http://www.as3dp.com/2011/11/28/can-javascript-create-design-patterns/">previous post</a>. In this way, we can create something both familiar and practical.</p>
<p>Every time I use the <strong>Chain of Responsibility</strong> pattern, I feel that I&#8217;m swatting a fly with a weapon of mass destruction. I see all of these classes, and I&#8217;m thinking that I could have done the same thing with a switch statement or something even slicker. Then I need to remind myself why I&#8217;d use the <strong>CoR </strong>in the first place.</p>
<blockquote><p>The Chain of Responsibility separates the handling of an event from the request to handle it.</p></blockquote>
<p> In a broader context, this allows the developer to make changes to either the request or the handling of the event without disrupting some other part of the program. It&#8217;s quite simple as well. Just imagine several people lined up with different kinds of expertise and/or authority. A request is issued, and once it reaches the person with the right expertise or authority, it is handled.  The request is separated from the way it will be handled. That&#8217;s up to the expert. Figure 1 shows the class design:</p>
<div id="attachment_57" class="wp-caption alignnone" style="width: 499px"><img src="http://www.as3dp.com/wp-content/uploads/2008/01/corcldiagram.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1: &lt;/strong&gt; Chain of Responsibility Pattern&lt;/em&gt;" title="Chain of Responsibility Pattern" width="489" height="253" class="size-full wp-image-57" /><p class="wp-caption-text"><em><strong>Figure 1: </strong> Chain of Responsibility Pattern</em></p></div>
<p><strong>Getting Started with CoR</strong></p>
<p>For details about ways to setup and use <strong>CoR</strong> see our <a href="http://www.as3dp.com/2008/01/14/actionscript-30-chain-of-responsibility-design-pattern-decoupling-request-and-request-handler/">original discussion </a>. The focal points are in the following:</p>
<ul>
<li>An interface (an abstract class or interface) establishes a handler operation</li>
<li>Each request handler has a separate class derived from the handler interface</li>
<li>Each handler class has a successor which is another handler class</li>
<li>The last handler in the chain has no successor</li>
<li>As soon as a handler can handle the request, the chain stops and the handler takes care of the request.</li>
</ul>
<p>You may be thinking that this is not very efficient since it requires a sequential path. We&#8217;ve considered ways to get around a sequence (see our posts on <a href="http://www.as3dp.com/2010/05/11/actionscript-3-0-skip-lists-1-the-quickest-route-home/">Skip Lists</a>), but for now we&#8217;ll stick with the traditional CoR and use a sequential search through the list of handlers.</p>
<p><strong>The Chain of Mobile Operating Systems</strong></p>
<p>This CoR simulates finding one of several types of mobile OS. Given the type of OS, it looks for ways to handle each one optimally. In the JavaScript version of this, we want to have it select a CSS file or another JS file to best use the kind of mobile device involved.  The concrete handler classes are the objects used to deal with whatever requirements are necessary. In the example, each handler simply traces out &#8220;Set up for xxOS,&#8221;  where &#8220;xx&#8221; is the found mobile OS. It also traces out which handlers were rejected so that you can better see how the chain works. Since Android is at the top of the chain, you see no rejections, while Windows CE at the bottom of the chain displays the whole chain.</p>
<p>It might be helpful if you take a look at the whole concept of <a href="http://www.as3dp.com/2010/05/07/actionscript-3-0-linked-lists-the-road-to-skip-lists/">Linked Lists</a>. The Chain of Responsibility is something like a linked list, but instead of linking lists, it links handlers.</p>
<p>The Client class makes the initial request and sets up a simple requesting UI for testing the application. It makes requests using a string. I just used the lowercase ID of the main mobile OS types as the &#8220;request-to-handle.&#8221;  After making the request, the &#8220;chain&#8221; takes over. In this example the top of the chain is the Android, but it could be any of the others as well. The developer has complete control over the sequence.</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('p6348code14'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p634814"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
</pre></td><td class="code" id="p6348code14"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #0066CC;">Button</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #006600;">TextInput</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #006600;">Label</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldAutoSize</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Handlers + Request</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> android:Handler= <span style="color: #000000; font-weight: bold;">new</span> Android<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> iphone:Handler= <span style="color: #000000; font-weight: bold;">new</span> IPhone<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> blackberry:Handler= <span style="color: #000000; font-weight: bold;">new</span> Blackberry<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> series60:Handler=<span style="color: #000000; font-weight: bold;">new</span> Series60<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> windowsce:Handler=<span style="color: #000000; font-weight: bold;">new</span> WindowsCE<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> cannothandle:Handler=<span style="color: #000000; font-weight: bold;">new</span> CannotHandle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">//UI</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> btn:<span style="color: #0066CC;">Button</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Button</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> iput:TextInput=<span style="color: #000000; font-weight: bold;">new</span> TextInput<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> lbl:Label=<span style="color: #000000; font-weight: bold;">new</span> Label<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setupChain<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			setupUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setupChain<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//Sequence set up here</span>
			android.<span style="color: #006600;">SetSuccessor</span><span style="color: #66cc66;">&#40;</span>iphone<span style="color: #66cc66;">&#41;</span>;
			iphone.<span style="color: #006600;">SetSuccessor</span><span style="color: #66cc66;">&#40;</span>blackberry<span style="color: #66cc66;">&#41;</span>;
			blackberry.<span style="color: #006600;">SetSuccessor</span><span style="color: #66cc66;">&#40;</span>series60<span style="color: #66cc66;">&#41;</span>;
			series60.<span style="color: #006600;">SetSuccessor</span><span style="color: #66cc66;">&#40;</span>windowsce<span style="color: #66cc66;">&#41;</span>;
			windowsce.<span style="color: #006600;">SetSuccessor</span><span style="color: #66cc66;">&#40;</span>cannothandle<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setupUI<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//This simulates getting the mobile OS from the using system</span>
			lbl.<span style="color: #0066CC;">autoSize</span> = TextFieldAutoSize.<span style="color: #0066CC;">LEFT</span>;
			lbl.<span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;Enter name of mobile device: (Use lower case.)&quot;</span>;
			lbl.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">50</span>,lbl.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">30</span>;
			addChild<span style="color: #66cc66;">&#40;</span>lbl<span style="color: #66cc66;">&#41;</span>;
			btn.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">50</span>, btn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">75</span>;
			btn.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Start the chain&quot;</span>;
			btn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,requestOS<span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btn<span style="color: #66cc66;">&#41;</span>;
			iput.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">50</span>,iput.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">50</span>;
			addChild<span style="color: #66cc66;">&#40;</span>iput<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> requestOS<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:MouseEvent<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			android.<span style="color: #006600;">HandleRequest</span><span style="color: #66cc66;">&#40;</span>iput.<span style="color: #0066CC;">text</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>For the end of the chain, I added a &#8220;<strong>CannotHandle</strong>&#8221; class that extends the <strong>Handler</strong> interface. This functions something like a <strong>default</strong> option in a <strong>switch</strong> statement. If none of the handlers can handle the request, you need to provide the user with some kind of feedback.<br />
<span id="more-6348"></span><br />
<strong>The Abstract and Concrete Handlers</strong></p>
<p>In this application the interface is an abstract class, named <strong>Handler</strong>.  The abstract method, <strong>HandleRequest()</strong> takes a string argument and checks if it&#8217;s the appropriate class to handle the request, and if not, passes it on to the successor. The last concrete <strong>Handler</strong> object is always the <strong>CannotHandle</strong> 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('p6348code15'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p634815"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code" id="p6348code15"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Abstract class</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Handler
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> successor:Handler;
		protected <span style="color: #000000; font-weight: bold;">var</span> mobileType:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SetSuccessor <span style="color: #66cc66;">&#40;</span>successor:Handler<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">successor</span>=successor;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> HandleRequest <span style="color: #66cc66;">&#40;</span>req:<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: #808080; font-style: italic;">//Provide details in subclasses</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>With ActionScript 3.0, you have to make up abstract classes by including at least one method that you want to be abstract and must be overridden when implemented. In this case the <strong>HandleRequest(reg:String)</strong> method expects to be overridden. On the other hand, the <strong>SetSuccessor</strong> method that expects a concrete handler as an argument is inherited all set to use. Just add an argument. In deciding where to implement the <strong>SetSuccessor</strong> method, I decided to use the Client. In this way the developer can clearly see the order of the chain. Further, if changes are made, the <strong>CannotHandle</strong> class can easily be moved to the end of the chain without having to change the sequencing within the concrete handler classes.</p>
<p>The two handler properties, <strong>successor</strong> and <strong>mobileType</strong> are used to identify the successor class (object) and store the name of the operating system, respectively. All that&#8217;s left is to provide the concrete implementations which involves two steps:</p>
<ol>
<li>Overidding the HandleRequest() method and adding whatever you want the class to do if it&#8217;s to handle the request.</li>
<li>Sending the search off to the successor if it cannot handle the request.</li>
</ol>
<p>That&#8217;s not exactly rocket science. This is a very easy design pattern to implement. I added a private  method to each class, <strong>sendBack()</strong> that represents a more complex handling of any request outside of the conditional statement that identifies the handler as the appropriate one for the request. The more differentiated and complex the handlers, the more that you can appreciate the CoR. You can add anything you need to handle a request without having to worry about the requester (Client) or the other ways available to handle a request. Further, when you add or change a handler to the chain, it will not disrupt the rest of the program.</p>
<p>The following six classes represent concrete handlers. All of them are almost identical other than having different mobile OS&#8217;s to sort out. The CannotHandle concrete handler is just a caboose the both mark the end of the chain and let the user know that his request cannot be handled. It has no successor and the <strong>sendBack()</strong> function tells the user that her request cannot be handled.</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('p6348code16'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p634816"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
</pre></td><td class="code" id="p6348code16"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Android</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Android <span style="color: #0066CC;">extends</span> Handler
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Concrete handler</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> HandleRequest<span style="color: #66cc66;">&#40;</span>req:<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: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>req ==<span style="color: #ff0000;">&quot;android&quot;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				mobileType = <span style="color: #ff0000;">&quot;Now setting up &quot;</span> + req + <span style="color: #ff0000;">&quot; environment.&quot;</span>;
				sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>successor <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Not Android&quot;</span><span style="color: #66cc66;">&#41;</span>;
				successor.<span style="color: #006600;">HandleRequest</span><span style="color: #66cc66;">&#40;</span>req<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mobileType<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//iPhone</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> IPhone <span style="color: #0066CC;">extends</span> Handler
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Concrete handler</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> HandleRequest<span style="color: #66cc66;">&#40;</span>req:<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: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>req ==<span style="color: #ff0000;">&quot;iphone&quot;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				mobileType = <span style="color: #ff0000;">&quot;Now setting up &quot;</span> + req + <span style="color: #ff0000;">&quot; environment.&quot;</span>;
				sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>successor <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Not iPhone&quot;</span><span style="color: #66cc66;">&#41;</span>;
				successor.<span style="color: #006600;">HandleRequest</span><span style="color: #66cc66;">&#40;</span>req<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mobileType<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//BlackBerry</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Blackberry <span style="color: #0066CC;">extends</span> Handler
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Concrete handler</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> HandleRequest<span style="color: #66cc66;">&#40;</span>req:<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: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>req ==<span style="color: #ff0000;">&quot;blackberry&quot;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				mobileType = <span style="color: #ff0000;">&quot;Now setting up &quot;</span> + req + <span style="color: #ff0000;">&quot; environment.&quot;</span>;
				sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>successor <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Not Blackberry&quot;</span><span style="color: #66cc66;">&#41;</span>;
				successor.<span style="color: #006600;">HandleRequest</span><span style="color: #66cc66;">&#40;</span>req<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mobileType<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Series60</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Series60 <span style="color: #0066CC;">extends</span> Handler
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Concrete handler</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> HandleRequest<span style="color: #66cc66;">&#40;</span>req:<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: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>req ==<span style="color: #ff0000;">&quot;series60&quot;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				mobileType = <span style="color: #ff0000;">&quot;Now setting up &quot;</span> + req + <span style="color: #ff0000;">&quot; environment.&quot;</span>;
				sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>successor <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Not Series60&quot;</span><span style="color: #66cc66;">&#41;</span>;
				successor.<span style="color: #006600;">HandleRequest</span><span style="color: #66cc66;">&#40;</span>req<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mobileType<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Windows CE</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> WindowsCE <span style="color: #0066CC;">extends</span> Handler
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Concrete handler</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> HandleRequest<span style="color: #66cc66;">&#40;</span>req:<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: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>req ==<span style="color: #ff0000;">&quot;windowsce&quot;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				mobileType = <span style="color: #ff0000;">&quot;Now setting up &quot;</span> + req + <span style="color: #ff0000;">&quot; environment.&quot;</span>;
				sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>successor <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Not WindowsCE&quot;</span><span style="color: #66cc66;">&#41;</span>;
				successor.<span style="color: #006600;">HandleRequest</span><span style="color: #66cc66;">&#40;</span>req<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mobileType<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//CannotHandle</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CannotHandle <span style="color: #0066CC;">extends</span> Handler
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Concrete handler</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> HandleRequest<span style="color: #66cc66;">&#40;</span>req:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			mobileType = <span style="color: #ff0000;">&quot;Rats! We looked and couldn't find that mobile OS. <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
			mobileType += <span style="color: #ff0000;">&quot;We'll just have to get to work and see if we can <span style="color: #000099; font-weight: bold;">\n</span>develop a way to handle it for you.&quot;</span>;
			sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> sendBack<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mobileType<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>Testing the Chain</strong></p>
<p><div id="attachment_6382" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/12/ui.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2: &lt;/strong&gt; UI provides a way to test the chain.&lt;/em&gt;" title="ui" width="250" height="100" class="size-full wp-image-6382" /><p class="wp-caption-text"><em><strong>Figure 2: </strong> UI provides a way to test the chain.</em></p></div>When all is said and done, this pattern is pretty simple. Essentially the Client makes a request through the Handler interface and one of the concrete handlers deals with the request. If it cannot deal with it, it sends it along the chain until one of the handlers can or report that it cannot handle the request. The UI in this example (see Figure 2) simulates a JavaScript method for finding the current user&#8217;s mobile OS.</p>
<p>If an OS near the end of the chain is selected, you can see all of the links in the chain used to find the correct handler. Figure 3 shows what occurs if &#8220;windowsce&#8221; is requested. Had &#8220;android&#8221; been the search OS, it would have popped up without any links in the chain showing it was not found.<br />
<div id="attachment_6383" class="wp-caption alignnone" style="width: 443px"><img src="http://www.as3dp.com/wp-content/uploads/2011/12/output.png" alt="&lt;em&gt;&lt;strong&gt;Figure 3: &lt;/strong&gt; Output showing steps in sequence and feedback.&lt;/em&gt;" title="output" width="433" height="151" class="size-full wp-image-6383" /><p class="wp-caption-text"><em><strong>Figure 3: </strong> Output showing steps in sequence and feedback.</em></p></div></p>
<p>Finally, if the request cannot be handled, an important development step is to add a handler that tells the user that his OS cannot be found. The final handler in a chain has no successor, and <em>you definitely <strong>do not</strong> want to link back to the beginning of the chain!</em> That would set up an infinite loop. Figure 4 shows the feedback when a request cannot be handled:<div id="attachment_6386" class="wp-caption alignnone" style="width: 505px"><img src="http://www.as3dp.com/wp-content/uploads/2011/12/cannot.png" alt="&lt;em&gt;&lt;strong&gt;Figure 4: &lt;/strong&gt; Feedback for an unhandled request.&lt;/em&gt;" title="cannot" width="495" height="193" class="size-full wp-image-6386" /><p class="wp-caption-text"><em><strong>Figure 4: </strong> Feedback for an unhandled request.</em></p></div></p>
<p><strong>JavaScript is the Next Link</strong></p>
<p>In Part II, I&#8217;d like to step through these same classes using JavaScript prototype classes and see if we can use the same pattern. I think that if each concrete handler can configure the screen for a particular mobile device, it would be quite useful. As new devices are added, such as iPad, Kindle Fire, and various other tablets, you will find that the flexibility afforded by the Chain of Responsibility. To get ready for Part II, go over the ActionScript 3.0 version and make sure everything makes sense. Send in comments if you have a question.</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%2F12%2Ffrom-actionscript-3-0-to-javascript-chain-of-responsibility-part-i%2F&amp;title=From%20ActionScript%203.0%20to%20JavaScript%20Chain%20of%20Responsibility%3A%20Part%20I" id="wpa2a_4"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/12/from-actionscript-3-0-to-javascript-chain-of-responsibility-part-i/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Can JavaScript Create Design Patterns?</title>
		<link>http://www.as3dp.com/2011/11/can-javascript-create-design-patterns/</link>
		<comments>http://www.as3dp.com/2011/11/can-javascript-create-design-patterns/#comments</comments>
		<pubDate>Mon, 28 Nov 2011 12:48:29 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Chain of Responsibility]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=6322</guid>
		<description><![CDATA[Ever since Adobe announced that they&#8217;d no longer be making a Flash player for mobile devices, I thought that it&#8217;d be a good idea to have some kind of filter to distinguish between desktop browsers and mobile ones. On this blog, I&#8217;d like to introduce a topic that is related to the continuing efforts to [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_6321" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/11/badDecision.jpg" alt="Face the Facts: We&#039;ve got to deal with Mobile" title="badDecision" width="250" height="185" class="size-full wp-image-6321" /><p class="wp-caption-text">Face the Facts: We've got to deal with Mobile</p></div>Ever since Adobe announced that they&#8217;d no longer be making a Flash player for mobile devices, I thought that it&#8217;d be a good idea to have some kind of filter to distinguish between desktop browsers and mobile ones. On this blog, I&#8217;d like to introduce a topic that is related to the continuing efforts to build apps and Web pages with ActionScript 3.0 using OOP and Design Patterns. The topic is a language: JavaScript.</p>
<p>At this point in time I can easily create a script to determine whether a browser is mobile or non-mobile. I want to get to the point where I can put together a Chain of Responsibility pattern so that I have a flexible design for tweaking  a specific kind of mobile device (e.g., calling up appropriate CSS). This requires OOP JavaScript, and so I had to revisit my old JavaScript work. One of the best books on both OOP and JavaScript is <em>Javascript Objects: Object Use and Data Manipulation with JavaScript</em> by Alenander Nakhimovsky and Tom Myers published by Wrox in 1998. It was written by a couple of professors at Colgate University and  is pitched at a pretty high level that most readers of this blog will appreciate. (You can still get it from Amazon, and it&#8217;s a gem to have no matter what language you favor.) Another good book that deals with JavaScript objects is Douglas Crockford&#8217;s <em>JavaScript: The Good Parts</em> (O&#8217;Reilly, 2008). Crockford understands JavaScript down to the metal and has great explanations and examples.</p>
<p>So the question is:</p>
<blockquote><p>Can JavaScript handle OOP and Design Patterns?</p></blockquote>
<p>To find out, I started writing some prototype classes but found I&#8217;d have to spend some more time trying to cobble something together that was both reasonable (it maintains the DP value of flexibility, update and reuse) and real (I didn&#8217;t want to make a phony DP with hack classes.) However, it didn&#8217;t take long to put together a little code that works to filter out both different mobile devices and non-mobile devices. My plan is to take this code and change it so that I can do the same thing with a <strong>Chain of Responsibility</strong> design pattern. Here&#8217;s the code:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p6322code18'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p632218"><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="p6322code18"><pre class="javascript" style="font-family:monospace;">// JavaScript Document
var sniffer=new Object();
var agentNow=navigator.userAgent.toLowerCase();
sniffer.android=(agentNow.search(&quot;android&quot;)&gt;=0);
sniffer.series60=(agentNow.search(&quot;series60&quot;)&gt;=0);
sniffer.iphone=(agentNow.search(&quot;iphone&quot;)&gt;=0);
sniffer.blackberry=(agentNow.search(&quot;blackberry&quot;)&gt;=0);
sniffer.windowsce=(agentNow.search(&quot;windows ce&quot;)&gt;=0);
var counter=0;
var flag=new Object();
flag.nonMobile=true;
for(var mobile in sniffer)
{
	if(sniffer[mobile])
	{
		switch(counter) {
		case 0:
		document.write(&quot;android&lt;br/&gt;&quot;);
		flag.nonMobile=false;
		break;
		case 1:
		document.write(&quot;series60&lt;br/&gt;&quot;);
		flag.nonMobile=false;
		break;
		case 2:
		document.write(&quot;iphone&lt;br/&gt;&quot;);
		flag.nonMobile=false;
		break;
		case 3:
		document.write(&quot;blackberry&lt;br/&gt;&quot;);
		flag.nonMobile=false;
		break;
		case 4:
		document.write(&quot;windows ce&lt;br/&gt;&quot;);
		flag.nonMobile=false;
		break;
		}
	}
	counter++;
}
if(flag.nonMobile)
{
		document.write(&quot;Non-mobile or unrecognized device.&quot;);
		document.write(&quot;&lt;br/&gt;&quot;);
}</pre></td></tr></table></div>

<p>Before you say anything (or think anything), I realize that this code is about as flexible as a rebar (AKA: reinforcing bar), and if you&#8217;re into JavaScript, you can think of 10 ways that this can be done better. However, the goal is to see if this can be  refactored into a design pattern. Here&#8217;s my plan:</p>
<ol>
<li>Set up a dummy Chain of Responsibility that links the different kinds of responsibilities.</li>
<li>Have each responsibility be actions taken for different kinds of mobile devices or a desktop.</li>
<li>Use JavaScript objects to do the work of the concrete handlers</li>
</ol>
<p>However, before I get started on this project, I&#8217;d really like to hear from you and even better, see what you can do with this challenge. So between now and the next post on this topic, let&#8217;s hear from you!  Also feel free to add different kinds of mobile devices such as tablets and other phone OSs.</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%2F11%2Fcan-javascript-create-design-patterns%2F&amp;title=Can%20JavaScript%20Create%20Design%20Patterns%3F" id="wpa2a_6"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/11/can-javascript-create-design-patterns/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>PHP Chain of Responsibility Design Pattern from ActionScript 3.0</title>
		<link>http://www.as3dp.com/2009/11/php-chain-of-responsibility-design-pattern-from-actionscript-30/</link>
		<comments>http://www.as3dp.com/2009/11/php-chain-of-responsibility-design-pattern-from-actionscript-30/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 00:13:56 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Chain of Responsibility]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=2131</guid>
		<description><![CDATA[ARE Design Patterns portable from one language to another? Of course they are. I&#8217;ve read some accounts that claim otherwise, but I&#8217;ve not found any design pattern that could not be used in any computer language that I&#8217;ve tried. (Maybe I&#8217;m just conversant in languages where design patterns do work, but I don&#8217;t think so.) [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.as3dp.com/wp-content/uploads/2009/11/smallchain.png" alt="smallchain" title="smallchain" width="240" height="90" class="alignleft size-full wp-image-2165" />ARE Design Patterns portable from one language to another? Of course they are. I&#8217;ve read some accounts that claim otherwise, but I&#8217;ve not found any design pattern that could not be used in any computer language that I&#8217;ve tried. (Maybe I&#8217;m just conversant in languages where design patterns do work, but I don&#8217;t think so.) Anyway, Chandima and I have been discussing doing a set of PHP design patterns, and I wanted one for a contact form I had done using PHP.</p>
<p>For this blog, though, I thought I&#8217;d use a translation from one of the design patterns we have on this blog so that you can more easily see the similarities and differences between  ActionScript 3.0 and PHP  in implementing the same design pattern.</p>
<p><strong>Chain of Responsibility</strong></p>
<p>As you may recall from our <a href="http://www.as3dp.com/2008/01/14/actionscript-30-chain-of-responsibility-design-pattern-decoupling-request-and-request-handler/">Chain Of Responsibility (CoR) post</a>, the design pattern is used when you want to have a system that takes care of requests when you have different outcomes.  In our original example, we added a helper class called Request, but otherwise we stuck with the basics of the CoR structure. Figure 1 shows the class diagram of the CoR we used in our initial post.<br />
<div id="attachment_2135" class="wp-caption alignnone" style="width: 499px"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/phpcordia.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1: &lt;/strong&gt; Chain of Responsibility Class Diagram&lt;/em&gt;" title="phpcordia" width="489" height="253" class="size-full wp-image-2135" /><p class="wp-caption-text"><em><strong>Figure 1: </strong> Chain of Responsibility Class Diagram</em></p></div></p>
<p>In this particular design pattern the Client class <em>is</em> a full-fledged participant in the pattern. Because <strong>Client</strong> classes are <em>request objects</em> themselves, it  may seem odd to have a <strong>Request</strong> class. Just think of the Request class as a helper class that encapsulates requests for the Client.<br />
<span id="more-2131"></span><br />
The back story to this CoR implementation is that you have a company that needs to distribute pick-up trucks using the minimum size truck for any given load. So, you set up a CoR design to check the load requirements sent in, and then dispatch the smallest possible truck for a given load. (For other details of the CoR and ActionScript 3.0, check out the original CoR post.)</p>
<p><strong>The PHP implementation of CoR</strong></p>
<p>Assuming you&#8217;ve looked at the original CoR post, let&#8217;s jump right in to the PHP implementation. Figure 1 shows the exact same set of classes that we&#8217;ll use with the PHP version of the design. We&#8217;ll start at the heart of the CoR with the <strong>Handler</strong> class. PHP has true <em>abstract classes</em> and we can create the Handler using an abstract class. (In case you&#8217;re wondering, we probably could have  done the same thing with an interface, but tweaking seems easier using abstract classes.)</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2131code22'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p213122"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p2131code22"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
	abstract <span style="color: #000000; font-weight: bold;">class</span> Handler
	<span style="color: #009900;">&#123;</span>
		abstract <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> handleRequest<span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		abstract <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setSuccessor<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nextTruck</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The <strong>handleRequest()</strong> method uses an encapsulated request from the Request class that is instantiated in the Client. As you can see, PHP does not have strong typing; so the parameters are not typed. The setSuccessor() establishes the next link in the chain. If the current object (class) cannot provide what is requested, the request is passed to the successor.</p>
<p>For this example, three concrete Handler objects—GMC, Dodge and Ford—handle all requests. If a load is too heavy for the lightest truck, the request is passed on to the next one and so on until the request reaches the end of the chain. For this example, there&#8217;s no &#8220;too heavy&#8221; notice if the load surpasses the capacity of the largest truck, but it&#8217;d be easy to add such a notice or another class that provides remedies for loads too heavy for the largest truck. (In case you&#8217;re considering a solution, <em>think modulo</em>.)</p>
<p><strong>The Concrete Handlers</strong></p>
<p>The three concrete handlers represent the load capacities of three trucks. Each concrete handler looks at the load and if it can handle it, it says so. Otherwise, it passes the request up the line until it reaches the end of the chain or a truck with in the load parameters.  The following three classes represent concrete handlers:</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('p2131code23'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p213123"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
</pre></td><td class="code" id="p2131code23"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//GMC Class</span>
	<span style="color: #000000; font-weight: bold;">class</span> GMC <span style="color: #000000; font-weight: bold;">extends</span> Handler
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$successor</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setSuccessor<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nextTruck</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">successor</span><span style="color: #339933;">=</span><span style="color: #000088;">$nextTruck</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> handleRequest <span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPounds</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1547</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;A GMC Canyon would be good for this. Weight: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPounds</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">successor</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">successor</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">handleRequest</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
//Dodge class
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #000000; font-weight: bold;">class</span> Dodge <span style="color: #000000; font-weight: bold;">extends</span> Handler
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$successor</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setSuccessor<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nextTruck</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">successor</span><span style="color: #339933;">=</span><span style="color: #000088;">$nextTruck</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> handleRequest<span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPounds</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1750</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;A Dodge Dakota can handle this load. Weight: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPounds</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">successor</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">successor</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">handleRequest</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
//Ford class
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #000000; font-weight: bold;">class</span> Ford <span style="color: #000000; font-weight: bold;">extends</span> Handler
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$successor</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setSuccessor<span style="color: #009900;">&#40;</span><span style="color: #000088;">$nextTruck</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">successor</span><span style="color: #339933;">=</span><span style="color: #000088;">$nextTruck</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> handleRequest <span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPounds</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1890</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">echo</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;This load requires a Ford 150. Weight: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPounds</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">successor</span> <span style="color: #339933;">!=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">successor</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">handleRequest</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>As you can see, each link in the chain (concrete Handler class) uses a simple conditional statement to handle the request. The &#8220;handling&#8221; is merely using an <strong>echo</strong> statement to announce that a particular truck can handle the load. (In this case the Ford may be hopelessly unrealistic, but never mind.)</p>
<p><strong>The Client and Request</strong></p>
<p>I always like it when the Client class is named as a participant in a design pattern, and with CoR, it is a main one directly tied into the Handler. Just the Client, Handler interface, and the Concrete Handler classes make up CoR. When using PHP, you need a calling file, and so an added file calls the Client. The Request object is actually one method smaller than the original Request created for ActionScript. As a getter/setter the setter is in the Constructor function, and so it seemed redundant to add a setter method in addition to the Constructor. The getter method encapsulates the value of the request generated originally in the Client. All three files are included in the following listing:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2131code24'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p213124"><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
</pre></td><td class="code" id="p2131code24"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//Request class</span>
<span style="color: #000000; font-weight: bold;">class</span> Request
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$weight</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span><span style="color: #339933;">=</span><span style="color: #000088;">$weight</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPounds<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">value</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//Client class</span>
<span style="color: #000000; font-weight: bold;">class</span> Client
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$Acme</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GMC<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$Paramount</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Dodge<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$Pinacle</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Ford<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$Acme</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setSuccessor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Paramount</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$Paramount</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setSuccessor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Pinacle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Generate and process load requests</span>
		<span style="color: #000088;">$loadup</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Request<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1200</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$Acme</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">handleRequest</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$loadup</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$loadup</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Request<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1650</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$Acme</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">handleRequest</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$loadup</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$loadup</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Request<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1795</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$Acme</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">handleRequest</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$loadup</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//Testing file</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;display_errors&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">ERROR_REPORTING</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">E_ALL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Client.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Handler.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Request.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'GMC.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Dodge.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">include_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Ford.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$testClient</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Client<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>When you test the program, you should see the following output:</p>
<p><strong>A GMC Canyon would be good for this. Weight: 1200<br />
A Dodge Dakota can handle this load. Weight: 1650<br />
This load requires a Ford 150. Weight: 1795</strong></p>
<p>I added the input values (weight) to show that this CoR knew what it was doing. Albeit, it&#8217;s not doing much other than demonstrating the CoR structure. However, I&#8217;ve got big plans for the CoR in PHP for automatically handling inquiries. The more options available, the more useful the CoR becomes. And of course, since it&#8217;s a design pattern, adding and changing options is quite easy.</p>
<p><strong>Some Differences</strong></p>
<p>The biggest differences in the programs are PHP and ActionScirpt 3.0 syntax differences. Here are some you can see immediately:</p>
<ul>
<li>PHP uses <strong>-></strong> instead of <strong>.</strong> for linking  properties and methods to an object (e.g., <strong>myObj->myProp</strong> instead of <strong>myObj.myProp</strong>)</li>
<li>PHP has no data typing</li>
<li>PHP requires <strong>include</strong> for all the class files in use (in the testing file)</li>
<li>All variables have dollar signs (<strong>$</strong>) in front of them</li>
<li>PHP concatenation uses a dot (<strong>.</strong>) instead of a plus sign(<strong>+</strong>)</li>
<li>PHP constructor functions use <strong>__construct()</strong> instead of the class name</li>
</ul>
<p>Other kinds of differences are the  idiosyncrasies of each language, and you may have to push and pull a bit when applying design patterns of one to the other. However, the nice thing about design patterns is that they are generally applicable to all languages. I had thought that lack of typing to an interface (because of the weak typing of PHP) would be a deal-breaker, but I don&#8217;t think so any more. I suppose with a very complex program, you&#8217;d miss stronger typing, but from what I&#8217;ve seen PHP programs with a good structure work well and are easy to change,  are reusable and are loosely coupled. So, in case you&#8217;re considering creating PHP design patterns, I&#8217;d definitely encourage it, and you&#8217;ll find that converting examples from ActionScript 3.0 isn&#8217;t all that difficult.</p>
<p>Naturally, we&#8217;d like to hear from both ActionScript 3.0 and PHP developers to get your thoughts on this topic. We&#8217;d be very interested in applications that use both ActionScript 3.0 and PHP and linking up optimal design patterns so that modules from the two languages can more easily be integrated.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2009%2F11%2Fphp-chain-of-responsibility-design-pattern-from-actionscript-30%2F&amp;title=PHP%20Chain%20of%20Responsibility%20Design%20Pattern%20from%20ActionScript%203.0" id="wpa2a_8"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/11/php-chain-of-responsibility-design-pattern-from-actionscript-30/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Chain of Responsibility Design Pattern: Decoupling Request and Request Handler</title>
		<link>http://www.as3dp.com/2008/01/actionscript-30-chain-of-responsibility-design-pattern-decoupling-request-and-request-handler/</link>
		<comments>http://www.as3dp.com/2008/01/actionscript-30-chain-of-responsibility-design-pattern-decoupling-request-and-request-handler/#comments</comments>
		<pubDate>Mon, 14 Jan 2008 15:53:02 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Chain of Responsibility]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/2008/01/14/actionscript-30-chain-of-responsibility-design-pattern-decoupling-request-and-request-handler/</guid>
		<description><![CDATA[The Chain of Responsibility (CoR) design pattern is used when you need a request handled by the most appropriate object for the request. You don’t need to worry about which object handles the request or even if they’ll handle it the same all the time. For example, suppose you have a constantly changing marketplace and [...]]]></description>
			<content:encoded><![CDATA[<p>The Chain of Responsibility (CoR) design pattern is used when you need a request handled by the most appropriate object for the request. You don’t need to worry about which object handles the request or even if they’ll handle it the same all the time. For example, suppose you have a constantly changing marketplace and the specs of your request change as well. Rather than building an application that links a specific request to a specific request handler the CoR pattern decouples the two so that when a request is sent, all you know is that the most appropriate object will handle it. Our department buys Flash drives in bulk from China. In the request for the drives I put in a set of criteria and send the request to my Chinese buyer. He is instructed to get the lowest price for the drives as long as they meet the specs in the required bulk. Now I don’t know which manufacturer will win the contract (which object will handle the request), but since I trust my agent in China, I am confident he’ll get the best price even though the price will vary depending on everything from the dollar’s exchange rate with China to the availability of Flash drives. Because so many variables change, I need the flexibility that changes with both the request and the request handler. That’s something like the way the CoR design pattern works—it takes a request and finds the most appropriate way to handle it.</p>
<p><strong>Chain of Responsibility Overview</strong></p>
<p>Because looking at a Class Diagram is useful for seeing the larger context of the design pattern, we’ll look at it first and then go about describing its features.<br />
<a href='http://www.as3dp.com/wp-content/uploads/2008/01/corcldiagram.png' title=''><img src='http://www.as3dp.com/wp-content/uploads/2008/01/corcldiagram.png' alt='' /></a><br />
<em>Figure 1: Chain of Responsibility Class Diagram</em></p>
<p>This looks fairly simple, and at the basic level it really is. Like some of the other design patterns, the Client is part of the pattern, and so it’s integral. At the center of the pattern is the Handler interface. For the time being, think of the interface as an abstract class because that is what is used in the initial example. The abstract class includes a function for setting successors in a chain and another to handle a request. Finally, the ConcreteHandler classes represent the specific and different classes that handle requests. Generally speaking, an application would include several ConcreteHandler classes, and each is set up in a chain to deal with requests where appropriate.<br />
<span id="more-56"></span></p>
<p><strong>Unchain My Request</strong></p>
<p>In their work, the Gang of Four, illustrate the CoR using a Help desk. The Help desk is set up in a hierarchy with different Help topics. As a request moves through the hierarchy it attempts to find the concrete handler that deals with its query. Another good example (my favorite) is provided by the Freemans in their <em>Head First Design Patterns</em> book. Their example envisions a company that gets different kinds of email, and to sort it out, the CoR design pattern is used. It has separate handlers (ConcreteHandler) for fan mail, spam, complains and so forth. So, when an email arrives, it is automatically handled in the appropriate manner moving it along a chain. Both of these examples share the concept of uncoupling the request from the handler. In this way, both requests and handlers can be changed without disrupting the larger program. As anyone who has worked with a program to handle products or services in a rapidly changing environment knows, your design needs this kind of flexibility.</p>
<p>Figure 2 provides a general idea of a chain of concrete handlers set up in succession. When the request comes in, it is examined by ConcreteHandler A. If A cannot handle the request, it is passed on to ConcreteHandle B and so forth until it either runs out of concrete handlers or is handled. The different colored diamonds represent the different ways of handling a request. The process terminates once the request is handled.</p>
<p><a href='http://www.as3dp.com/wp-content/uploads/2008/01/chain.png' title=''><img src='http://www.as3dp.com/wp-content/uploads/2008/01/chain.png' alt='' /></a><br />
<em>Figure 2: Request Chain</em></p>
<p>If you want some kind of handler to deal with unhandled requests, the last concrete handler in the chain (ConcreteHandler E in Figure 2) could be a residual one that issues a notice informing the user that the request could not be handled. However,  the alternative of simply not handling a request because the criteria for handling is not in the chain of concrete handlers is perfectly acceptale.</p>
<p><strong>The Implicit Receiver</strong></p>
<p>One of the two key concepts in the CoR pattern is <strong><em>implicit receiver</em></strong>. Even though the requesting object may not have any idea of which concrete handler will handle a request, the one that actually handles it is the implicit receiver. That clears up a lot of possible confusion. Given the criteria of the request, only one of the concrete handlers can meet that criteria, and so implicitly that is the receiver. It’s a simple concept but one that you need to keep in mind.</p>
<p>Because the request is uncoupled from the receiver, you can easily add more concrete classes to be the implicit receiver. Imagine a business with a CoR that finds items in certain price ranges. Let’s say that a merchant finds that most people will spend $50 on a project. He has handlers for under $50 and from $50 to $100. Research shows that most of the under $50 sales are actually for around $20. To increase revenue, he adds a new concrete handler that offers a $35 to $65 range. Because he used a CoR design pattern, all he needs to do is to add a handler for that range and make minor adjustments to the concrete handlers below and above that range. Because the receivers are implicit and uncoupled from the request, such adjustments are easy and the program can be changed without having to refactor the entire application.</p>
<p><strong>The Successor</strong></p>
<p>The second key concept in the CoR is <strong><em>successor</em></strong>. To move a request along a chain and to keep the receiver implicit, each object must maintain a common interface. The successor defines a way to move along the chain of objects and shares the same interface as do the receiver objects (concrete classes). As a result, the pattern requires some method for specifying successors in the interface but must hold the interface. In the class diagram in Figure 2, you can see the successor in the Handler interface looping back to itself.</p>
<p>One way of setting up this method can be seen in the interface itself. A successor object is typed as the interface (Handler). Then the method parametizes the successor object. The following snippet shows how this looks:</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('p56code30'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5630"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p56code30"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Handler
        <span style="color: #66cc66;">&#123;</span>
                protected <span style="color: #000000; font-weight: bold;">var</span> successor:Handler;
&nbsp;
                <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SetSuccessor <span style="color: #66cc66;">&#40;</span>successor:Handler<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
                <span style="color: #66cc66;">&#123;</span>
                        <span style="color: #0066CC;">this</span>.<span style="color: #006600;">successor</span>=successor;
                <span style="color: #66cc66;">&#125;</span>
….</pre></td></tr></table></div>

<p>Each of the concrete handlers includes a way to pass on the request to the next  concrete handler using the successor object .</p>
<p><strong>A Minimalist Chain of Command Application </strong></p>
<p>In coming up with an application, I envisioned a building contractor who needed to haul different loads to a job site. In order to save money, he wanted to use a pickup truck that would best haul a given load but no more. In that way, he would not waste money on both gas and trucks that hauled greater loads that might be needed elsewhere.</p>
<p>The client sends out a request with a certain load weight, and the CoR picks out the best truck for the job. It begins with the smallest truck and works up the chain until he finds a truck that optimally handles the required load. It’s a simple application, but one where you can see all of the parts working together. The application has the following components:</p>
<ul>
<li>Handler class that acts as an interface for the application</li>
<li>Three concrete handler classes that act as receivers</li>
<li>A client class that makes the request</li>
<li>A request class that encapsulates the request</li>
</ul>
<p><strong>Handler Class</strong><br />
This first class acts as an interface. It is an abstract class in spirit (since ActionScript 3.0 and ECMAScript r4 have no real abstract 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('p56code31'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5631"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p56code31"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">//Abstract class</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Handler
	<span style="color: #66cc66;">&#123;</span>
		protected <span style="color: #000000; font-weight: bold;">var</span> successor:Handler;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SetSuccessor <span style="color: #66cc66;">&#40;</span>successor:Handler<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">successor</span>=successor;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> HandleRequest <span style="color: #66cc66;">&#40;</span>request:Request<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;">//Provide details in subclasses</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, the Handler class has two methods. The first method will be used to set the successor and the second method to handle requests. However, because the request handler involves different requirements for each concrete handler, the method remains abstract in the Handler class.</p>
<p><strong>Concrete Handler Classes</strong></p>
<p>Because the Handler class is an abstract one, all of the subclasses must override the methods in the Handler class. The HandleRequest () method needs to communicate  the limits of its ability to handle the request and  pass on to its successor if it cannot handle it. The following three concrete handlers do that:</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('p56code32'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5632"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
</pre></td><td class="code" id="p56code32"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//GMC.as</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// &quot;ConcreteHandler&quot;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GMC <span style="color: #0066CC;">extends</span> Handler
	<span style="color: #66cc66;">&#123;</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> HandleRequest <span style="color: #66cc66;">&#40;</span>request:Request<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>request.<span style="color: #006600;">Pounds</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">1547.0</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;A GMC Canyon would be good for this. Weight: &quot;</span>+request.<span style="color: #006600;">Pounds</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>successor <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				successor.<span style="color: #006600;">HandleRequest</span> <span style="color: #66cc66;">&#40;</span>request<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Dodge.as</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// &quot;ConcreteHandler&quot;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Dodge <span style="color: #0066CC;">extends</span> Handler
	<span style="color: #66cc66;">&#123;</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> HandleRequest <span style="color: #66cc66;">&#40;</span>request:Request<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>request.<span style="color: #006600;">Pounds</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">1750.0</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Dodge Dakota can handle this load. Weight: &quot;</span>+ request.<span style="color: #006600;">Pounds</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>successor <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				successor.<span style="color: #006600;">HandleRequest</span> <span style="color: #66cc66;">&#40;</span>request<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Ford.as</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// &quot;ConcreteHandler&quot;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Ford <span style="color: #0066CC;">extends</span> Handler
	<span style="color: #66cc66;">&#123;</span>
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> HandleRequest <span style="color: #66cc66;">&#40;</span>request:Request<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>request.<span style="color: #006600;">Pounds</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">1890</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;This load required a Ford 150. Weight: &quot;</span>+request.<span style="color: #006600;">Pounds</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>successor <span style="color: #66cc66;">!</span>= <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				successor.<span style="color: #006600;">HandleRequest</span> <span style="color: #66cc66;">&#40;</span>request<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>The concrete handlers evaluate the weight of the request. If the request is within the range of the limit, the request is handled, but if not, the request is sent to a successor, if any. In this particular implementation of the CoR, if the weight is over 1889, the request is not handled.</p>
<p><strong>Request and Client Classes</strong></p>
<p>The Client class implements the request and establishes the hierarchy in the chain of command. The Request class is simply a way of encapsulating the request, which is nothing more than a number. The getter and setter methods are set up and the value for a load is paramaritized in the main 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('p56code33'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5633"><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="p56code33"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Request.as</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">// Request details</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Request
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loadWeight:<span style="color: #0066CC;">Number</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">// Constructor</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Request <span style="color: #66cc66;">&#40;</span>loadWeight:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">loadWeight</span>=loadWeight;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">// Getters and Setters</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> Pounds <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> loadWeight;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> Pounds <span style="color: #66cc66;">&#40;</span>loadWeight:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			loadWeight=loadWeight;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>You will notice that all of the concrete handlers hold a reference to the Request class. In effect it is a mixin class used by both the Client class and ConcreteHandler classes. However, it is only instantiated by the Client class. If you feel queasy about using the Request class, you can easily change the ConcreteHandler classes to accept what amounts to a numeric literal. Then, each HandleRequest would simply accept a literal value directly rather than using the Request 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('p56code34'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5634"><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
</pre></td><td class="code" id="p56code34"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Client.as</span>
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #808080; font-style: italic;">// This is the Client</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> Acme:Handler;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> Paramount:Handler;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> Pinacle:Handler;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loadup:Request;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">// Setup Chain of Responsibility</span>
			Acme = <span style="color: #000000; font-weight: bold;">new</span> GMC<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			Paramount = <span style="color: #000000; font-weight: bold;">new</span> Dodge<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			Pinacle = <span style="color: #000000; font-weight: bold;">new</span> Ford<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			Acme.<span style="color: #006600;">SetSuccessor</span> <span style="color: #66cc66;">&#40;</span>Paramount<span style="color: #66cc66;">&#41;</span>;
			Paramount.<span style="color: #006600;">SetSuccessor</span> <span style="color: #66cc66;">&#40;</span>Pinacle<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// Generate and process load requests</span>
			loadup = <span style="color: #000000; font-weight: bold;">new</span> Request<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1200</span><span style="color: #66cc66;">&#41;</span>;
			Acme.<span style="color: #006600;">HandleRequest</span> <span style="color: #66cc66;">&#40;</span>loadup<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			loadup = <span style="color: #000000; font-weight: bold;">new</span> Request<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1650</span><span style="color: #66cc66;">&#41;</span>;
			Acme.<span style="color: #006600;">HandleRequest</span> <span style="color: #66cc66;">&#40;</span>loadup<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			loadup = <span style="color: #000000; font-weight: bold;">new</span> Request<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1795</span><span style="color: #66cc66;">&#41;</span>;
			Acme.<span style="color: #006600;">HandleRequest</span> <span style="color: #66cc66;">&#40;</span>loadup<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>In looking at the Client class, all of the request objects are typed as the interface—Handler rather than to a concrete class. Then, each instantiates a concrete handler, and sets up the succession. Three different request values should test each of the three concrete classes’ ability to recognize itself as the implicit receiver.</p>
<p>The Client also begins each request with the object that is at the beginning of the chain of responsibility. The smallest load is handled by the GMC class, and so the instance of that class (Acme) is always used to send out a request.</p>
<p>When you test the program, you will get the following output:</p>
<blockquote><p>    A GMC Canyon would be good for this. Weight: 1200<br />
    Dodge Dakota can handle this load. Weight: 1650<br />
    This load required a Ford 150. Weight: 1795</p></blockquote>
<p>Each of the three requests was handled by the appropriate concrete handler. Even better, if new kinds of requirements or requests are brought in, the changes are only in the concrete handlers.</p>
<p><strong>Reader Challenge</strong></p>
<p>As usual I’d like any feedback as far as improving the application, but more importantly, I’d like to see three added concrete handler classes. Here’s what they are:</p>
<ul>
<li>A lower base weight of 500 pounds for a mini-pickup.</li>
<li>A mid-size weight that can be inserted between the Dodge and the Ford</li>
<li>A class that would respond to the fact that the load weight they requested is too big for any of the available trucks.</li>
</ul>
<p>If the CoR design pattern has the kind of flexibility advertised, this ought to be a snap.</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%2F2008%2F01%2Factionscript-30-chain-of-responsibility-design-pattern-decoupling-request-and-request-handler%2F&amp;title=ActionScript%203.0%20Chain%20of%20Responsibility%20Design%20Pattern%3A%20Decoupling%20Request%20and%20Request%20Handler" id="wpa2a_10"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2008/01/actionscript-30-chain-of-responsibility-design-pattern-decoupling-request-and-request-handler/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

