<?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; PureMVC</title>
	<atom:link href="http://www.as3dp.com/category/frameworks/puremvc/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>PureMVC Goes Mobile 2: MVC-driven &amp; ready-to-compile Mobile Application with the Flex Framework! by Christian Peters</title>
		<link>http://www.as3dp.com/2011/07/puremvc-goes-mobile-2-mvc-driven-ready-to-compile-mobile-application-with-the-flex-framework-by-christian-peters/</link>
		<comments>http://www.as3dp.com/2011/07/puremvc-goes-mobile-2-mvc-driven-ready-to-compile-mobile-application-with-the-flex-framework-by-christian-peters/#comments</comments>
		<pubDate>Mon, 04 Jul 2011 01:09:14 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[PureMVC]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=5858</guid>
		<description><![CDATA[Gentle Readers: This is the second part of a post on using ActionScript in a mobile environment by our guest blogger, Christian Peters who maintains a blog at http://digitale-avantgarde.com. He was kind enough to translate his post from German into English. (For those of you who would prefer the original German version, visit Chris’ site.) [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_5859" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/06/PureMVC2.png" alt="Make ActionScript Design Patterns Mobile" title="PureMVC2" width="250" height="146" class="size-full wp-image-5859" /><p class="wp-caption-text">Make ActionScript Design Patterns Mobile</p></div><em><strong>Gentle Readers:</strong> This is the second part of a post on using ActionScript in a mobile environment by our guest blogger, Christian Peters who maintains a <a href=" http://digitale-avantgarde.com">blog</a> at http://digitale-avantgarde.com. He was kind enough to translate his post from German into English. (For those of you who would prefer the original German version, visit Chris’ site.) Because of the size of the post, we’ve had to divide it into two separate posts. The first half of this post is can be found in the previous posting. We and Chris would appreciate your comments.</em></p>
<p><strong>Registering Mediators</strong></p>
<p>If you look back at our ViewNavigatorApplication at the beginning, you’ll notice, that I’ve added an initial view:</p>
<p><code>1.	firstView="com.digitaleavantgarce.view.components.HomeView"</code></p>
<p>This is the first view and the ViewNavigator sets it as the active view at the beginning.<br />
I’ve moved it from the default package to a package that is more compliant to my overall structure.<br />
It can be any view you like, but let’s say it has a button with the label “Click me” in it. It should read something like this:</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('p5858code7'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p58587"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p5858code7"><pre class="actionscript" style="font-family:monospace;"><span style="color: #cc66cc;">1</span>.	<span style="color: #66cc66;">&lt;</span>s:View xmlns:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span>
<span style="color: #cc66cc;">2</span>.	                <span style="color: #006600;">xmlns</span>:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span>
<span style="color: #cc66cc;">3</span>.	                <span style="color: #006600;">initialize</span>=<span style="color: #ff0000;">&quot;onInitalization(event)&quot;</span>
<span style="color: #cc66cc;">4</span>.	                <span style="color: #006600;">title</span>=<span style="color: #ff0000;">&quot;Getting Started&quot;</span><span style="color: #66cc66;">&gt;</span>
<span style="color: #cc66cc;">5</span>.	        <span style="color: #66cc66;">&lt;</span>fx:Script<span style="color: #66cc66;">&gt;</span>
<span style="color: #cc66cc;">6</span>.	                <span style="color: #66cc66;">&lt;!</span><span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span>
<span style="color: #cc66cc;">7</span>.	                        <span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">events</span>.<span style="color: #006600;">FlexEvent</span>;
<span style="color: #cc66cc;">8</span>.
<span style="color: #cc66cc;">9</span>.	                        <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const <span style="color: #0066CC;">NAME</span>:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">'HomeView'</span>;
<span style="color: #cc66cc;">10</span>.	                        <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const CLICKED:<span style="color: #0066CC;">String</span> = <span style="color: #0066CC;">NAME</span>+<span style="color: #ff0000;">'Clicked'</span>;
<span style="color: #cc66cc;">11</span>.
<span style="color: #cc66cc;">12</span>.	                        <span style="color: #006600;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> onInitalization<span style="color: #66cc66;">&#40;</span>event:FlexEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #cc66cc;">13</span>.	                        <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">14</span>.	                                <span style="color: #0066CC;">this</span>.<span style="color: #006600;">id</span> = <span style="color: #0066CC;">NAME</span>;
<span style="color: #cc66cc;">15</span>.	                        <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">16</span>.	                <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&gt;</span>
<span style="color: #cc66cc;">17</span>.	        <span style="color: #66cc66;">&lt;/</span>fx:Script<span style="color: #66cc66;">&gt;</span>
<span style="color: #cc66cc;">18</span>.	        <span style="color: #66cc66;">&lt;</span>s:<span style="color: #0066CC;">Button</span> label=<span style="color: #ff0000;">&quot;Click me&quot;</span> horizontalCenter=<span style="color: #ff0000;">&quot;0&quot;</span> verticalCenter=<span style="color: #ff0000;">&quot;0&quot;</span> click=<span style="color: #ff0000;">&quot;dispatchEvent(new Event(CLICKED));&quot;</span><span style="color: #66cc66;">/&gt;</span>
<span style="color: #cc66cc;">19</span>.	<span style="color: #66cc66;">&lt;/</span>s:View<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>This should be familiar, but note two things:</p>
<p>First: Since the View does not have a name Property and I don’t want to implement an interface, I make sure that the id is equal to the NAME constant on initalization!</p>
<p>Second: When the button is clicked, it’s dispatching an event named CLICKED. Remember, that the views should not host logic, they just should forward the action to the framwork. Thus, a mediator should take care of the action.<br />
<span id="more-5858"></span><br />
But for now, there’s no mediator registered to the HomeView.  Fortunately, we have already a ViewNavigatorMediator, which sends the Notification that our HomeView becomes the active view. And, fortunately again, we have a MediateViewCommand that get’s triggered, when the view changes. Let’s register the HomeViewMediator there!</p>
<p>A quick summary: The HomeView is getting instanciated and registered automatically by the ViewNavigator. When this happens, our ViewNavigatorMediator broadcasts the Notification with a reference to the HomeView. The notification itself triggers the MediateViewCommand. Here we can register the appropriate view.<br />
That’s done with a few lines of code:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p5858code8'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p58588"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code" id="p5858code8"><pre class="actionscript" style="font-family:monospace;"><span style="color: #cc66cc;">1</span>.	<span style="color: #006600;">package</span> com.<span style="color: #006600;">digitaleavantgarde</span>.<span style="color: #006600;">controller</span>
<span style="color: #cc66cc;">2</span>.	<span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">3</span>.	        <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">digitaleavantgarde</span>.<span style="color: #006600;">ApplicationFacade</span>;
<span style="color: #cc66cc;">4</span>.	        <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">digitaleavantgarde</span>.<span style="color: #006600;">model</span>.<span style="color: #006600;">SomeUsefulProxy</span>;
<span style="color: #cc66cc;">5</span>.	        <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">digitaleavantgarde</span>.<span style="color: #006600;">view</span>.<span style="color: #006600;">HomeViewMediator</span>;
<span style="color: #cc66cc;">6</span>.
<span style="color: #cc66cc;">7</span>.	        <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">as3</span>.<span style="color: #006600;">interfaces</span>.<span style="color: #006600;">ICommand</span>;
<span style="color: #cc66cc;">8</span>.	        <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">as3</span>.<span style="color: #006600;">interfaces</span>.<span style="color: #006600;">INotification</span>;
<span style="color: #cc66cc;">9</span>.	        <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">as3</span>.<span style="color: #006600;">patterns</span>.<span style="color: #006600;">command</span>.<span style="color: #006600;">SimpleCommand</span>;
<span style="color: #cc66cc;">10</span>.	        <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">as3</span>.<span style="color: #006600;">patterns</span>.<span style="color: #006600;">mediator</span>.<span style="color: #006600;">Mediator</span>;
<span style="color: #cc66cc;">11</span>.
<span style="color: #cc66cc;">12</span>.	        <span style="color: #0066CC;">import</span> spark.<span style="color: #006600;">components</span>.<span style="color: #006600;">View</span>;
<span style="color: #cc66cc;">13</span>.
<span style="color: #cc66cc;">14</span>.	        <span style="color: #808080; font-style: italic;">/**
15.	         * Selects the appropriate Mediator and registeres it.
16.	         */</span>
<span style="color: #cc66cc;">17</span>.	        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MediateViewCommand <span style="color: #0066CC;">extends</span> SimpleCommand <span style="color: #0066CC;">implements</span> ICommand
<span style="color: #cc66cc;">18</span>.	        <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">19</span>.	                <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const <span style="color: #0066CC;">NAME</span>:<span style="color: #0066CC;">String</span> = ‘MediateViewCommand’;
<span style="color: #cc66cc;">20</span>.
<span style="color: #cc66cc;">21</span>.	                <span style="color: #006600;">override</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> execute<span style="color: #66cc66;">&#40;</span>notification:INotification<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #cc66cc;">22</span>.	                <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">23</span>.	                        <span style="color: #000000; font-weight: bold;">var</span> view:View = notification.<span style="color: #006600;">getBody</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as View;
<span style="color: #cc66cc;">24</span>.
<span style="color: #cc66cc;">25</span>.	                        <span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>view.<span style="color: #006600;">id</span>+‘Mediator’<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">26</span>.	                                <span style="color: #b1b100;">case</span> HomeViewMediator.<span style="color: #0066CC;">NAME</span>:
<span style="color: #cc66cc;">27</span>.	                                        <span style="color: #006600;">facade</span>.<span style="color: #006600;">registerMediator</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> HomeViewMediator<span style="color: #66cc66;">&#40;</span>view<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #cc66cc;">28</span>.	                                        <span style="color: #006600;">facade</span>.<span style="color: #006600;">registerProxy</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SomeUsefulProxy<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #cc66cc;">29</span>.	                                        <span style="color: #b1b100;">break</span>;
<span style="color: #cc66cc;">30</span>.	                                <span style="color: #808080; font-style: italic;">// Add other Mediators likewise</span>
<span style="color: #cc66cc;">31</span>.	                        <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">32</span>.	                <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">33</span>.	        <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">34</span>.	<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Remember that the id of the view is equal to its Name. When we follow good practice, we’ll name our mediators by the scheme NameOfTheViewMediator. Thus, we can build a switch/case construct to select the appropriate mediator and register it by appending “Mediator” to the id of the view.</p>
<p>We also register a proxy here. The Proxy may load any data that is necessary for the view. This is not the scope of this article, but there’s an appendix at the end to make things complete!</p>
<p>That’s it. Now we can be sure that every view is registered automatically once the assigned view get’s active! In bigger applications you may want to unregister unused mediators, but that’s not necessary in most scenarios (because the ViewNavigator doesn’t unregister the views either).</p>
<p>Let’s take a look at the HomeViewMediator:</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('p5858code9'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p58589"><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
</pre></td><td class="code" id="p5858code9"><pre class="actionscript" style="font-family:monospace;"><span style="color: #cc66cc;">1</span>.	<span style="color: #006600;">package</span> com.<span style="color: #006600;">digitaleavantgarde</span>.<span style="color: #006600;">view</span>
<span style="color: #cc66cc;">2</span>.	<span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">3</span>.	        <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
<span style="color: #cc66cc;">4</span>.
<span style="color: #cc66cc;">5</span>.	        <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">as3</span>.<span style="color: #006600;">interfaces</span>.<span style="color: #006600;">IMediator</span>;
<span style="color: #cc66cc;">6</span>.	        <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">as3</span>.<span style="color: #006600;">interfaces</span>.<span style="color: #006600;">INotification</span>;
<span style="color: #cc66cc;">7</span>.	        <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">as3</span>.<span style="color: #006600;">patterns</span>.<span style="color: #006600;">mediator</span>.<span style="color: #006600;">Mediator</span>;
<span style="color: #cc66cc;">8</span>.
<span style="color: #cc66cc;">9</span>.	        <span style="color: #0066CC;">import</span> spark.<span style="color: #006600;">components</span>.<span style="color: #006600;">View</span>;
<span style="color: #cc66cc;">10</span>.	        <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">digitaleavantarde</span>.<span style="color: #006600;">view</span>.<span style="color: #006600;">components</span>.<span style="color: #006600;">HomeView</span>;
<span style="color: #cc66cc;">11</span>.
<span style="color: #cc66cc;">12</span>.	        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HomeViewMediator <span style="color: #0066CC;">extends</span> Mediator <span style="color: #0066CC;">implements</span> IMediator
<span style="color: #cc66cc;">13</span>.	        <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">14</span>.	                <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const <span style="color: #0066CC;">NAME</span>:<span style="color: #0066CC;">String</span> = ‘HomeViewMediator’;
<span style="color: #cc66cc;">15</span>.
<span style="color: #cc66cc;">16</span>.	                <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> homeView:HomeView;
<span style="color: #cc66cc;">17</span>.	                <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> HomeViewMediator<span style="color: #66cc66;">&#40;</span>view:View<span style="color: #66cc66;">&#41;</span>
<span style="color: #cc66cc;">18</span>.	                <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">19</span>.	                        <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">NAME</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #cc66cc;">20</span>.	                        <span style="color: #0066CC;">this</span>.<span style="color: #006600;">homeView</span> = view as HomeView;
<span style="color: #cc66cc;">21</span>.
<span style="color: #cc66cc;">22</span>.	                <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">23</span>.
<span style="color: #cc66cc;">24</span>.	                <span style="color: #006600;">override</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> onRegister<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #cc66cc;">25</span>.	                <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">26</span>.	                        <span style="color: #006600;">homeView</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>HomeView.<span style="color: #006600;">CLICKED</span>, onButtonPushed<span style="color: #66cc66;">&#41;</span>;
<span style="color: #cc66cc;">27</span>.	                <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">28</span>.
<span style="color: #cc66cc;">29</span>.	                <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> onButtonPushed<span style="color: #66cc66;">&#40;</span>evt:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">30</span>.	                        <span style="color: #808080; font-style: italic;">// Do something when the button is pushed</span>
<span style="color: #cc66cc;">31</span>.	                <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">32</span>.
<span style="color: #cc66cc;">33</span>.	                <span style="color: #006600;">override</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> listNotificationInterests<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">34</span>.	                        <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#91;</span>
<span style="color: #cc66cc;">35</span>.
<span style="color: #cc66cc;">36</span>.	                        <span style="color: #66cc66;">&#93;</span>;
<span style="color: #cc66cc;">37</span>.	                <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">38</span>.
<span style="color: #cc66cc;">39</span>.	                <span style="color: #006600;">override</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> handleNotification<span style="color: #66cc66;">&#40;</span>notification:INotification<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">40</span>.
<span style="color: #cc66cc;">41</span>.	                        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">name</span>:<span style="color: #0066CC;">String</span> = notification.<span style="color: #006600;">getName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #cc66cc;">42</span>.
<span style="color: #cc66cc;">43</span>.	                        <span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #cc66cc;">44</span>.	                        <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">45</span>.	                                <span style="color: #808080; font-style: italic;">// Code goes here</span>
<span style="color: #cc66cc;">46</span>.	                        <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">47</span>.	                <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">48</span>.	        <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">49</span>.	<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The mediator gets a reference to the home view and starts listening for events when the “Click me” button gets clicked. It can than perform the necessary action. However, it can listen to notifications itself and handle them appropriate.<br />
If you’d want to navigate to another view, for example after the CLICKED event, you’d just use one of ViewNavigators navigation functions. You have access to it via the navigation property of the view, in this case homeView.navigator!<br />
This is PureMVC, ported to Flex Mobile!</p>
<p><strong>Wrapping Up</strong></p>
<p>This is complex stuff. Thank you so much for reading. We’ve build a MVC-Driven Flex Mobile App!<br />
The basic idea is to hook to ViewNavigator and listen for the Event that is dispatched when the view changes – and register the appropriate Mediator on demand. We’ve covered some fundamentals of PureMVC along the way.<br />
For those of you, who are already familiar with Pure MVC, the procedure may seem counterintuitive: We’re registering Mediators AFTER the associated View. But: We’ve managed to maintain the essence of the framework: We’ve kept things decoupled.<br />
I’m looking forward to hear from your great mobile applications build with PureMVC!</p>
<p>APPENDIX<br />
Alright, for those who have still energy, let’s briefly discuss the concept of proxies. We’ve instantiated a SomeUsefulProxy in our StartupCommand. A simple implementation of the proxy could read like this:</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('p5858code10'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p585810"><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="p5858code10"><pre class="actionscript" style="font-family:monospace;"><span style="color: #cc66cc;">1</span>.	<span style="color: #006600;">package</span> com.<span style="color: #006600;">digitaleavantgarde</span>.<span style="color: #006600;">model</span>
<span style="color: #cc66cc;">2</span>.	<span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">3</span>.	        <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">digitaleavantgarde</span>.<span style="color: #006600;">model</span>.<span style="color: #006600;">vo</span>.<span style="color: #006600;">SomeUsefulVO</span>;
<span style="color: #cc66cc;">4</span>.
<span style="color: #cc66cc;">5</span>.	        <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">as3</span>.<span style="color: #006600;">interfaces</span>.<span style="color: #006600;">IProxy</span>;
<span style="color: #cc66cc;">6</span>.	        <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">as3</span>.<span style="color: #006600;">patterns</span>.<span style="color: #006600;">proxy</span>.<span style="color: #006600;">Proxy</span>;
<span style="color: #cc66cc;">7</span>.
<span style="color: #cc66cc;">8</span>.	        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeUsefulProxy <span style="color: #0066CC;">extends</span> Proxy <span style="color: #0066CC;">implements</span> IProxy
<span style="color: #cc66cc;">9</span>.	        <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">10</span>.
<span style="color: #cc66cc;">11</span>.	                <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const <span style="color: #0066CC;">NAME</span>:<span style="color: #0066CC;">String</span> = ‘SomeUsefulProxy’;
<span style="color: #cc66cc;">12</span>.	                <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const IMAGE_READY:<span style="color: #0066CC;">String</span> = <span style="color: #0066CC;">NAME</span>+‘ImageReady’;
<span style="color: #cc66cc;">13</span>.
<span style="color: #cc66cc;">14</span>.	                <span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;data/image.png&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #cc66cc;">15</span>.	                <span style="color: #0066CC;">private</span> const UsefulImage:<span style="color: #000000; font-weight: bold;">Class</span>;
<span style="color: #cc66cc;">16</span>.
<span style="color: #cc66cc;">17</span>.	                <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> SomeUsefulProxy<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #cc66cc;">18</span>.	                <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">19</span>.	                        <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">NAME</span>, <span style="color: #000000; font-weight: bold;">new</span> SomeUsefulVO<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #cc66cc;">20</span>.	                        <span style="color: #006600;">vo</span>.<span style="color: #006600;">image</span> = <span style="color: #000000; font-weight: bold;">new</span> UsefulImage<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #cc66cc;">21</span>.	                        <span style="color: #006600;">sendNotification</span><span style="color: #66cc66;">&#40;</span>IMAGE_READY, vo.<span style="color: #006600;">image</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #cc66cc;">22</span>.	                <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">23</span>.
<span style="color: #cc66cc;">24</span>.	                <span style="color: #808080; font-style: italic;">/**
25.	                 * @return data as SomeUsefulVO
26.	                 */</span>
<span style="color: #cc66cc;">27</span>.	                <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> vo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:SomeUsefulVO
<span style="color: #cc66cc;">28</span>.	                <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">29</span>.	                        <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">data</span> as SomeUsefulVO;
<span style="color: #cc66cc;">30</span>.	                <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">31</span>.	        <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">32</span>.	<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>To keep things simple, I’ve just embedded an image. You may opt to load an XML here or connect to a database or whatever.<br />
We then assign this image to our ValueObject and broadcast a notification to inform interested parties that the image is ready. I’ve also implemented a method named vo() that returns our ValueObject, this is just good practise.<br />
The class of the ValueObject would read like this:</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('p5858code11'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p585811"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p5858code11"><pre class="actionscript" style="font-family:monospace;"><span style="color: #cc66cc;">1</span>.	<span style="color: #006600;">package</span> com.<span style="color: #006600;">digitaleavantgarde</span>.<span style="color: #006600;">model</span>.<span style="color: #006600;">vo</span>
<span style="color: #cc66cc;">2</span>.	<span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">3</span>.	        <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Bitmap</span>;
<span style="color: #cc66cc;">4</span>.
<span style="color: #cc66cc;">5</span>.	        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SomeUsefulVO
<span style="color: #cc66cc;">6</span>.	        <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">7</span>.	                <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> image:Bitmap;
<span style="color: #cc66cc;">8</span>.	        <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">9</span>.	<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Just a class with the data as public property. If you’d want to access this image, say in the HomeViewMediator, you’d have to adjust the listNotificationInterest()-Method and the handleNotification()-Method like this:</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('p5858code12'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p585812"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code" id="p5858code12"><pre class="actionscript" style="font-family:monospace;"><span style="color: #cc66cc;">1</span>.	<span style="color: #006600;">override</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> listNotificationInterests<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">2</span>.	        <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#91;</span>
<span style="color: #cc66cc;">3</span>.	                <span style="color: #006600;">SomeUsefulProxy</span>.<span style="color: #006600;">IMAGE_READY</span>
<span style="color: #cc66cc;">4</span>.	        <span style="color: #66cc66;">&#93;</span>;
<span style="color: #cc66cc;">5</span>.	<span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">6</span>.
<span style="color: #cc66cc;">7</span>.	<span style="color: #006600;">override</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> handleNotification<span style="color: #66cc66;">&#40;</span>notification:INotification<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">8</span>.
<span style="color: #cc66cc;">9</span>.	        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">name</span>:<span style="color: #0066CC;">String</span> = notification.<span style="color: #006600;">getName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #cc66cc;">10</span>.
<span style="color: #cc66cc;">11</span>.	        <span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #cc66cc;">12</span>.	        <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">13</span>.	                <span style="color: #b1b100;">case</span> SomeUsefulProxy.<span style="color: #006600;">IMAGE_READY</span>:
<span style="color: #cc66cc;">14</span>.	                      <span style="color: #000000; font-weight: bold;">var</span> bmp:Bitmap = notification.<span style="color: #006600;">getBody</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as Bitmap;
<span style="color: #cc66cc;">15</span>.	                      <span style="color: #808080; font-style: italic;">// Do something with the bitmap, like place it somewhere in the view</span>
<span style="color: #cc66cc;">16</span>.	       <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">17</span>.	<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>You could also retrieve the proxy and its value object via the facade.retrieveProxy() Method, if you wouldn’t include the image as body of the notification. This may be the case, if more complex data is involved.<br />
Hope that helps to get you started!<br />
If you want to play around with the example: Download the entire project and add <a href="http://puremvc.org/pages/downloads/AS3/PureMVC_AS3.zip">PureMVC </a>to the build path.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2011%2F07%2Fpuremvc-goes-mobile-2-mvc-driven-ready-to-compile-mobile-application-with-the-flex-framework-by-christian-peters%2F&amp;title=PureMVC%20Goes%20Mobile%202%3A%20MVC-driven%20%26amp%3B%20ready-to-compile%20Mobile%20Application%20with%20the%20Flex%20Framework%21%20by%20Christian%20Peters" 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/2011/07/puremvc-goes-mobile-2-mvc-driven-ready-to-compile-mobile-application-with-the-flex-framework-by-christian-peters/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>PureMVC Goes Mobile 1: MVC-driven &amp; ready-to-compile Mobile Application with the Flex Framework! by Christian Peters</title>
		<link>http://www.as3dp.com/2011/06/puremvc-goes-mobile-1-mvc-driven-ready-to-compile-mobile-application-with-the-flex-framework-by-christian-peters/</link>
		<comments>http://www.as3dp.com/2011/06/puremvc-goes-mobile-1-mvc-driven-ready-to-compile-mobile-application-with-the-flex-framework-by-christian-peters/#comments</comments>
		<pubDate>Tue, 28 Jun 2011 11:04:30 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[PureMVC]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=5824</guid>
		<description><![CDATA[Gentle Readers: Every now and then Chandima and I have a guest post. In this case, Christian Peters who maintains a blog at http://digitale-avantgarde.com was kind enough to translate his post from German into English. (For those of you who would prefer the original German version, visit Chris&#8217; site.) Because of the size of the [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_5853" class="wp-caption alignleft" style="width: 260px"><img src="http://www.as3dp.com/wp-content/uploads/2011/06/PureMVC1.png" alt="Optimizing Mobile Apps with Design Patterns" title="PureMVC1" width="250" height="146" class="size-full wp-image-5853" /><p class="wp-caption-text">Optimizing Mobile Apps with Design Patterns</p></div><em><strong>Gentle Readers:</strong> Every now and then Chandima and I have a guest post. In this case, Christian Peters who maintains a <a href="http://digitale-avantgarde.com">blog</a> at http://digitale-avantgarde.com was kind enough to translate his post from German into English. (For those of you who would prefer the original German version, visit Chris&#8217; site.) Because of the size of the post, we&#8217;ve had to divide it into two separate posts. The second half of the post will be available next week. We and Chris would appreciate your comments.</em> Click the download button to get the entire code:<br />
<a href="http://nemo.mwd.hartford.edu/~wsanders/blog/pureMVCMobile/GettingStarted.fxp.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>PureMVC Goes Mobile</strong></p>
<p>Let’s build a cross-platform compatible, MVC-driven &#038; ready-to-compile Mobile Application with the Flex Framework!<br />
A year ago, finding people who bet serious money on Flash missing a connection to the mobile world was an easy task. This is no longer the case nowadays.  Adobe has introduced an iOS Compiler and AIR for Android &#038; Blackberry Playbook. With the latest release of Flex SDK 4.5.1, the average Flash-Coder is transformed to a crossplatform-ready mobile developer. Amazing times. Let’s look at how to adjust the Standard Port of the famous PureMVC Framework to be mobile-ready! The Flex SDK ships with an integrated Navigation for Mobile Views, the ViewNavigator. There is an excellent introduction to the topic, that can be found <a href="http://corlan.org/2011/01/12/understanding-flex-mobile-views-and-viewnavigator/">here</a>, but I’ll give a brief overview about the concept:</p>
<p>Mobile Applications – especially on Smartphones – are restricted in a very important feature: Space. If you adapt a Web app to mobile, you’ll typically have much more views because you have to present the same content on smaller displays. Flex comes to the rescue by introducing the ViewNavigator. The ViewNavigator is a container, that holds references to the available views and makes navigation between them easy. You can imagine it as an array – where the view at the zero index is visible &#038; active. It’s not a coincidence that the ViewNavigator has array-esque methods like <strong>popView()</strong> or <strong>pushView()</strong>!</p>
<p>The <strong>ViewNavigator</strong> is a nice way to handle views, because it’s an easy way to navigate through an app: Every view holds a reference to the ViewNavigtor – the navigator property – and can forward the application to another view on demand.<br />
Well, it’s not all roses with the ViewNavigator! A view has its own control-mechanism as property – while it should be vice versa. Another downside: The registering and instantiation of views are becoming tasks of Flex, while you normally want to handle those tasks in your infrastructure framework of choice! Mine is PureMVC. I want to show you a way to hook PureMVC to the registering process of the ViewNavigator to build nicely decoupled applications for the mobile world. Some experience with PureMVC is recommended, but I’ll give you a quick …<br />
<span id="more-5824"></span><br />
<strong>Crash Course in PureMVC</strong></p>
<p>You might have guessed, that PureMVC is a reference implementation of the MVC-Design Pattern. By my personal definition, MVC is not a design pattern but a composition of design patterns. And, again by my personal definition, PureMVC is a perfect composition. <a href="http://puremvc.org/component/option,com_wrapper/Itemid,34/">Take a look at the conceptual diagram</a>! (Also, take a look at Chandima&#8217;s work on PureMVC on this blog. <a href="http://www.as3dp.com/2007/12/27/minimalist-mvc-example-using-the-puremvc-framework/">Minimalist PureMVC</a> and <a href="http://www.as3dp.com/2009/07/22/a-non-flex-actionscript-example-of-a-puremvc-multicore-application/">Move the Cheese</a> )</p>
<p>PureMVC is build around four singletons, known as the Core Actors. Three of them are predictably named: Model, View and Controller. I’ll come to them in a moment. The fourth is the Facade, a design pattern meant to hide complexity from the client. The client may be a Flex Application or the Document Class of an Actionscript Project, respectively the Stage (it may be much more, because PureMVC is ported for many languages)! The client forwards a reference to itself to the Facade, and the Facade boots the framework. Thus, if views are instantiated later, the Facade forwards it to the client.</p>
<p>The participants of the model are named proxies, but they are not following the exact definition of the Proxy pattern. There task is to supply data from all kind of sources (XML, Database …) to  Value Objects. Value Objects are simple classes without methods. They host the supplied data as public properties. All proxies are hosted in the Model-Singleton and can be retrieved from it via the Facade.<br />
The business logic is shared among commands – stateless classes that are executing tasks. Commands are hosted in the Controller-Singleton. You are registering commands via the Facade.</p>
<p>The last actor – the View – hosts the Mediators. They are the interface between views and the framework. The views itself are standing outside the MVC-implementation, so they can be replaced easily. The Mediators are registering the views and listen for user actions inside of them. Then they decide how a model should be manipulated based on that action. Or they trigger a command to do it and await a notification on how to present the changed model state to the user. Since all core actors are hidden from the client, you are registering mediators like its companions: via the Facade.</p>
<p>Notifications are PureMVC’s own implementation of the observer pattern. Mediators can register for Notifications they care about, and Commands are registered to a notification. Models do not listen to Notifications. Notifications can be send by all participants of MVC and they can contain a generic object. Notifications are a very cool alternative to Actionscripts event system, as they are available to classes that do not implement the IEventDispatcher Interface. If you want to dive in any further, read the excellent documentation on <a href="http://puremvc.org/">puremvc.org</a>! If you’re hungry to build a MVC-driven mobile app, keep reading. I’ll explain details along the way.</p>
<p>Finally, we’re arriving the essential topic of this article: How to adjust this composition for mobile needs?</p>
<p><strong>The Bootstrap</strong></p>
<p>As said above, normally the Mediators are taking care of the instanciation and registering of the views. However, in Flex Mobile Applications, the ViewNavigator claims to do it – and we have to live with it. My approach is to provide a mediator for the ViewNavigator itself and listen for events that indicate a change of the view. Then we forward this information to a command that figures out, which mediator to register.</p>
<p>Normally you’d give a reference to the basic view to the Facade. That way, all views that are instantiated by the mediators can be placed in the base view. Since we do not place or instantiate views in mobile navigation (the ViewNavigator does), we give a reference to the ViewNavigator to the Facade. Thus, we can build our hook to <strong>ViewNavigatorEvents</strong> later.<br />
Fire up a new Flex Mobile Project and adjust the code like this:</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('p5824code17'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p582417"><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="p5824code17"><pre class="actionscript" style="font-family:monospace;"><span style="color: #cc66cc;">1</span>.	<span style="color: #66cc66;">&lt;</span>?<span style="color: #0066CC;">xml</span> <span style="color: #0066CC;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #66cc66;">&gt;</span>
<span style="color: #cc66cc;">2</span>.	<span style="color: #66cc66;">&lt;</span>s:ViewNavigatorApplication
<span style="color: #cc66cc;">3</span>.	        <span style="color: #006600;">xmlns</span>:fx=<span style="color: #ff0000;">&quot;http://ns.adobe.com/mxml/2009&quot;</span>
<span style="color: #cc66cc;">4</span>.	        <span style="color: #006600;">xmlns</span>:s=<span style="color: #ff0000;">&quot;library://ns.adobe.com/flex/spark&quot;</span>
<span style="color: #cc66cc;">5</span>.	        <span style="color: #006600;">firstView</span>=<span style="color: #ff0000;">&quot;com.digitaleavantgarde.view.components.HomeView&quot;</span>
<span style="color: #cc66cc;">6</span>.	        <span style="color: #006600;">applicationDPI</span>=<span style="color: #ff0000;">&quot;240&quot;</span>
<span style="color: #cc66cc;">7</span>.	        <span style="color: #006600;">initialize</span>=<span style="color: #ff0000;">&quot;onInitalization(event)&quot;</span><span style="color: #66cc66;">&gt;</span>
<span style="color: #cc66cc;">8</span>.	        <span style="color: #66cc66;">&lt;</span>fx:Script<span style="color: #66cc66;">&gt;</span>
<span style="color: #cc66cc;">9</span>.	                <span style="color: #66cc66;">&lt;!</span><span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span>
<span style="color: #cc66cc;">10</span>.	                        <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">digitaleavantgarde</span>.<span style="color: #006600;">ApplicationFacade</span>;
<span style="color: #cc66cc;">11</span>.	                        <span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">events</span>.<span style="color: #006600;">FlexEvent</span>;
<span style="color: #cc66cc;">12</span>.
<span style="color: #cc66cc;">13</span>.	                        <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> facade:ApplicationFacade = ApplicationFacade.<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #cc66cc;">14</span>.
<span style="color: #cc66cc;">15</span>.	                        <span style="color: #006600;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> onInitalization<span style="color: #66cc66;">&#40;</span>event:FlexEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #cc66cc;">16</span>.	                        <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">17</span>.	                                <span style="color: #808080; font-style: italic;">// Booting Framework</span>
<span style="color: #cc66cc;">18</span>.	                                <span style="color: #006600;">facade</span>.<span style="color: #006600;">startup</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">navigator</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #cc66cc;">19</span>.	                        <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">20</span>.	                <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&gt;</span>
<span style="color: #cc66cc;">21</span>.	        <span style="color: #66cc66;">&lt;/</span>fx:Script<span style="color: #66cc66;">&gt;</span>
<span style="color: #cc66cc;">22</span>.	<span style="color: #66cc66;">&lt;/</span>s:ViewNavigatorApplication<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>This is just the prelude to PureMVC. Now let’s look how to use the framework! Let’s start with the Facade:</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('p5824code18'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p582418"><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
</pre></td><td class="code" id="p5824code18"><pre class="actionscript" style="font-family:monospace;"><span style="color: #cc66cc;">1</span>.	<span style="color: #006600;">package</span> com.<span style="color: #006600;">digitaleavantgarde</span>
<span style="color: #cc66cc;">2</span>.	<span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">3</span>.	        <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">digitaleavantgarde</span>.<span style="color: #006600;">controller</span>.<span style="color: #66cc66;">*</span>;
<span style="color: #cc66cc;">4</span>.
<span style="color: #cc66cc;">5</span>.	        <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">as3</span>.<span style="color: #006600;">interfaces</span>.<span style="color: #006600;">IFacade</span>;
<span style="color: #cc66cc;">6</span>.	        <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">as3</span>.<span style="color: #006600;">patterns</span>.<span style="color: #006600;">facade</span>.<span style="color: #006600;">Facade</span>;
<span style="color: #cc66cc;">7</span>.	        <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">as3</span>.<span style="color: #006600;">patterns</span>.<span style="color: #006600;">observer</span>.<span style="color: #006600;">Notification</span>;
<span style="color: #cc66cc;">8</span>.
<span style="color: #cc66cc;">9</span>.	        <span style="color: #0066CC;">import</span> spark.<span style="color: #006600;">components</span>.<span style="color: #006600;">ViewNavigator</span>;
<span style="color: #cc66cc;">10</span>.
<span style="color: #cc66cc;">11</span>.	        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ApplicationFacade <span style="color: #0066CC;">extends</span> Facade <span style="color: #0066CC;">implements</span> IFacade
<span style="color: #cc66cc;">12</span>.	        <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">13</span>.	                <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const <span style="color: #0066CC;">NAME</span>:<span style="color: #0066CC;">String</span> = ‘ApplicationFacade’;
<span style="color: #cc66cc;">14</span>.	                <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const STARTUP:<span style="color: #0066CC;">String</span> = <span style="color: #0066CC;">NAME</span> + ‘Startup’;
<span style="color: #cc66cc;">15</span>.	                <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const MEDIATE_VIEW:<span style="color: #0066CC;">String</span> = <span style="color: #0066CC;">NAME</span> + ‘MediateView’;
<span style="color: #cc66cc;">16</span>.
<span style="color: #cc66cc;">17</span>.	                <span style="color: #808080; font-style: italic;">/**
18.	                 * Returns an instance of the ApplicationFacade Singleton.
19.	                 *
20.	                 * @return ApplicationFacade
21.	                 */</span>
<span style="color: #cc66cc;">22</span>.	                <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> <span style="color: #000000; font-weight: bold;">function</span> getInstance<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:ApplicationFacade
<span style="color: #cc66cc;">23</span>.	                <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">24</span>.	                        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>instance == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> instance = <span style="color: #000000; font-weight: bold;">new</span> ApplicationFacade<span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#41;</span>;
<span style="color: #cc66cc;">25</span>.	                        <span style="color: #b1b100;">return</span> instance as ApplicationFacade;
<span style="color: #cc66cc;">26</span>.	                <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">27</span>.
<span style="color: #cc66cc;">28</span>.	                <span style="color: #808080; font-style: italic;">/**
29.	                 * Register controllers.
30.	                 */</span>
<span style="color: #cc66cc;">31</span>.	                <span style="color: #006600;">override</span> protected <span style="color: #000000; font-weight: bold;">function</span> initializeController<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #cc66cc;">32</span>.	                <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">33</span>.	                        <span style="color: #0066CC;">super</span>.<span style="color: #006600;">initializeController</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #cc66cc;">34</span>.	                        <span style="color: #006600;">registerCommand</span><span style="color: #66cc66;">&#40;</span>STARTUP, StartupCommand<span style="color: #66cc66;">&#41;</span>;
<span style="color: #cc66cc;">35</span>.	                        <span style="color: #006600;">registerCommand</span><span style="color: #66cc66;">&#40;</span>MEDIATE_VIEW, MediateViewCommand<span style="color: #66cc66;">&#41;</span>;
<span style="color: #cc66cc;">36</span>.	                <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">37</span>.
<span style="color: #cc66cc;">38</span>.	                <span style="color: #808080; font-style: italic;">/**
39.	                 * Fired by the viewComponent on startup.
40.	                 */</span>
<span style="color: #cc66cc;">41</span>.	                <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startup<span style="color: #66cc66;">&#40;</span>viewNavigator:ViewNavigator<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #cc66cc;">42</span>.	                <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">43</span>.	                        <span style="color: #006600;">sendNotification</span><span style="color: #66cc66;">&#40;</span>STARTUP,  viewNavigator<span style="color: #66cc66;">&#41;</span>;
<span style="color: #cc66cc;">44</span>.	                <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">45</span>.	        <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">46</span>.	<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>What’s happening here? I’m implementing some necessary stuff and am following some best practices of building a PureMVC application. In sequence … As said, the Facade is a singleton. Thus, I’m implementing a getter to the instance in the getInstance()-Method. It’s returning a reference from the heap or is instantiating one, if none is already instantiated.<br />
Before that, I’ve defined a static constant NAME. This is good practice, to avoid naming conflicts. You should do this in every – again EVERY – member of your PureMVC Application. This is essentially in proxies and mediators, as they can be retrieved from the Facade by name.</p>
<p>I also defined to other static constants, <strong>STARTUP</strong> and <strong>MEDIATE_VIEW</strong>. They will be associated with the Observer-System of PureMVC, the Notifications. If you want to broadcast a Notification along your application, you’ll do this by name. You can see this in action in the method startup(), which we called in our <strong>ViewNavigtorApplication</strong>! The method startup() does broadcast a notification, using the sendNotification() Method, that is implemented by the Facade, the proxies, the commands &#038; the mediators!</p>
<p>It takes three parameters: The name of the notification (equal to the name of an event in Actionscripts event system) and a generic object (in our case a reference to the view navigator). You might add a third parameter, the type as String. This is normally obsolete, as the receiver of the notification should know how to cast the object.</p>
<p>Notifications can be caught by mediators and commands. The <strong>STARTUP</strong> Notification is associated with the <strong>StartupCommand</strong>: I’ve overridden the initializeController() Method to register some commands. I usually register all my commands here. In bigger projects, you might consider to register them on demand.<br />
I’ve registered a command which class is named StartupCommand, that is instantiated when the Notification STARTUP is broadcasted, and a command which class is named MediateView. This will be triggered later, when the view changes.<br />
To summarize: I have given access to the Facade via a getInstance() method, I have defined a name and some constants that are used as name for Notifications and I’ve registered two Commands. One of them is executed on startup.<br />
The StartupCommands kicks in after the framework is booted. Its job is to start you application:</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('p5824code19'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p582419"><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
</pre></td><td class="code" id="p5824code19"><pre class="actionscript" style="font-family:monospace;"><span style="color: #cc66cc;">1</span>.	<span style="color: #006600;">package</span> com.<span style="color: #006600;">digitaleavantgarde</span>.<span style="color: #006600;">controller</span>
<span style="color: #cc66cc;">2</span>.	<span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">3</span>.
<span style="color: #cc66cc;">4</span>.	        <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">digitaleavantgarde</span>.<span style="color: #006600;">view</span>.<span style="color: #006600;">ViewNavigatorMediator</span>;
<span style="color: #cc66cc;">5</span>.
<span style="color: #cc66cc;">6</span>.	        <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">as3</span>.<span style="color: #006600;">interfaces</span>.<span style="color: #006600;">ICommand</span>;
<span style="color: #cc66cc;">7</span>.	        <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">as3</span>.<span style="color: #006600;">interfaces</span>.<span style="color: #006600;">INotification</span>;
<span style="color: #cc66cc;">8</span>.	        <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">as3</span>.<span style="color: #006600;">patterns</span>.<span style="color: #006600;">command</span>.<span style="color: #006600;">SimpleCommand</span>;
<span style="color: #cc66cc;">9</span>.
<span style="color: #cc66cc;">10</span>.	        <span style="color: #0066CC;">import</span> spark.<span style="color: #006600;">components</span>.<span style="color: #006600;">ViewNavigator</span>;
<span style="color: #cc66cc;">11</span>.
<span style="color: #cc66cc;">12</span>.	        <span style="color: #808080; font-style: italic;">/**
13.	         * Registers the ViewNavigationMediator,
14.	         */</span>
<span style="color: #cc66cc;">15</span>.	        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StartupCommand <span style="color: #0066CC;">extends</span> SimpleCommand <span style="color: #0066CC;">implements</span> ICommand
<span style="color: #cc66cc;">16</span>.	        <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">17</span>.
<span style="color: #cc66cc;">18</span>.	                <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const <span style="color: #0066CC;">NAME</span>:<span style="color: #0066CC;">String</span> = ‘StartupCommand’;
<span style="color: #cc66cc;">19</span>.
<span style="color: #cc66cc;">20</span>.	                <span style="color: #006600;">override</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> execute<span style="color: #66cc66;">&#40;</span>notification:INotification<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #cc66cc;">21</span>.	                <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">22</span>.	                    <span style="color: #006600;">facade</span>.<span style="color: #006600;">registerMediator</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ViewNavigatorMediator<span style="color: #66cc66;">&#40;</span>notification.<span style="color: #006600;">getBody</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as ViewNavigator<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #cc66cc;">23</span>.	                <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">24</span>.	        <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">25</span>.	<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>There are two types of commands available in PureMVC: Simple &#038; Macro. MacroCommands are classes that are executing multiple SimpleCommands, if you want to spread business logic among multiple SimpleCommands. We’re using the SimpleCommand here, as we do not have much logic to implement.<br />
SimpleCommands have to overwrite the execute() method, were all actions take place.<br />
We are registering the<strong> ViewNavigatorMediator</strong>.</p>
<p><strong>The ViewNavigatorMediator</strong></p>
<p>Let’s take a look!</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('p5824code20'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p582420"><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
</pre></td><td class="code" id="p5824code20"><pre class="actionscript" style="font-family:monospace;"><span style="color: #cc66cc;">1</span>.	<span style="color: #006600;">package</span> com.<span style="color: #006600;">digitaleavantgarde</span>.<span style="color: #006600;">view</span>
<span style="color: #cc66cc;">2</span>.	<span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">3</span>.	        <span style="color: #0066CC;">import</span> com.<span style="color: #006600;">digitaleavantgarde</span>.<span style="color: #006600;">ApplicationFacade</span>;
<span style="color: #cc66cc;">4</span>.
<span style="color: #cc66cc;">5</span>.	        <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">as3</span>.<span style="color: #006600;">interfaces</span>.<span style="color: #006600;">IMediator</span>;
<span style="color: #cc66cc;">6</span>.	        <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">as3</span>.<span style="color: #006600;">interfaces</span>.<span style="color: #006600;">INotification</span>;
<span style="color: #cc66cc;">7</span>.	        <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">as3</span>.<span style="color: #006600;">patterns</span>.<span style="color: #006600;">mediator</span>.<span style="color: #006600;">Mediator</span>;
<span style="color: #cc66cc;">8</span>.
<span style="color: #cc66cc;">9</span>.	        <span style="color: #0066CC;">import</span> spark.<span style="color: #006600;">components</span>.<span style="color: #006600;">View</span>;
<span style="color: #cc66cc;">10</span>.	        <span style="color: #0066CC;">import</span> spark.<span style="color: #006600;">components</span>.<span style="color: #006600;">ViewNavigator</span>;
<span style="color: #cc66cc;">11</span>.	        <span style="color: #0066CC;">import</span> spark.<span style="color: #006600;">events</span>.<span style="color: #006600;">ElementExistenceEvent</span>;
<span style="color: #cc66cc;">12</span>.
<span style="color: #cc66cc;">13</span>.
<span style="color: #cc66cc;">14</span>.	        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ViewNavigatorMediator <span style="color: #0066CC;">extends</span> Mediator <span style="color: #0066CC;">implements</span> IMediator
<span style="color: #cc66cc;">15</span>.	        <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">16</span>.	                <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const <span style="color: #0066CC;">NAME</span>:<span style="color: #0066CC;">String</span> = ‘ViewNavigatorMediator’;
<span style="color: #cc66cc;">17</span>.
<span style="color: #cc66cc;">18</span>.	                <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> viewNavigator:ViewNavigator;
<span style="color: #cc66cc;">19</span>.	                <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> activeView:View;
<span style="color: #cc66cc;">20</span>.
<span style="color: #cc66cc;">21</span>.	                <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ViewNavigatorMediator<span style="color: #66cc66;">&#40;</span>viewNavigator:ViewNavigator<span style="color: #66cc66;">&#41;</span>
<span style="color: #cc66cc;">22</span>.	                <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">23</span>.	                        <span style="color: #0066CC;">this</span>.<span style="color: #006600;">viewNavigator</span> = viewNavigator;
<span style="color: #cc66cc;">24</span>.	                        <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span> <span style="color: #0066CC;">NAME</span>, viewNavigator <span style="color: #66cc66;">&#41;</span>;
<span style="color: #cc66cc;">25</span>.	                <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">26</span>.
<span style="color: #cc66cc;">27</span>.	                <span style="color: #006600;">override</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> onRegister<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #cc66cc;">28</span>.	                <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">29</span>.	                        <span style="color: #006600;">viewNavigator</span>.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>ElementExistenceEvent.<span style="color: #006600;">ELEMENT_ADD</span>, switchView<span style="color: #66cc66;">&#41;</span>;
<span style="color: #cc66cc;">30</span>.	                <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">31</span>.
<span style="color: #cc66cc;">32</span>.	                <span style="color: #006600;">override</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> listNotificationInterests<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">33</span>.	                        <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#91;</span>
<span style="color: #cc66cc;">34</span>.
<span style="color: #cc66cc;">35</span>.	                        <span style="color: #66cc66;">&#93;</span>;
<span style="color: #cc66cc;">36</span>.	                <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">37</span>.
<span style="color: #cc66cc;">38</span>.	                <span style="color: #006600;">override</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> handleNotification<span style="color: #66cc66;">&#40;</span>notification:INotification<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">39</span>.
<span style="color: #cc66cc;">40</span>.	                        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">name</span>:<span style="color: #0066CC;">String</span> = notification.<span style="color: #006600;">getName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #cc66cc;">41</span>.
<span style="color: #cc66cc;">42</span>.	                        <span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">name</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #cc66cc;">43</span>.	                        <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">44</span>.	                                <span style="color: #808080; font-style: italic;">// ToDo</span>
<span style="color: #cc66cc;">45</span>.	                        <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">46</span>.	                <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">47</span>.
<span style="color: #cc66cc;">48</span>.	                <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> switchView<span style="color: #66cc66;">&#40;</span>evt:ElementExistenceEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
<span style="color: #cc66cc;">49</span>.	                        <span style="color: #006600;">sendNotification</span><span style="color: #66cc66;">&#40;</span>ApplicationFacade.<span style="color: #006600;">MEDIATE_VIEW</span>, evt.<span style="color: #006600;">element</span> as View<span style="color: #66cc66;">&#41;</span>;
<span style="color: #cc66cc;">50</span>.	                <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">51</span>.	        <span style="color: #66cc66;">&#125;</span>
<span style="color: #cc66cc;">52</span>.	<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you’ve seen in the StartupCommand, we’ve given the ViewNavigatorMediator a reference to the ViewNavigator. We forward it to the parent class, to hold it as generic object. However, to have a strictly typed ViewNavigator, we save it to a private var, too.<br />
We then override the <strong>onRegister()</strong> method and register an EventListener for the <strong>ElementExistenceEvent.ELEMENT_ADD</strong>-Event on the ViewNavigator (previously, the event was named <strong>ViewNavigatorEvent.VIEW_ADD</strong> but was changed).<br />
This Event is dispatched every time when the view changes. This gives us the chance to register the appropriate mediator.<br />
Note that we do not register the mediator here, because it’s business logic. We just take the view-property of the event – a reference to the new active view – and broadcast it along with a <strong>MEDIATE_VIEW</strong> notification. Please remember, that we have registered the <strong>MediateViewCommand</strong> for this notification in our Facade. Thus, everytime the view changes, the Command gets triggered and the new active view gets injected in it. That’s our hook!</p>
<p>Before we go on, let’s briefly discuss the l<strong>istNotificationInterests()</strong> and <strong>handleNotifications()</strong> methods. Similar to registering commands to a specific Notification, you can register Mediators to them: Just return an array with the name of the notification, that the mediator cares about in <strong>listNotficationInterests()</strong>! The <strong>handleNotfications()</strong> method will be triggered, when that Notification is broadcast. You can then choose the appropriate action with the getName() method of the Notification and a simple switch/case construct. This is mainly associated with proxies, which are not focus of this article. Read the appendix for an example!</p>
<p>(Part II of this Post will be Available Next Week.)</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2011%2F06%2Fpuremvc-goes-mobile-1-mvc-driven-ready-to-compile-mobile-application-with-the-flex-framework-by-christian-peters%2F&amp;title=PureMVC%20Goes%20Mobile%201%3A%20MVC-driven%20%26amp%3B%20ready-to-compile%20Mobile%20Application%20with%20the%20Flex%20Framework%21%20by%20Christian%20Peters" id="wpa2a_4"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2011/06/puremvc-goes-mobile-1-mvc-driven-ready-to-compile-mobile-application-with-the-flex-framework-by-christian-peters/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Move the Cheese: A non-Flex Actionscript example of a PureMVC Multicore Application</title>
		<link>http://www.as3dp.com/2009/07/a-non-flex-actionscript-example-of-a-puremvc-multicore-application/</link>
		<comments>http://www.as3dp.com/2009/07/a-non-flex-actionscript-example-of-a-puremvc-multicore-application/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 02:52:35 +0000</pubDate>
		<dc:creator>Chandima Cumaranatunge</dc:creator>
				<category><![CDATA[MVC]]></category>
		<category><![CDATA[PureMVC]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1169</guid>
		<description><![CDATA[Although it may be a small conceptual leap for someone who has experience with the single-core version of PureMVC to visualize how multicore works, it can be a little daunting for a novice. This post describes the design and development of a simple game concept to exemplify the utility of PureMVC multicore. This is a [...]]]></description>
			<content:encoded><![CDATA[<p>Although it may be a small conceptual leap for someone who has experience with the single-core version of PureMVC to visualize how multicore works, it can be a little daunting for a novice. This post describes the design and development of a simple game concept to exemplify the utility of PureMVC multicore. This is a pure AS3 app that doesn&#8217;t use the Flex framework. You can download the source and build the app  in Flex/Flash Builder (as an AS3 project) or Flash CS3/4.</p>
<table style="font-size: 150%; font-weight: bold; width: 100%;" border="0" cellspacing="10" cellpadding="5">
<tbody>
<tr bgcolor="#CCCC33">
<td width="33%">
<p align="center"><a style="text-decoration: none;" href="http://www.as3dp.com/wp-content/uploads/2009/07/savethecheese-bin-release/MoveTheCheese.html" target="_blank">DEMO</a></p>
</td>
<td width="33%">
<p align="center"><a style="text-decoration: none;" href="http://www.as3dp.com/wp-content/uploads/2009/07/savethecheese-bin-release/srcview/" target="_blank">VIEW SOURCE</a></p>
</td>
<td width="33%">
<p align="center"><a style="text-decoration: none;" href="http://www.as3dp.com/wp-content/uploads/2009/07/savethecheese-bin-release/srcview/MoveTheCheese.zip">DOWNLOAD</a></p>
</td>
</tr>
</tbody>
</table>
<p>The basic difference between single-core ( now known as the <em>standard version</em> ) and multicore is that multicore allows multiple MVC triads (multiple <em>cores</em> in PureMVC parlance) to be embedded inside a single application. The general recommendation is to use PureMVC multicore for all projects as it affords a lot more power in terms of developing modular applications and unit testing. However, developers who are new to PureMVC should first develop a simple single-core application first to get familiar with the framework. My previous <a href="http://www.as3dp.com/2007/12/27/minimalist-mvc-example-using-the-puremvc-framework/">PureMVC post</a> describing a minimalist example will get you started with the basics.  From here onwards, I&#8217;m assuming that you have a basic understanding of how the standard version of PureMVC works.<br />
<span id="more-1169"></span></p>
<h4>The coding differences between standard and multicore</h4>
<p>The coding difference between the standard and multicore versions is indeed trivial. In the standard version,  there is only one PureMVC core ( MVC triad ). So, the call to access the <em>facade</em> and initialize the single core is as follows:</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('p1169code27'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p116927"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p1169code27"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">// standard version</span>
<span style="color: #6699cc; font-weight: bold;">var</span> facade<span style="color: #000066; font-weight: bold;">:</span>ApplicationFacade = ApplicationFacade<span style="color: #000066; font-weight: bold;">.</span>getInstance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></td></tr></table></div>

<p>The facade is a <em>singleton</em> in the standard version. The <code>getinstance</code> call will always return the same facade instance as should be the case when the application is restricted to only one MVC triad.  In the multicore version, there can be multiple PureMVC cores. Each facade is a named singleton that represents a separate PureMVC core. To access the facade of a particular core, you must pass its name ( or key ) as an argument in the <code>getInstance</code> call. The <em>key</em> is a string that serves as the unique identifier for a particular PureMVC core.</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('p1169code28'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p116928"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p1169code28"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">// multicore version</span>
<span style="color: #6699cc; font-weight: bold;">var</span> facade<span style="color: #000066; font-weight: bold;">:</span>ApplicationFacade =
              ApplicationFacade<span style="color: #000066; font-weight: bold;">.</span>getInstance<span style="color: #000000;">&#40;</span> applicationKey <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></td></tr></table></div>

<p>After you access a particular core through its facade, the programming idioms for multicore are almost identical to the standard version. There are a couple of small caveats and this <a href="http://lowpitch.com/blog/puremvc-multicore-vs-standard-singlecore/" target="_blank">post</a> by Toby Ashley has a more detailed explanation of the differences.</p>
<h4>The conceptual differences between standard and multicore</h4>
<p>There is no real point in having multiple PureMVC cores in an application if they are deaf and mute. The power of multicore is realized when the cores talk to each other. However, this is where multicore throws another layer of abstraction ( and an ulcer or two ) into the mix. Cores don&#8217;t talk with each other using <em>notifications</em>, the now familiar communication method between the <em>model</em>, <em>view</em> and <em>controller</em> tiers. Communication between cores can be accomplished in two ways:</p>
<ol>
<li>Using <em>interfaces</em>.</li>
<li>Using the <a href="http://trac.puremvc.org/Utility_AS3_MultiCore_Pipes" target="_blank">Pipes Utility</a>.</li>
</ol>
<h4>It&#8217;s all about communication</h4>
<p>Understanding multicore is really about figuring out how to <strong>plumb</strong> your application to enable cores to talk with each other. However, at this point in time, there are very few examples out in the wild. On top of that, there are hardly any non-Flex examples. The rest of this post will show how to develop a non-Flex PureMVC multicore application that uses <em>interfaces</em> for inter-core communication.</p>
<p>When multicore came out, I felt like someone had <a href="http://www.whomovedmycheese.com/" target="_blank">moved my cheese</a> because I was very comfortable with the standard version. Don&#8217;t get me wrong, multicore was the next logical iteration for PureMVC and Cliff Hall has masterfully shepherded its growth. If you feel like the cheese has moved a little bit, you will like the example app. It is called &#8220;Move the Cheese&#8221; (obviously).</p>
<h4>Move the cheese</h4>
<p>Move the cheese is a little game &#8211; move the piece of cheese to prevent the rodents from getting to it. Don&#8217;t expect any compelling gameplay &#8211; I chose this particular scenario to exemplify the utility of PureMVC multicore.</p>
<div id="attachment_1191" class="wp-caption aligncenter" style="width: 305px"><img class="size-full wp-image-1191 " title="movethecheese" src="http://www.as3dp.com/wp-content/uploads/2009/07/movethecheese.jpg" alt="movethecheese" width="295" height="222" /><p class="wp-caption-text">Screenshot showing two rats moving towards the cheese.</p></div>
<p>The Flex framework examples of multicore have a modular architecture. For example, the <a href="http://trac.puremvc.org/Demo_AS3_MultiCore_Flex_Modularity" target="_blank"><strong>Modularity</strong></a> multicore demo  ( see <a href="http://puremvc.org/pages/demos/AS3/Demo_AS3_MultiCore_Flex_Modularity/srcview/" target="_blank">source</a> ) has a main application (the <em><strong>shell</strong></em>) that loads two types of <em><strong>widgets</strong></em>: <em>coolwidget</em> and <em>superwidget</em>. Each widget has a separate PureMVC core associated with it. The shell application has a PureMVC core as well. Coding-wise, the advantage of this modular approach is the <em>loose coupling</em> between the application shell and the widgets as they are completely defined by <em>interfaces.</em> This is particularly useful in the sense that you can write some specs for the behavior of a widget, provide the <code>IWidget</code> interface and let a developer go his/her merry way and develop the widget. It can be plugged into the main application shell when it&#8217;s done. This is the primary advantage of multicore, it enables the development of independently functioning modules.</p>
<p>In a game context, each level can be a separate module that can be loaded when the player gets to that stage. This is advantageous in a resource allocation sense as well because assets are loaded only when needed. I&#8217;m taking a different approach with &#8220;Move the Cheese&#8221; ( for simplicity&#8217;s sake ). Instead of loading levels as modules, I&#8217;m going to consider the actors ( Rats in this case ) as loadable modules. The argument being that the same actor can appear in multiple levels and will be loaded as needed. Will each rat need a separate PureMVC core? How about the cheese?</p>
<h4>Actors vs. props</h4>
<p>Everything is happening on the stage, so let&#8217;s extend the theater metaphor. It is helpful to consider <em>view components</em> that need to function independently as actors on the stage; everything else is a prop. If you think a view component  needs a brain ( logic ) to figure how they need to behave ( state ), it is quite possible that it may benefit from an MVC architecture. The rats have to figure out where the cheese is located, and move towards it at a particular speed. I think that requires some grey matter ( in a figurative sense ). Coupled with the potential re-use, I argue that each rat should be a loadable module with an associated PureMVC core.</p>
<p>The cheese on the other hand is a prop. Yes, it is an interactive object because the user can drag it, but it doesn&#8217;t function independently (no logic and no state). It just sits there until moved &#8211; just like a prop on the stage.</p>
<p>The stage ( a.k.a application shell) should have a core associated with it by default because it places actors and props on stage and brokers the communication between them.</p>
<div id="attachment_1201" class="wp-caption aligncenter" style="width: 360px"><img class="size-full wp-image-1201 " title="actors_with_cores" src="http://www.as3dp.com/wp-content/uploads/2009/07/actors_with_cores.jpg" alt="actors_with_cores" width="350" height="300" /><p class="wp-caption-text">The application shell and the rat view components have PureMVC cores associated with them.</p></div>
<h4>What is a PureMVC core?</h4>
<p>It is helpful to take a step back at this point and visualize the birth cycle of a PureMVC core. All PureMVC applications do something like the following &#8211; usually in the constructor.</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('p1169code29'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p116929"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p1169code29"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> facade<span style="color: #000066; font-weight: bold;">:</span>ApplicationFacade =
         ApplicationFacade<span style="color: #000066; font-weight: bold;">.</span>getInstance<span style="color: #000000;">&#40;</span> applicationKey <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
facade<span style="color: #000066; font-weight: bold;">.</span>startup<span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">this</span> <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></td></tr></table></div>

<p>The first statement initializes the PureMVC core ( e.g. creates the model, view and controller named singletons). In the second statement, the application passes a reference to itself to the &#8216;startup&#8217; method in the facade. The application in most cases is the root view component (e.g. the document class in a Flash CS3 app, a Flex MXML application). In short, the root view component initializes a PureMVC core and injects itself into it. I couldn&#8217;t help but visualize this scenario as a snail without a shell, creating one and crawling inside it.</p>
<div id="attachment_1210" class="wp-caption aligncenter" style="width: 410px"><img class="size-full wp-image-1210 " title="birthing_a_core" src="http://www.as3dp.com/wp-content/uploads/2009/07/birthing_a_core.gif" alt="Birthing a PureMVC core: The snail and shell analogy." width="400" height="276" /><p class="wp-caption-text">Birthing a PureMVC core: The snail and shell analogy.</p></div>
<h4>How do cores talk to each other using interfaces?</h4>
<p>Cores talking with each other is a bit of a misnomer. Each PureMVC core is well encapsulated like a snail that hides inside its shell. Talking depends on how much the root view component (snail body) exposes itself outside the PureMVC core. It is the view components that really do the talking between cores. The language they speak is defined by <em>interfaces</em>. A core that doesn&#8217;t talk is like a snail that never comes out of its shell.</p>
<div id="attachment_1215" class="wp-caption aligncenter" style="width: 360px"><img class="size-full wp-image-1215 " title="talking_snails" src="http://www.as3dp.com/wp-content/uploads/2009/07/talking_snails.gif" alt="talking_snails" width="350" height="200" /><p class="wp-caption-text">The view component (snail body) needs to expose itself outside the PureMVC core (shell) to talk.</p></div>
<p><strong><span style="font-weight: normal;">We can now get back to the application and define some interfaces. </span></strong></p>
<h4><span style="font-weight: normal;">Application specs</span></h4>
<p>Now that we have thought through the application, let&#8217;s nail down some specifications. The application shell will know where the cheese is located, so it has to expose the cheese location to the rats. The rats in turn need to have a reference to the shell so that they can query the shell and get the cheese location. The application shell also takes the responsibility to place the rats on the stage and  update them at specific time intervals. So, the rats needs to expose initialization and update methods as well. Let&#8217;s write a couple of interfaces: <code>IActor</code> for the rats and <code>IApplicationShell</code> for the application.</p>
<p><strong><code>IApplicationShell</code> interface</strong></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('p1169code30'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p116930"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p1169code30"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com<span style="color: #000066; font-weight: bold;">.</span>as3dp<span style="color: #000066; font-weight: bold;">.</span>puremvc<span style="color: #000066; font-weight: bold;">.</span>movethecheese<span style="color: #000066; font-weight: bold;">.</span>interfaces
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.geom</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Point</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> interface IApplicationShell
	<span style="color: #000000;">&#123;</span>
		<span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> cheeseLoc<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Point</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p><strong><code>IActor </code> interface</strong></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('p1169code31'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p116931"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p1169code31"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> com<span style="color: #000066; font-weight: bold;">.</span>as3dp<span style="color: #000066; font-weight: bold;">.</span>puremvc<span style="color: #000066; font-weight: bold;">.</span>movethecheese<span style="color: #000066; font-weight: bold;">.</span>interfaces
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> interface IActor
	<span style="color: #000000;">&#123;</span>
		<span style="color: #339966; font-weight: bold;">function</span> setApplicationShell<span style="color: #000000;">&#40;</span> shell<span style="color: #000066; font-weight: bold;">:</span>IApplicationShell <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">init</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
		<span style="color: #339966; font-weight: bold;">function</span> update<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<div id="attachment_1234" class="wp-caption aligncenter" style="width: 360px"><img class="size-full wp-image-1234 " title="root_view_components" src="http://www.as3dp.com/wp-content/uploads/2009/07/root_view_components.gif" alt="root_view_components" width="350" height="350" /><p class="wp-caption-text">Inter-core communication through interfaces implemented by their root view components</p></div>
<p>The root view component of  the application shell will implement the <code>IApplicationShell</code> interface and the rat view component will implement the <code>IActor</code> interface.    The rest is mostly code and I don&#8217;t want to do a huge code dump on the post. So feel free to browse the source, or better yet, download it into a Flex/Flash builder or Flash CS3/4 and make it your own. I will discuss a few things to look out for before wrapping up the post.</p>
<table style="font-size: 150%; font-weight: bold; width: 100%;" border="0" cellspacing="10" cellpadding="5">
<tbody>
<tr bgcolor="#CCCC33">
<td width="33%">
<p align="center"><a style="text-decoration: none;" href="http://www.as3dp.com/wp-content/uploads/2009/07/savethecheese-bin-release/MoveTheCheese.html" target="_blank">DEMO</a></p>
</td>
<td width="33%">
<p align="center"><a style="text-decoration: none;" href="http://www.as3dp.com/wp-content/uploads/2009/07/savethecheese-bin-release/srcview/" target="_blank">VIEW SOURCE</a></p>
</td>
<td width="33%">
<p align="center"><a style="text-decoration: none;" href="http://www.as3dp.com/wp-content/uploads/2009/07/savethecheese-bin-release/srcview/MoveTheCheese.zip">DOWNLOAD</a></p>
</td>
</tr>
</tbody>
</table>
<h4>Don&#8217;t expose core keys</h4>
<p>If you expose the key, which is the unique identifier that identifies a core, anyone can get its facade and manipulate it. This breaks encapsulation. Use the <code>UIDUtil.getUID</code> static method and use the private getter (learned this from an <a href="http://joelhooks.com/2009/05/18/piping-the-machine-puremvc-multicore-with-pipes-and-the-finite-state-machine-fsm/" target="_blank">app</a> created by Joel Hooks).</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('p1169code32'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p116932"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p1169code32"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #3f5fbf;">/**
 * Get unique identifier for this puremvc core
 * Use this key in the call to getInstance( key )
 */</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> key<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">return</span> UIDUtil<span style="color: #000066; font-weight: bold;">.</span>getUID<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>See <code>MoveTheCheese.a</code>s and <code>Rat.as</code> to see how this is used in context.</p>
<h4>Building multiple applications in a Flex project</h4>
<p>If you download the source and build the app, <code>MoveTheCheese.a</code>s is the default application. <code>Rat.as</code> need to be built as an application as well because it is loaded in the application mediator. Right click on the project in the Flex Navigator view and choose <em>properties &gt; ActionScript Applications</em>. Now add <code>Rat.as</code> to the runnable applications list.</p>
<h4>Advantages of loading Rat.swf</h4>
<p>Take a look at the ApplicationMediator class ( ApplicationMediator.as ) constructor. The first rat is instantiated using the <code>new</code> keyword and typed as <code>Rat</code>. The second rat is a loaded <code>Rat.swf</code> file and is typed as <code>DisplayObject</code>. All we know about <code>Rat.swf</code> is that it is a display object that implements the <code>IActor</code> <em>interface</em>. All implementation details are hidden. The loadable rat exemplifies the power of loadable modules. We can easily load a whole bunch of rodents, ants and all kind of pests that conform to the<code>IActor</code> <em>interface</em>. We can distribute development to multiple developers requiring only the swf file in return.</p>
<p>Have a blast with multicore &#8211; make sure those snails stick their heads out of their shells.</p>
<table style="font-size: 150%; font-weight: bold; width: 100%;" border="0" cellspacing="10" cellpadding="5">
<tbody>
<tr bgcolor="#CCCC33">
<td width="33%">
<p align="center"><a style="text-decoration: none;" href="http://www.as3dp.com/wp-content/uploads/2009/07/savethecheese-bin-release/MoveTheCheese.html" target="_blank">DEMO</a></p>
</td>
<td width="33%">
<p align="center"><a style="text-decoration: none;" href="http://www.as3dp.com/wp-content/uploads/2009/07/savethecheese-bin-release/srcview/" target="_blank">VIEW SOURCE</a></p>
</td>
<td width="33%">
<p align="center"><a style="text-decoration: none;" href="http://www.as3dp.com/wp-content/uploads/2009/07/savethecheese-bin-release/srcview/MoveTheCheese.zip">DOWNLOAD</a></p>
</td>
</tr>
</tbody>
</table>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2009%2F07%2Fa-non-flex-actionscript-example-of-a-puremvc-multicore-application%2F&amp;title=Move%20the%20Cheese%3A%20A%20non-Flex%20Actionscript%20example%20of%20a%20PureMVC%20Multicore%20Application" 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/2009/07/a-non-flex-actionscript-example-of-a-puremvc-multicore-application/feed/</wfw:commentRss>
		<slash:comments>45</slash:comments>
		</item>
	</channel>
</rss>

