<?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; Visitor</title>
	<atom:link href="http://www.as3dp.com/category/design-patterns/visitor/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>ActionScript 3.0 Visitor Design Pattern: A Tale of Traverser and the Double-Dispatch Kid</title>
		<link>http://www.as3dp.com/2008/12/actionscript-30-visitor-design-pattern-a-tale-of-traverser-and-the-double-dispatch-kid/</link>
		<comments>http://www.as3dp.com/2008/12/actionscript-30-visitor-design-pattern-a-tale-of-traverser-and-the-double-dispatch-kid/#comments</comments>
		<pubDate>Sat, 06 Dec 2008 22:05:26 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Visitor]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=318</guid>
		<description><![CDATA[The Visitor design pattern allows you to add operations to classes without changing the classes. That pretty well summarizes what the Visitor can do for you. Imagine you’ve got a great big game program and you want to tweak the functionality of certain objects in the game by adding operations (methods and functions). Rather than [...]]]></description>
			<content:encoded><![CDATA[<p>The Visitor design pattern allows you to add operations to classes without changing the classes. That pretty well summarizes what the Visitor can do for you. Imagine you’ve got a great big game program and you want to tweak the functionality of certain objects in the game by adding operations (methods and functions). Rather than opening a bag of marbles to spill out in every direction when you add functionality to a class, all you need to do is to use the Visitor to add that functionality without interfering with the rest of the class. If you’ve read Chandima’s chapter and posts on the <a href="http://www.as3dp.com/2007/05/26/composite-pattern-book-part-1/">Composite</a> pattern, you’re two jumps ahead. Typically, the Visitor is used with a Composite pattern. Likewise, Chandima’s post on the <a href="http://www.as3dp.com/category/design-patterns/iterator/">Iterator</a> will aid in understanding the traverse process used in the Visitor. Of course you can work perfectly well with the Visitor without background in these other patterns, but you might want to peek at them for further reference and understanding as you review the Visitor.</p>
<p><strong>The Visitor Design Pattern’s Formal Features</strong></p>
<p>First, take a look at the Visitor’s class diagram. You will note that the implied Client holds references to two different classes within the pattern. The factory patterns (Abstract Factory, Factory, Flyweight) all have similar multiple references from the Client, and so too does the Command, Interpreter and Iterator patterns. So don’t let the multiple references from the Client throw you. Figure 1 shows the Visitor class diagram:<br />
 <a href="http://www.as3dp.com/wp-content/uploads/2008/12/visitor.png"><img class="alignnone size-full wp-image-319" title="visitor" src="http://www.as3dp.com/wp-content/uploads/2008/12/visitor.png" alt="" width="500" height="309" /></a><br />
 <em> <strong>Figure 1:</strong>Visitor design pattern diagram</em></p>
<p>The Client reference to the Visitor interface (IVisitor in the example program) sets up implementations for concrete visitors. The concrete visitors visit the different concrete elements. At the same time, the client goes through the<strong> ObjectStructure</strong> and the ObjectStructure has multiple object references to the Element interface. The black ball at the end of the ObjectStructure reference arrow means that it has multiple references. Figure 2 isolates that relationship from the Visitor diagram.<br />
 <a href="http://www.as3dp.com/wp-content/uploads/2008/12/multreferences.png"><img class="alignnone size-full wp-image-320" title="multreferences" src="http://www.as3dp.com/wp-content/uploads/2008/12/multreferences.png" alt="" width="341" height="55" /></a></p>
<p><em> <strong>Figure 2: </strong>Multiple object references</em></p>
<p>Getting the ObjectStructure and its references to the Element class is an important part of working successfully with the Visitor pattern.</p>
<p><strong>Double-Dispatch</strong></p>
<p>One of the key processes behind the Visitor’s ability to add operations to existing classes is the <strong>double-dispatch</strong> technique. Like the name implies, <em>double-dispatch</em> involves two receivers. A single-dispatch involves the name of the request and the type of receiver. In double-dispatch the executed operation depends on the kind of request and <em>two</em> receivers. In the Visitor example program, you will see that the <strong>accept</strong> method is a double dispatch operation. Within a concrete element, it is written as:<br />
 <code><br />
 public function accept(visitor:IVisitor):void<br />
 {<br />
&nbsp;&nbsp;&nbsp;&nbsp;visitor.visitConcreteElementA(this);<br />
 }<br />
 </code></p>
<p>The <code>this</code> statement refers to the Concrete Element class it is contained within. In this context you can see the visitor and element receivers that make it a double-dispatch operation.  The actual operation that is executed depends on the type of Visitor and Element. Using this technique allows you to consolidate the operation in the Visitor.</p>
<p><span id="more-318"></span></p>
<p>In order to make the double-dispatch more visible in an example, the program uses an unsigned color value distributed between the Visitor and Element. The generated value is displayed as a string representing an unsigned integer.(e.g., 0x00aa04). The Element contains the first six characters (e.g., 0xff00) and the Visitor provides the last two characters (e.g.. ff). The operation in the Element expects a parameter containing the last two characters, and when the operation is launched from a concrete Visitor, it supplies the required characters. The combined string is displayed in a <strong>trace</strong> statement and also converted into an unsigned integer (just to keep me honest!).</p>
<p><strong>Traverse the Elements</strong></p>
<p>The procedure whereby the visitor visits all of the elements is the <strong>traverse</strong> process. Gamma <em>et al</em> suggest using the object structure, an iterator or add a traversal algorithm in the visitor. Of these methods for traversing the elements I selected the object structure because it&#8217;s the clearest and easiest to get started with. Included in the object structure (class<strong> ObjectStructure</strong>) is an array of the elements and a loop iterates through the array where the array elements are pushed onto the array. A second operation (the <strong>accept</strong> method) goes through the elements and accepts the visitor.</p>
<p><strong>The Visitor Interface and Concrete Classes</strong></p>
<p>The minimalist example is designed to illustrate the structure of the Visitor design pattern. To that end, <strong>trace</strong> statements show where the visitor visits and show how the different combinations of visitor and visited can be used to generate unsigned integer values typically used for assigning values to colors.</p>
<p>First , I used an interface instead of abstract classes for both the visitor and element classes. You might want to consider using abstract classes for the element interface, especially if you want to add a bit more detail to the operations. However, for this example, the <strong>interface</strong> is sufficient. The visitor participants are listed in the following scripts:</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('p318code4'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3184"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
</pre></td><td class="code" id="p318code4"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Visitor Interface</span>
package
<span style="color: #66cc66;">&#123;</span>
<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IVisitor
     <span style="color: #66cc66;">&#123;</span>
          <span style="color: #000000; font-weight: bold;">function</span> visitConcreteElementA<span style="color: #66cc66;">&#40;</span>concreteElementA:ConcreteElementA<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
          <span style="color: #000000; font-weight: bold;">function</span> visitConcreteElementB<span style="color: #66cc66;">&#40;</span>concreteElementB:ConcreteElementB<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
     <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Concrete Visitor 1</span>
package
<span style="color: #66cc66;">&#123;</span>
     <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteVisitor1 <span style="color: #0066CC;">implements</span> IVisitor
     <span style="color: #66cc66;">&#123;</span>
          <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> visNow:<span style="color: #0066CC;">String</span> = <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> visitConcreteElementA<span style="color: #66cc66;">&#40;</span>concreteElementA:ConcreteElementA<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
          <span style="color: #66cc66;">&#123;</span>
               visNow = <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
               <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>visNow.<span style="color: #0066CC;">slice</span><span style="color: #66cc66;">&#40;</span>visNow.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;C&quot;</span><span style="color: #66cc66;">&#41;</span>,visNow.<span style="color: #0066CC;">length</span> - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot; visits Concrete Element A&quot;</span><span style="color: #66cc66;">&#41;</span>;
               concreteElementA.<span style="color: #006600;">operationA</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ff&quot;</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #66cc66;">&#125;</span>
&nbsp;
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> visitConcreteElementB<span style="color: #66cc66;">&#40;</span>concreteElementB:ConcreteElementB<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
          <span style="color: #66cc66;">&#123;</span>
               visNow = <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
               <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>visNow.<span style="color: #0066CC;">slice</span><span style="color: #66cc66;">&#40;</span>visNow.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;C&quot;</span><span style="color: #66cc66;">&#41;</span>,visNow.<span style="color: #0066CC;">length</span> - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot; visits Concrete Element B&quot;</span><span style="color: #66cc66;">&#41;</span>;
               concreteElementB.<span style="color: #006600;">operationB</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;ff&quot;</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #66cc66;">&#125;</span>
     <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Concrete Visitor 2</span>
package
<span style="color: #66cc66;">&#123;</span>
     <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteVisitor2 <span style="color: #0066CC;">implements</span> IVisitor
     <span style="color: #66cc66;">&#123;</span>
          <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> visNow:<span style="color: #0066CC;">String</span> = <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> visitConcreteElementA<span style="color: #66cc66;">&#40;</span>concreteElementA:ConcreteElementA<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
          <span style="color: #66cc66;">&#123;</span>
               visNow = <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
               <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>visNow.<span style="color: #0066CC;">slice</span><span style="color: #66cc66;">&#40;</span>visNow.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;C&quot;</span><span style="color: #66cc66;">&#41;</span>,visNow.<span style="color: #0066CC;">length</span> - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot; visits Concrete Element A&quot;</span><span style="color: #66cc66;">&#41;</span>;
               concreteElementA.<span style="color: #006600;">operationA</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;00&quot;</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #66cc66;">&#125;</span>
&nbsp;
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> visitConcreteElementB<span style="color: #66cc66;">&#40;</span>concreteElementB:ConcreteElementB<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
          <span style="color: #66cc66;">&#123;</span>
               visNow = <span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
               <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>visNow.<span style="color: #0066CC;">slice</span><span style="color: #66cc66;">&#40;</span>visNow.<span style="color: #0066CC;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;C&quot;</span><span style="color: #66cc66;">&#41;</span>,visNow.<span style="color: #0066CC;">length</span> - <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #ff0000;">&quot; visits Concrete Element B&quot;</span><span style="color: #66cc66;">&#41;</span>;
               concreteElementB.<span style="color: #006600;">operationB</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;00&quot;</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #66cc66;">&#125;</span>
     <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Each of the concrete visitor classes has methods for visiting the two elements that make up the program. The <strong>trace</strong> statement does a little fancy footwork to extract the name of the class from the <strong>this</strong> statement, but all it is really doing is to show that the visitor is visiting each of the concrete elements in the program. Each of the visit operations also calls the element operations and passes values to them. The first visitor passes “ff” and the second passes “00.” When combined with the elements, four unique values are generated.</p>
<p><strong>The Element Interface and Classes </strong></p>
<p>As noted above the <strong>interface</strong> structure is used with the element classes. I tested it with an abstract class, and it worked just as well, but unless I need to use an abstract class, I prefer the interface.</p>
<p>The element interface (IElement) establishes the <strong> accept( visitor:IVisitor)</strong> method. Each of the two element classes include an <strong>accept()</strong> method (required by the interface) as well as a generic operation and a <strong>getState()</strong> method. The generic operation is set up to accept a string parameter and display a color value string (e.g.,0xaa22d7) based on the parameter  value from the visitor and the current state provided by the getState() method. (The current state is set up as a literal for illustrative purposes.)</p>
<p>The Freemans (authors of <em>Head First Design Patterns</em>) point out that the visitors need to get the state of the elements and that each element should have a getState() method. So I just created a getState() private function and then tucked it in the trace statement as a value that is displayed when the visitor invokes the operation and passes a value for the operation’s parameter. In this way, the design implies a far more dynamic system.</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('p318code5'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3185"><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
</pre></td><td class="code" id="p318code5"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Element Interface</span>
package
<span style="color: #66cc66;">&#123;</span>
     <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IElement
     <span style="color: #66cc66;">&#123;</span>
          <span style="color: #000000; font-weight: bold;">function</span>  accept<span style="color: #66cc66;">&#40;</span> visitor:IVisitor<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
     <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//Concrete Element 1</span>
package
<span style="color: #66cc66;">&#123;</span>
     <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteElementA <span style="color: #0066CC;">implements</span> IElement
     <span style="color: #66cc66;">&#123;</span>
          <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> currentState:<span style="color: #0066CC;">String</span>;
&nbsp;
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> accept<span style="color: #66cc66;">&#40;</span>visitor:IVisitor<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
          <span style="color: #66cc66;">&#123;</span>
               visitor.<span style="color: #006600;">visitConcreteElementA</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #66cc66;">&#125;</span>
&nbsp;
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> operationA<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">color</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
          <span style="color: #66cc66;">&#123;</span>
               <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>getState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>+<span style="color: #0066CC;">color</span> +<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span>;
               <span style="color: #808080; font-style: italic;">//Convert into actual number</span>
               <span style="color: #000000; font-weight: bold;">var</span> clr:uint=uint<span style="color: #66cc66;">&#40;</span>getState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>+<span style="color: #0066CC;">color</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> getState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
          <span style="color: #66cc66;">&#123;</span>
               currentState=<span style="color: #ff0000;">&quot;0xff00&quot;</span>;
               <span style="color: #b1b100;">return</span> currentState;
          <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;">//Concrete Element 2</span>
package
<span style="color: #66cc66;">&#123;</span>
     <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConcreteElementB <span style="color: #0066CC;">implements</span> IElement
     <span style="color: #66cc66;">&#123;</span>
          <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> currentState:<span style="color: #0066CC;">String</span>;
&nbsp;
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> accept<span style="color: #66cc66;">&#40;</span>visitor:IVisitor<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
          <span style="color: #66cc66;">&#123;</span>
               visitor.<span style="color: #006600;">visitConcreteElementB</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
          <span style="color: #66cc66;">&#125;</span>
&nbsp;
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> operationB<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">color</span>:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
          <span style="color: #66cc66;">&#123;</span>
               <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>getState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>+<span style="color: #0066CC;">color</span> +<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span>;
               <span style="color: #808080; font-style: italic;">//Convert into actual number</span>
               <span style="color: #000000; font-weight: bold;">var</span> clr:uint = uint<span style="color: #66cc66;">&#40;</span>getState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #0066CC;">color</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> getState<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
          <span style="color: #66cc66;">&#123;</span>
               currentState = <span style="color: #ff0000;">&quot;0x00ff&quot;</span>;
               <span style="color: #b1b100;">return</span> currentState;
          <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 final part is to set up a class where we can organize the structure providing a doorway for the client.</p>
<p><strong>The Object Structure and Client</strong></p>
<p>As noted above, the<strong> ObjectStructure</strong> participant in the Visitor design pattern is where an operation is set up so that the visitor traverses the different concrete elements. The class has no constructor, but instead serves as a set of operations providing traverse services for the client to use for making calls to the Visitor.</p>
<p>As seen in the diagram structure, the ObjectStructure holds multiple references to the element classes in the <strong> attach()</strong> operation. The accept() function represents the double-dispatch technique so critical to the Visitor pattern.</p>
<p>Once the ObjectStructure class is set up, the Client class can use its operations to send requests to the Element side of the Visitor. However, as you remember, the Client also holds a reference to the Visitor classes, and so you will see both instances and references to the Visitor classes and the Element classes. The references to the visitor side is direct, but the references to the elements are through the object structure via the attach() 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('p318code6'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3186"><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
</pre></td><td class="code" id="p318code6"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//Object Structure</span>
package
<span style="color: #66cc66;">&#123;</span>
     <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ObjectStructure
     <span style="color: #66cc66;">&#123;</span>
          <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> elements:<span style="color: #0066CC;">Array</span>=<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> attach<span style="color: #66cc66;">&#40;</span>element:IElement <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
          <span style="color: #66cc66;">&#123;</span>
               elements.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>element<span style="color: #66cc66;">&#41;</span>;
          <span style="color: #66cc66;">&#125;</span>
&nbsp;
          <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> accept<span style="color: #66cc66;">&#40;</span> visitor:IVisitor<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
          <span style="color: #66cc66;">&#123;</span>
               <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">e</span>:IElement <span style="color: #b1b100;">in</span> elements<span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#123;</span>
                    <span style="color: #0066CC;">e</span>.<span style="color: #006600;">accept</span><span style="color: #66cc66;">&#40;</span>visitor<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>
&nbsp;
<span style="color: #808080; font-style: italic;">//Client</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;">//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> snoopy1:IVisitor;
          <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> snoopy2:IVisitor;
          <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> obStru:ObjectStructure;
&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>
               setUp<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> setUp<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;">//Setup</span>
               obStru = <span style="color: #000000; font-weight: bold;">new</span> ObjectStructure;
               obStru.<span style="color: #006600;">attach</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ConcreteElementA<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
               obStru.<span style="color: #006600;">attach</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ConcreteElementB<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
               <span style="color: #808080; font-style: italic;">// Create visitors</span>
               snoopy1 = <span style="color: #000000; font-weight: bold;">new</span> ConcreteVisitor1<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
               snoopy2 = <span style="color: #000000; font-weight: bold;">new</span> ConcreteVisitor2<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
               <span style="color: #808080; font-style: italic;">// Accept visitors</span>
               obStru.<span style="color: #006600;">accept</span><span style="color: #66cc66;">&#40;</span>snoopy1<span style="color: #66cc66;">&#41;</span>;
               obStru.<span style="color: #006600;">accept</span><span style="color: #66cc66;">&#40;</span>snoopy2<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>When you test the program in either Flex or Flash, you get the following output:<br />
 <strong><br />
 ConcreteVisitor1 visits Concrete Element A<br />
 0xff00ff</p>
<p>ConcreteVisitor1 visits Concrete Element B<br />
 0x00ffff</p>
<p>ConcreteVisitor2 visits Concrete Element A<br />
 0xff0000</p>
<p>ConcreteVisitor2 visits Concrete Element B<br />
 0x00ff00<br />
 </strong></p>
<p>As you can see, the element states work out to different “color” values depending on what the visitor brings to the plate. Being a bare bones minimalist example, you should be able to see all of the parts and how they interact.  You can also add a “detach” operation so that you can pick and choose which visitors visit which elements.</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%2F12%2Factionscript-30-visitor-design-pattern-a-tale-of-traverser-and-the-double-dispatch-kid%2F&amp;title=ActionScript%203.0%20Visitor%20Design%20Pattern%3A%20A%20Tale%20of%20Traverser%20and%20the%20Double-Dispatch%20Kid" 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/2008/12/actionscript-30-visitor-design-pattern-a-tale-of-traverser-and-the-double-dispatch-kid/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

