<?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; Facade</title>
	<atom:link href="http://www.as3dp.com/category/design-patterns/facade/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.as3dp.com</link>
	<description>OOP Techniques for Flash and Flex Developers</description>
	<lastBuildDate>Sun, 29 Jan 2012 17:00:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ActionScript Facade Design Pattern: The Cat Herder</title>
		<link>http://www.as3dp.com/2008/09/actionscript-facade-design-pattern-the-cat-herder/</link>
		<comments>http://www.as3dp.com/2008/09/actionscript-facade-design-pattern-the-cat-herder/#comments</comments>
		<pubDate>Sat, 20 Sep 2008 20:59:47 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Facade]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=222</guid>
		<description><![CDATA[The bigger and more complex a system based on subsystems gets, the more difficult it is for a client to make requests and get what is expected. The Facade acts as an interface to simplify communication with the elements that make up the subsystem and the client. So instead of communicating with all of the [...]]]></description>
			<content:encoded><![CDATA[<p>The bigger and more complex a system based on subsystems gets, the more difficult it is for a client to make requests and get what is expected. The Facade acts as an interface to simplify communication with the elements that make up the subsystem and the client. So instead of communicating with all of the components of the subsystem, the client interacts with a single component, the Facade. </p>
<p>The class diagram for the Façade looks a good deal different from most other design patterns. The classes that make up the subsystem are intentionally vague to represent <em>any</em> subsystem. The problem, though, is clearly illustrated by the network of possible connections between the classes in the subsystem and how a client might run into problems when it needs to interact with them all. Figure 1 shows a modified Façade class diagram. In the original GoF diagram, the client is not listed as a participant—only the Façade and the subsystem classes. However, following the Freemans’ diagram (with some slight adjustments of my own to make it closer to the GoF diagram) I included a client but made it implicit.<br />
<a href="http://www.as3dp.com/wp-content/uploads/2008/09/facade.png"><img src="http://www.as3dp.com/wp-content/uploads/2008/09/facade.png" alt="" title="facade" width="427" height="346" class="aligncenter size-full wp-image-223" /></a></p>
<p><strong>Figure 1:</strong> <em>Facade Class Diagram</em><br />
<span id="more-222"></span><br />
Gamma and associates use the compiler as an example of a <strong>Façade</strong>. It communicates with the different computer system classes, and anything outside of the subsystem communicates through the compiler. The Freemans (<em>Head First Design Patterns</em>) use an example of a super remote control that controls several different components of a home theater. Included are eight components to control such features as a screen, amplifier, tuner and even a popcorn maker. In each case, the point is that a single interface is easier to deal with than several. At the same time, the subsystem can remain loosely coupled and all the client has to worry about is communicating with the Façade. </p>
<p><strong>A Simplified Interface for Using Chroma Keys</strong></p>
<p>The subsystem can be anything from a collection of interacting classes, to other design patterns or even a collection of design patterns. Following the examples of GoF and the Freemans, I decided to see if I could make a Façade design pattern to clarify the use of chromakeys with streaming videos. The process requires a good deal of steps and different software, each of which can be treated as a class.</p>
<ol>
<li>Set up a chroma screen. (BackDrop)</li>
<li>Add the proper lighting (Lighting)</li>
<li>Film the shots (Film)</li>
<li>Transfer video to digital file using Quicktime (Conversion)</li>
<li>Edit the video using Adobe Premier and save as avi file (Edit)</li>
<li>Open avi file in Ultra 2 for adding chroma key effects and backdrops (Chroma)</li>
<li>Add avi file to Flash video encoder for conversion into flv file. (MakeFLV)</li>
<li>Create an FMS/Flash/Flex app to play flv file and publish it (Stream)</li>
</ol>
<p>Any one of the above steps can be simple or complex, but trying to encapsulate it as a process can be daunting. To get started in seeing how the Façade design pattern works, I made a simple subsystem of two classes (BackDrop and Film) that are part of the process of creating a streaming video with chroma key effects. Normally, you’d not need a Façade for such a simple subsystem, but two classes work as well as ten to show how to create and use the Façade design pattern.</p>
<p><strong>The Minimalist Façade</strong></p>
<p>First, we’ll create the subsystem of the steps to set up a chroma screen and film the video. (Both have been simplified.) The <strong>BackDrop</strong> class has methods for setting up and putting away the chroma screen as well as choosing a blue or green screen. The <strong>Film</strong> class simply starts and stops filming.</p>
<p><em>BackDrop.as</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('p222code5'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2225"><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
</pre></td><td class="code" id="p222code5"><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> BackDrop
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> task:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BackDrop<span style="color: #66cc66;">&#40;</span>task:<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;">task</span> = task;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setBlue<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>task+<span style="color: #ff0000;">&quot;Set blue screen&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> setGreen<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>task+<span style="color: #ff0000;">&quot;Set green screen&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> setStands<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>task+<span style="color: #ff0000;">&quot;Set up supports for screen&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> storeStands<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>task+<span style="color: #ff0000;">&quot;Take supports down and store&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> storeBackdrop<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>task+<span style="color: #ff0000;">&quot;Fold backdrop and put away&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><em>Film.as</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('p222code6'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2226"><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
</pre></td><td class="code" id="p222code6"><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> Film
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> task:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Film<span style="color: #66cc66;">&#40;</span>task:<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;">task</span> = task;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> shoot<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>task+<span style="color: #ff0000;">&quot;Start filming&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> stopShoot<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>task+<span style="color: #ff0000;">&quot;Stop filming&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>As you can see, the above classes are simple ones with nothing more than a constructor class and collection of methods. Both constructor classes are parameterized, suggesting openness for communication and variety.</p>
<p>Next comes the <strong>Façade</strong>. This façade class is named<strong> FilmFacade</strong> and holds references to both the <strong>BackDrop</strong> and <strong>Film</strong> classes. It contains two methods: one to start the chroma key process and one to end it. Within each method are the different operations based on methods from the classes in the subsystem.</p>
<p><em>FilmFacade.as</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('p222code7'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2227"><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
</pre></td><td class="code" id="p222code7"><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> FilmFacade <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> film:Film;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> backDrop:BackDrop;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> FilmFacade<span style="color: #66cc66;">&#40;</span>film:Film,backDrop:BackDrop<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">film</span> = film;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">backDrop</span> = backDrop;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> doChroma<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;Start project&quot;</span><span style="color: #66cc66;">&#41;</span>;
			backDrop.<span style="color: #006600;">setStands</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			backDrop.<span style="color: #006600;">setGreen</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			film.<span style="color: #006600;">shoot</span><span style="color: #66cc66;">&#40;</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> endChroma<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;Finish project&quot;</span><span style="color: #66cc66;">&#41;</span>;
			film.<span style="color: #006600;">stopShoot</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			backDrop.<span style="color: #006600;">storeBackdrop</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			backDrop.<span style="color: #006600;">storeStands</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>The payoff for the client class (cleverly named <strong>Client</strong>) is that it does not have to deal directly with the classes in the subsystem. Instead, the client only deals with the FilmFacade class.</p>
<p><em>Client.as</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('p222code8'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2228"><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="p222code8"><pre class="actionscript" style="font-family:monospace;">&nbsp;
package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</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> film:Film;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> backDrop:BackDrop;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> filmFacade:FilmFacade;
&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>
			film = <span style="color: #000000; font-weight: bold;">new</span> Film<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;The Chroma Process at Work: &quot;</span><span style="color: #66cc66;">&#41;</span>;
			backDrop = <span style="color: #000000; font-weight: bold;">new</span> BackDrop<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Acme Back Drops: &quot;</span><span style="color: #66cc66;">&#41;</span>;
			filmFacade = <span style="color: #000000; font-weight: bold;">new</span> FilmFacade<span style="color: #66cc66;">&#40;</span>film,backDrop<span style="color: #66cc66;">&#41;</span>;
			filmFacade.<span style="color: #006600;">doChroma</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			filmFacade.<span style="color: #006600;">endChroma</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>When you test this application, you will see the following output:</p>
<p><strong><code>Start project<br />
Acme Back Drops: Set up supports for screen<br />
Acme Back Drops: Set green screen<br />
The Chroma Process at Work: Start filming<br />
Finish project<br />
The Chroma Process at Work: Stop filming<br />
Acme Back Drops: Fold backdrop and put away<br />
Acme Back Drops: Take supports down and store</code></strong></p>
<p>The client simply instructs the Façade class to start and stop the chroma key operation. With only two classes in the subsystem, that’s no great shakes, but were the client to deal directly with the subsystem classes (which it can do), it would be a bit more involved, even with only two classes in the subsystem. As the subsystem becomes more complex, the need for the Façade grows. </p>
<p><strong>The Moral to the Façade Design Pattern: Don’t Talk to Strangers</strong></p>
<p>One of my favorite features of <em>Head First Design Patterns</em> is its clear rendering of design principles. Most of these principles are directly or indirectly from GoF, but sometimes the Freemans put together a principle that appears to be constructed from the logic of the pattern. For the <strong>Façade</strong> (and <strong>Adapter</strong>) pattern, they put forth the <em>Principle of Least Knowledge</em>. This principle pre-dates design patterns by a few years (1987) but is still an object oriented programming principle. Characterized as</p>
<blockquote><p>Only talk to your immediate friends</p>
</blockquote>
<p> the principle works something like this: A can talk to B, and B can talk to A and C, but A cannot talk to C without going through B. The Façade, is the interface between the client and the subsystem and in order to communicate with the subsystem, the client must go through the Façade. That follows the Principle of Least Knowledge.</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%2F09%2Factionscript-facade-design-pattern-the-cat-herder%2F&amp;title=ActionScript%20Facade%20Design%20Pattern%3A%20The%20Cat%20Herder" id="wpa2a_2"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2008/09/actionscript-facade-design-pattern-the-cat-herder/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

