<?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; MVC</title>
	<atom:link href="http://www.as3dp.com/category/design-patterns/mvc/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>Separated Presentation: The Classic Model-View-Controller Pattern</title>
		<link>http://www.as3dp.com/2010/03/separated-presentation-the-classic-model-view-controller-pattern/</link>
		<comments>http://www.as3dp.com/2010/03/separated-presentation-the-classic-model-view-controller-pattern/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 15:01:33 +0000</pubDate>
		<dc:creator>Chandima Cumaranatunge</dc:creator>
				<category><![CDATA[MVC]]></category>
		<category><![CDATA[Presentation Patterns]]></category>
		<category><![CDATA[Smalltalk-80]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=2627</guid>
		<description><![CDATA[This is not an introduction to the MVC pattern, but a look into its implementation in Smalltalk-80 to understand the original intent and function of the Model, View, and Controller triad. Starting with the &#8220;classic&#8221; MVC helped me understand the evolution of the presentation patterns that came after it. We will look at how a simple MVC [...]]]></description>
			<content:encoded><![CDATA[<p>This is not an introduction to the MVC pattern, but a look into its implementation in <em>Smalltalk-80 to</em> understand the original intent and function of the <em>Model</em>, <em>View</em>, and <em>Controller </em>triad. Starting with the &#8220;classic&#8221; MVC helped me understand the evolution of the presentation patterns that came after it. We will look at how a simple MVC app works in Smalltalk-80 and examine how it can be implemented in ActionScript. I am not a Smalltalk programmer and my first task was to find some good resources.</p>
<p>Trygve Reenskaug is <a href="http://heim.ifi.uio.no/~trygver/themes/mvc/mvc-index.html" target="_blank">credited with the first MVC formulation </a>in <em>Smalltalk</em> in the late 70&#8242;s. However, the MVC metaphor was burned into Smalltalk and matured after Reenskaug left Xerox Parc. I found two useful resources on MVC in Smalltalk-80. The first is  <em><a href="http://www.ics.uci.edu/~redmiles/ics227-SQ04/papers/KrasnerPope88.pdf" target="_blank">A cookbook for using the model-view controller user interface paradigm in Smalltalk-80</a></em> by Glenn Krasner and Stephen Pope. The second resource is <a href="http://st-www.cs.illinois.edu/users/smarch/st-docs/mvc.html" target="_blank">Applications Programming in Smalltalk-80: How to use Model-View-Controller (MVC)</a> by Steve Burbeck.</p>
<p>Although comprehensive, Krasner and Pope&#8217;s <a href="http://www.ics.uci.edu/~redmiles/ics227-SQ04/papers/KrasnerPope88.pdf" target="_blank">article</a> is very readable from an ActionScript perspective. I could just about follow the <em>Smalltalk</em> code listings, identify the early OOP features and appreciate the ancestors of contemporary UI components.  What stood out was how little things have changed in the last 20 to 30 years in OOP and GUI programming. I think the more appropriate observation is how <strong>advanced</strong> <em>Smalltalk</em> was for its time. Krasner and Pope&#8217;s essay is the primary resource for this post.</p>
<h4>Model-View-Controller Implementation in <em>Smalltalk-80</em></h4>
<p>The MVC pattern facilitates the <em>separation of concerns</em> when developing interactive graphical applications. The logic and state of the application, how users interact with the application, and how application state is presented to the user are handled by separate elements of the MVC triad. S<em>malltalk-80 </em>uses the MVC metaphor to provide built-in support for interactive application development. The idea was to provide a set of built-in user interface widgets such as buttons, menus and lists that can be plugged into a GUI application. To use these built-in UI widgets effectively, the application had to be built in accordance with the MVC metaphor. Let&#8217;s take a look at the conceptual diagram of a Smalltalk-80 MVC app.</p>
<div id="attachment_2645" class="wp-caption alignnone" style="width: 500px"><a href="http://www.as3dp.com/wp-content/uploads/2010/02/mvc_pope_krasner.png"><img class="size-medium wp-image-2645" title="Model-View-Controller (from Krasner &amp; Pope)" src="http://www.as3dp.com/wp-content/uploads/2010/02/mvc_pope_krasner-490x233.png" alt="Model-View-Controller (from Krasner &amp; Pope)" width="490" height="233" /></a><p class="wp-caption-text">Conceptual diagram of MVC in Smalltalk-80 (from Krasner &amp; Pope)</p></div>
<p>All objects in Smalltalk communicate with each other via <em>messages</em>. Think of <em>messages</em> as a way to invoke methods in an object.</p>
<p>At first glance, the MVC conceptual diagram seemed a little strange. The multitude of arrows indicated more acquaintances than I&#8217;m used to seeing in an MVC diagram. <em>Model-View</em> and <em>Controller-Model</em> dependencies ran both ways. The second interesting aspect was that <em>user input </em>goes directly to the <em>Controller</em>. This is a significant change as we generally expect users to interact with UI widgets in the <em>View</em>. In Smalltalk-80, it looks like the raw keyboard and mouse input is fed directly to the <em>Controller</em>. Let&#8217;s take a quick look at how the MVC metaphor is supported in <em>Smalltalk-80</em>.<span id="more-2627"></span></p>
<h4>MVC Classes in <em>Smalltalk-80</em></h4>
<p>There are three abstract classes called <strong><code>Model</code></strong>, <strong><code>View</code></strong> and <strong><code>Controller</code></strong>. All concrete implementations of <em>model</em>, <em>view</em> and <em>controller</em> have to subclass these abstract superclasses. Let&#8217;s take a look at the classes (this is an abbreviated description &#8211; see Krasner and Pope for more detail).</p>
<h5 style="font-size: 0.83em;">The <code>Model</code> Class</h5>
<p>The abstract superclass <code>Model</code> implements generic <em>model</em> behavior. It implements a dependency maintenance mechanism. Views can register with <em>concrete models</em> to become <em>dependents</em> and receive <strong>update</strong> messages.  When a <em>concrete model</em> sends itself a <strong>changed</strong> message, an <strong>update</strong> message is sent to all of its <em>dependent views</em> automatically. This is an implementation of an <em>Observer Pattern</em>. The <em>concrete</em> <em>model</em>, being the <em>subject</em> in the pattern,  doesn&#8217;t explicitly know about its <em>views</em>.</p>
<h5>The <code>View</code> Class</h5>
<p>The abstract superclass <code>View</code> implements generic <em>view</em> behavior. The built-in UI Widgets subclass <code>View</code> (e.g. <code>StandardSystemView</code> and <code>TextEditorView</code>). Views can be nested to develop a complex user interface. For example, the <code>StandardSystemView</code> (i.e. window) contains a sub view (child view) called <code>TextEditorView</code> in the figure below.</p>
<div id="attachment_2668" class="wp-caption alignnone" style="width: 500px"><a href="http://www.as3dp.com/wp-content/uploads/2010/02/smalltalk_views_and_menus.png"><img class="size-medium wp-image-2668" title="smalltalk_views_and_menus" src="http://www.as3dp.com/wp-content/uploads/2010/02/smalltalk_views_and_menus-490x145.png" alt="Nested views (built-in widgets) and corresponding controller create a text editor (from Krasner and Pope)" width="490" height="145" /></a><p class="wp-caption-text">A text editor built using UI widgets showing nested views and corresponding controller (from Krasner and Pope)</p></div>
<p>Each view can have <strong>one</strong> <em>model</em> and <strong>one</strong> <em>controller</em>. The built-in UI Widgets have a pre-specified default <em>controller </em>class to implement its default behavior (<code>TextEditor</code> is the default controller for the <code>TextEditorView</code> UI widget). To instantiate a built-in UI Widget, you need to set its <em>model </em>and<em> </em>open its window. The UI Widget will then initialize its default <em>controller</em> with the same <em>model</em> instance and register with the <em>model</em> as a <em>dependent</em>. When a window is closed, its nested views are released. When a view is released, it removes itself as a dependent form the <em>model</em>. All this plumbing and initialization is part of the generic code in the superclass <code>View</code>. If you want a UI widget to have custom behavior, you can set a custom controller for it.</p>
<h5 style="font-size: 0.83em;">The <code>Controller</code> Class</h5>
<p>The abstract superclass <code>Controller</code> implements generic <em>controller</em> behavior. Each controller has a reference to <strong>one</strong> <em>model,</em> <strong>one</strong> <em>controller, </em>and a global variable called <em>Sensor</em> that provides an interface to the user-input devices for mouse, keyboard, and cursor interactions. The <code>Controller</code> superclass implements generic behavior to determine if a concrete controllers&#8217; corresponding view has mouse focus. If the corresponding view has focus, then its controller maintains control of all user input.</p>
<p>The controller needs to have an explicit reference to its view because it queries the view to determine if the mouse cursor is within the views&#8217; display rectangle. This is non-trivial as the nested view hierarchy has to be traversed. Controllers need to be well behaved, as they grab and release user input. If a view is not interactive, then its controller is a <code>NoController</code> instance that doesn&#8217;t take control of user input.</p>
<p>It is significant that only controllers, and not views, receive user input. There is a clean separation of concerns as views&#8217; only display model data.</p>
<h4>Key Features of the MVC implementation in Smalltalk-80</h4>
<ul>
<li>Each built-in UI Widget is a view.</li>
<li>Views can be nested.</li>
<li>Each view has a corresponding controller.</li>
<li>The controller queries its view to determine if it has focus to receive user input.</li>
<li>The active controller (whose view has focus) receives user input.</li>
<li>Controller can update the view (UI Widget) based on user input.</li>
<li>Controller modifies the model based on user input.</li>
<li>Views update themselves using <em>observer synchronization</em> with the model.</li>
</ul>
<h4>Comparing Smalltalk-80 UI Widgets (<em>views</em>)  to Flash Components</h4>
<p>UI Widgets in Smalltalk-80 are built according to the MVC metaphor. UI Widgets represent the view in the MVC triad. In addition to the pre-built UI widgets, Smalltalk comes with a number of controller and model classes that can be matched with UI widgets (views) to provide default behavior. For example, there is a subclass of <code>Model</code> called <code>TextHolder</code> that can serve as the <em>model</em> for the <code>TextEditorView </code>UI widget and its corresponding default controller <code>TextEditor</code>. Default component behavior can be overridden by plugging in a custom controller to the UI Widget.</p>
<p>ActionScript built-in components do not provide this much abstraction. Unlike Smalltalk, users interact directly with UI Components in Flash. How the component responds to user input is not abstracted out to a public controller class &#8211; the default action is encapsulated and implemented within the component itself. However, all AS3 components implement the <code>IEventDisptacher</code> interface.  We can register with a UI Component to receive the same user events that the component acts on. There are several ways to <a href="http://www.darronschall.com/weblog/2008/01/creating-default-cancelable-event-handlers.cfm" target="_blank">prevent default behavior</a> if the component <a href="http://livedocs.adobe.com/flex/201/langref/flash/events/Event.html#preventDefault()" target="_blank">allows it</a>.</p>
<p>Unlike UI Widgets in Smalltalk, much of the controller functionality is implemented by Flash components. Essentially, the component handles its own logic and state. For example, let&#8217;s examine the class hierarchy of the AS3 Button Component.</p>
<table style="margin-top: 20px; margin-bottom: 20px;border:1px solid #cccccc;" border="1" cellspacing="0" cellpadding="5">
<caption>AS3 Button Component Class Hierarchy</caption>
<tbody>
<tr>
<td style="font-size: 13px; font-weight: bold; padding-right: 15px; vertical-align: top; padding-bottom: 4px;">Package</td>
<td style="font-size: 13px; vertical-align: top; padding-bottom: 4px;"><a style="color: #0000cc; text-decoration: none;" onclick="javascript:loadClassListFrame('class-list.html')" href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/fl/controls/package-detail.html">fl.controls</a></td>
</tr>
<tr>
<td style="font-size: 13px; font-weight: bold; padding-right: 15px; vertical-align: top; padding-bottom: 4px;">Class</td>
<td style="font-size: 13px; text-indent: -20px; padding-left: 20px; vertical-align: top; padding-bottom: 4px;">public class Button</td>
</tr>
<tr>
<td style="font-size: 13px; font-weight: bold; padding-right: 15px; vertical-align: top; padding-bottom: 4px;">Inheritance</td>
<td style="font-size: 13px; text-indent: -20px; padding-left: 20px; line-height: 19px; vertical-align: top; padding-bottom: 4px;">Button <img style="width: 15px; height: 9px; border: 0px initial initial;" title="Inheritance" src="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/images/inherit-arrow.gif" alt="Inheritance" /> <a style="color: #0000cc; text-decoration: none;" href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/fl/controls/LabelButton.html">LabelButton</a> <img style="width: 15px; height: 9px; border: 0px initial initial;" title="Inheritance" src="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/images/inherit-arrow.gif" alt="Inheritance" /> <a style="color: #0000cc; text-decoration: none;" href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/fl/controls/BaseButton.html">BaseButton</a> <img style="width: 15px; height: 9px; border: 0px initial initial;" title="Inheritance" src="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/images/inherit-arrow.gif" alt="Inheritance" /> <a style="color: #0000cc; text-decoration: none;" href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/fl/core/UIComponent.html">UIComponent</a> <img style="width: 15px; height: 9px; border: 0px initial initial;" title="Inheritance" src="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/images/inherit-arrow.gif" alt="Inheritance" /> <a style="color: #0000cc; text-decoration: none;" href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/Sprite.html">Sprite</a> <img style="width: 15px; height: 9px; border: 0px initial initial;" title="Inheritance" src="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/images/inherit-arrow.gif" alt="Inheritance" /> <a style="color: #0000cc; text-decoration: none;" href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/DisplayObjectContainer.html">DisplayObjectContainer</a> <img style="width: 15px; height: 9px; border: 0px initial initial;" title="Inheritance" src="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/images/inherit-arrow.gif" alt="Inheritance" /> <a style="color: #0000cc; text-decoration: none;" href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/InteractiveObject.html">InteractiveObject</a> <img style="width: 15px; height: 9px; border: 0px initial initial;" title="Inheritance" src="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/images/inherit-arrow.gif" alt="Inheritance" /> <a style="color: #0000cc; text-decoration: none;" href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/DisplayObject.html">DisplayObject</a> <img style="width: 15px; height: 9px; border: 0px initial initial;" title="Inheritance" src="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/images/inherit-arrow.gif" alt="Inheritance" /> <a style="color: #0000cc; text-decoration: none;" href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/events/EventDispatcher.html">EventDispatcher</a> <img style="width: 15px; height: 9px; border: 0px initial initial;" title="Inheritance" src="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/images/inherit-arrow.gif" alt="Inheritance" /> <a style="color: #0000cc; text-decoration: none;" href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Object.html">Object</a></td>
</tr>
<tr>
<td style="font-size: 13px; font-weight: bold; padding-right: 15px; vertical-align: top; padding-bottom: 4px;">Implements</td>
<td style="font-size: 13px; vertical-align: top; padding-bottom: 4px;"><a style="color: #0000cc; text-decoration: none;" href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/fl/managers/IFocusManagerComponent.html">IFocusManagerComponent</a></td>
</tr>
</tbody>
</table>
<p>All AS3 components subclass <a style="color: #0000cc; text-decoration: none;" href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/fl/core/UIComponent.html">UIComponent</a> whose parent classes provides functionality to manage focus, receive user events in addition to the traditional view responsibilities of coordinate transformations and nested view management.</p>
<p>This is the interesting dilemma when implementing the classic MVC pattern in AS3. Because UI components implement much of traditional controller functionality, does the controller have any utility in an AS3 MVC application. The best way to answer this question is to develop a simple classic MVC app in AS3.</p>
<h4>A simple Smalltak-80 MVC application</h4>
<p>Krasner and Pope implement a simple example application in their article to demonstrate the MVC paradigm. The application is a simple counter. The model stores the value of the counter. There is a top view (window) and three sub views: a view that displays the value of the counter (a text display area), a view that increments the counter (button), and a view that decrements the counter (button).</p>
<div id="attachment_2700" class="wp-caption alignnone" style="width: 500px"><a href="http://www.as3dp.com/wp-content/uploads/2010/02/counter-smalltalk-80.png"><img class="size-medium wp-image-2700" src="http://www.as3dp.com/wp-content/uploads/2010/02/counter-smalltalk-80-490x246.png" alt="Counter example application showing view hierarchy" width="490" height="246" /></a><p class="wp-caption-text">Counter example application showing view hierarchy</p></div>
<p>I&#8217;m going to develop the counter application in ActionScript using built-in Flash components while remaining true to the classic MVC implementation in Smalltalk-80. The first step is to explore support for the MVC paradigm in ActionScript.</p>
<h4>Support for classic MVC implementation in ActionScript 3.0</h4>
<p>Unlike Smalltalk-80, there is no explicit support for MVC development in ActionScript 3.0. That being said, let&#8217;s compare the key features of MVC implementation in Smalltalk-80 to what is available in AS3.</p>
<p><a href="http://developer.yahoo.com/flash/astra-flash/layout-containers/" target="_blank"></a></p>
<table style="margin-top: 20px; background-color: white; border: 1px solid #cccccc;" border="1" cellspacing="0" cellpadding="5" align="left">
<caption>Comparing Smalltalk-80 MVC features with AS3</caption>
<tbody>
<tr>
<td align="left" valign="top"><span><strong>Smalltalk-80</strong></span></td>
<td align="left" valign="top"><span><strong>ActionScript 3</strong></span></td>
</tr>
<tr>
<td align="left" valign="top">Each built-in UI Widget is a view.</td>
<td align="left" valign="top">We can treat each built-in UI component as a view.</td>
</tr>
<tr>
<td align="left" valign="top">Views can be nested.</td>
<td align="left" valign="top">Built-in Flash components (V3) are controls and don&#8217;t contain layout components that can be nested. Third party component frameworks such as Yahoo Astra do contain layout components. However, we can treat the Flash stage as the top view that can contain nested UI Components.</td>
</tr>
<tr>
<td align="left" valign="top">Each view has a corresponding controller</td>
<td align="left" valign="top"><span>We can register an</span><span> object (controller) to receive user events from built-in components.</span></td>
</tr>
<tr>
<td align="left" valign="top">Controller manages view focus</td>
<td align="left" valign="top">UI Components implement focus management.</td>
</tr>
<tr>
<td align="left" valign="top">The active controller receives user input.</td>
<td align="left" valign="top"><span>Active component receives user input. Component can dispatch user events to registered </span><span>controller</span><span> object.</span></td>
</tr>
<tr>
<td align="left" valign="top">Controller can update the view (UI Widget) based on user input.</td>
<td align="left" valign="top"><span>Component updates itself based on user input (default behavior). However, components can allow default behavior to be prevented through the generated event object. Registered </span><span>controller</span><span> can update component through the </span><span>target</span><span> property in the passed </span><span>event object</span><span>.</span></td>
</tr>
<tr>
<td align="left" valign="top">Controller modifies model based on user input</td>
<td align="left" valign="top"><span>A </span><span>controller</span><span> object can observe component events and update </span><em>model </em>accordingly<em>.</em></td>
</tr>
<tr>
<td align="left" valign="top"><span>Views update themselves using </span><span>observer synchronization</span><span> with the model.</span></td>
<td align="left" valign="top">View can observe the <em>model</em> if it implements the <code>IEventDispatcher</code> interface.</td>
</tr>
</tbody>
</table>
<p>Let&#8217;s implement the Counter application based on these accommodations. I&#8217;m not implying that this is the best way to implement an MVC architecture in AS3. The intent is to explore the good, the bad and ugly of appropriating a classic MVC architecture into an AS3 application.</p>
<h4>Implementing the <em>Counter</em> application in ActionScript 3 (using V3 components)</h4>
<p>The first step is to define the <code>Model</code>, <code>View</code> and <code>Controller</code> abstract superclasses. The first limitation is that AS3 does not support abstract classes. However, as UI Components implement much of the required default behavior already, we can define pure interfaces. The <em>model</em> needs to implement <code>IEventDispatcher</code> and the <em>controller</em> needs to define a handler that can receive user events. All built-in components subclass UIComponent and we can consider it as the superclass for views.</p>
<div id="attachment_2863" class="wp-caption alignnone" style="width: 500px"><a href="http://www.as3dp.com/wp-content/uploads/2010/02/counter-uml-diagram.png"><img class="size-medium wp-image-2863" title="UML Diagram of Counter Application" src="http://www.as3dp.com/wp-content/uploads/2010/02/counter-uml-diagram-490x374.png" alt="The AS3 implementation showing class relationships" width="490" height="374" /></a><p class="wp-caption-text">The AS3 implementation showing class relationships</p></div>
<p><code>CounterView</code> is the top level view that contains the <code>DisplayView</code>, <code>IncrementButtonView</code>, and <code>DecrementButtonView</code>. <code>DisplayView</code> subclasses the <code>Label</code> component and registers with <code>CounterModel</code> to receive <code>CHANGE</code> events when the counter value is updated. <code>DisplayView</code> doesn&#8217;t respond to user input and doesn&#8217;t have a corresponding controller. In contrast, <code>IncrementButtonView</code> and <code>DecrementButtonView</code> subclass the <code>Button</code> component and respond to user gestures. Each button view registers a corresponding controller to receive <code>CLICK</code> events. The controllers modify the model based on these events.</p>
<p>Although model &#8211; view relationships are similar to the classic Smalltalk-80 MVC implementation, the controller relationships are radically different. The controllers essentially observe the views to catch user events. In addition, controllers don&#8217;t need to have an explicit reference to their views either. The event object that is passed to the controller when the event occurs has a reference to the UI component targeted by the event.</p>
<table style="cursor: default; font-size: 20px; width: 100%; font-weight: bold; border: 1px dashed #bbbbbb;" border="0" cellspacing="10" cellpadding="5">
<tbody>
<tr bgcolor="#CCCC33">
<td style="color: #000000; cursor: text; margin: 8px; border: 1px dashed #bbbbbb;" width="33%">
<p align="center"><a style="text-decoration: none;" href="http://www.as3dp.com/wp-content/uploads/2010/03/counter-bin-release/CounterView.swf" target="_blank">DEMO</a></p>
</td>
<td style="color: #000000; cursor: text; margin: 8px; border: 1px dashed #bbbbbb;" width="33%">
<p align="center"><a style="text-decoration: none;" href="http://www.as3dp.com/wp-content/uploads/2010/03/counter-bin-release/srcview/index.html" target="_blank">VIEW SOURCE</a></p>
</td>
<td style="color: #000000; cursor: text; margin: 8px; border: 1px dashed #bbbbbb;" width="33%">
<p align="center"><a style="text-decoration: none;" href="http://www.as3dp.com/wp-content/uploads/2010/03/counter-bin-release/srcview/Counter_MVC.zip">DOWNLOAD</a></p>
</td>
</tr>
</tbody>
</table>
<p>Take a look at the implementation in AS3 and inspect the classes. Flash V3 components are not available by default in Flex/Flash Builder. Therefore, the components have to be brought over from Flash CS3 in a swc file. This <a href="http://joshblog.net/2008/02/10/how-to-use-the-flash-cs3-component-set-in-a-flex-builder-actionscript-project/" target="_blank">post</a> by Josh Tynjala explains the process.</p>
<div id="attachment_2795" class="wp-caption alignnone" style="width: 500px"><a href="http://www.as3dp.com/wp-content/uploads/2010/02/counter-as3.png"><img class="size-full wp-image-2795" title="Counter application implemented in AS3" src="http://www.as3dp.com/wp-content/uploads/2010/02/counter-as3.png" alt="The AS3 implementation of the Counter showing the package hierarchy and classes" width="490" height="380" /></a><p class="wp-caption-text">The AS3 implementation of the Counter showing the package hierarchy and classes</p></div>
<p><strong>The Good, bad and ugly of the ActionScript implementation</strong></p>
<p>The big difference in the AS3 implementation concerns the role of the controller. Flash UI components have taken over the role of the classic controller and relegated it to observe corresponding UI components. Does the controller play its intended role in the MVC triad, which is to serve as a strategy for handling user input? As UI components handle their own user input, the controller has become a <strong>strategy for handling change in UI components</strong>. Is there a big difference?</p>
<p><strong>The good aspects</strong></p>
<ul>
<li>Separated presentation is maintained.</li>
<li>Controller is a strategy for handling user input (or is it?).</li>
<li>Controller is essentially a command object that can be invoked by multiple views.</li>
</ul>
<p><strong>The disconcerting aspects</strong></p>
<ul>
<li>Controller is essentially a command object &#8211; it makes sense if multiple views invoke the same command. Otherwise it may as well be tightly bound to its view.</li>
<li>Event bubbling in complex nested views obsoletes the necessity to have a controller for each UI component.</li>
<li>Flex and data binding may offer simpler ways of implementing separated presentation.</li>
</ul>
<h4>Form your own conclusions</h4>
<p>The intent of this post was to lay some groundwork for the upcoming presentation patterns in this series. Exploring the utility of the classic MVC paradigm with a concrete example made a lot of issues explicit. There is a lot of heated discussion regarding the role of MVC in contemporary development with rich components. Bill posted <a href="http://www.as3dp.com/2008/10/12/is-mvc-obsolete-flex-actionscript-30-and-the-mvc-design-pattern/">Is MVC Obsolete? Flex, ActionScript 3.0 and the MVC Design Pattern</a> on the issues raised by Brian Lessig&#8217;s <a href="http://broadcast.oreilly.com/2008/10/mvc-as-anti-pattern.html" target="_blank">MVC as Anti-Pattern </a>article sometime back. The spirited discussion that followed is very relevant to this post.</p>
<h4>References</h4>
<ul>
<li><a href="http://www.ics.uci.edu/~redmiles/ics227-SQ04/papers/KrasnerPope88.pdf" target="_blank">A cookbook for using the model-view controller user interface paradigm in Smalltalk-80</a> by Glenn Krasner and Stephen Pope.</li>
<li><a href="http://st-www.cs.illinois.edu/users/smarch/st-docs/mvc.html" target="_blank">Applications Programming in Smalltalk-80: How to use Model-View-Controller (MVC)</a> by Steve Burbeck.</li>
<li><a href="http://portal.acm.org/citation.cfm?id=81688" target="_blank">Inside Smalltalk Volume II</a> by Wilf Lalonde and John Pugh.</li>
</ul>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2010%2F03%2Fseparated-presentation-the-classic-model-view-controller-pattern%2F&amp;title=Separated%20Presentation%3A%20The%20Classic%20Model-View-Controller%20Pattern" id="wpa2a_2"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2010/03/separated-presentation-the-classic-model-view-controller-pattern/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Truckin’ Through ActionScript 3.0 MVC: Part V—Purpose and Use</title>
		<link>http://www.as3dp.com/2009/11/truckin-through-actionscript-30-mvc-part-v-purpose-and-use/</link>
		<comments>http://www.as3dp.com/2009/11/truckin-through-actionscript-30-mvc-part-v-purpose-and-use/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 10:53:04 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=2024</guid>
		<description><![CDATA[The MVC remains one of the most valuable structures for understanding and using Design Patterns, and yet I believe it to be misunderstood and misused on just about every level imaginable. In the first chapter of their book, the Gang of Four spend a mere two pages describing MVC in order to help readers understand [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_1659" class="wp-caption alignleft" style="width: 92px"><img src="http://www.as3dp.com/wp-content/uploads/2009/09/truckin.png" alt="Truckin&#039; thru MVC" title="truckin" width="82" height="56" class="size-full wp-image-1659" /><p class="wp-caption-text">Truckin' thru MVC</p></div> The MVC remains one of the most valuable structures for understanding and using Design Patterns, and yet I believe it to be misunderstood and misused on just about every level imaginable. In the first chapter of their book, the Gang of Four spend a mere two pages describing MVC in order to help readers understand the concept of a &#8220;pattern.&#8221; Unfortunately, the language used may have been the culprit leading to widespread uses for which the MVC was never intended. In introducing the MVC, GoF note,<br />
<blockquote>Looking at the design patterns inside MVC should help you see what we mean by the term &#8220;pattern.&#8221;</p></blockquote>
<p> The phrase, <em>the design patterns inside MVC</em> is where the trolly may have jumped the tracks.</p>
<p><span id="more-2024"></span><br />
 I believe that the design pattens that GoF are talking about in reference to the MVC are actually classes that have certain design pattern features found in patterns such as <em>Strategy, Observer</em> and<em> Composite</em>, but they are not referring to the entire pattern. Many design patterns have classes or operations that reflect the entire design pattern. For example, the Template Method pattern has a function (a method) that is a called a <em>template method</em>. So you can have just a single method that describes an entire pattern. Likewise the Stratey pattern contains an interface called <em>strategy</em>, the Observer has an interface known as, <em>observer</em> and the <em>Composite</em> pattern has a concrete class named <em>composite</em>. <em>What makes the MVC valuable is that it contains elements of these other patterns to aid in understanding design patterns in programming</em>. In and of itself, it is good for certain applications, but only to a limited extent. The point is that GoF use MVC to,</p>
<blockquote><p><img src="http://www.as3dp.com/wp-content/uploads/2009/11/rosei.png" alt="rosei" title="rosei" width="242" height="233" class="alignleft size-full wp-image-2032" />LLUSTRATE how classes and objects work interdependently to accomplish a task that can be reused and easily changed.</p></blockquote>
<p>Another source of possible confusion is the figure on page 5. The different graphs and charts are labeled <em>views</em> and the percentages at the bottom are labeled <em>model</em>. It is used to explain the role of the model and view in the MVC. So naturally, in situations where you have multiple views for a single or multiple data sources, you think of MVC. However, that exact same figure is found on page 293 in the chapter on the Observer pattern, but the charts and graphs are labeled <em>observers</em> and the data source, <em>subject</em>. The lines between the different parts are identical in both illustrations—including the labels on the lines. On pages 317-329 of our book, you&#8217;ll find where we used the charts and tables to show how the Observer design pattern can be usefully adopted to generate different views from a single data source. In the context of the GoF book, the illustration on page 5 is to show how different classes interact and is a useful tool for an illustration—and really nothing more. Figure 1 shows how users changed a demonstration  into something it was not intended to be:</p>
<div id="attachment_2035" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/mvc.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; What the MVC is (left) and how it has been used (right).&lt;/em&gt;" title="mvc" width="500" height="208" class="size-full wp-image-2035" /><p class="wp-caption-text"><em><strong>Figure 1:</strong> What the MVC is (left) and how it has been used (right).</em></p></div>
<p>Often, critics beat up the MVC for not being a one-man band when all it was meant to do was to play the cornet.</p>
<p><strong>The True Value of the MVC</strong></p>
<p>Because the MVC was the <em>last</em> pattern I spent any amount of time with, I looked at it with a different perspective. I knew of several different patterns that could be used to accomplish what the MVC does, and so I didn&#8217;t consider the MVC as the be-all and end-all of design patterns. It is most valuable when used as GoF intended—a leaning tool. After working through an example, I found the circular communication between the Model, View and Controller very educational. As a device for learning how event dispatching works, I can think of no better. Knowing  how classes communicate is essential to understanding design patterns, and for that, the MVC does an excellent job. I truly believe that was the intention of GoF and nothing more. They say as much.</p>
<p>After going over Chapter 12, in our book (one that Chandima did), I learned far more about inter-class communication than I believed was left to learn. (By the way, if you found this series useful, you&#8217;re going to be amazed by what Chandima has done with MVC in Chapter 12, both in ways of MVC use and as an explanatory tool for inter-class communication.) My own conclusion is that everyone interested in design patterns needs to spend some time with the MVC to better understand design patterns in general. However, I see it as a fairly limited tool that does a very good job of just a few things and other design patterns are better suited for most tasks. At the same time, I do not think it is the villain some have portrayed it to be, and it has often been criticized for not being an apple because it is an orange. By the same token, I don&#8217;t think it&#8217;s the Swiss Army Knife of design patterns. It is what it always has been in the context of design patterns—an illustration of interacting objects.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2009%2F11%2Ftruckin-through-actionscript-30-mvc-part-v-purpose-and-use%2F&amp;title=Truckin%E2%80%99%20Through%20ActionScript%203.0%20MVC%3A%20Part%20V%E2%80%94Purpose%20and%20Use" 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/2009/11/truckin-through-actionscript-30-mvc-part-v-purpose-and-use/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Truckin’ Through ActionScript 3.0 MVC: Part IV—Making Changes</title>
		<link>http://www.as3dp.com/2009/11/truckin-through-actionscript-30-mvc-part-iv-making-changes/</link>
		<comments>http://www.as3dp.com/2009/11/truckin-through-actionscript-30-mvc-part-iv-making-changes/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 21:26:02 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=2004</guid>
		<description><![CDATA[I mentioned that updating and changing programs is the sine qua non of Design Patterns. Further, I promised that it wouldn&#8217;t take very long to get to Part IV of this series because all I was going to do was to update the original compass program. So that&#8217;s what I did, and here is Part [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_1659" class="wp-caption alignleft" style="width: 92px"><img src="http://www.as3dp.com/wp-content/uploads/2009/09/truckin.png" alt="Truckin&#039; thru MVC" title="truckin" width="82" height="56" class="size-full wp-image-1659" /><p class="wp-caption-text">Truckin' thru MVC</p></div> I mentioned that updating and changing programs is the <em>sine qua non</em> of Design Patterns. Further, I promised that it wouldn&#8217;t take very long to get to Part IV of this series because all I was going to do was to update the original compass program. So that&#8217;s what I did, and here is Part IV of <em>Truckin&#8217; Through ActionScript 3.0 MVC</em>.</p>
<p><strong>Adding an Interface for the View</strong></p>
<p>Before adding another view, I wanted to add an interface so that I could use the same Model and Controller classes. The changes are minimal in the revised View classes, and I changed the Client so that it could more easily request any view the user would like. Since you have all of the details about how the MVC works in Parts I-III, I&#8217;ll get right into the interface.</p>
<p>In looking at the View class in Part III, you can see that the class has only two methods. By putting them into an interface, we can program to the interface instead of the implementation. That means that as long as the same interface is used, you can use the same Model and Controller without any changes at all.  As usual, interfaces are fairly short, and this one is no exception.</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('p2004code5'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20045"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p2004code5"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">interface</span> IView
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">function</span> createView<span style="color: #66cc66;">&#40;</span>vessel:Sprite<span style="color: #66cc66;">&#41;</span>:Sprite;
		<span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Before continuing, click the Play button to test both compasses that have their roots in that interface. (compi?)<br />
<a href="http://www.sandlight.net/dpBlog/mvc/"><img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignnone size-full wp-image-1257" /></a><br />
The Model and Controller classes are identical for both compass examples. Read on to see how easy it is to add views.</p>
<p><span id="more-2004"></span><br />
Because we now have an interface for the View, we need to update the original View class so that it implements the IView interface. The name of the View classes need to be unique; so the name has been changed from View to ViewAnalog. Likewise, the <strong>createAnalog()</strong> method has been given the more general name, <strong>createView()</strong> in line with the more general task of the method.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2004code6'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20046"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
</pre></td><td class="code" id="p2004code6"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #006600;">Slider</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">events</span>.<span style="color: #006600;">SliderEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ViewAnalog <span style="color: #0066CC;">implements</span> IView
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> model:Model;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> controller:Controller;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> base:Sprite;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> compassRose:Sprite;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> pointer:Sprite;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> slider:Slider;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ViewAnalog<span style="color: #66cc66;">&#40;</span>model:Model, controller:Controller, vessel:Sprite<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">model</span>=model;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">controller</span>=controller;
			base=createView<span style="color: #66cc66;">&#40;</span>vessel<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createView<span style="color: #66cc66;">&#40;</span>vessel:Sprite<span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//add compass rose and needle</span>
			compassRose=<span style="color: #000000; font-weight: bold;">new</span> CompassRose<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			compassRose.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">250</span>,compassRose.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">250</span>;
			vessel.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>compassRose<span style="color: #66cc66;">&#41;</span>;
			pointer=<span style="color: #000000; font-weight: bold;">new</span> Pointer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			pointer.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">0</span>,pointer.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">0</span>;
			compassRose.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>pointer<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//**add slider</span>
			<span style="color: #000000; font-weight: bold;">var</span> slideHolder=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			vessel.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>slideHolder<span style="color: #66cc66;">&#41;</span>;
			slider=<span style="color: #000000; font-weight: bold;">new</span> Slider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			slider.<span style="color: #006600;">maximum</span>=<span style="color: #cc66cc;">360</span>;
			slider.<span style="color: #0066CC;">width</span>=slider.<span style="color: #006600;">maximum</span>;
			slideHolder.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>slider<span style="color: #66cc66;">&#41;</span>;
			slider.<span style="color: #006600;">x</span>=<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">250</span><span style="color: #66cc66;">&#41;</span> - <span style="color: #66cc66;">&#40;</span>slider.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
			slider.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">20</span>;
			slider.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>SliderEvent.<span style="color: #006600;">CHANGE</span>, controller.<span style="color: #006600;">newDirection</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">return</span> compassRose;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			pointer.<span style="color: #006600;">rotation</span>=model.<span style="color: #006600;">getDir</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, it&#8217;s almost identical to the original View class. Now for the payoff of loose coupling. Instead of an analog compass, this next one is a retro-digital compass. With a black background and green text, it looks like the old monitors with black screens and green output. Placed next to the analog compass, you can see the same exact data in a wholly different framework. Figure 1 compares the different views with the same data:</p>
<div id="attachment_2027" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2009/11/two.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1: &lt;/strong&gt;Adding new views is easy with MVC&lt;/em&gt;" title="two" width="500" height="255" class="size-full wp-image-2027" /><p class="wp-caption-text"><em><strong>Figure 1: </strong>Adding new views is easy with MVC</em></p></div>
<p>The following listings shows the changes that were made. Note that instead of using MovieClip objects for display, this next view uses a Shape for the background and a TextField and TextFormat for the value state in the Model. However, both are placed in the vessel Sprite and work just fine. The same Slider with the same code is reused except instead of changing the rotation, the value is placed in a TextField object.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p2004code7'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20047"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
</pre></td><td class="code" id="p2004code7"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #006600;">Slider</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">events</span>.<span style="color: #006600;">SliderEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextField</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #0066CC;">text</span>.<span style="color: #0066CC;">TextFormat</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Shape</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ViewRetro <span style="color: #0066CC;">implements</span> IView
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> model:Model;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> controller:Controller;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> base:Sprite;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> slider:Slider;
		<span style="color: #808080; font-style: italic;">//Instantiate text field and format</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> txtFld:<span style="color: #0066CC;">TextField</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextField</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> format:<span style="color: #0066CC;">TextFormat</span>=<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">TextFormat</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> ViewRetro<span style="color: #66cc66;">&#40;</span>model:Model, controller:Controller, vessel:Sprite<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">model</span>=model;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">controller</span>=controller;
			base=createView<span style="color: #66cc66;">&#40;</span>vessel<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> createView<span style="color: #66cc66;">&#40;</span>vessel:Sprite<span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//Create backdrop using a Shape</span>
			<span style="color: #000000; font-weight: bold;">var</span> crt:Shape=<span style="color: #000000; font-weight: bold;">new</span> Shape  ;
			crt.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">beginFill</span><span style="color: #66cc66;">&#40;</span>0x000000<span style="color: #66cc66;">&#41;</span>;
			crt.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">lineStyle</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,0x000000<span style="color: #66cc66;">&#41;</span>;
			crt.<span style="color: #006600;">graphics</span>.<span style="color: #006600;">drawRoundRect</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span>,<span style="color: #cc66cc;">60</span>,<span style="color: #cc66cc;">300</span>,<span style="color: #cc66cc;">50</span>,<span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>;
			crt.<span style="color: #006600;">graphics</span>.<span style="color: #0066CC;">endFill</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			vessel.<span style="color: #006600;">addChildAt</span><span style="color: #66cc66;">&#40;</span>crt,<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			format.<span style="color: #0066CC;">color</span>=0x00ff00;
			format.<span style="color: #0066CC;">font</span>=<span style="color: #ff0000;">&quot;OCR A Std&quot;</span>;
			format.<span style="color: #0066CC;">size</span>=<span style="color: #cc66cc;">32</span>;
&nbsp;
			txtFld.<span style="color: #006600;">defaultTextFormat</span>=format;
			txtFld.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">220</span>;
			txtFld.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">70</span>;
			<span style="color: #808080; font-style: italic;">//Set default value to 0 so that</span>
			<span style="color: #808080; font-style: italic;">//it does not display null--NaN</span>
			txtFld.<span style="color: #0066CC;">text</span>=<span style="color: #cc66cc;">0</span>;
			vessel.<span style="color: #006600;">addChildAt</span><span style="color: #66cc66;">&#40;</span>txtFld,<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//add slider</span>
			<span style="color: #000000; font-weight: bold;">var</span> slideHolder=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			vessel.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>slideHolder<span style="color: #66cc66;">&#41;</span>;
			slider=<span style="color: #000000; font-weight: bold;">new</span> Slider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			slider.<span style="color: #006600;">maximum</span>=<span style="color: #cc66cc;">360</span>;
			slider.<span style="color: #0066CC;">width</span>=slider.<span style="color: #006600;">maximum</span>;
			slideHolder.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>slider<span style="color: #66cc66;">&#41;</span>;
			slider.<span style="color: #006600;">x</span>=<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">250</span><span style="color: #66cc66;">&#41;</span> - <span style="color: #66cc66;">&#40;</span>slider.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
			slider.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">20</span>;
			slider.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>SliderEvent.<span style="color: #006600;">CHANGE</span>, controller.<span style="color: #006600;">newDirection</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">return</span> vessel;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			txtFld.<span style="color: #0066CC;">text</span>=model.<span style="color: #006600;">getDir</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Finally, the Client has to be changed a bit so that selecting one view or another is simple to do. (You can get fancier if you like and set up a UI selection.) The following listing shows the Client.</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('p2004code8'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p20048"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p2004code8"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> model:Model;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> view:IView;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> controller:Controller;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setMVC<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;r&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> setMVC<span style="color: #66cc66;">&#40;</span>viewType:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			model = <span style="color: #000000; font-weight: bold;">new</span> Model<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			controller=<span style="color: #000000; font-weight: bold;">new</span> Controller<span style="color: #66cc66;">&#40;</span>model<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>viewType<span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;a&quot;</span> :
					view=<span style="color: #000000; font-weight: bold;">new</span> ViewAnalog<span style="color: #66cc66;">&#40;</span>model,controller,root<span style="color: #66cc66;">&#41;</span>;
					<span style="color: #b1b100;">break</span>;
				<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">&quot;r&quot;</span> :
					view=<span style="color: #000000; font-weight: bold;">new</span> ViewRetro<span style="color: #66cc66;">&#40;</span>model,controller,root<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
			model.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Model.<span style="color: #006600;">UPDATE</span>, view.<span style="color: #006600;">update</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As far as I can see, the MVC fulfills exactly what GoF had intended for it to do—illustrate how by loosening up classes, updating and adding new elements is easy. While I&#8217;ve been busy with OOPSLA and work, it took me a good deal of time to develop the original MVC. However, when it came to updating the program, I was able to do it quite quickly. If you&#8217;d like to download the entire set of files, click the download button that Barbara Parkman send us.<br />
<a href="http://www.sandlight.net/dpBlog/compass/compassMVC.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>Make Your Own View</strong></p>
<p>To really understand how the MVC works, you will find it helpful to create your own View for this program. Just add any kind of view you want, from simple to elaborate, and have it display data from the Model. There&#8217;s nothing you can break, and it will demonstrate how useful Design Patterns are when adding new elements to a program. In fact, we can make it into a Golden Lunch Bucket Contest if enough of you are interested. Enjoy!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2009%2F11%2Ftruckin-through-actionscript-30-mvc-part-iv-making-changes%2F&amp;title=Truckin%E2%80%99%20Through%20ActionScript%203.0%20MVC%3A%20Part%20IV%E2%80%94Making%20Changes" 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/11/truckin-through-actionscript-30-mvc-part-iv-making-changes/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Truckin&#039; Through ActionScript 3.0 MVC: Part III—An Analog Compass</title>
		<link>http://www.as3dp.com/2009/10/truckin-through-actionscript-30-mvc-part-iii-an-analog-compass/</link>
		<comments>http://www.as3dp.com/2009/10/truckin-through-actionscript-30-mvc-part-iii-an-analog-compass/#comments</comments>
		<pubDate>Sat, 31 Oct 2009 21:40:06 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1954</guid>
		<description><![CDATA[It&#8217;s been a busy month, and finishing up a simple MVC example took me longer than I expected. Chandima&#8217;s chapter (Chapter 12) lighted the way, and I also found valuable ActionScript 3.0 examples by Bill Trikojus and Anthony Kolber . So if you experience a sense of déjà vu, it&#8217;s from the MVC&#8217;s by Chandima, [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_1659" class="wp-caption alignleft" style="width: 92px"><img src="http://www.as3dp.com/wp-content/uploads/2009/09/truckin.png" alt="Truckin&#039; thru MVC" title="truckin" width="82" height="56" class="size-full wp-image-1659" /><p class="wp-caption-text">Truckin' thru MVC</p></div> It&#8217;s been a busy month, and finishing up a simple MVC example took me longer than I expected. Chandima&#8217;s chapter (Chapter 12) lighted the way, and I also found valuable ActionScript 3.0 examples by  <a href="http://www.swinburne.edu.au/design/tutorials/P-flash/T-The-Model-View-Controller-Design-Pattern-in-Actionscript-3/ID-144/">Bill Trikojus and Anthony Kolber </a>. So if you experience a sense of <em>déjà vu</em>, it&#8217;s from the MVC&#8217;s by Chandima, Bill and Anthony.</p>
<p><strong>Taking Control</strong></p>
<p>In Part II of this series on the MVC, we saw how we could separate the Model (data provider) from the View (a representation of the Model&#8217;s data—a graphic compass.) The process was simple enough, especially since all we needed from the Model was a value between 0 and 360. We made it even simpler by using constants with cardinal directions. In this final piece of the MVC puzzle, we&#8217;re adding a Controller class. The Gang of Four state that the<br />
<blockquote>&#8230;Controller defines the way the user interface reacts to the user input</p></blockquote>
<p> The UI for the controller is a Slider component set up with values ranging from 0 to 360. The View class instantiates the Sldier with Slider change events sent to the Controller class. In turn the Controller sends the Slider values to the Model, which updates its current value. The update is sent to the View class which rotates the compass needle.<br />
<span id="more-1954"></span><br />
So as not to lose sight of why we&#8217;re going through all this trouble, keep in mind that by decoupling the model, view and controller we can increase the flexibility and reuse of the program. In this post, we&#8217;ll see how we can create an analog compass, and in Part IV of this series how we can add different kinds of views that display the same information. To get started, take a look at the revised Analog Compass using a Slider for changing the position of the compass needle. Slide the Slider thumb-press along the top left and right. (Also, try pressing and holding down the left and right arrow keys.)<a href="http://www.sandlight.net/dpBlog/compass/"><br />
<img src="http://www.as3dp.com/wp-content/uploads/2009/07/play.png" alt="play" title="play" width="99" height="47" class="alignnone size-full wp-image-1257" /></a></p>
<p>As you can see, the compass needle moves as soon as the Slider stops and the thumb-press is released. You could get a livelier motion using a few more lines of code, but for now, we&#8217;ll keep it simple and focus on the design. Figure 1 shows what you will see.</p>
<div id="attachment_1977" class="wp-caption alignnone" style="width: 508px"><img src="http://www.as3dp.com/wp-content/uploads/2009/10/fig1.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; Analog Compass controlled by Slider and Arrow Keys&lt;/em&gt;" title="fig1" width="498" height="519" class="size-full wp-image-1977" /><p class="wp-caption-text"><em><strong>Figure 1:</strong> Analog Compass controlled by Slider and Arrow Keys</em></p></div>
<p>The compass rose and the needle are both instantiated as Sprites in the program but were created in the Library as MovieClip classes. No Timeline commands were required so instead of treating them as MovieClips, both could be used as Sprites.  You could do the same thing with externally loaded SWF files placed into dynamically created MovieClip classes.</p>
<p><strong>We Need to Talk</strong></p>
<p>For everything to work, the classes need to talk. A Client class uses the Model, View and Controller classes that in turn communicate with one another. Figure 2 shows the communication pattern the MVC classes. The Client instantiates all three classes and sets an event listener to a Model instance to listen for an update notice and to invoke a View method when an update occurs.</p>
<div id="attachment_1974" class="wp-caption alignnone" style="width: 457px"><img src="http://www.as3dp.com/wp-content/uploads/2009/10/mvc.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; MVC Communication&lt;/em&gt;" title="mvc" width="447" height="290" class="size-full wp-image-1974" /><p class="wp-caption-text"><em><strong>Figure 2:</strong> MVC Communication</em></p></div>
<p>As you can see in Figure 2, the Model and View keep each informed of states and the Controller sends the Model changes made by the user who is either dragging the Slider control or pressing the left and right arrow keys.</p>
<p><strong>The Client</strong></p>
<p>Like all Client classes, this class makes requests. However, rather than communicating with a single class or multiple classes through an interface, this Client uses all three—Model, View and Controller. One of the most interesting elements I found in fiddling around with the MVC is the way it uses events and event handling. The Client sets up a Model event listener that fires an update method in the View instance.</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('p1954code13'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p195413"><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="p1954code13"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> model:Model;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> view:View;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> controller:Controller;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			model = <span style="color: #000000; font-weight: bold;">new</span> Model<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			controller=<span style="color: #000000; font-weight: bold;">new</span> Controller<span style="color: #66cc66;">&#40;</span>model<span style="color: #66cc66;">&#41;</span>;
			view=<span style="color: #000000; font-weight: bold;">new</span> View<span style="color: #66cc66;">&#40;</span>model,controller,root<span style="color: #66cc66;">&#41;</span>;
			model.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Model.<span style="color: #006600;">UPDATE</span>, view.<span style="color: #006600;">update</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Notice that both the View and Controller instances hold a reference to a model instance in their constructors. Further, note the use of the <strong>root</strong> property. When ActionScript 3.0 came along, I gave up using any reference to root because I associated it with the old ActionScript. However, had I bothered to read about root in the ActionScirpt 3.0 Reference, I would have found that it is a <em>new</em> <strong>root</strong>. It&#8217;s very handy for placing objects where you want them to go.</p>
<p><strong>Model</strong></p>
<p>As you can see and no doubt expected, the Model contains a getter and setter for the value to be used by the rotation value of the compass needle. However, much more interesting, is how the EventDispatcher is employed. In Chandima&#8217;s work, he uses both the IEventDispatcher and EventDispatcher, and Bill and Anthony use the EventDispatcher. It allows you to make an event in your object that can be fired when called upon. The <strong>notify</strong> method may remind you of the Observer Design Pattern, and indeed it should. When changes in state occur in the Observer, it notifies subscribers of the changes.</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('p1954code14'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p195414"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code" id="p1954code14"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">EventDispatcher</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Model <span style="color: #0066CC;">extends</span> EventDispatcher
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const UPDATE:<span style="color: #0066CC;">String</span>=<span style="color: #ff0000;">&quot;inform view&quot;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> dirNow:<span style="color: #0066CC;">Number</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setDir<span style="color: #66cc66;">&#40;</span>dirNow:<span style="color: #0066CC;">Number</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">dirNow</span>=dirNow;
			notify<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getDir<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Number</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> dirNow;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		protected <span style="color: #000000; font-weight: bold;">function</span> notify<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			dispatchEvent<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Event<span style="color: #66cc66;">&#40;</span>Model.<span style="color: #006600;">UPDATE</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The static construct UPDATE is a string with any value. (I used &#8220;inform view&#8221; to remind us that it&#8217;s the View instance that uses the data from the Model.) When the direction is re-set by a call to setDir() by the Controller, it informs the View of the change. The <strong>static</strong> attribute assigns the <em>constant to a member of the class</em> rather than of <em>an instance of the class</em>. In that way you can use a self-reference to the event through Model.UPDATE within the Model class.</p>
<p><strong>View</strong></p>
<p>The View class probably should have an interface in the form of either an abstract class or interface. However, we&#8217;ll save that for Part IV of the discussion of MVC. The reason for an interface is that we&#8217;re likely to implement more than a single <em>view</em>. (The Gang of Four show three different possible views in their brief MVC discussion.) For now, we&#8217;ll just settle for a good understanding of the role of the View class in the MVC.</p>
<p>The <strong>view</strong> refers to <em>the view of the data from the model</em>. The Model data is set through the Controller sending it values from the UI. The View is notified when the data changes and requests the new data by a call to the Model&#8217;s getter. The View displays that data any way it wants, and in this case its through an analog compass. The values 0 to 360 are passed to the <strong>rotation</strong> property of a Sprite object representing the compass needle.</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('p1954code15'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p195415"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
</pre></td><td class="code" id="p1954code15"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">controls</span>.<span style="color: #006600;">Slider</span>;
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">events</span>.<span style="color: #006600;">SliderEvent</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">Event</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> View
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> model:Model;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> controller:Controller;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> base:Sprite;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> compassRose:Sprite;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> pointer:Sprite;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> slider:Slider;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> View<span style="color: #66cc66;">&#40;</span>model:Model, controller:Controller, vessel:Sprite<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">model</span>=model;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">controller</span>=controller;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">base</span>=createAnalog<span style="color: #66cc66;">&#40;</span>vessel<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">function</span> createAnalog<span style="color: #66cc66;">&#40;</span>vessel:Sprite<span style="color: #66cc66;">&#41;</span>:Sprite
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//add compass rose and needle</span>
			compassRose=<span style="color: #000000; font-weight: bold;">new</span> CompassRose<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			compassRose.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">250</span>,compassRose.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">250</span>;
			vessel.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>compassRose<span style="color: #66cc66;">&#41;</span>;
			pointer=<span style="color: #000000; font-weight: bold;">new</span> Pointer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			pointer.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">0</span>,pointer.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">0</span>;
			compassRose.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>pointer<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">//add slider</span>
			<span style="color: #000000; font-weight: bold;">var</span> slideHolder=<span style="color: #000000; font-weight: bold;">new</span> Sprite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			vessel.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>slideHolder<span style="color: #66cc66;">&#41;</span>;
			slider=<span style="color: #000000; font-weight: bold;">new</span> Slider<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			slider.<span style="color: #006600;">maximum</span>=<span style="color: #cc66cc;">360</span>;
			slider.<span style="color: #0066CC;">width</span>=slider.<span style="color: #006600;">maximum</span>;
			slideHolder.<span style="color: #006600;">addChild</span><span style="color: #66cc66;">&#40;</span>slider<span style="color: #66cc66;">&#41;</span>;
			slider.<span style="color: #006600;">x</span>=<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">250</span><span style="color: #66cc66;">&#41;</span> - <span style="color: #66cc66;">&#40;</span>slider.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;
			slider.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">20</span>;
			slider.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>SliderEvent.<span style="color: #006600;">CHANGE</span>, controller.<span style="color: #006600;">newDirection</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">return</span> compassRose;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			pointer.<span style="color: #006600;">rotation</span>=model.<span style="color: #006600;">getDir</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The Slider object has an event listener that reacts to changes in the Slider&#8217;s value property. When it changes, it calls a Controller method that passes on that value to the Model. That change sets off an update event that fires a View function to move the pointer to the changed position of the Model.</p>
<p><strong>Controller</strong></p>
<p>Finally, we come to the Controller. It&#8217;s job is to define the way the user interface reacts to the user input. In this case, it waits for a SliderEvent and uses the Slider.value (e.target.value) to update the Model&#8217;s current value. That&#8217;s it! It&#8217;s job is very simple.</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('p1954code16'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p195416"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code" id="p1954code16"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> fl.<span style="color: #006600;">events</span>.<span style="color: #006600;">SliderEvent</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Controller
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> model:Model;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Controller<span style="color: #66cc66;">&#40;</span>model:Model<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">model</span>=model;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> newDirection<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:SliderEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> dirNow=<span style="color: #0066CC;">e</span>.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">value</span>;
			model.<span style="color: #006600;">setDir</span><span style="color: #66cc66;">&#40;</span>dirNow<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p><strong>Going Next Door Around the World</strong></p>
<p>Back in the old days, hackers idea of a good time was to tap into the phone system and see if they could call the house next door by routing the call around the world. With this example of the MVC, that may have seemed to be what we did. Starting with the View, the program sends a message to the Controller, which changes the state of the Model which is used by the View to rotate the compass needle. Of course, the purpose is not to convolute a perfectly simple operation but rather to create a design that is wide open for change and update. The purpose of the MVC as used by the Gang of Four is <em>to help you see what we mean by the term &#8220;pattern.&#8221;</em> So rather than looking for the shortest distance between two points, you should do as GoF suggests—look to see how <em>pattern</em> is used. In the next installment, we should be able to see it even more so as we add a new view to the MVC. (The wait for Part IV will not be long because design patterns are easy to update and change, remember?)</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2009%2F10%2Ftruckin-through-actionscript-30-mvc-part-iii-an-analog-compass%2F&amp;title=Truckin%26%23039%3B%20Through%20ActionScript%203.0%20MVC%3A%20Part%20III%E2%80%94An%20Analog%20Compass" id="wpa2a_8"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/10/truckin-through-actionscript-30-mvc-part-iii-an-analog-compass/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Truckin&#039; Through ActionScript 3.0 MVC: Part II—Reality Sui Generis or Collection of Spare Parts?</title>
		<link>http://www.as3dp.com/2009/10/truckin-through-actionscript-30-mvc-part-ii-reality-sui-generis-or-collection-of-spare-parts/</link>
		<comments>http://www.as3dp.com/2009/10/truckin-through-actionscript-30-mvc-part-ii-reality-sui-generis-or-collection-of-spare-parts/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 10:39:27 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1848</guid>
		<description><![CDATA[I was going over Chapter 12 in our book that Chandima wrote on the MVC. It&#8217;s one of those chapters that has everything you need to get started on the MVC; so, I&#8217;ll use it as an anchor for my comments and make references to different parts of it to so as not to be [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_1659" class="wp-caption alignleft" style="width: 92px"><img src="http://www.as3dp.com/wp-content/uploads/2009/09/truckin.png" alt="Truckin&#039; thru MVC" title="truckin" width="82" height="56" class="size-full wp-image-1659" /><p class="wp-caption-text">Truckin' thru MVC</p></div> I was going over Chapter 12 in our book that Chandima wrote on the MVC. It&#8217;s one of those chapters that has everything you need to get started on the MVC; so, I&#8217;ll use it as an anchor for my comments and  make references to different parts of it to so as not to be redundant—in other words I won&#8217;t repeat what Chandima has  so well and clearly stated. Instead, I want to focus on moving toward problem-solving using larger structures.</p>
<p><strong>Straws and Stacks: MVC as a Reality <em>sui generis</em></strong></p>
<p>Years ago in an introductory sociology course, I became acquainted with the concept of a <em>reality sui generis</em>. Some of you may have come across the term elsewhere with different meanings in different contexts. Biology, law and political science have their contextual uses of the term, but the one we&#8217;re using here is the one from sociology. Emile Durkheim employed the term in his work, <em>Suicide</em>, to describe a social fact. For Durkheim, a <em>social fact</em> is a phenomenon of social behavior not reducible to its component parts. A <strong>bureaucracy</strong> is an organizational arrangement that we think of independent of those who actually make up the bureaucracy. It is a <em>reality sui generis</em>—a reality in and of itself. So, we can study phenomenon like families, groups, and organizations as being separate from the people who make them up. Each has characteristics that cannot be seen in the individuals.</p>
<p>To help us understand the concept, we were given the example of a <em>stack</em> and the individual <em>straws</em> that make up the stack. Straws and stacks have wholly different features even though one exists because of the other. By the same token, the MVC, while made up of objects, has characteristics independent of those objects. So, we might envision the MVC as a reality <em>sui generis</em>.<br />
<span id="more-1848"></span><br />
Having noted the different characteristics of straws and stacks, the characteristics of the straws can alter the stack. A stack made of shards of glass is certainly going to be different than one made of wheat or alfalfa straws. So, yes, the components do make a difference. An MVC can be made of different component objects, resulting in different capabilities, is still an MVC.</p>
<p><strong>Comfort Level of Abstraction</strong></p>
<p>For too long I remained in a stultifying comfort level in programming. After tip-toeing into OOP, I was quite content with my algorithms and syntaxes, but other than thinking this was cooler than procedural programming, I never really left the entry level of OOP. Therefore, I never saw the advantages of design patterns or architectures outside of procedural steps. I could always learn a new algorithm or dig deeper into the syntax giving myself the illusion of making progress.</p>
<p>The MVC can be a way to step up and out of the statement level of abstraction. Once we realize that a class can be a reality <em>sui generis</em>—<strong>an object</strong>—the next level of abstraction asks us to think of design patterns as a reality <em>sui generis</em>. Ironically, the MVC begins by breaking up a single entity that combines the model, view and controller and restructures them for better use. You can think of this as breaking up a great big class and creating smaller objects to add flexibility to programming. (Imagine a giant straw being chopped up to make a stack—the straw can never be a stack by itself.)</p>
<p>In that process, the MVC clearly defines the <strong>relationships</strong> between the objects. That&#8217;s why the first chapter of GoF spends so much time on relationships. It is not about algorithms and syntaxes for a reason. The MVC is initially used as a heuristic<strong>*</strong> device to show readers how objects relate to each other. GoF describe relationships in the MVC in terms of the general problem:<br />
<blockquote>&#8230;decoupling objects so that changes to one can affect any number of others without requiring the changed object to know details of the others.</p></blockquote>
<p> That means that you can introduce new elements to a program <em>by attending to the relationships between objects</em>. Because the objects are decoupled, a change in one will not break the other or even require that you do anything to the related objects. <em>However, you do need to think of the relationships between the objects.</em></p>
<p><strong>*</strong><em>By <strong>heuristic</strong>, I&#8217;m referring to a strategy to help the reader understand design patterns—not the way the term is used in computer engineering or computer programming</em>.</p>
<p><strong>The Model and View Relationship</strong></p>
<p>For the time being, forget about the Controller. We&#8217;ll just look at the Model and View. On Page 5 of their book, GoF show a simple example of three variables with different assigned values labeling it as the <strong>model</strong>. Three different images display the values in the form of a table, bar chart and pie chart. These are the <strong>views</strong> of the data in the model. So all that the <em>view</em> is doing is presenting the values generated in the <em>model</em>. It doesn&#8217;t matter how much data there is in the model or its data values, it just models the values that the view uses to present it in any way it wants.</p>
<p>The relationship between the model and view is a two-way one. The model tells the view to update itself, and the view reads the data in the model. That&#8217;s it! The MV relationship is simple. We&#8217;ll get to the role of the controller, but for now, what you need to know is that the MVC first identifies and then separates the three elements that makes up the model, view, and controller.</p>
<p><em>Programming a Compass</em></p>
<p>Now that we have an idea of the MVC, the best place to start is with an UnMVC. That is, we&#8217;ll start with a program that is in a single class and then break it up into an MVC. For the example, we&#8217;ll use a compass. The compass is made up of two movie clip classes. One is a compass rose that serves as a background, and the other is a compass red and gray needle. The needle is positioned in the middle of the compass rose using its center as a pivot point. The compass rose center point is the center of the rose and not the upper-left hand corner. One MovieClip has the Class name, CompassRose and the other Pointer. The following program displays the compass with one of four selected directions:</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('p1848code21'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p184821"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p1848code21"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UnMVC <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//Model</span>
		<span style="color: #0066CC;">private</span> const NORTH:uint=<span style="color: #cc66cc;">360</span>;
		<span style="color: #0066CC;">private</span> const NORTHEAST:uint=<span style="color: #cc66cc;">45</span>;
		<span style="color: #0066CC;">private</span> const EAST:uint=<span style="color: #cc66cc;">90</span>;
		<span style="color: #0066CC;">private</span> const SOUTHEAST:uint=<span style="color: #cc66cc;">135</span>;
		<span style="color: #0066CC;">private</span> const SOUTH:uint=<span style="color: #cc66cc;">180</span>;
		<span style="color: #0066CC;">private</span> const SOUTHWEST:uint=<span style="color: #cc66cc;">225</span>;
		<span style="color: #0066CC;">private</span> const WEST:uint=<span style="color: #cc66cc;">270</span>;
		<span style="color: #0066CC;">private</span> const NORTHWESTT:uint=<span style="color: #cc66cc;">315</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> currentDirection:uint;
		<span style="color: #808080; font-style: italic;">//View</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> compassRose:Sprite=<span style="color: #000000; font-weight: bold;">new</span> CompassRose<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> pointer:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Pointer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> UnMVC<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			compassRose.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">200</span>,compassRose.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">200</span>;
			addChild<span style="color: #66cc66;">&#40;</span>compassRose<span style="color: #66cc66;">&#41;</span>;
			pointer.<span style="color: #006600;">x</span>=<span style="color: #66cc66;">&#40;</span>compassRose.<span style="color: #006600;">x</span><span style="color: #66cc66;">&#41;</span>,pointer.<span style="color: #006600;">y</span>=<span style="color: #66cc66;">&#40;</span>compassRose.<span style="color: #006600;">y</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">//Controller</span>
			pointer.<span style="color: #006600;">rotation</span>=SOUTHWEST;
			addChild<span style="color: #66cc66;">&#40;</span>pointer<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Figure 1 shows what you can expect to see when you test the program:</p>
<div id="attachment_1903" class="wp-caption alignnone" style="width: 498px"><img src="http://www.as3dp.com/wp-content/uploads/2009/10/compass.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt; Compass needle points to value in Model&lt;/em&gt;" title="compass" width="488" height="497" class="size-full wp-image-1903" /><p class="wp-caption-text"><em><strong>Figure 1:</strong> Compass needle points to value in Model</em></p></div>
<p>Now, you may be thinking that the little program is too tiny to be an MVC. Of course, it&#8217;s not an MVC because it&#8217;s all in a single class, and that&#8217;s why it&#8217;s named <strong>UnMVC</strong>. However, you can see all of the elements of the MVC in the comments.</p>
<p><strong>Breaking Up Is Hard to Do: From UnMVC to MVC</strong></p>
<p> The first step is to break up the UnMVC class into  Model and View classes. We&#8217;ll also pick up a Client class to launch the whole thing. Before we do that, though, take a look at  page 429 [Figure 12-1] of our book. Chandima has made a very clear illustration of the relationships between all three elements of the MVC. Further, he shows their flow sequence. For now, though, we&#8217;ll just be focusing on a single relationship—how the Model provides data to the View.  The manner in which this can be done can take many forms, but in the example, the View simply requests the Model for data with the Model deciding which direction to provide. The Client acts something like a Controller, but just treat it as a Client for now. The following three scripts are the first steps in breaking up the UnMVC into a MVC.</p>
<p>First, the Model just has the data to be used.</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('p1848code22'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p184822"><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
</pre></td><td class="code" id="p1848code22"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Model <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> const NORTH:uint=<span style="color: #cc66cc;">360</span>;
		<span style="color: #0066CC;">private</span> const NORTHEAST:uint=<span style="color: #cc66cc;">45</span>;
		<span style="color: #0066CC;">private</span> const EAST:uint=<span style="color: #cc66cc;">90</span>;
		<span style="color: #0066CC;">private</span> const SOUTHEAST:uint=<span style="color: #cc66cc;">135</span>;
		<span style="color: #0066CC;">private</span> const SOUTH:uint=<span style="color: #cc66cc;">180</span>;
		<span style="color: #0066CC;">private</span> const SOUTHWEST:uint=<span style="color: #cc66cc;">225</span>;
		<span style="color: #0066CC;">private</span> const WEST:uint=<span style="color: #cc66cc;">270</span>;
		<span style="color: #0066CC;">private</span> const NORTHWESTT:uint=<span style="color: #cc66cc;">315</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> currentDirection:uint;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> currentDirection:uint;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Model<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			setDir<span style="color: #66cc66;">&#40;</span>NORTHEAST<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setDir<span style="color: #66cc66;">&#40;</span>currentDirection:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">currentDirection</span>=currentDirection;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getDir<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:uint
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> currentDirection;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Whenever the Model is instantiated, it fires a function to set the current value. This value is picked up by the View using a getter function in the Model. So the request comes from the View as illustrated in Figure 2.</p>
<div id="attachment_1894" class="wp-caption alignnone" style="width: 349px"><img src="http://www.as3dp.com/wp-content/uploads/2009/10/model_view.png" alt="&lt;em&gt;&lt;strong&gt;Figure 2: &lt;/strong&gt;View requests data from Model&lt;/em&gt;" title="model_view" width="339" height="242" class="size-full wp-image-1894" /><p class="wp-caption-text"><em><strong>Figure 2: </strong>View requests data from Model</em></p></div>
<p>In the following View listing, the constructor function has been omitted, and all of the work is done by calling methods from within the View or by calls to the Model.</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('p1848code23'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p184823"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code" id="p1848code23"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> View <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> model:Model=<span style="color: #000000; font-weight: bold;">new</span> Model<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> compassRose:Sprite=<span style="color: #000000; font-weight: bold;">new</span> CompassRose<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> pointer:Sprite=<span style="color: #000000; font-weight: bold;">new</span> Pointer<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setView<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			compassRose.<span style="color: #006600;">x</span>=<span style="color: #cc66cc;">200</span>,compassRose.<span style="color: #006600;">y</span>=<span style="color: #cc66cc;">200</span>;
			addChild<span style="color: #66cc66;">&#40;</span>compassRose<span style="color: #66cc66;">&#41;</span>;
			pointer.<span style="color: #006600;">x</span>=compassRose.<span style="color: #006600;">x</span>,pointer.<span style="color: #006600;">y</span>=compassRose.<span style="color: #006600;">y</span>;
			pointer.<span style="color: #006600;">rotation</span>=model.<span style="color: #006600;">getDir</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>pointer<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Before going on, I want to quickly mention <a href="http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/">Miško Hevery&#8217;s post on <em><strong>not</strong></em> having the constructor function doing any real work</a>. Anything more than field assignments in constructor functions is considered a flaw in writing testable code. I believe that this rule is suspended for the Client class, but as I&#8217;ve been cautiously removing any real work from constructors, I&#8217;ve found my code far easier to debug and reuse. We need to have a separate post on the issue of constructors using  ActionScript 3.0 examples, but in the meantime take a look at Miško&#8217;s post on constructors.</p>
<p>Finally, We&#8217;ll use a Client class that launches the Model and View classes and places the compass on the stage pointing East this time.</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('p1848code24'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p184824"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p1848code24"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Client <span style="color: #0066CC;">extends</span> Sprite
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> view:View=<span style="color: #000000; font-weight: bold;">new</span> View<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> model:Model=<span style="color: #000000; font-weight: bold;">new</span> Model<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Client<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			view.<span style="color: #006600;">setView</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			addChild<span style="color: #66cc66;">&#40;</span>view<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>As a start in understanding the MVC, we began with a single class that contained the elements of an MVC. It was broken apart to reveal the Model and View by placing them in separate classes. However, the important information to take away from this is the <em><strong>relationship</strong> between the Model and View classes</em>. Because we have no Controller object, we have no way to change the View independent of the Model. In cases where the Controller can add data directly to the View, the View needs to inform the Model so that it can change its data. For now, though, we just have the View class requesting data from the Model and using it to change the direction of the compass pointer.</p>
<p>In the next and last post of <em>Truckin&#8217; Through ActionScript 3.0 MVC</em>, we&#8217;ll finish up the MVC by adding a Controller module and consider replacing the class objects with design pattern objects.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2009%2F10%2Ftruckin-through-actionscript-30-mvc-part-ii-reality-sui-generis-or-collection-of-spare-parts%2F&amp;title=Truckin%26%23039%3B%20Through%20ActionScript%203.0%20MVC%3A%20Part%20II%E2%80%94Reality%20Sui%20Generis%20or%20Collection%20of%20Spare%20Parts%3F" id="wpa2a_10"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2009/10/truckin-through-actionscript-30-mvc-part-ii-reality-sui-generis-or-collection-of-spare-parts/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Truckin&#039; Through ActionScript 3.0 MVC: Part I—Structures and Materials</title>
		<link>http://www.as3dp.com/2009/09/truckin-through-actionscript-30-mvc-part-i-structures-and-materials/</link>
		<comments>http://www.as3dp.com/2009/09/truckin-through-actionscript-30-mvc-part-i-structures-and-materials/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 08:13:10 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=1653</guid>
		<description><![CDATA[First off, let me say that Chandima is the go-to guy when it comes to the Model-View-Controller (MVC) framework, including PureMVC. So if you&#8217;re interested in MVCs, I&#8217;d suggest you start where I did—Chapter 12 in our book and any of Chandima&#8217;s posts on PureMVC. Further, if you have GoF&#8217;s original Design Patterns you might [...]]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_1659" class="wp-caption alignleft" style="width: 92px"><img src="http://www.as3dp.com/wp-content/uploads/2009/09/truckin.png" alt="Truckin&#039; thru MVC" title="truckin" width="82" height="56" class="size-full wp-image-1659" /><p class="wp-caption-text">Truckin' thru MVC</p></div>First off, let me say that Chandima is the go-to guy when it comes to the Model-View-Controller (MVC) framework, including PureMVC. So if you&#8217;re interested in MVCs, I&#8217;d suggest you start where I did—Chapter 12 in our book and any of Chandima&#8217;s posts on PureMVC. Further, if you have GoF&#8217;s original <a href="http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1253352158&#038;sr=1-1">Design Patterns</a> you might want to look up the several references to the MVC there, especially the initial explanation of its purpose in Chapter 1. Finally, by way of introduction, last year about this time, I broached the query, <a href="http://www.as3dp.com/2008/10/12/is-mvc-obsolete-flex-actionscript-30-and-the-mvc-design-pattern/">Is MVC Obsolete?</a> based on a post by Brian Lesser. That kicked up very little dust either on our blog or Brian&#8217;s, but it helped to take a closer look at the MVC. You&#8217;ll be relieved to hear that I&#8217;m not going to trash MVCs in this series. Rather I want to explore the MVC as a compound pattern (or perhaps framework) to see some different things we might do with it.</p>
<p><strong>MVC in a Nutshell: The Great Decoupler</strong></p>
<p>To get started, GoF show the MVC to be three distinct components in what they call a <em>a triad of classes</em>. As such, they discuss its elements in terms of objects. The <em>application object</em> is defined by the <strong>Model</strong>, the <em>screen presentation</em> by the <strong>View</strong> and the <em>way the way the user interface reacts to the user input</em> by the <strong>Controller</strong>. Prior to MVC, the application, its appearance on the screen and the UI were grouped together in a single class. By decoupling the objects into separate units, the MVC loosens the relationship following a <a href="http://www.as3dp.com/2009/03/12/design-pattern-principles-for-actionscript-30-loose-coupling/">key good practice</a>. (I used to cram them all together in a single class thinking that I was tightening up my code—how dumb was that?) As a first image of the MVC, then, think of it as a tool to  loosen up code.<br />
<span id="more-1653"></span><br />
<strong>Metallurgy and Structural Engineering</strong></p>
<p>Every now and again, I see the perp walk of a contractor who&#8217;s been arrested for using inferior materials on a public structure. In one case, the contractor increased his profits by using cheaper steel beams in a bridge he built. At the trial the prosecution proved that the beams were substandard by the expert testimony of a metallurgist.  The metallurgist compared the beams used in the bridge with those that met the building standards and found the ones used by the contractor to be substandard.</p>
<p>Structural engineers will tell you that the strongest structure in construction is a triangle. That&#8217;s why you see so many triangles in bridge structures. While studying at Cambridge, I visited the wooden bridge at Queen&#8217;s College known as the <em>Mathematical Bridge</em>—lots of triangles in its structure. The bridge was originally built in 1749 and then rebuilt in 1866 and and again in 1905. Given that history and the fact that it&#8217;s made of wood says more for the structure than it does for the building materials.</p>
<p>Figure 1 shows two different bridge structures. The top structure shows the bridge held up by beams arranged in a square that fail when too much pressure is placed on the bridge. The bottom structure shows triangles used to hold up the bridge withstanding pressure:<br />
<div id="attachment_1679" class="wp-caption alignnone" style="width: 510px"><img src="http://www.as3dp.com/wp-content/uploads/2009/09/structure.png" alt="&lt;em&gt;&lt;strong&gt;Figure 1:&lt;/strong&gt;Structure is independent of the materials that make up the structure&lt;/em&gt;" title="structure" width="500" height="229" class="size-full wp-image-1679" /><p class="wp-caption-text"><em><strong>Figure 1:</strong> Structure is independent of the materials that make up the structure</em></p></div></p>
<p>If you look at a problem from the viewpoint of a structural engineer, you tend to see how the different <em>elements relate to one another</em>. If you look at the problem from the view point of a metallurgist, you tend to look at the <em>composition of the materials</em>. Think of the beams as ActionScript 3.0 and algorithms, and think of the design patterns and the MVC as the structures arranging the beams.</p>
<p>Ever since the advent of ActionScript 3.0, a lot of us have been very happy with it as the <em>basic construction material</em> of our programs. Earlier versions of ActionScript were unsatisfactory in many ways, and while ActionScript 3.0 is far from perfect, it is light years better than the original. So unlike the contractor who is being frog walked into court for using inferior materials, we&#8217;ve got very good materials. The problems arise when developers fail to use good structures. Even the best materials will fail without good structure as Figure 2 shows.</p>
<p><strong>Confusing Materials and Structures: The Drunk&#8217;s Search</strong></p>
<p>An old joke tells the story of a drunk who has dropped his keys in a dark doorway. He staggers out to a street light and proceeds to look for them. A police officer comes by and asks the drunk what he&#8217;s doing. The drunk explains that he has dropped his keys in the dark doorway and he&#8217;s looking for them. Confused, the police officer asks, &#8220;Well why are you looking here under the street light instead of the doorway where you dropped them?&#8221; The drunk looks up and says, &#8220;<em>Because this is where the light is!</em>&#8221;</p>
<p>The point of this story is that we often look to ActionScript, it&#8217;s syntax, methods, events, and properties because we understand it better. Instead of looking at the structures of our programs, we focus on getting something to work because for many of us, <em>ActionScript is where the light is!</em>. We&#8217;ve spent years trying to understand and use ActionScript 3.0, and we tend to define problems and solutions in terms of that understanding. The MVC is a great place to break off from our focus on the materials of ActionScript 3.0 and to re-focus on the structures we are putting our ActionScript 3.0 into. By breaking up the application, screen presentation and user interface, we have to re-think what we&#8217;re doing with our code. We have to use it differently.</p>
<p>Now, lest we think that we can work with structure while ignoring materials, let me disabuse you of that notion. The corrupt contractor who uses inferior materials (algorithms) is going to build an inferior bridge, no matter how good his structure is. It&#8217;ll hold up, but sooner or later, the materials fall apart along with his well-structured bridge. Likewise in programming, we must attend to our algorithms, but like the language itself, they must be placed in the best possible structure.</p>
<p>To give you an example, PHP is not a typed language—the data are weakly typed. Yet you will find plenty of PHP design patterns. In fact IBM sponsored a whole series of <a href="http://www.ibm.com/developerworks/library/os-php-designptrns/">PHP design patterns</a>. I&#8217;ve never understood how you can follow one of the first principles of design patterns,<em> type to an interface; not an implementation</em>, using PHP.  Given that PHP and ActionScript 3.0 are different materials is only important insofar as syntax and symbols require different ways to implement design patterns. The classes in the design patterns have the same relationship, however. So if you understand the design patterns in one, it&#8217;s not so difficult to understand them in the other. In the same way that your can use bamboo or steel I-beams in bridge construction with the same structure, the details of using the different materials vary independent of the structures.</p>
<p><strong>Selecting Design Patterns for the MVC</strong></p>
<p>In the next installment of this discussion of the MVC, I&#8217;d like to look at the possible design patterns we might use. We know that to select a design pattern for a task, we must ask, <em>What varies?</em> However, since we are in a position to select multiple design patterns as part of a larger pattern or framework, what are the questions we need to ask and answer to accomplish a goal where we use an MVC?</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2009%2F09%2Ftruckin-through-actionscript-30-mvc-part-i-structures-and-materials%2F&amp;title=Truckin%26%23039%3B%20Through%20ActionScript%203.0%20MVC%3A%20Part%20I%E2%80%94Structures%20and%20Materials" id="wpa2a_12"><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/09/truckin-through-actionscript-30-mvc-part-i-structures-and-materials/feed/</wfw:commentRss>
		<slash:comments>4</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('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
</pre></td><td class="code" id="p1169code31"><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('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
</pre></td><td class="code" id="p1169code32"><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('p1169code33'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p116933"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p1169code33"><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('p1169code34'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p116934"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p1169code34"><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('p1169code35'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p116935"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p1169code35"><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('p1169code36'); return false;">View Code</a> ACTIONSCRIPT3</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p116936"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p1169code36"><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_14"><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>
		<item>
		<title>Is MVC Obsolete? Flex, ActionScript 3.0 and the MVC Design Pattern</title>
		<link>http://www.as3dp.com/2008/10/is-mvc-obsolete-flex-actionscript-30-and-the-mvc-design-pattern/</link>
		<comments>http://www.as3dp.com/2008/10/is-mvc-obsolete-flex-actionscript-30-and-the-mvc-design-pattern/#comments</comments>
		<pubDate>Sun, 12 Oct 2008 13:35:29 +0000</pubDate>
		<dc:creator>William B. Sanders</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/?p=245</guid>
		<description><![CDATA[Another View of the MVC Design Patterns A while back Brian Lesser, another O&#8217;Reilly author and top notch Flash Media Server developer, mentioned that he did not especially care for the Model View Controller (MVC) design pattern. While he cited some references, I never paid much attention to it at the time, especially since Chandima [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Another View of the MVC Design Patterns</strong></p>
<p>A while back Brian Lesser, another O&#8217;Reilly author and top notch Flash Media Server developer, mentioned that he did not especially care for the Model View Controller (MVC) design pattern. While he cited some references, I never paid much attention to it at the time, especially since Chandima kept writing these brilliant pieces on Pure MVC. Further, I had promised myself that I wouldn&#8217;t touch MVC until I had worked my way through all of the design patterns using ActionScript 3.0 and felt comfortable with Flex. So I really am no expert of MVC and have relied on Chandima for that particular compound design.</p>
<p>After weeks of pounding my head on the Prototype pattern, I was thinking of taking a break and sending a torpedo into the Singleton to let off some steam. However, before sticking my neck out (yet again) I thought I&#8217;d ask Brian why he didn&#8217;t particularly care for the MVC, resulting in a very thoughtful article. (See <a href="http://broadcast.oreilly.com/2008/10/mvc-as-anti-pattern.html">http://broadcast.oreilly.com/2008/10/mvc-as-anti-pattern.html</a>) Brian asked for  Chandima&#8217;s and my reaction. Not having spent that much time on the MVC, my reaction was based on the logic of the article which to me made perfect sense. However, this is a case of &#8220;What do I know?&#8221; The annual OOPSLA conference is coming up this next week and I will be bringing up Brian&#8217;s points to big brains that attend OOPSLA&#8211;and I hope some more Flex/ActionScript/Flash developers will be there this year! So maybe after next week I&#8217;ll have some comments from seriously bright programmers in addition to our seriously bright programmer, Chandima.</p>
<p>So take a look at <a href="http://broadcast.oreilly.com/2008/10/mvc-as-anti-pattern.html">http://broadcast.oreilly.com/2008/10/mvc-as-anti-pattern.html</a> and we&#8217;d like to get any copies of your comments, thoughts, and wisdom that have you about Brian&#8217;s article to post here as well.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2008%2F10%2Fis-mvc-obsolete-flex-actionscript-30-and-the-mvc-design-pattern%2F&amp;title=Is%20MVC%20Obsolete%3F%20Flex%2C%20ActionScript%203.0%20and%20the%20MVC%20Design%20Pattern" id="wpa2a_16"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2008/10/is-mvc-obsolete-flex-actionscript-30-and-the-mvc-design-pattern/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Minimalist MVC example using the PureMVC Framework</title>
		<link>http://www.as3dp.com/2007/12/minimalist-mvc-example-using-the-puremvc-framework/</link>
		<comments>http://www.as3dp.com/2007/12/minimalist-mvc-example-using-the-puremvc-framework/#comments</comments>
		<pubDate>Fri, 28 Dec 2007 02:48:27 +0000</pubDate>
		<dc:creator>Chandima Cumaranatunge</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/2007/12/28/minimalist-mvc-example-using-the-puremvc-framework/</guid>
		<description><![CDATA[This is not a full-fledged application, but a minimal example meant to explain the inner workings of the PureMVC framework. Has an introduction to some of the big concepts in PureMVC including Collaboration pairs, Notifications, and the Concrete Facade with full source code.]]></description>
			<content:encoded><![CDATA[<p>Several months ago, I was looking for a framework that would streamline AS3 application development.  <a href="http://labs.adobe.com/wiki/index.php/Cairngorm" target="_blank">Cairngorm</a> and <a href="http://puremvc.org/" target="_blank">PureMVC</a> were the most mature frameworks out there and I remember being particularly impressed with PureMVC mainly due to the solid documentation that came with it. Cairngorm may be robust, but I just couldn&#8217;t get my head wrapped around it by glancing at the docs and examples. In contrast, PureMVC came with a <a href="http://puremvc.org/pages/docs/current/PureMVC_Conceptual_and_Intro.pdf" target="_blank">Conceptual Diagram</a> that explained the framework using the design patterns that I was familiar with. Cairngorm does have similar diagrams that explain its <a href="http://www.cairngormdocs.org/cairngormDiagram/cairngorm2_rpc.swf" target="_blank">microarchitecture</a>, but  it exemplifies my initial thoughts &#8212; I just couldn&#8217;t get a quick, big picture understanding of it easily. Not only has Cliff Hall done a masterful job on the PureMVC framework, but the effort he&#8217;s put into the documentation really underscores the importance of documentation  and diagrams on dissemination and adoption.</p>
<p><strong>Cairngorm or PureMVC?</strong><br />
Recently, Ali Mills and  Luke Bayes made a <a href="http://www.asserttrue.com/articles/2007/10/17/silvafug-application-frameworks-presentation" target="_blank">presentation</a> on Flex Application Frameworks. Their choices essentially boiled down to Cairngorm and PureMVC and concluded that PureMVC came out on top. However, I highly recommend that developers watch the whole presentation as many enterprise developers in the audience made comments that challenged the easy differentiation  of the two frameworks. Having the backing of a stable organization like Adobe, as is the case with Cairngorm, means a lot for enterprise development. Also, <a href="http://geekglue.blogspot.com/2007/10/cairngorm-vs-puremvc-quick-comparison.html" target="_blank">this post</a> on Bill Lane&#8217;s blog and several comments implying that PureMVC is much harder to learn than Cairngorm got me thinking if a really simple application would help scaffold the transition to PureMVC.</p>
<p>I&#8217;ve implemented the minimalist example from the MVC chapter of our book (available as a <a href="http://www.adobe.com/devnet/actionscript/articles/ora_as3_design_patterns.html">free download</a> from Adobe Devnet) using the PureMVC framework. Now this is not a full-fledged application, but a very minimal example meant to explain the inner workings of the PureMVC framework. Before diving in, a brief introduction to some of the important aspects of the framework will help. This was the hard stuff that I had to internalize before starting out. These concepts are more eloquently explained in much greater detail by Cliff Hall in the framework <a href="http://puremvc.org/pages/docs/current/PureMVC_Implementation_Idioms_and_Best_Practices.pdf" target="_blank">documentation</a>.</p>
<p><span id="more-52"></span><br />
<strong>&#8220;You can&#8217;t touch this&#8221; &#8211; models, views and controllers that is</strong><br />
Although PureMVC is built on the traditional Model-View-Controller meta-pattern, you cannot create or directly manipulate these entities. The Model, View and Controller are already implemented in PureMVC as singletons and are essentially hidden. You do not develop Model, View and Controller classes but implement <em>proxy</em>, <em>mediator</em>, and <em>command</em> classes that broker the communication in your application. The PureMVC <a href="http://puremvc.org/pages/docs/current/PureMVC_Conceptual_and_Intro.pdf" target="_blank">Conceptual Diagram</a> shows this visually.</p>
<p><strong>&#8220;Collaboration pairs&#8221; are better than singles</strong><br />
Cliff introduces the concept of &#8220;collaboration pairs&#8221;.  In the traditional MVC pattern, the <em>Model</em> contains the data and business logic; the <em>View</em> presents the user interface components; and the <em>Controller</em> handles the user input and manipulates the model. However, since the model, view and controller classes are not directly accessible in PureMVC, they form collaboration pairs with other design patterns to provide more abstraction and loose-coupling than the traditional MVC pattern.</p>
<p style="text-indent:20pt;">The <strong><em>Model &#8211; Proxy </em></strong><strong>collaboration pair </strong>provides a means to access and manipulate application data. You develop and register a concrete proxy class with the model to provide controlled access to a data object. For example, a proxy can load data files from disk, or provide access to a remote database or web service. The proxy pattern provides an interface to the core application that hides the implementation details of accessing and manipulating data and application state.</p>
<p style="text-indent:20pt;">The <strong>View &#8211; Mediator collaboration pair</strong> provides the means by which the user interface elements are presented and updated.  You develop concrete <em>Mediator</em> classes that register with the <em>View</em> to provide the interface by which the core application communicates with the UI components. A single mediator class can have multiple UI components. For example. a login screen will have one mediator class but can contain username and password fields, and a submit button. The mediator pattern can hide the complex interactions between related UI elements from the core application.</p>
<p style="text-indent:20pt;">The <strong><em>Command &#8211; Controller</em></strong><strong> collaboration pair</strong> represents the means by which UI elements can manipulate the model. In a traditional MVC app, the Controller acts as a concrete strategy in a strategy pattern. In this sense, controllers are specific implementations that determine how the application responds to user input. For example, controllers can do validation checks on user input and decide if application data needs to be updated (through a proxy). In PureMVC, the strategy code is encapsulated inside Command Objects. This essentially hides the proxy class and proxy methods that are called to manipulate data objects from the mediator, reducing the coupling between the mediator and the proxy.</p>
<p>Again. the PureMVC <a href="http://puremvc.org/pages/docs/current/PureMVC_Conceptual_and_Intro.pdf" target="_blank">Conceptual Diagram</a> does a really nice job of showing the &#8220;collaboration pairs.&#8221;</p>
<p><strong>Think in </strong><strong><em>Notifications</em></strong><strong> before </strong><strong><em>Events</em></strong><br />
The mediators, proxies and commands talk to each other using a unique message passing scheme called <em>Notifications</em>. Notifications are essentially triggers that can carry data. Commands are bound to specific notifications. Only mediators listen for notifications. So, a proxy or command can send a notification that will trigger a Mediator to update a UI component. Proxies can send, but do not listen to notifications. A mediator can manipulate application data by sending a notification that will trigger a command to update a data object through a proxy. This shrewd design decision to develop a custom notification scheme has allowd Cliff to <a href="http://trac.puremvc.org/PureMVC" target="_blank">port the PureMVC framework</a> to AS2, C#, Java, PHP, and ColdFusion among others.</p>
<p>The Events in Flash and Flex are not discarded, but used at the boundary of the application. Mediators can register and listen for events from UI components such as buttons and fields. Proxies register and listen for events when dealing with data objects such as when loading an XML data file asynchronously.</p>
<p><strong>The Facade is your friend</strong><br />
You must create a concrete Facade class that makes the whole framework easier to use. The Facade provides a single class that unifies all the methods required to make PureMVC work. In addition, instantiating a facade initializes the PureMVC framework by creating the model, view and controller singletons.</p>
<p>These were the four big concepts that I needed to understand before diving into creating a PureMVC application.</p>
<p><strong>The Minimalist Example<br />
</strong>The example was developed using Flash CS3 and basically listens for user key presses, stores the last key pressed, and displays the key (<a href="http://www.as3dp.com/wp-content/uploads/2007/12/mvc-minimalist-v1.zip">MVC_minimalist_v1.zip</a>). The example app was written in PureMVC Release 1.6. The <a href="http://forums.puremvc.org/index.php?topic=168.0" target="_blank">package structure will change in release 2.0</a> and the import statements will need to be changed accordingly.<br />
Make sure to  <a href="http://puremvc.org/content/view/34/103/" target="_blank">download and install the PureMVC framework</a> and make it accessible in your classpath first. The package hierarchy as shown in the Flash <em>Project Panel</em> is shown here for reference.</p>
<p><img src="http://www.as3dp.com/wp-content/uploads/2007/12/screenshot-01-4.jpg" border="1" alt="Screenshot 01-4" hspace="4" vspace="4" width="196" height="177" /></p>
<p>Here is a conceptual diagram that shows the classes and the notification messages. To start the application, call the <code>startup()</code> method in the the Concrete Facade class called <code>ApplicationFacade</code>. The numbers show the sequence of actions that take place.</p>
<p><img src="http://www.as3dp.com/wp-content/uploads/2007/12/conceptual-diagram2-1.jpg" border="1" alt="Conceptual Diagram2-1" hspace="4" vspace="4" width="532" height="383" /></p>
<ol>
<li>The Concrete Facade class called <span style="font-family:monospace;font-size:9pt;">ApplicationFacade</span> broadcasts a STARTUP Notification.</li>
<li>The STARTUP Notification triggers the <code>StartupCommand</code> command object which registers the <code>KeyDataProxy</code> proxy, and <code>StageMediator</code> mediator.</li>
<li><code>StageMediator</code> mediator registers a KEY_DOWN <em>Event</em> handler to the stage to intercept key presses. It also registers to receive KEYUPDATE <em>Notifications</em>.</li>
<li>User presses a key generating a KEY_DOWN <em>Event </em>that is intercepted by the key down event handler in the <code>StageMediator</code>.</li>
<li><code>StageMediator</code> broadcasts a KEYDOWN <em>Notification</em> with the character code of the key in the notification body.</li>
<li>The KEYDOWN Notification triggers the <code>StoreKeyCommand</code> command object, which accesses the <code>KeyDataProxy</code> proxy and updates the model (data object).</li>
<li>The <code>KeyDataProxy</code> proxy stores the data in a data object (just a property in the proxy in this case).</li>
<li>The <code>KeyDataProxy</code> proxy broadcasts a KEYUPDATE <em>Notification</em> with the character code of the key in the notification body.</li>
<li>The KEYUPDATE <em>Notification</em> handler in <code>StageMediator</code> intercepts the KEYUPDATE <em>Notification</em> and traces the key value.</li>
</ol>
<p>The <code>StageMediator</code> mediator could have directly updated the model in step 5 through the <code>KeyDataProxy</code> proxy, without going through a command object. However,  this introduces a tighter coupling between the Mediator and Proxy that should best be avoided.<strong></strong></p>
<p><strong> </strong></p>
<p><strong>The Concrete Facade</strong><br />
The first step is to create a concrete Facade class called <span style="font-family:monospace;font-size:9pt;">ApplicationFacade</span> to declare notification names and bind them to command objects.</p>
<p>The  <span style="font-family:monospace;font-size:9pt;">ApplicationFacade </span>class defines the STARTUP,  KEYDOWN  and KEYUPDATE notification name constants. STARTUP and KEYDOWN are bound to the <span style="font-family:monospace;font-size:9pt;">StartupCommand</span> and <span style="font-family:monospace;font-size:9pt;">StoreKeyCommand</span> comand objects. In contrast, the KEYUPDATE notification is not bound to a command object. It is broadcast by the proxy to mediators informing them that the model has been updated.</p>
<p>The Facade is the entry point into the application and defines  the <span style="font-family:monospace;font-size:9pt;">startup()</span> method that  broadcasts a STARTUP Notification to the rest of the application. The <span style="font-family:monospace;font-size:9pt;">startup()</span> method has to be called from the document class of your Flash document (see Document Class below). Typically, the argument passed to the startup method should be the <em>stage</em> instance.</p>
<p><strong><em>ApplicationFacade.as<br />
</em></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('p52code37'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5237"><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
</pre></td><td class="code" id="p52code37"><pre class="actionscript" style="font-family:monospace;">package <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">interfaces</span>.<span style="color: #006600;">IFacade</span>;
    <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">patterns</span>.<span style="color: #006600;">facade</span>.<span style="color: #006600;">Facade</span>;
    <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">patterns</span>.<span style="color: #006600;">observer</span>.<span style="color: #006600;">Notification</span>;
    <span style="color: #0066CC;">import</span> controller.<span style="color: #66cc66;">*</span>;
&nbsp;
    <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: #66cc66;">&#123;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// Notification name constants</span>
        <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const STARTUP:<span style="color: #0066CC;">String</span>          = <span style="color: #ff0000;">&quot;startup&quot;</span>;
        <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const KEYDOWN:<span style="color: #0066CC;">String</span>          = <span style="color: #ff0000;">&quot;key down&quot;</span>;
        <span style="color: #0066CC;">public</span> <span style="color: #0066CC;">static</span> const KEYUPDATE:<span style="color: #0066CC;">String</span>        = <span style="color: #ff0000;">&quot;key update&quot;</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">// Singleton ApplicationFacade Factory Method</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: #66cc66;">&#123;</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> <span style="color: #66cc66;">&#123;</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: #66cc66;">&#125;</span>
            <span style="color: #b1b100;">return</span> instance as ApplicationFacade;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// Broadcast the STARTUP Notification</span>
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> startup<span style="color: #66cc66;">&#40;</span>app:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            notifyObservers<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Notification<span style="color: #66cc66;">&#40;</span>STARTUP, app<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// Register Commands with the Controller</span>
        override 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: #66cc66;">&#123;</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>;
            registerCommand<span style="color: #66cc66;">&#40;</span>STARTUP, StartupCommand<span style="color: #66cc66;">&#41;</span>;
            registerCommand<span style="color: #66cc66;">&#40;</span>KEYDOWN, StoreKeyCommand<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The <code>KEYDOWN</code> notification is sent by the <code>StageMediator</code> Mediator (see below) when a user presses a key. The <code>KEYUPDATE</code> notification is sent by the <code>KeyDataProxy</code> Proxy (see below) to all mediators informing them that the model has changed.</p>
<p><strong>The Commands</strong><br />
There are two command objects <code>StartupCommand</code> and <code>StoreKeyCommand</code>.</p>
<p><strong><em>StartupCommand.as</em></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('p52code38'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5238"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code" id="p52code38"><pre class="actionscript" style="font-family:monospace;">package controller <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">Stage</span>;
    <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">interfaces</span>.<span style="color: #006600;">ICommand</span>;
    <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">interfaces</span>.<span style="color: #006600;">INotification</span>;
    <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">patterns</span>.<span style="color: #006600;">command</span>.<span style="color: #006600;">SimpleCommand</span>;
    <span style="color: #0066CC;">import</span> view.<span style="color: #66cc66;">*</span>;
    <span style="color: #0066CC;">import</span> model.<span style="color: #66cc66;">*</span>;
&nbsp;
    <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: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// Register the Proxies and Mediators</span>
        override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> execute<span style="color: #66cc66;">&#40;</span>note:INotification<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>    <span style="color: #66cc66;">&#123;</span>
            <span style="color: #808080; font-style: italic;">// Create and register proxy</span>
            facade.<span style="color: #006600;">registerProxy</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> KeyDataProxy<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #808080; font-style: italic;">// Create and register StageMediator with the stage instance as an argument</span>
            <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">stage</span>:<span style="color: #0066CC;">Stage</span> = note.<span style="color: #006600;">getBody</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as <span style="color: #0066CC;">Stage</span>;
            facade.<span style="color: #006600;">registerMediator</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> StageMediator<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">stage</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>When the  <span style="font-family:monospace;font-size:9pt;">StartupCommand</span> command object is triggered by the <span style="font-family:monospace;font-size:9pt;">STARTUP</span> notification it first registers the proxy called <span style="font-family:monospace;font-size:9pt;">KeyDataProxy</span>. It then registers the <span style="font-family:monospace;font-size:9pt;">StageMediator</span> Mediator and passes the stage to it. This is an essential practice in Flash applications as the Mediators require access to the stage to add UI components to it. Note that the <span style="font-family:monospace;font-size:9pt;">StartupCommand</span> command object expects the <span style="font-family:monospace;font-size:9pt;">STARTUP</span> notification to contain a reference to the stage in the notification body.</p>
<p><strong><em>StoreKeyCommand.as</em></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('p52code39'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5239"><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="p52code39"><pre class="actionscript" style="font-family:monospace;">package controller <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">Stage</span>;
    <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">interfaces</span>.<span style="color: #006600;">ICommand</span>;
    <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">interfaces</span>.<span style="color: #006600;">INotification</span>;
    <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">patterns</span>.<span style="color: #006600;">command</span>.<span style="color: #006600;">SimpleCommand</span>;
    <span style="color: #0066CC;">import</span> model.<span style="color: #66cc66;">*</span>;
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StoreKeyCommand <span style="color: #0066CC;">extends</span> SimpleCommand <span style="color: #0066CC;">implements</span> ICommand <span style="color: #66cc66;">&#123;</span>
&nbsp;
        override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> execute<span style="color: #66cc66;">&#40;</span>note:INotification<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>    <span style="color: #66cc66;">&#123;</span>
            <span style="color: #808080; font-style: italic;">// Get the KeyDataProxy proxy</span>
            <span style="color: #000000; font-weight: bold;">var</span> keyDataProxy:KeyDataProxy = facade.<span style="color: #006600;">retrieveProxy</span><span style="color: #66cc66;">&#40;</span>KeyDataProxy.<span style="color: #0066CC;">NAME</span><span style="color: #66cc66;">&#41;</span> as KeyDataProxy;
            <span style="color: #808080; font-style: italic;">// Set the char code of the key pressed in KeyDataProxy</span>
            <span style="color: #000000; font-weight: bold;">var</span> charCode:uint = note.<span style="color: #006600;">getBody</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as uint;
            keyDataProxy.<span style="color: #0066CC;">key</span> = charCode;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The <span style="font-family:monospace;font-size:9pt;">StoreKeyCommand</span> comand object is triggered by the <span style="font-family:monospace;font-size:9pt;">KEYDOWN</span> notification that is sent by the <span style="font-family:monospace;font-size:9pt;">StageMediator</span> Mediator (see below). The <span style="font-family:monospace;font-size:9pt;">KEYDOWN</span> notification contains the character code of the pressed key in the message body. The command code retrieves the  <span style="font-family:monospace;font-size:9pt;">KeyDataProxy</span> using the <code>retrieveProxy()</code> method and stores the character code using a setter method.</p>
<p><strong>The Mediator</strong><br />
The example application just has one Mediator class called <span style="font-family:monospace;font-size:9pt;">StageMediator</span> that mediates interactions with the stage. It registers a Key Down even handler called <code>onKeyDownEvent() </code> with the stage. It also registers to receive <span style="font-family:monospace;font-size:9pt;">KEYUPDATE</span> notifications by overriding the <code>listNotificationInterests()</code> method. It also overrides the <code>handleNotification()</code> method to intercept and act on <span style="font-family:monospace;font-size:9pt;">KEYUPDATE</span> notifications.</p>
<p><strong><em>StageMediator.as<br />
</em></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('p52code40'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5240"><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="p52code40"><pre class="actionscript" style="font-family:monospace;">package view <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">events</span>.<span style="color: #006600;">KeyboardEvent</span>;
    <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">Stage</span>;
    <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">interfaces</span>.<span style="color: #006600;">IMediator</span>;
    <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">interfaces</span>.<span style="color: #006600;">INotification</span>;
    <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">patterns</span>.<span style="color: #006600;">mediator</span>.<span style="color: #006600;">Mediator</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// Mediator for interacting with the Stage.</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> StageMediator <span style="color: #0066CC;">extends</span> Mediator <span style="color: #0066CC;">implements</span> IMediator <span style="color: #66cc66;">&#123;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// Cannonical name of the Mediator</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;">'StageMediator'</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">// Constructor</span>
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> StageMediator<span style="color: #66cc66;">&#40;</span>viewComponent:<span style="color: #0066CC;">Stage</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #808080; font-style: italic;">// pass the viewComponent to the superclass</span>
            <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span>viewComponent<span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #808080; font-style: italic;">// Listen for events from the view component</span>
            viewComponent.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>KeyboardEvent.<span style="color: #006600;">KEY_DOWN</span>, onKeyDownEvent<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// Key down event handler</span>
        <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> onKeyDownEvent<span style="color: #66cc66;">&#40;</span>event:KeyboardEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            sendNotification<span style="color: #66cc66;">&#40;</span>ApplicationFacade.<span style="color: #006600;">KEYDOWN</span>, event.<span style="color: #006600;">charCode</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// Return list of Nofitication names that Mediator is interested in</span>
        override <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: #b1b100;">return</span> <span style="color: #66cc66;">&#91;</span>ApplicationFacade.<span style="color: #006600;">KEYUPDATE</span><span style="color: #66cc66;">&#93;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// Handle all notifications this Mediator is interested in</span>
        override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> handleNotification<span style="color: #66cc66;">&#40;</span>note:INotification<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>  <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>note.<span style="color: #006600;">getName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #b1b100;">case</span> ApplicationFacade.<span style="color: #006600;">KEYUPDATE</span>:
                    <span style="color: #000000; font-weight: bold;">var</span> charCode:uint = note.<span style="color: #006600;">getBody</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> as uint;
                    <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>charCode<span style="color: #66cc66;">&#41;</span>;
                    <span style="color: #b1b100;">break</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// Get the Mediator name</span>
        override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getMediatorName<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #0066CC;">NAME</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The <span style="font-family:monospace;font-size:9pt;">StageMediator</span> mediator broadcasts a KEYDOWN Notification when it receives a KEY_DOWN even from the Flash event model. The character code of the key pressed is sent in the body of the Notification. On receipt of a KEYUPDATE Notification, the <span style="font-family:monospace;font-size:9pt;">StageMediator</span> simply traces it to the output panel (to keep things simple). It could have updated a UI element (e.g. Text Field) to show a change.</p>
<p><strong>The Proxy</strong><br />
The minimalist example has one proxy called  <span style="font-family:monospace;font-size:9pt;">KeyDataProxy</span> keeps track of the last key pressed by the user. The character code of the key is stored in the <code>lastKeyPressed</code> property in the proxy itself. The proxy could just have easily stored this value in an external data file or remote database if needed.</p>
<p><strong><em>KeyDataProxy.as<br />
</em></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('p52code41'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5241"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code" id="p52code41"><pre class="actionscript" style="font-family:monospace;">package model <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">interfaces</span>.<span style="color: #006600;">IProxy</span>;
    <span style="color: #0066CC;">import</span> org.<span style="color: #006600;">puremvc</span>.<span style="color: #006600;">patterns</span>.<span style="color: #006600;">proxy</span>.<span style="color: #006600;">Proxy</span>;
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> KeyDataProxy <span style="color: #0066CC;">extends</span> Proxy <span style="color: #0066CC;">implements</span> IProxy <span style="color: #66cc66;">&#123;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// Cannonical name of the Proxy</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;">'KeyDataProxy'</span>;
&nbsp;
        <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> lastKeyPressed:uint = <span style="color: #0066CC;">undefined</span>;
&nbsp;
        <span style="color: #808080; font-style: italic;">// Constructor</span>
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> KeyDataProxy<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</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: #808080; font-style: italic;">// pass the cannonical name to the superclass</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> <span style="color: #0066CC;">key</span><span style="color: #66cc66;">&#40;</span>charCode:uint<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>lastKeyPressed <span style="color: #66cc66;">!</span>= charCode<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                lastKeyPressed = charCode;
                <span style="color: #808080; font-style: italic;">// Create and send a KEYUPDATE Notification and pass the charCode as an argument</span>
                sendNotification<span style="color: #66cc66;">&#40;</span>ApplicationFacade.<span style="color: #006600;">KEYUPDATE</span>, charCode<span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Note that the model stores the new character code only if it is different from the one already stored. It also doesn&#8217;t broadcast a KEYUPDATE Notification unless this is the case. This is why we needed two Notification messages, one to broadcast that a key has been pressed (KEYDOWN) and another to inform all interested mediators that the model has been updated with a new character code (KEYUPDATE).</p>
<p><strong>The Document Class</strong><br />
The document class of the Flash document initializes the concrete facade and broadcasts the STARTUP notification.</p>
<p><strong><em>Main.as</em></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('p52code42'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p5242"><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="p52code42"><pre class="actionscript" style="font-family:monospace;">package <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #0066CC;">MovieClip</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
    *   Main Class
    *   @ purpose:      Document class for movie
    */</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">MovieClip</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #808080; font-style: italic;">// Instantiate the ApplicationFacade</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>;
&nbsp;
            <span style="color: #808080; font-style: italic;">// Call the application startup method and pass the stage as the argument</span>
            facade.<span style="color: #006600;">startup</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">stage</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>So, there you have it, a minimalist example in PureMVC implemented in the Flash CS3 environment. Make sure you <em>Disable keyboard shortcuts</em> from the <em>Control</em> menu when testing the application. The source can be downloaded below.<br />
<a href="http://www.as3dp.com/wp-content/uploads/2007/12/mvc-minimalist-v1.zip">MVC_minimalist_v1.zip</a>.</p>
<p>I&#8217;ll follow this up with another post extending the minimalist application a little bit (e.g. adding more UI elements and mediators)  to demonstrate the value of the design decisions that went into PureMVC. This post is quite long and I&#8217;m sure there are some errors &#8211; comments and corrections are welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2007/12/minimalist-mvc-example-using-the-puremvc-framework/feed/</wfw:commentRss>
		<slash:comments>84</slash:comments>
		</item>
		<item>
		<title>MVC and Factory Method Pattern Chapters on Adobe DEVNET</title>
		<link>http://www.as3dp.com/2007/09/mvc-and-factory-method-pattern-chapters-on-adobe-devnet/</link>
		<comments>http://www.as3dp.com/2007/09/mvc-and-factory-method-pattern-chapters-on-adobe-devnet/#comments</comments>
		<pubDate>Sat, 22 Sep 2007 03:09:48 +0000</pubDate>
		<dc:creator>Chandima Cumaranatunge</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Factory Method]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/2007/09/21/mvc-and-factory-method-pattern-chapters-on-adobe-devnet/</guid>
		<description><![CDATA[Two chapters from the ActionScript 3.0 Design Patterns book are available as a free download from devnet. Each chapter contains multiple examples. The factory method pattern chapter includes a print shop application, a sprite factory, and a shooter game that uses the factory method pattern to create different types of projectiles. The MVC chapter includes [...]]]></description>
			<content:encoded><![CDATA[<p>Two chapters from the <em>ActionScript 3.0 Design Patterns</em> book are available as a free download from <a href="http://www.adobe.com/devnet/actionscript/articles/ora_as3_design_patterns.html" target="_blank">devnet</a>. Each chapter contains multiple examples. The factory method pattern chapter includes a print shop application, a sprite factory, and a shooter game that uses the factory method pattern to create different types of projectiles. The MVC chapter includes a weather map application and a car chase app.</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%2F2007%2F09%2Fmvc-and-factory-method-pattern-chapters-on-adobe-devnet%2F&amp;title=MVC%20and%20Factory%20Method%20Pattern%20Chapters%20on%20Adobe%20DEVNET" id="wpa2a_18"><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/2007/09/mvc-and-factory-method-pattern-chapters-on-adobe-devnet/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

