<?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; Proxy Pattern</title>
	<atom:link href="http://www.as3dp.com/category/design-patterns/proxy/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>Another Approach to the Protection Proxy: Adding Helper Classes</title>
		<link>http://www.as3dp.com/2010/09/another-approach-to-the-protection-proxy-adding-helper-classes/</link>
		<comments>http://www.as3dp.com/2010/09/another-approach-to-the-protection-proxy-adding-helper-classes/#comments</comments>
		<pubDate>Tue, 21 Sep 2010 11:14:08 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Proxy Pattern]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=3548</guid>
		<description><![CDATA[During the summer I was heads down on a book project that got more urgent as the summer progressed. As a result, I&#8217;m a bit late on a suggested revision of the Protection Proxy sent in by Gil Amran way back in June. In the original article, I questioned the practice of hauling a reference [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_3547" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2010/09/improved.jpg" alt="Protection Proxy with DataLoaded Helper Classes" title="improved" width="250" height="200" class="size-full wp-image-3547" /><p class="wp-caption-text">Protection Proxy with DataLoaded Helper Classes</p></div> During the summer I was heads down on a book project that got more urgent as the summer progressed. As a result, I&#8217;m a bit late on a suggested revision of the Protection Proxy sent in by Gil Amran way back in June.</p>
<p>In the <a href="http://www.as3dp.com/2010/06/13/actionscript-3-0-protection-proxy-design-pattern-1-shielding-the-real-subject/">original article</a>, I questioned the practice of hauling a reference to the output UI all the way through the program. However, focusing on the design pattern, I didn&#8217;t concern myself with certain details that would have been optimal in a larger sense. Also, I don&#8217;t like using helper classes when trying to focus on the basics of a pattern.  So, I just put in  bare essentials hoping that readers will better be  able to see how the pattern works.</p>
<p>However, I&#8217;m always looking for better ways of programming, and to misquote Ralph Waldo Emerson,</p>
<blockquote><p>Build a better proxy and the world will beat a path to your door.</p></blockquote>
<p>So download Gil&#8217;s code, and let&#8217;s take a look at it:<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/dpWork/GilsProtectionProxy.zip"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a></p>
<p><strong>A Better Mousetrap</strong></p>
<p>The revision of the Proxy was by Gil Amran and here, in Gil&#8217;s words, is what was changed:</p>
<p>I&#8217;ve changed few things:</p>
<ol>
<li> I used AbsSubject and not the interface. (Abstract class)</li>
<li>I&#8217;ve overrided the clone method in the 2 custom events.</li>
<li>I&#8217;ve re-dispatched any events that came from he realSubject</li>
</ol>
<p>Further Gil notes,</p>
<blockquote><p>The AbsSubject extends EventDispatcher, and there&#8217;s no way to avoid that. Because the actual loading takes time and works with events, using callbacks might be another way to solve this and not use events.</p></blockquote>
<p>In comparing Gil&#8217;s proxy with the original, the key differences are in handling events. Figure 1 is a file class diagram of Gil&#8217;s protection proxy you can compare to the original:<br />
<div id="attachment_3565" class="wp-caption alignnone" style="width: 401px"><img src="http://www.as3dp.com/wp-content/uploads/2010/09/FileDiagram.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt;Protection Proxy with Event Dispatch Classes" title="FileDiagram" width="391" height="388" class="size-full wp-image-3565" /><p class="wp-caption-text"><em><strong>Figure 1:</strong>Protection Proxy with Event Dispatch Classes</em></p></div><br />
The two helpers classes function to deal with event handling and dispatch.<br />
<span id="more-3548"></span><br />
<strong>It All Starts with the Client</strong></p>
<p>After looking at both implementations of the Protection Proxy, you will see that the key issue is the delay in <em>loading the text file and sending either the data from the text file or an error message back to the user in some kind of UI</em>. Using the <strong>trace()</strong> statement resolves most of these issues because you don&#8217;t need any kind of link back to a source where your UI is instantiated. However, in an application that <em>people</em> (not programmers) actually use, you need a UI.</p>
<p>To resolve this issue, I just hauled a reference to the original UI from the Client through the interface, through the proxy and on to the real subject. This was done through a parameter that held the login information checked by the proxy object which could be passed from the proxy to the real subject where the text file data are loaded.</p>
<p>Gil&#8217;s solution is to use an event handler class for the loaded data (DataLoadedEvent) and one for errors (DataLoadedErrorEvent). Event listeners were added to the proxy instance (of SubProxy) so that when an event is fired it can capture the returns from it. In order for this to work with the RealSubject, the abstract class that replaced the interface had to extend the EventDispatcher class. Because both the RealSubject and SubProxy are extended from the AbsSubject (abstract subject), they both inherit EventDispatcher properties and methods.</p>
<p>The tricky part comes where the dispatched event comes back to the client. Remember that the proxy class has the event listener and not the real class where the data are loaded. So how is the proxy class instance going to &#8220;hear&#8221; the event dispatched from the real subject? Well, remember the cardinal rule of design patterns?</p>
<blockquote><p>Program to an interface and not an implementation.</p></blockquote>
<p>In the client, Gil typed the SubProxy instance to the interface&#8211;the abstract class (which is an interface in the broader sense); so it catches events of the interface&#8217;s child classes. That&#8217;s possible because the interface is an extension of the EventDispatcher class.</p>
<p><strong>Lest We Devolve into a Discussion of Algorithms&#8230;.</strong></p>
<p>Generally on this blog, I avoid discussions (and quibbles more so) about algorithms. However, I did bring up the discussion of Skip Lists and a few other places where we can knock around algorithms instead of relationships between participants in a design pattern. However, I believe that event dispatching and event handlers are part of the communication between the objects that make up design patterns, and so I welcomed Gil&#8217;s revision of the Protection Proxy. If others would like to join in this discussion of how to best communicate between the classes in the Proxy (or design patterns in general) dispatching messages using event dispatchers or other techniques, I believe it would be useful.</p>
<p>This is not a bugle call for reeling out some warmed over algorithms and telling everyone why they&#8217;re your favorite, but rather one for inter-object communication in ActionScript 3.0 design patterns. I think there&#8217;s some fertile soil here and one that would be fun to explore.</p>
<p><strong>Gil&#8217;s Solution</strong></p>
<p>The classes that make up Gil&#8217;s solution are equally appropriate for Flash or Flex, and while the downloaded files have a Flash CS5 FLA file, just about any version of Flash or Flex (Flash Builder) can be used. They are presented here with minimum discussion.</p>
<p><em>Helper Classes</em></p>
<p>First, Gil&#8217;s two helper classes extend the Event 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('p3548code7'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p35487"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p3548code7"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DataLoadedEvent <span style="color: #0066CC;">extends</span> Event
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> m_dataStr:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const DATA_LOADED_EVENT:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;DATA_LOADED_EVENT&quot;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> DataLoadedEvent<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span>,dataStrParam:<span style="color: #0066CC;">String</span>,bubbles:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">false</span>,cancelable:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">dataStr</span>=dataStrParam;
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>,bubbles,cancelable<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> <span style="color: #0066CC;">set</span> dataStr<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			m_dataStr=value;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> dataStr<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> m_dataStr;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> clone<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Event
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> DataLoadedEvent<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>,dataStr<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>


<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('p3548code8'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p35488"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p3548code8"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DataLoadedErrorEvent <span style="color: #0066CC;">extends</span> Event
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> m_errorStr:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const DATA_LOADED_ERROR_EVENT:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;DATA_LOADED_ERROR_EVENT&quot;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> DataLoadedErrorEvent<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>:<span style="color: #0066CC;">String</span>,errorStrParam:<span style="color: #0066CC;">String</span>,bubbles:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">false</span>,cancelable:<span style="color: #0066CC;">Boolean</span>=<span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">errorStr</span>=errorStrParam;
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>,bubbles,cancelable<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> <span style="color: #0066CC;">set</span> errorStr<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			m_errorStr=value;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> errorStr<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> m_errorStr;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> clone<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:Event
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> DataLoadedErrorEvent<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">type</span>,errorStr<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>I really liked the way that Gil used static constants to set up a string property for dispatch. Also, note the parameters used in the DataLoaded and DataLoadedError events. The <strong>super()</strong> reference is to the Event class.</p>
<p><em>The Client</em></p>
<p>The Client class is pretty busy making requests and setting up event listeners and returning data for the 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('p3548code9'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p35489"><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
</pre></td><td class="code" id="p3548code9"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</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;">Label</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #006600;">TextArea</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #006600;">TextInput</span>;
&nbsp;
	<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;">Event</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> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldAutoSize</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</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> proxy:AbsSubject;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> lblUN:Label=<span style="color: #000000; font-weight: bold;">new</span> Label  ;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> lblPW:Label=<span style="color: #000000; font-weight: bold;">new</span> Label  ;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txt:<span style="color: #0066CC;">TextField</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span>  ;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> format:<span style="color: #0066CC;">TextFormat</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span>  ;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtArea:TextArea=<span style="color: #000000; font-weight: bold;">new</span> TextArea  ;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> userInput:TextInput=<span style="color: #000000; font-weight: bold;">new</span> TextInput  ;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> pwInput:TextInput=<span style="color: #000000; font-weight: bold;">new</span> TextInput  ;
		<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>  ;
&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>
			setText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			setIO<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			btn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>,requestTextVars<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> setText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			format.<span style="color: #0066CC;">font</span>=<span style="color: #ff0000;">&quot;Arial Black&quot;</span>;
			format.<span style="color: #0066CC;">size</span>=<span style="color: #cc66cc;">24</span>;
			format.<span style="color: #0066CC;">color</span>=<span style="color: #ff0000;">&quot;0x990000&quot;</span>;
			txt.<span style="color: #006600;">defaultTextFormat</span>=format;
			addChild<span style="color: #66cc66;">&#40;</span>txt<span style="color: #66cc66;">&#41;</span>;
			txt.<span style="color: #0066CC;">autoSize</span>=TextFieldAutoSize.<span style="color: #0066CC;">LEFT</span>;
			txt.<span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;Protected Proxy&quot;</span>;
			txt.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">32</span>,txt.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">8</span>;
			txt.<span style="color: #0066CC;">height</span>=<span style="color: #cc66cc;">24</span>;
			lblUN.<span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;User name&quot;</span>;
			lblUN.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">36</span>,lblUN.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">48</span>;
			addChild<span style="color: #66cc66;">&#40;</span>lblUN<span style="color: #66cc66;">&#41;</span>;
			lblPW.<span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;Password&quot;</span>;
			lblPW.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">36</span>,lblPW.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">72</span>;
			addChild<span style="color: #66cc66;">&#40;</span>lblPW<span style="color: #66cc66;">&#41;</span>;
			btn.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">220</span>,btn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">72</span>;
			btn.<span style="color: #006600;">label</span>=<span style="color: #ff0000;">&quot;Show Data&quot;</span>;
			btn.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">80</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btn<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setIO<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			addChild<span style="color: #66cc66;">&#40;</span>userInput<span style="color: #66cc66;">&#41;</span>;
			userInput.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">100</span>,userInput.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">48</span>;
			userInput.<span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;Bill&quot;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>pwInput<span style="color: #66cc66;">&#41;</span>;
			pwInput.<span style="color: #006600;">displayAsPassword</span>=<span style="color: #000000; font-weight: bold;">true</span>;
			pwInput.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">100</span>,pwInput.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">72</span>;
			pwInput.<span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;as3&quot;</span>;
&nbsp;
			addChild<span style="color: #66cc66;">&#40;</span>txtArea<span style="color: #66cc66;">&#41;</span>;
			txtArea.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">248</span>;
			txtArea.<span style="color: #0066CC;">height</span>=<span style="color: #cc66cc;">250</span>;
			txtArea.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">32</span>,txtArea.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">104</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> requestTextVars<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>
			proxy=<span style="color: #000000; font-weight: bold;">new</span> SubProxy  ;
			proxy.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>DataLoadedEvent.<span style="color: #006600;">DATA_LOADED_EVENT</span>,onDataLoaded<span style="color: #66cc66;">&#41;</span>;
			proxy.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>DataLoadedErrorEvent.<span style="color: #006600;">DATA_LOADED_ERROR_EVENT</span>,onDataLoadedError<span style="color: #66cc66;">&#41;</span>;
			proxy.<span style="color: #006600;">pxRequest</span><span style="color: #66cc66;">&#40;</span>userInput.<span style="color: #0066CC;">text</span>,pwInput.<span style="color: #0066CC;">text</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> onDataLoaded<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:DataLoadedEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			txtArea.<span style="color: #0066CC;">text</span>=<span style="color: #0066CC;">e</span>.<span style="color: #006600;">dataStr</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> onDataLoadedError<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:DataLoadedErrorEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			txtArea.<span style="color: #0066CC;">text</span>=<span style="color: #0066CC;">e</span>.<span style="color: #006600;">errorStr</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>You might notice that the client looks a lot like the original, and that is very valuable. In this way, you can better compare how each deals with the information that is passed back to the UI in the client—either as an error or data.</p>
<p><em>The Main Classes</em></p>
<p>This is a golden opportunity for me to start a rant about those who call their Client classes Main, but I won&#8217;t until I have more time to waste. However, when thinking <em><strong>Main</strong></em>, you should be thinking about the classes that make up the main part of the design pattern and not the Client who is making requests from the main structure of the program. (Yeah, I know, even the Gang of Four have classes called Main that are really clients.)</p>
<p>Gil included a constructor class in the AbsSubject. I took it out for two reasons. First, it&#8217;s an abstract class that you&#8217;d never instantiate. Second, it detracts from the key methods and properties in the class. If someone can tell me when you&#8217;d ever need a constructor class in an abstract class, let me know. (I mean a good reason.)</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('p3548code10'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p354810"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p3548code10"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">EventDispatcher</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AbsSubject <span style="color: #0066CC;">extends</span> EventDispatcher
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pxRequest<span style="color: #66cc66;">&#40;</span>userName:<span style="color: #0066CC;">String</span>,<span style="color: #0066CC;">password</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: #808080; font-style: italic;">// override</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><em>The Proxy Class</em></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('p3548code11'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p354811"><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
</pre></td><td class="code" id="p3548code11"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">EventDispatcher</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SubProxy <span style="color: #0066CC;">extends</span> AbsSubject
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> m_realSub:AbsSubject;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> m_userName:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> m_password:<span style="color: #0066CC;">String</span>;
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pxRequest<span style="color: #66cc66;">&#40;</span>userName:<span style="color: #0066CC;">String</span>, <span style="color: #0066CC;">password</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: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>userName==<span style="color: #ff0000;">&quot;Bill&quot;</span><span style="color: #66cc66;">&amp;&amp;</span>password==<span style="color: #ff0000;">&quot;as3&quot;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				m_userName=userName;
				m_password=<span style="color: #0066CC;">password</span>;
				m_realSub = <span style="color: #000000; font-weight: bold;">new</span> RealSubject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				m_realSub.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>DataLoadedEvent.<span style="color: #006600;">DATA_LOADED_EVENT</span>, onDataLoaded<span style="color: #66cc66;">&#41;</span>;
				m_realSub.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>DataLoadedErrorEvent.<span style="color: #006600;">DATA_LOADED_ERROR_EVENT</span>, onDataLoadedError<span style="color: #66cc66;">&#41;</span>;
				m_realSub.<span style="color: #006600;">pxRequest</span><span style="color: #66cc66;">&#40;</span>m_userName, m_password<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">// PROTECTION errors are dispatched from the protection proxy (ONLY error events)</span>
				dispatchEvent<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DataLoadedErrorEvent<span style="color: #66cc66;">&#40;</span>DataLoadedErrorEvent.<span style="color: #006600;">DATA_LOADED_ERROR_EVENT</span>, <span style="color: #ff0000;">&quot;Adios...&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> onDataLoaded<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:DataLoadedEvent<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;">// resend (clone) the events from the realSubject to the proxy's listenners</span>
			dispatchEvent<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</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> onDataLoadedError<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:DataLoadedErrorEvent<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;">// resend (clone) the events from the realSubject to the proxy's listenners</span>
			dispatchEvent<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</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><em>The Real Subject</em></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('p3548code12'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p354812"><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
</pre></td><td class="code" id="p3548code12"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">EventDispatcher</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLLoader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequestMethod</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLVariables</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RealSubject <span style="color: #0066CC;">extends</span> AbsSubject
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> urlString:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;fromText.txt&quot;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dataNow:URLVariables;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtLoader:URLLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<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> requestNow:URLRequest=<span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>urlString<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pxRequest<span style="color: #66cc66;">&#40;</span>userName:<span style="color: #0066CC;">String</span>, <span style="color: #0066CC;">password</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>
			requestNow.<span style="color: #006600;">method</span>=URLRequestMethod.<span style="color: #006600;">POST</span>;
			txtLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> Event.<span style="color: #006600;">COMPLETE</span>, loadTXT <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">try</span>
			<span style="color: #66cc66;">&#123;</span>
				txtLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span> requestNow <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #0066CC;">catch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:ArgumentError<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">// INTERNAL errors are dispatched from the subject too</span>
				dispatchEvent<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DataLoadedErrorEvent<span style="color: #66cc66;">&#40;</span>DataLoadedErrorEvent.<span style="color: #006600;">DATA_LOADED_ERROR_EVENT</span>, <span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #0066CC;">catch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:SecurityError<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">// INTERNAL errors are dispatched from the subject too</span>
				dispatchEvent<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DataLoadedErrorEvent<span style="color: #66cc66;">&#40;</span>DataLoadedErrorEvent.<span style="color: #006600;">DATA_LOADED_ERROR_EVENT</span>, <span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> loadTXT<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">e</span>:Event <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			txtLoader.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span> Event.<span style="color: #006600;">COMPLETE</span>, loadTXT <span style="color: #66cc66;">&#41;</span>;
			dataNow=<span style="color: #000000; font-weight: bold;">new</span> URLVariables<span style="color: #66cc66;">&#40;</span>txtLoader.<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> resultStr:<span style="color: #0066CC;">String</span>=dataNow.<span style="color: #006600;">record1</span>;
			resultStr+=dataNow.<span style="color: #006600;">record2</span>;
			resultStr+=dataNow.<span style="color: #006600;">record3</span>;
			resultStr+=dataNow.<span style="color: #006600;">record4</span>;
			resultStr+=dataNow.<span style="color: #006600;">record5</span>;
			resultStr+=dataNow.<span style="color: #006600;">record6</span>;
			resultStr+=dataNow.<span style="color: #006600;">record7</span>;
			resultStr+=dataNow.<span style="color: #006600;">record8</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// success are sent from the actual subjects (NOT only success events)</span>
			dispatchEvent<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DataLoadedEvent<span style="color: #66cc66;">&#40;</span>DataLoadedEvent.<span style="color: #006600;">DATA_LOADED_EVENT</span>, resultStr<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>I want to thank Gil for this and he gets 4 World Cup points for adding this to our discussion of ActionScript 3.0 Design patterns.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2010%2F09%2Fanother-approach-to-the-protection-proxy-adding-helper-classes%2F&amp;title=Another%20Approach%20to%20the%20Protection%20Proxy%3A%20Adding%20Helper%20Classes" 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/2010/09/another-approach-to-the-protection-proxy-adding-helper-classes/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Protection Proxy Design Pattern 2: The FCNY Meeting</title>
		<link>http://www.as3dp.com/2010/07/actionscript-3-0-protection-proxy-design-pattern-2-the-fcny-meeting/</link>
		<comments>http://www.as3dp.com/2010/07/actionscript-3-0-protection-proxy-design-pattern-2-the-fcny-meeting/#comments</comments>
		<pubDate>Fri, 02 Jul 2010 15:01:23 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Proxy Pattern]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=3416</guid>
		<description><![CDATA[On June 30, 2010 I met with the Flash Coders New York (FCNY). I walked over to the meeting site from Grand Central Station—it&#8217;s a straight shot up Park Ave., dogleg at Broadway and Bob&#8217;s your uncle! It&#8217;s right there near 4th on Mercer in a joint called Think Coffee. (East Village area near NYU.) [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_3418" class="wp-caption alignleft" style="width: 260px"><a href="http://www.as3dp.com/2010/07/02/actionscript-3-0-protection-proxy-design-pattern-2-the-fcny-meeting/fcny-2/" rel="attachment wp-att-3418"><img src="http://www.as3dp.com/wp-content/uploads/2010/07/FCNY.png" alt="FCNY Meeting" title="FCNY" width="250" height="203" class="size-full wp-image-3418" /></a><p class="wp-caption-text">FCNY Meeting</p></div> On June 30, 2010 I met with the Flash Coders New York (FCNY). I walked over to the meeting site from Grand Central Station—it&#8217;s a straight shot up Park Ave., dogleg at Broadway and Bob&#8217;s your uncle! It&#8217;s right there near 4th on Mercer in a joint called <strong>Think Coffee</strong>. (East Village area near NYU.) I was lugging my Mac PowerBook and it and the case weighted 11 pounds. (After the two mile hike, my computer felt considerably heavier. If you plan to go, you&#8217;ll find that the subway— #6 to Brooklyn Bridge—from Grand Central is easier.)</p>
<p><strong>Making Changes to the Original Protection Proxy</strong></p>
<p>FCNY meets in the basement room at Think Coffee that reminded me of a (smoke free) anarchist&#8217;s safe house. (I was expecting to see Sacco and Vanzetti there.) They helped me hook up my PowerBook to a projector, and off we went. Having found myself in a perfect storm of doing the author edits of a Flash Catalyst book and starting on another new book, I was pressed for time; so I took the <a href="http://www.as3dp.com/2010/06/13/actionscript-3-0-protection-proxy-design-pattern-1-shielding-the-real-subject/">protection proxy from this blog</a> and changed the proxy participant to include an algorithm that would filter out different &#8220;levels&#8221; of access. The level of sophistication of the algorithm and the added classes for different levels of access were quite simple. However, the point is to <em>focus on the design pattern</em> and not my acumen at writing algorithms. (If you don&#8217;t like the algorithms, you can always re-write them and send them in.) You can download the package by clicking the download button—the .zip file includes the  PPT/KeyNote pages as well.<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/dpWork/FCNY/fcny.zip"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="alignnone size-full wp-image-2020" /></a></p>
<p>What was most exciting for me was the fact that the Protection Proxy was so easy to change. Here was a program with several different classes, and I had to add new functionality in a hurry that did several different things that would not crash my program. Essentially, I was adding three new concrete RealSubject classes that would work with the current program and make changes in the Proxy (SubProxy) to filter through three different &#8220;level of access&#8221; options. I knew that if I followed the interface rules, no matter what algorithm I used, it&#8217;d work. Figure 1 shows the class diagram of the revised Protection Proxy (from the FCNY presentation.)<br />
<div id="attachment_3441" class="wp-caption alignnone" style="width: 495px"><a href="http://www.as3dp.com/2010/07/02/actionscript-3-0-protection-proxy-design-pattern-2-the-fcny-meeting/proxy/" rel="attachment wp-att-3441"><img src="http://www.as3dp.com/wp-content/uploads/2010/07/proxy.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1: &lt;/strong&gt;Stratified Protection Proxy&lt;/em&gt;" title="proxy" width="485" height="362" class="size-full wp-image-3441" /></a><p class="wp-caption-text"><em><strong>Figure 1: </strong>Stratified Protection Proxy</em></p></div><br />
The three RealSubject participants (International, National, Local) are pretty much the same except that each loads a different text file. However, as long as each maintains the interface, no matter how different the algorithms are, everything happily hums along.</p>
<p><span id="more-3416"></span></p>
<p><strong>Let the Proxy Do the Work</strong></p>
<p>A Proxy design pattern has many different functions, but one of the most important is to handle all of the filtering work from the requested object. The hypothetical situation in the example is one where different people have different levels of access to data. In order to determine the level of access, the Proxy (SubProxy) object does all of the deciding work instead of passing it to the individual objects that contain the actual operations requested by the Client. In this way, you keep out duplicate code (filtering work) and you protect the RealSubject participants from unwarranted access. As you can see in the following Proxy class, it both protects and filters in a single operation:</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('p3416code14'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p341614"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
</pre></td><td class="code" id="p3416code14"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #006600;">TextArea</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SubProxy <span style="color: #0066CC;">implements</span> ISubject
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Declare as interface data type</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> realSub:ISubject;
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtArea:TextArea;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> un:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> pw:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pxRequest<span style="color: #66cc66;">&#40;</span>tx:TextArea,un:<span style="color: #0066CC;">String</span>,pw:<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;">this</span>.<span style="color: #006600;">un</span>=un;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">pw</span>=pw;
			txtArea=tx;
                        <span style="color: #808080; font-style: italic;">//Access to international data</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">un</span>==<span style="color: #ff0000;">&quot;Lisa&quot;</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">pw</span>==<span style="color: #ff0000;">&quot;fms4&quot;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				realSub=<span style="color: #000000; font-weight: bold;">new</span> International<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				realSub.<span style="color: #006600;">pxRequest</span><span style="color: #66cc66;">&#40;</span>txtArea,<span style="color: #000000; font-weight: bold;">null</span>,<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
                        <span style="color: #808080; font-style: italic;">//Access to national data</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">un</span>==<span style="color: #ff0000;">&quot;Tyler&quot;</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">pw</span>==<span style="color: #ff0000;">&quot;dp&quot;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				realSub=<span style="color: #000000; font-weight: bold;">new</span> National<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				realSub.<span style="color: #006600;">pxRequest</span><span style="color: #66cc66;">&#40;</span>txtArea,<span style="color: #000000; font-weight: bold;">null</span>,<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
                        <span style="color: #808080; font-style: italic;">//Access to local data</span>
			<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">un</span>==<span style="color: #ff0000;">&quot;Will&quot;</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">pw</span>==<span style="color: #ff0000;">&quot;flex&quot;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				realSub=<span style="color: #000000; font-weight: bold;">new</span> Local<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				realSub.<span style="color: #006600;">pxRequest</span><span style="color: #66cc66;">&#40;</span>txtArea,<span style="color: #000000; font-weight: bold;">null</span>,<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span>
			<span style="color: #66cc66;">&#123;</span>
				txtArea.<span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;Adios...&quot;</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>Also notice that the <strong>realSub</strong> object is typed as the<strong> ISubject</strong> interface. As a result, the conditional options can use the same object name to instantiate the requested object while simultaneously filtering access. Also, the exact same request is forwarded using the identical call because all of the RealSubject participants implement the same interface (as does the Proxy).</p>
<p><strong>Real World Protection Proxy</strong></p>
<p>When I started preparing materials for the FCNY presentation, I was planning on setting one up with different access to a MySQL database using PHP. However, I got jammed up with editorial and writing tasks that I could not put off. Nevertheless, if you have the time (and inclination) you can make some modifications to this example and change each of the RealSubject classes to different database access such as, read data, add data, change data and even delete data. Anyone working on a dating site isn&#8217;t going to let all of its members have unlimited access; instead you have access depending on your role. Likewise, the Protection Proxy can protect and filter access to objects of virtually any kind. So the next time you have to deal with this kind of issue, consider using the Proxy.</p>
<p>I&#8217;d be remiss if I didn&#8217;t thank Will Saunders (my long lost son) for setting up the visit to NYC, and I do. It was fun meeting all of the FCNY members, and I encourage any of you Flash developers who live in the NYC area to pay them a visit. If you enjoy their meeting half as much as I did, you&#8217;ll have a great time.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2010%2F07%2Factionscript-3-0-protection-proxy-design-pattern-2-the-fcny-meeting%2F&amp;title=ActionScript%203.0%20Protection%20Proxy%20Design%20Pattern%202%3A%20The%20FCNY%20Meeting" id="wpa2a_4"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2010/07/actionscript-3-0-protection-proxy-design-pattern-2-the-fcny-meeting/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Meet in NYC on June 30, 2010</title>
		<link>http://www.as3dp.com/2010/06/meet-in-nyc-on-june-30-2010/</link>
		<comments>http://www.as3dp.com/2010/06/meet-in-nyc-on-june-30-2010/#comments</comments>
		<pubDate>Mon, 14 Jun 2010 09:05:17 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Proxy Pattern]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=3396</guid>
		<description><![CDATA[I&#8217;ll be at the Flash Coders New York (FCNY) weekly meeting at Think Coffee, 248 Mercer St. (NYC) on Wednesday, June 30 (Time: 7-9pm) to talk about ActionScript 3.0 Design Patterns. The plan is to look at some of the fundamental principles of design patterns and implementations with ActionScript 3.0. The Protection Proxy used in [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.as3dp.com/2010/06/14/meet-in-nyc-on-june-30-2010/fcny/" rel="attachment wp-att-3399"><img src="http://www.as3dp.com/wp-content/uploads/2010/06/fcny.png" alt="fcny" title="fcny" width="250" height="163" class="alignleft size-full wp-image-3399" /></a>I&#8217;ll be at the Flash Coders New York (FCNY) weekly meeting at <strong>Think Coffee</strong>, 248 Mercer St. (NYC) on Wednesday, June 30 (Time: 7-9pm) to talk about ActionScript 3.0 Design Patterns. The plan is to look at some of the fundamental principles of design patterns and implementations with ActionScript 3.0. The Protection Proxy used in combination with PHP and multiple levels of access is the planned application that will be discussed plus any other ActionScript 3.0 Design Pattern of interest. Bill</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2010%2F06%2Fmeet-in-nyc-on-june-30-2010%2F&amp;title=Meet%20in%20NYC%20on%20June%2030%2C%202010" 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/2010/06/meet-in-nyc-on-june-30-2010/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>ActionScript 3.0 Protection Proxy Design Pattern 1: Shielding the Real Subject</title>
		<link>http://www.as3dp.com/2010/06/actionscript-3-0-protection-proxy-design-pattern-1-shielding-the-real-subject/</link>
		<comments>http://www.as3dp.com/2010/06/actionscript-3-0-protection-proxy-design-pattern-1-shielding-the-real-subject/#comments</comments>
		<pubDate>Mon, 14 Jun 2010 01:08:01 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Proxy Pattern]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=3334</guid>
		<description><![CDATA[The images that people use on dating sites may not exactly represent who they are. So instead of putting in a real picture, they may use a proxy that is better looking, younger (or older), and generally more likely to attract a date. The Proxy Design Pattern deals with object access. In our book, we [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_3305" class="wp-caption alignleft" style="width: 294px"><img src="http://www.as3dp.com/wp-content/uploads/2010/06/proxReal.jpg" alt="Proxy and Real Subject" title="proxReal" width="284" height="188" class="size-full wp-image-3305" /><p class="wp-caption-text">Proxy and Real Subject</p></div>The images that people use on dating sites may not exactly represent who they are. So instead of putting in a real picture, they may use a proxy that is better looking, younger (or older), and generally more likely to attract a date. The Proxy Design Pattern deals with object access. In our book, we employed an unusual proxy, the <strong>Symmetric Proxy Pattern</strong> that uses a <strong>remote proxy</strong>. In fact, it uses mirrored proxies to access two different objects simultaneously. Our example showed how to use it with Flash Media Server, and so probably not a lot of developers use it as a model for understanding a Proxy pattern. However,  on this blog we provided a <strong><a href="http://www.as3dp.com/2008/08/26/actionscript-proxy-design-pattern-the-virtual-proxy-a-minimal-abstract-example/">virtual proxy</a></strong> that should be a clear and simple example of a Proxy design pattern. So, we covered two of the three Proxy designs. The sole remaining type is the <strong>Protection Proxy</strong>, and that&#8217;s what we&#8217;re going to look at now. (You can run it and download the files using the buttons below:)</p>
<p><a href="http://www.sandlight.net/proproxy/"  target="_blank"><img src="http://www.as3dp.com/wp-content/uploads/2009/12/play.jpg" alt="play" title="play" width="106" height="46" class="alignleft size-full wp-image-2255" /></a><a href="http://www.sandlight.net/proproxy/proproxy.zip"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/kilroy.png" alt="kilroy" title="kilroy" width="112" height="56" class="none size-full wp-image-2020" /></a></p>
<p><strong>The Easiest Proxy</strong></p>
<p>Of all of the Proxy designs, the Protection Proxy is very simple both in terms of implementation and concept. The Client makes a request to the Proxy, and the Proxy decides whether to give the Client access to the Subject.  Where  more than a single Subject is available, the Proxy decides on the level of access. For example, we started off with an example of a dating site. Some users have no access to the dating site until they sign up as a customer. Basically, the Proxy denies them all access to the subject. Once they sign up, they may be given access only to a specific gender. For example, the girls may only be given access to boys if that&#8217;s who they&#8217;re interested in meeting. The Proxy controls that access. Also, they may be given access only to change their own profile; again controlled by the Proxy. Of course the site administrator needs total access so that he can make sure that no one is posting materials that they shouldn&#8217;t be. <em>(When I posted that I was an Astronaut, Rock Star who owns a Silicon Valley software company, the administrator changed it—and without my permission!.</em>)</p>
<p>This design requires only three participants to implement : Subject (interface), Proxy and RealSubject with the Client making a request through the Subject interface to the Proxy. Figure 1 shows the Proxy class diagram shows and the different relationships:<br />
<div id="attachment_3328" class="wp-caption alignnone" style="width: 493px"><img src="http://www.as3dp.com/wp-content/uploads/2010/06/proxydiagram.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; Proxy class diagram&lt;/strong&gt;" title="proxydiagram" width="483" height="203" class="size-full wp-image-3328" /><p class="wp-caption-text"><em><strong>Figure 1:</strong> Proxy class diagram</em></p></div></p>
<p>The sequence is unique in that the request, while for the RealSubject, goes through the Proxy as shown in the object diagram in Figure 2:<br />
<div id="attachment_3332" class="wp-caption alignnone" style="width: 495px"><img src="http://www.as3dp.com/wp-content/uploads/2010/06/proxyobjectdiagram.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2: &lt;/strong&gt; Proxy object diagram&lt;/em&gt;" title="proxyobjectdiagram" width="485" height="81" class="size-full wp-image-3332" /><p class="wp-caption-text"><em><strong>Figure 2: </strong> Proxy object diagram</em></p></div></p>
<p>As you can see, the Proxy design is pretty straightforward, and given the three modes (<em>remote, virtual,</em> and<em> protection</em>), it&#8217;s versatile. However, in looking at it, it doesn&#8217;t seem to do too much in terms of loosening up code and reusability. According to the GoF,</p>
<blockquote><p>The Proxy pattern introduces a level of indirection when accessing an object. The additional indirection has many uses, depending on the kind of proxy: p. 210</p></blockquote>
<p>So the key to understanding the Proxy pattern is <em>indirection</em>. We need to consider indirection a bit more, which we begin on the next section.<br />
<span id="more-3334"></span></p>
<p>GoF list three uses of <strong>indirection</strong>:</p>
<ol>
<li>A remote proxy can hide the fact that an object resides in a different address space.</li>
<li>A virtual proxy can perform optimizations such as creating an object on demand.</li>
<li>Both <strong>protection proxies</strong> and smart references allow additional housekeeping when the object is accessed.</li>
</ol>
<p>Since we&#8217;re looking at the Protection proxy, we need to consider what <em>additional housekeeping</em> may entail.</p>
<p>Begin with the premise that a key function of the Proxy pattern is to prevent unnecessary creation of an object—especially a large, expensive one. Imagine a Web site that does not use a proxy but first creates an object with every request and then decides whether or not the request is valid. Thousands of requests along with the creation of the object could possibly slow operations to a crawl and none of the requests would result in using the object. With a Proxy, none of the blocked requests would create an object in the first place because the Proxy would handle the request.</p>
<p>Once the Proxy decides that the request will be allowed, it can handle any number of other chores. We discussed different access rights above when considering a dating site. Why should the requested object have to deal with access rights when the Proxy could do it instead? No reason at all. For this first post on the Protection Proxy, we will only look at handling a single chore—access. However, we should consider how the design pattern might be important for dealing with options.</p>
<p><strong>Loose Coupling and RealSubject Options</strong></p>
<p>One way to look at the Protection Proxy as a way to maintain loose coupling is to consider the flexibility one has in using, changing and adding RealSubject participants. If the Proxy and RealSubject share an interface the same set of parameters and methods can be used for both. Further, having more than one RealSubject is possible because the additional housekeeping is handled by the Proxy. Instead of having a single RealSubject responsible for different levels of access, the Proxy can determine the level of access and call the appropriate RealSubject for any level of access. As a result, the RealSubjects can be only large enough to handle what the access level requires. For example, if one is designing an application where access is to a database object, the object may have the following options:</p>
<ul>
<li>Access to data only</li>
<li>Access to data and adding data</li>
<li>Access to data, adding data and changing data</li>
<li>Access to data, adding data, changing data and deleting records</li>
</ul>
<p>Rather than have the program create a single large complex object for every access, a more efficient design would be to create different RealSubjects depending on level of access. So if most users only have access to read data, the program does not have to create a large complex object that reads, adds, changes and deletes data. At the same time the design can handle all the other levels of access by calling the appropriate RealSubject; it just doesn&#8217;t have to invoke them where they&#8217;re not required.</p>
<p>So in terms of design patterns, I believe that the Proxy should be considered beyond the basic indirection it provides. As long as the Proxy is handling the access details, the developer does not have to worry about access coding when developing the RealSubjects. Further, he can add, change and remove RealSubjects as needed without disturbing the entire program.</p>
<p><strong>Accessing Text Data using a Name/Value Pair</strong></p>
<p>To start with a simple example, this Protection Proxy monitors requests to open and read a text file. The text file represents any external data, and virtually the same code can be used to read a database. So, treat it as a generic database instead of a text file, and in Part 2, we&#8217;ll use a real MySQL database that is called via a PHP program. However, to kick things off, you don&#8217;t need a database or PHP—all you need is ActionScript 3.0 and a text file.</p>
<p>The <strong>name/value pair</strong> is a common data representation. As used with ActionScript 3.0 and external data, each name/value pair is separated by an ampersand (&#038;) in the loaded data stream. For example, suppose we we want to have three variables representing name, city and country, the data stream would be organized as follows:</p>
<p><strong>name=Nelson Mandela&#038;city=Johannesburg&#038;country=South Africa</strong></p>
<p>The variables are accessed as properties of the loaded object. In this case the object is a <strong>URLVariable</strong>, and to access it use the following code where <strong>NCC</strong> is the URLVariable instance:</p>
<p>     <strong>display.txt=NCC.name;<br />
     display.txt +=NCC.city;<br />
     display.txt +=NCC.country;</strong></p>
<p>In order to better simulate a database table, the variables (name) will be given the name <em><strong>record</strong></em> followed by a number. Open a text editor (<strong>Notepad</strong> or <strong>TextEdit</strong> will work fine) and enter the following:</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('p3334code20'); return false;">View Code</a> TEXT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p333420"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p3334code20"><pre class="text" style="font-family:monospace;">record1=001 Fred Smith  programmer  ActionScript 3.0
&amp;record2=002 Hanna Brauer programmer JavaScript
&amp;record3=003 Aalok Gupta designer  Adobe Illustrator
&amp;record4=004 Anna Petrov  designer  Adobe Photoshop
&amp;record5=005 Iracema Galleti programmer ActionScript 3.0
&amp;record6=006 Roberto Gonzalez programmer PHP
&amp;record7=007 Abeeku Badu programmer Java
&amp;record8=008 Fang Yin Zhao designer Adobe Illustrator</pre></td></tr></table></div>

<p>Save the text file as <strong>fromText.txt</strong>. Place the file in the same directory as the other ActionScript 3.0 files you will create for the Protection Proxy.</p>
<p><strong>The Subject and its Children</strong></p>
<p>The Subject participant is some kind of <strong>interface</strong>—either an interface or abstract class. For this project, I used an interface named <strong>ISubject</strong>. It has a single abstract method, <strong>pxRequest()</strong>. (Were <strong>request</strong> not a built-in method in ActionScript 3.0, I would have used that name.)</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('p3334code21'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p333421"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p3334code21"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #006600;">TextArea</span>;
	<span style="color: #0066CC;">interface</span> ISubject
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> pxRequest<span style="color: #66cc66;">&#40;</span>tx:TextArea,un:<span style="color: #0066CC;">String</span>,pw:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As far as interfaces go, ISubject is a bit more busy than most. It only has a single method, but that method has three parameters and the interface had to import a TextArea control. The reference to the TextArea is to &#8220;carry back&#8221; the reference to the Client request. However, because I was unable to find a way to return the contents of the text file, I carried the output control all the way to the RealSubject. The user name and password made up the two string parameters.</p>
<p><strong>The Proxy Class</strong></p>
<p>I named the Proxy class <strong>SubProxy</strong> (for Subject Proxy) because Proxy is a reserved word (for an ActionScript 3.0 class.) In this implementation, the SubProxy class simply checks to see if the username and password or acceptable, and if so, it calls the RealSubject. Otherwise it dismisses the request.</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('p3334code22'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p333422"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code" id="p3334code22"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #006600;">TextArea</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SubProxy <span style="color: #0066CC;">implements</span> ISubject
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> realSub:ISubject;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtArea:TextArea;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> un:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> pw:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pxRequest<span style="color: #66cc66;">&#40;</span>tx:TextArea,un:<span style="color: #0066CC;">String</span>,pw:<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;">this</span>.<span style="color: #006600;">un</span>=un;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">pw</span>=pw;
			txtArea=tx;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">un</span>==<span style="color: #ff0000;">&quot;Bill&quot;</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">pw</span>==<span style="color: #ff0000;">&quot;as3&quot;</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				realSub=<span style="color: #000000; font-weight: bold;">new</span> RealSubject<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				realSub.<span style="color: #006600;">pxRequest</span><span style="color: #66cc66;">&#40;</span>txtArea,<span style="color: #000000; font-weight: bold;">null</span>,<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #b1b100;">else</span>
			<span style="color: #66cc66;">&#123;</span>
				txtArea.<span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;Adios...&quot;</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>An important feature to note is in the SubProxy class is the RealSubject request (<strong>realSub.pxRequest(txtArea,null,null);</strong>). The second two parameters requesting strings for a username and password are set to <strong>null</strong>. The null primary expression keyword is not a typed object and works well for filling empty parameters were required by the interface. In this case I wanted to bring it to your attention to emphasize that all housekeeping chores are handled by the SubProxy class. In Part II, you will see far more work by the Proxy, but the RealSubject only does what it&#8217;s requested to do—which does not include housekeeping.</p>
<p><strong>The RealSubject Class</strong></p>
<p>Think of the RealSubject participant in the Protection Proxy design pattern as <em>any useful object</em>. In this case, the useful object is one that returns data from a text file. It does so by taking the data from the text file and putting it into a TextArea object.</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('p3334code23'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p333423"><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
</pre></td><td class="code" id="p3334code23"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequest</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLVariables</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLLoader</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">net</span>.<span style="color: #006600;">URLRequestMethod</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #006600;">TextArea</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RealSubject <span style="color: #0066CC;">implements</span> ISubject
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> urlString:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;fromText.txt&quot;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dataNow:URLVariables;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtLoader:URLLoader = <span style="color: #000000; font-weight: bold;">new</span> URLLoader<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> requestNow:URLRequest=<span style="color: #000000; font-weight: bold;">new</span> URLRequest<span style="color: #66cc66;">&#40;</span>urlString<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> tx:TextArea;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> pxRequest<span style="color: #66cc66;">&#40;</span>tx:TextArea,un:<span style="color: #0066CC;">String</span>,pw:<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;">this</span>.<span style="color: #006600;">tx</span>=tx;
			requestNow.<span style="color: #006600;">method</span>=URLRequestMethod.<span style="color: #006600;">POST</span>;
			txtLoader.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> Event.<span style="color: #006600;">COMPLETE</span>, loadTXT <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">try</span>
			<span style="color: #66cc66;">&#123;</span>
				txtLoader.<span style="color: #0066CC;">load</span><span style="color: #66cc66;">&#40;</span> requestNow <span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #0066CC;">catch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:ArgumentError<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">tx</span>.<span style="color: #0066CC;">text</span>=<span style="color: #0066CC;">e</span>;
			<span style="color: #66cc66;">&#125;</span>
			<span style="color: #0066CC;">catch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:SecurityError<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">tx</span>.<span style="color: #0066CC;">text</span>=<span style="color: #0066CC;">e</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> loadTXT<span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">e</span>:Event <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			txtLoader.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span> Event.<span style="color: #006600;">COMPLETE</span>, loadTXT <span style="color: #66cc66;">&#41;</span>;
			dataNow=<span style="color: #000000; font-weight: bold;">new</span> URLVariables<span style="color: #66cc66;">&#40;</span>txtLoader.<span style="color: #0066CC;">data</span><span style="color: #66cc66;">&#41;</span>;
			tx.<span style="color: #0066CC;">text</span>=dataNow.<span style="color: #006600;">record1</span>;
			tx.<span style="color: #006600;">appendText</span><span style="color: #66cc66;">&#40;</span>dataNow.<span style="color: #006600;">record2</span><span style="color: #66cc66;">&#41;</span>;
			tx.<span style="color: #006600;">appendText</span><span style="color: #66cc66;">&#40;</span>dataNow.<span style="color: #006600;">record3</span><span style="color: #66cc66;">&#41;</span>;
			tx.<span style="color: #006600;">appendText</span><span style="color: #66cc66;">&#40;</span>dataNow.<span style="color: #006600;">record4</span><span style="color: #66cc66;">&#41;</span>;
			tx.<span style="color: #006600;">appendText</span><span style="color: #66cc66;">&#40;</span>dataNow.<span style="color: #006600;">record5</span><span style="color: #66cc66;">&#41;</span>;
			tx.<span style="color: #006600;">appendText</span><span style="color: #66cc66;">&#40;</span>dataNow.<span style="color: #006600;">record6</span><span style="color: #66cc66;">&#41;</span>;
			tx.<span style="color: #006600;">appendText</span><span style="color: #66cc66;">&#40;</span>dataNow.<span style="color: #006600;">record7</span><span style="color: #66cc66;">&#41;</span>;
			tx.<span style="color: #006600;">appendText</span><span style="color: #66cc66;">&#40;</span>dataNow.<span style="color: #006600;">record8</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>The difficulty I had with this program was trying to return the data. The original request relies on an event (COMPLETE) to set the stage for using the data. Prior to that event, nothing can be returned, and the event handler cannot be called directly. Using a <strong>trace</strong> statement works fine, but beyond debugging it&#8217;s useless. I could find no way to use the TextArea component beyond the Client without passing it along as a parameter. (Apparently, no one else could either from searching the Web. However, if you have a way to return data from the RealSubject using a <strong>return</strong> statement—please send it in with working code.) Anyway, as you have seen, this technique works perfectly well.</p>
<p><strong>The Client</strong></p>
<p>All the Client does (even though it is the largest of the classes) is to provide a place to enter a username and password and request the data.</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('p3334code24'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p333424"><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
</pre></td><td class="code" id="p3334code24"><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: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #006600;">TextFieldAutoSize</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> fl.<span style="color: #006600;">controls</span>.<span style="color: #006600;">TextArea</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> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">MouseEvent</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> proxy:ISubject;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> lblUN:Label=<span style="color: #000000; font-weight: bold;">new</span> Label<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> lblPW:Label=<span style="color: #000000; font-weight: bold;">new</span> Label<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> txt:<span style="color: #0066CC;">TextField</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> format:<span style="color: #0066CC;">TextFormat</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtArea:TextArea=<span style="color: #000000; font-weight: bold;">new</span> TextArea<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> userInput: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> pwInput: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> 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>;
&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>
			setText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			setIO<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			btn.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>MouseEvent.<span style="color: #006600;">CLICK</span>, requestTextVars<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> setText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			format.<span style="color: #0066CC;">font</span>=<span style="color: #ff0000;">&quot;Arial Black&quot;</span>;
			format.<span style="color: #0066CC;">size</span>=<span style="color: #cc66cc;">24</span>;
			format.<span style="color: #0066CC;">color</span>=<span style="color: #ff0000;">&quot;0x990000&quot;</span>;
			txt.<span style="color: #006600;">defaultTextFormat</span>=format;
			addChild<span style="color: #66cc66;">&#40;</span>txt<span style="color: #66cc66;">&#41;</span>;
			txt.<span style="color: #0066CC;">autoSize</span>=TextFieldAutoSize.<span style="color: #0066CC;">LEFT</span>;
			txt.<span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;Protected Proxy&quot;</span>;
			txt.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">32</span>,txt.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">8</span>;
			txt.<span style="color: #0066CC;">height</span>=<span style="color: #cc66cc;">24</span>;
			lblUN.<span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;User name&quot;</span>;
			lblUN.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">136</span>,lblUN.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">48</span>;
			addChild<span style="color: #66cc66;">&#40;</span>lblUN<span style="color: #66cc66;">&#41;</span>;
			lblPW.<span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;Password&quot;</span>;
			lblPW.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">136</span>,lblPW.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">72</span>;
			addChild<span style="color: #66cc66;">&#40;</span>lblPW<span style="color: #66cc66;">&#41;</span>;
			btn.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">200</span>, btn.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">72</span>;
			btn.<span style="color: #006600;">label</span>= <span style="color: #ff0000;">&quot;Show Data&quot;</span>;
			btn.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">80</span>;
			addChild<span style="color: #66cc66;">&#40;</span>btn<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setIO<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			addChild<span style="color: #66cc66;">&#40;</span>userInput<span style="color: #66cc66;">&#41;</span>;
			userInput.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">32</span>,userInput.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">48</span>;
			addChild<span style="color: #66cc66;">&#40;</span>pwInput<span style="color: #66cc66;">&#41;</span>;
			pwInput.<span style="color: #006600;">displayAsPassword</span>=<span style="color: #000000; font-weight: bold;">true</span>;
			pwInput.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">32</span>,pwInput.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">72</span>;
			addChild<span style="color: #66cc66;">&#40;</span>txtArea<span style="color: #66cc66;">&#41;</span>;
			txtArea.<span style="color: #0066CC;">width</span>=<span style="color: #cc66cc;">248</span>;
			txtArea.<span style="color: #0066CC;">height</span>=<span style="color: #cc66cc;">250</span>;
			txtArea.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">32</span>,txtArea.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">104</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> requestTextVars<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>
			proxy=<span style="color: #000000; font-weight: bold;">new</span> SubProxy<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			proxy.<span style="color: #006600;">pxRequest</span><span style="color: #66cc66;">&#40;</span>txtArea,userInput.<span style="color: #0066CC;">text</span>,pwInput.<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>Using the <strong>requestTextVars()</strong>method with literals, the whole thing could have been done with three lines, but to be realistic, the Client needs to have user entry—username and password. The TextArea is automatically supplied; so the user simply types in the username and password and clicks the button to request the data. (Little does she know that the request is re-directed to the Proxy.)</p>
<p><strong>No Constructor Functions, Please</strong></p>
<p>You may have noticed that no constructor functions were used in any of the participants. I&#8217;ve started leaving out constructor functions unless they have something to do—as is the case with the Client. I have noted in several previous posts that the Google programming standards bearer, <a href="http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/" target="_blank">Miško Hevery</a>, cautions that constructor functions should do no real work. Here&#8217;s why:</p>
<blockquote><p> When your constructor has to instantiate and initialize its collaborators, the result tends to be an inflexible and prematurely coupled design.</p></blockquote>
<p> One way to help remember that is not to put them in the participant classes. True, constructor functions are automatically generated if none is placed in a class; however, by leaving out explicit constructor functions I&#8217;m not tempted to give them any real work.</p>
<p>In Part II, we&#8217;re going to give the Proxy a real workout and several options. Also, instead of a single RealSubject, there will be several. This means that the Proxy is going to have to filter through different levels of access and then select which will be appropriate for the request.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2010%2F06%2Factionscript-3-0-protection-proxy-design-pattern-1-shielding-the-real-subject%2F&amp;title=ActionScript%203.0%20Protection%20Proxy%20Design%20Pattern%201%3A%20Shielding%20the%20Real%20Subject" 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/2010/06/actionscript-3-0-protection-proxy-design-pattern-1-shielding-the-real-subject/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>ActionScript Proxy Design Pattern : The Virtual Proxy (A Minimal Abstract Example)</title>
		<link>http://www.as3dp.com/2008/08/actionscript-proxy-design-pattern-the-virtual-proxy-a-minimal-abstract-example/</link>
		<comments>http://www.as3dp.com/2008/08/actionscript-proxy-design-pattern-the-virtual-proxy-a-minimal-abstract-example/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 20:46:43 +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[Proxy Pattern]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=99</guid>
		<description><![CDATA[The Gang of Four divide up the Proxy design pattern into three types: remote, virtual, and protection. In our book, Chandima and I used a remote proxy in the Symmetric Proxy example(Chapter 13). Each player in a game over the Internet was able to make simultaneous moves using the remote proxy representing an opponent on [...]]]></description>
			<content:encoded><![CDATA[<p>The <em>Gang of Four </em> divide up the Proxy design pattern into three types: <strong>remote</strong>, <strong>virtual</strong>, and <strong>protection</strong>. In our book, Chandima and I used a <strong>remote proxy</strong> in the <strong>Symmetric Proxy</strong> example(Chapter 13).  Each player in a game over the Internet was able to make simultaneous moves using the remote proxy representing an opponent on their own system. That leaves the virtual and protection proxies to cover, and we’ll start with the <em>virtual proxy</em> in this article.</p>
<p><strong>Avoiding Repetition</strong></p>
<p>Our university uses a software package called <em><strong>Blackboard</strong></em> for course administration. It’s quite handy, and I use it a good deal. As a Web-based application requiring a login, the initial login is time-consuming as the application gathers up all of the information it has stored for all of my classes. However, if I quit the program and return within a certain time window, I don’t have to go through login again. More importantly, the application does not have to reload all of the materials for me again. When you consider that hundreds of faculty and thousands of students are all using the same system on a daily basis, the ability to re-use previously loaded materials is a huge savings.</p>
<p>For the most part, design patterns, do not improve the performance of your programs with a few exceptions, such as the <strong>Flyweight pattern</strong> previously discussed on this blog. I believe that the virtual proxy design pattern is another one of those patterns that improves the performance of your program because your application is not constantly re-doing something expensive (time-consuming) that’s already been done. Most of the examples, including the one provided by GoF, are of loading graphics. Once loaded, most graphics do not have to be reloaded for repeated display after the initial loading. So, rather than reload graphics or files of any kind, the virtual proxy design first checks to see if your materials are already loaded, and if they are, it uses the extant materials. If not, it simply calls the real loader and loads up what’s required. Any application that has multiple users over the Web (which is most programs) the virtual proxy can significantly improve the performance of the program because it reuses loaded materials.</p>
<p><strong>How the Proxy Design Pattern Works </strong></p>
<p>The Proxy pattern does not have different class diagrams for the different types of proxies. Figure 1 shows the pattern diagram used for all variations of the pattern.</p>
<p> <a href="http://www.as3dp.com/wp-content/uploads/2008/08/proxydiagram.png"><img src="http://www.as3dp.com/wp-content/uploads/2008/08/proxydiagram.png" alt="" title="proxydiagram" width="500" height="197" class="alignnone size-full wp-image-100" /></a><br />
<em>Figure 1: Proxy Class Diagram</em></p>
<p>The client is not a participant, but it is included to indicate where it sends its request. It is loosely coupled to the participants following the path illustrated in Figure 2.</p>
<p> <a href="http://www.as3dp.com/wp-content/uploads/2008/08/proxyobjectdiagram.png"><img src="http://www.as3dp.com/wp-content/uploads/2008/08/proxyobjectdiagram.png" alt="" title="proxyobjectdiagram" width="500" height="77" class="alignnone size-full wp-image-101" /></a><br />
<em>Figure 2: Proxy Object Diagram</em></p>
<p>The proxy is used to check and see if a real subject is required, and if so, it sends the request to the real subject. However, if the proxy can handle the request without using the real subject, it will do so. In effect, the proxy acts like a gatekeeper. It inspects all requests, and if it can deal with the requests it does, but no requests go directly to the real subject from the client. Figure 2 shows the intermediary position of the proxy. The only problem with the object diagram is that it looks like the proxy is a stop along the way to the real subject, but depending on the application, the request may never get to the real subject.<br />
<span id="more-99"></span><br />
<strong>A Login Example </strong></p>
<p>The login example stemmed from my experiences with <em><strong>Blackboard</strong></em>. The ease with which I am able to “re-log” without having to wait for a total reload is the driving rationale behind this abstract, virtual proxy design pattern. It is simple, yet informative both as an example of good OOP and design patterns.</p>
<p>To get started, I put together a class diagram with the classes I planned to use reflecting the Proxy design pattern as shown in Figure 3:</p>
<p><a href="http://www.as3dp.com/wp-content/uploads/2008/08/proxylogin.png"><img src="http://www.as3dp.com/wp-content/uploads/2008/08/proxylogin.png" alt="" title="proxylogin" width="500" height="197" class="alignnone size-full wp-image-102" /></a><br />
<em>Figure 3</em></p>
<p>The client instantiates a ProxyLogin typed as ISecurity, the interface. The proxy instance checks the username to see if the user is logged in; if so, it executes the login method that indicates that the user is already logged in. Otherwise, the proxy instantiates an instance of the RealLogin class, and it loads everything that needs loading and then informs the user that the login has been successful.</p>
<p><strong>The Subject Interface, Proxy and Real Deal</strong></p>
<p>First, the Subject class is an interface, ISecurity. It has a single method as the first listing shows:</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('p99code29'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9929"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p99code29"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> ISecurity
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> login<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Next, the Proxy class, LoginProxy, checks to see if the user is logged in. by checking the object containing login information:</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('p99code30'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9930"><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="p99code30"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ProxyLogin <span style="color: #0066CC;">implements</span> ISecurity
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> username:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> loggedIn:ISecurity;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ProxyLogin<span style="color: #66cc66;">&#40;</span>username:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">username</span>=username;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> login<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: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>loggedIn==<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				loggedIn=<span style="color: #000000; font-weight: bold;">new</span> RealLogin<span style="color: #66cc66;">&#40;</span>username<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			loggedIn.<span style="color: #006600;">login</span><span style="color: #66cc66;">&#40;</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>Now, the proxy will use a RealLogin object and its operations  <em>only if</em> it finds that no object with the username has logged in. It instantiates a new instance of RealLogin() with the username as a parameter. So let’s see what happens next:</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('p99code31'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9931"><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
</pre></td><td class="code" id="p99code31"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RealLogin <span style="color: #0066CC;">implements</span> ISecurity
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> username:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> RealLogin<span style="color: #66cc66;">&#40;</span>username:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">username</span>=username;
			doLogin<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> doLogin<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><span style="color: #ff0000;">&quot;Connect &quot;</span>+username+<span style="color: #ff0000;">&quot; to server&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Check &quot;</span>+username+<span style="color: #ff0000;">&quot;'s credentials&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Load data from database for &quot;</span>+username<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Compare &quot;</span>+username+<span style="color: #ff0000;">&quot;'s IP with banned list&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Verify &quot;</span>+username+<span style="color: #ff0000;">&quot;'s security level <span style="color: #000099; font-weight: bold;">\n</span>&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> login<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>username + <span style="color: #ff0000;">&quot; has logged in.<span style="color: #000099; font-weight: bold;">\n</span>&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>All of trace() statements in the doLogin() method represent heavy lifting by the application. The whole point is to avoid the doLogin() method if at all possible by re-using an existing login through the Proxy participant. After each login, the login() method indicates who has logged in.</p>
<p><strong>The Client</strong></p>
<p>The client requests a login by instantiating a ProxyLogin object. Then each object makes a request through the login() method. Because none have logged in initially, the ProxyLogin must invoke the RealLogin . To simulate re-logging in after a user is currently logged in, the client (SecurityProxy), one of the users (steve) logs in multiple times. However, as you will see, after the first login, repeat logins are <strong>free</strong> in that the materials generated initially no longer require the RealLogin.</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('p99code32'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9932"><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
</pre></td><td class="code" id="p99code32"><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>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SecurityProxy <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> bill:ISecurity;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> steve:ISecurity;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> stephen:ISecurity;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SecurityProxy<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			bill=<span style="color: #000000; font-weight: bold;">new</span> ProxyLogin<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Gates&quot;</span><span style="color: #66cc66;">&#41;</span>;
			steve=<span style="color: #000000; font-weight: bold;">new</span> ProxyLogin<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Jobs&quot;</span><span style="color: #66cc66;">&#41;</span>;
			stephen=<span style="color: #000000; font-weight: bold;">new</span> ProxyLogin<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Hawking&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			bill.<span style="color: #006600;">login</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			steve.<span style="color: #006600;">login</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			steve.<span style="color: #006600;">login</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			stephen.<span style="color: #006600;">login</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			steve.<span style="color: #006600;">login</span><span style="color: #66cc66;">&#40;</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>Note where the <strong>\n </strong> is placed to force an additional linefeed. That’s to separate the login announcement from all of the chores required to login. When all you see is repeated login announcement without all of the trace statements, it indicates that the user has re-logged. The following output is generated by the client’s request:</p>
<p><code>Connect Gates to server<br />
Check Gates's credentials<br />
Load data from database for Gates<br />
Compare Gates's IP with banned list<br />
Verify Gates's security level</p>
<p>Gates has logged in.</p>
<p>Connect Jobs to server<br />
Check Jobs's credentials<br />
Load data from database for Jobs<br />
Compare Jobs's IP with banned list<br />
Verify Jobs's security level</p>
<p>Jobs has logged in.</p>
<p>Jobs has logged in.</p>
<p>Connect Hawking to server<br />
Check Hawking's credentials<br />
Load data from database for Hawking<br />
Compare Hawking's IP with banned list<br />
Verify Hawking's security level</p>
<p>Hawking has logged in.</p>
<p>Jobs has logged in.<br />
</code><br />
Note that instances bill (“Gates”) and stephen (“Hawking”) log in only once. However, steve (“Jobs”) logs in two additional times after an initial login. The logins after the initial ones show the value of the virtual proxy design. All of the other tasks for login are not repeated, but instead the user is logged in without additional work by the RealLogin object.</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%2F08%2Factionscript-proxy-design-pattern-the-virtual-proxy-a-minimal-abstract-example%2F&amp;title=ActionScript%20Proxy%20Design%20Pattern%20%3A%20The%20Virtual%20Proxy%20%28A%20Minimal%20Abstract%20Example%29" 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/08/actionscript-proxy-design-pattern-the-virtual-proxy-a-minimal-abstract-example/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

