<?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>guy in the chair . com &#187; bitching</title>
	<atom:link href="http://guyinthechair.com/category/bitching/feed/" rel="self" type="application/rss+xml" />
	<link>http://guyinthechair.com</link>
	<description>//the blog of Paul Taylor</description>
	<lastBuildDate>Fri, 18 Nov 2011 01:22: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>Design-by-Contract in RobotLegs 1.4</title>
		<link>http://guyinthechair.com/2011/07/design-by-contract-in-robotlegs-1-4/</link>
		<comments>http://guyinthechair.com/2011/07/design-by-contract-in-robotlegs-1-4/#comments</comments>
		<pubDate>Tue, 05 Jul 2011 20:12:08 +0000</pubDate>
		<dc:creator>Paul Taylor</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[bitching]]></category>
		<category><![CDATA[community]]></category>
		<category><![CDATA[covariance]]></category>
		<category><![CDATA[invariance]]></category>
		<category><![CDATA[robotlegs]]></category>
		<category><![CDATA[robotlegs-variance-utility]]></category>

		<guid isPermaLink="false">http://guyinthechair.com/?p=754</guid>
		<description><![CDATA[If you talked to me at a conference sometime in the last nine months, chances are I ranted loudly about type-variant auto-mediation. At the time, I meant an implementation I wrote in PureMVC because I assumed robotlegs already supported covariant auto-mediation. It doesn&#8217;t. Explanation to follow. Flash and the City Before I begin, this was [...]]]></description>
			<content:encoded><![CDATA[<p>If you talked to me at a conference sometime in the last nine months, chances are I ranted loudly about <a href="https://github.com/dnalot/robotlegs-utilities-variance">type-variant auto-mediation</a>. At the time, I meant an implementation I wrote in PureMVC because I assumed <a href="http://www.robotlegs.org/" title="robotlegs-as3" target="_blank">robotlegs</a> already supported covariant auto-mediation. It doesn&#8217;t. Explanation to follow.</p>
<h2>Flash and the City</h2>
<p>Before I begin, this was the topic of my <a href="http://fatc.co/" title="Flash and the City" target="_blank">Flash and the City</a> talk this year. You can find the slides <a href="http://www.slideshare.net/pauletaylor/design-by-contract-in-robotlegs-as3" title="Design by Contract in robotlegs AS3" target="_blank">here</a> if you want.</p>
<h2>Auto Mediation</h2>
<p>Robotlegs has an interface called <a href="http://api.robotlegs.org/org/robotlegs/core/IMediatorMap.html" title="IMediatorMap interface" target="_blank">IMediatorMap</a>, which maps UIComponent types to Mediator types. Robotlegs watches the display list, and when instances of any mapped components are added to (or removed from) the stage, it will create (or destroy) an instance of the mapped Mediator for it.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">mediatorMap<span style="color: #000066; font-weight: bold;">.</span>mapView<span style="color: #000000;">&#40;</span>MyViewType<span style="color: #000066; font-weight: bold;">,</span> MyViewMediator<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>Each time you create and add a new instance of MyViewType to the stage, a corresponding MyViewMediator instance will be created.</p>
<h2>Problems</h2>
<p>The problem with MediatorMap is two-fold:</p>
<ol>
<li>It enforces an arbitrary restriction that each View instance can only have one Mediator instance. This is dangerous, because it blurs the line between view logic and business logic. Does this functionality belong in the UIComponent or the Mediator? Developers end up writing Views and Mediators with poor separation of concerns, and inevitably overflow View logic into their Mediators. Mediators aren&#8217;t seen as reusable bits of business logic, they&#8217;re seen as an extension to the UI component.
</li>
<li>MediatorMap is a <a href="http://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)" title="Covariance and Contravariance" target="_blank">type invariant</a> mapping. The map creates a Mediator only if the view component is an exact instance of a mapped type.
<p>For example, if you register the <code>MyViewComponentMediator</code> type for <code>MyViewComponent</code>, subclasses of <code>MyViewComponent</code> (say, <code>MyViewComponentSubclass</code>) don&#8217;t get an instance of <code>MyViewComponentMediator</code> automatically created for them.</p>
<p>If <code>MyViewComponent</code> encapsulates enough useful functionality to supplement it with its own Mediator, doesn&#8217;t <code>MyViewComponentSubclass</code> still implement (through inheritance) and require the same functionality from the MediatorMap? Sure, the subclass may override certain functions, but that&#8217;s just polymorphism at work.
</li>
</ol>
<h2>Covariant Auto Mediation</h2>
<p>The idea behind <a href="http://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)" title="Covariance and Contravariance" target="_blank">covariant</a> mediation is that a Mediator is created if a component <em>extends</em> or <em>implements</em> a mapped view component type.</p>
<p>Here&#8217;s the practical difference between an invariant check and a covariant check:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">//Invariant check:</span>
<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>myViewComponent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">constructor</span> == MyViewComponent<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
    <span style="color: #009900; font-style: italic;">//Do something</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">//Covariant check:</span>
<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>myViewComponent <span style="color: #0033ff; font-weight: bold;">is</span> MyViewComponent<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
    <span style="color: #009900; font-style: italic;">//Do something else</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>If the instance of <code>myViewComponent</code> is actually a subclass of the MyViewComponent type, the first check will fail, but the second check will pass.</p>
<h2>Extends or Implements</h2>
<p>Extending classes for functionality is all well and good, but it&#8217;s not as flexible or as clean as implementing an interface. For starters, AS3 allows you only one parent class, so you can&#8217;t compose much functionality through inheritance before running into the <a href="http://en.wikipedia.org/wiki/Diamond_problem" title="The Diamond Problem -- why multiple inheritance is problematic" target="_blank">diamond problem</a>.</p>
<p>Before I go any further, yes, this means you can map a Mediator for all instances or subclasses of <code>Sprite</code> or <code>UIComponent</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">// An instance of SpriteMediator will be created each time an instance</span>
<span style="color: #009900; font-style: italic;">// or subclass of Sprite is added to the display list. Crazy, huh?</span>
<span style="color: #004993;">map</span><span style="color: #000066; font-weight: bold;">.</span>mapMediator<span style="color: #000000;">&#40;</span><span style="color: #004993;">Sprite</span><span style="color: #000066; font-weight: bold;">,</span> SpriteMediator<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>Naturally this introduces performance problems, so everything from the &#8220;flash.*&#8221; and &#8220;mx.*&#8221; packages are filtered out by default.</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">map</span><span style="color: #000066; font-weight: bold;">.</span>registerPackageFilter<span style="color: #000000;">&#40;</span><span style="color: #990000;">'flash.*'</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #004993;">map</span><span style="color: #000066; font-weight: bold;">.</span>registerPackageFilter<span style="color: #000000;">&#40;</span><span style="color: #990000;">'mx.*'</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>And you can add your own package filter definitions (like if you&#8217;ve got a particle emitter and want to skip inspection of each particle):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">map</span><span style="color: #000066; font-weight: bold;">.</span>registerPackageFilter<span style="color: #000000;">&#40;</span><span style="color: #990000;">'com.myApp.particles.*'</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>Anyway, covariantly auto-mediating base classes is alright I guess, but who wants to maintain a complex inheritance tree? Not me. <strong>The real sexy magic happens when you start mapping Interfaces for auto-mediation.</strong></p>
<p>Interfaces are covariantly checked:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>myComponent<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">constructor</span> == IMyInterface<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//will always be false</span>
<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>myComponent <span style="color: #0033ff; font-weight: bold;">is</span> IMyInterface<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span> <span style="color: #009900; font-style: italic;">//can evaluate to true</span></pre></div></div>

<h2>Demo Time</h2>
<p>This post has been highly theoretical so far, and if you let me continue extolling the virtues of designing interfaces to apply behaviors to components via covariant mediation it will only devolve into something more abstract and academic.</p>
<p>So instead, I&#8217;ll show off some code to do it for me. Here&#8217;s the demo app I created for my FATC talk.</p>
<p>This demo has three progressively more impressive examples of common problems solved in very few LOC using covariant mediation:</p>
<ol>
<li>An example of handling system-wide error events.</li>
<li>An example of managing selected screen state.</li>
<li>An example DataGrid where each cell is representative of a unique and open streaming channel from the server.</li>
</ol>

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="swfobj_0" width="595" height="400">
      <param name="movie" value="http://guyinthechair.com/wp-content/flex/covariant_mediation/FATC.swf" />
      <!--[if !IE]>-->
      <object type="application/x-shockwave-flash" data="http://guyinthechair.com/wp-content/flex/covariant_mediation/FATC.swf" width="595" height="400">
      <!--<![endif]-->
        
      <!--[if !IE]>-->
      </object>
      <!--<![endif]-->
    </object>

<h2>Rundown</h2>
<p>I&#8217;ll run through just the meaty parts of this demo. The source for the whole thing is <a href="http://guyinthechair.com/wp-content/flex/covariant_mediation/srcview/index.html" target="_blank">here</a>.</p>
<h3>App.mxml and IoC Mappings</h3>
<p>Here&#8217;s the important part of the Application MXML file:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;">override <span style="color: #0033ff; font-weight: bold;">protected</span> <span style="color: #339966; font-weight: bold;">function</span> createChildren<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;">&#123;</span>
	<span style="color: #009900; font-style: italic;">// Normally these IoC mappings are done inside the Context.</span>
	<span style="color: #009900; font-style: italic;">// Do them here because we have access to our children.</span>
	<span style="color: #6699cc; font-weight: bold;">var</span> context<span style="color: #000066; font-weight: bold;">:</span>FATC_Context = <span style="color: #0033ff; font-weight: bold;">new</span> FATC_Context<span style="color: #000000;">&#40;</span>systemManager <span style="color: #0033ff; font-weight: bold;">as</span> <span style="color: #004993;">DisplayObjectContainer</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">map</span><span style="color: #000066; font-weight: bold;">:</span>IVariantMediatorMap = context<span style="color: #000066; font-weight: bold;">.</span>variantMap<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">// Map implementations of ISystemErrorUI to have</span>
	<span style="color: #009900; font-style: italic;">// SystemErrorUIMediators registered for them automatically</span>
	<span style="color: #004993;">map</span><span style="color: #000066; font-weight: bold;">.</span>mapMediator<span style="color: #000000;">&#40;</span>ISystemErrorUI<span style="color: #000066; font-weight: bold;">,</span> SystemErrorUIMediator<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// Map implementations of IScreen to have ScreenMediators</span>
	<span style="color: #009900; font-style: italic;">// registered for them automatically</span>
	<span style="color: #004993;">map</span><span style="color: #000066; font-weight: bold;">.</span>mapMediator<span style="color: #000000;">&#40;</span>IScreen<span style="color: #000066; font-weight: bold;">,</span> ScreenMediator<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// Map implementations of IStreamingServiceUI to have</span>
	<span style="color: #009900; font-style: italic;">// StreamingServiceUIMediator registered for them automatically</span>
	<span style="color: #004993;">map</span><span style="color: #000066; font-weight: bold;">.</span>mapMediator<span style="color: #000000;">&#40;</span>IStreamingServiceUI<span style="color: #000066; font-weight: bold;">,</span> StreamingServiceUIMediator<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #6699cc; font-weight: bold;">var</span> injector<span style="color: #000066; font-weight: bold;">:</span>IInjector = context<span style="color: #000066; font-weight: bold;">.</span>theInjector<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #009900; font-style: italic;">// Tell the injector to create and inject a new instance of</span>
	<span style="color: #009900; font-style: italic;">// StreamingService each time it sees a dependency on IStreamingService</span>
	injector<span style="color: #000066; font-weight: bold;">.</span>mapClass<span style="color: #000000;">&#40;</span>IStreamingService<span style="color: #000066; font-weight: bold;">,</span> StreamingService<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000066; font-weight: bold;">.</span>createChildren<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #009900; font-style: italic;">// Register the mediators for the Screens class immediately.</span>
	<span style="color: #009900; font-style: italic;">// (normally the registration is deferred to the next frame).</span>
	<span style="color: #004993;">map</span><span style="color: #000066; font-weight: bold;">.</span>registerMediators<span style="color: #000000;">&#40;</span>screens<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<h3>ISystemErrorUI and the <a href="http://guyinthechair.com/wp-content/flex/covariant_mediation/srcview/source/ptaylor/bdd/errors/SystemErrorUIMediator.as.html" target="_blank">SystemErrorUIMediator</a></h3>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> interface ISystemErrorUI
<span style="color: #000000;">&#123;</span>
	<span style="color: #339966; font-weight: bold;">function</span> handleError<span style="color: #000000;">&#40;</span><span style="color: #004993;">error</span><span style="color: #000066; font-weight: bold;">:</span>SystemErrorEvent<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>
&nbsp;
	<span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> errorAcknowledged<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>ISignal<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Components that are interested in being notified when errors occur can implement the <code>ISystemErrorUI</code> interface. Whenever an error happens in the Application, it&#8217;s the responsibility of the errored operation to dispatch a SystemErrorEvent, either bubbling on the display list, or on robotlegs&#8217; shared <code>eventBus</code>. Each instance of SystemErrorUIMediator will notify its ISystemErrorUI instance.</p>
<h3>IScreen and the <a href="http://guyinthechair.com/wp-content/flex/covariant_mediation/srcview/source/ptaylor/bdd/screens/ScreenMediator.as.html" target="_blank">ScreenMediator</a></h3>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> interface IScreen
<span style="color: #000000;">&#123;</span>
	<span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">set</span> selectedScreen<span style="color: #000000;">&#40;</span>screen<span style="color: #000066; font-weight: bold;">:</span>IScreen<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>
&nbsp;
	<span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> screenChangedSignal<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>ScreenChangedSignal<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Implementations of IScreen will be notified when the <code>selectedScreen</code> changes. Implementations of IScreen can cause the screen to change by dispatching their <code>screenChangedSignal</code> member.</p>
<p>Whether the implementor of IScreen truly wishes to change the <code>selectedScreen</code>, or whether it&#8217;s simply interested when the <code>selectedScreen</code> is changed, the ScreenMediator doesn&#8217;t care. In the demo, three of the four implementations of <code>IScreen</code> respond uniquely when their <code>selectedScreen</code> setter is called:</p>
<ol>
<li>The <code><a href="http://guyinthechair.com/wp-content/flex/covariant_mediation/srcview/source/ptaylor/bdd/screens/components/Screens.mxml.html" target="_blank">Screens</a></code> class is a ViewStack. When its <code>selectedScreen</code> setter is called, it checks first whether the <code>IScreen</code> instance is a child of itself. If not, <code>Screens</code> adds it as a child. <code>Screens</code> ultimately changes its <code>selectedChild</code> property to the new <code>IScreen</code>.</li>
<li>The <code><a href="http://guyinthechair.com/wp-content/flex/covariant_mediation/srcview/source/ptaylor/bdd/errors/components/ErrorNotificationView.mxml.html" target="_blank">ErrorNotificationView</a></code> class is the bar at the top that drops down when an error occurs. If the bar is showing and the <code>selectedScreen</code> setter is called, the bar hides itself.</li>
<li>The <code><a href="http://guyinthechair.com/wp-content/flex/covariant_mediation/srcview/source/ptaylor/bdd/grid/components/StreamingServiceItemRenderer.as.html" target="_blank">StreamingServiceItemRenderer</a></code> class is the item renderer for the DataGrid. When the <code>selectedScreen</code> changes to and from the <code>GridScreen</code> instance, <code>StreamingServiceItemRenderer</code> starts or stops the incoming data stream. In this way, we can shut down any data sources for screens that aren&#8217;t currently visible.</li>
</ol>
<h3>IStreamingServiceUI and the <a href="http://guyinthechair.com/wp-content/flex/covariant_mediation/srcview/source/ptaylor/bdd/grid/StreamingServiceUIMediator.as.html" target="_blank">StreamingServiceUIMediator</a></h3>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">public</span> interface IStreamingServiceUI
<span style="color: #000000;">&#123;</span>
	<span style="color: #339966; font-weight: bold;">function</span> setStreamData<span style="color: #000000;">&#40;</span><span style="color: #004993;">value</span><span style="color: #000066; font-weight: bold;">:</span>StreamData<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>
&nbsp;
	<span style="color: #339966; font-weight: bold;">function</span> <span style="color: #0033ff; font-weight: bold;">get</span> updateStreamInfo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span>ChangeStreamInfoSignal<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>There&#8217;s only one implementation of <code>IStreamingServiceUI</code>: <code><a href="http://guyinthechair.com/wp-content/flex/covariant_mediation/srcview/source/ptaylor/bdd/grid/components/StreamingServiceItemRenderer.as.html" target="_blank">StreamingServiceItemRenderer</a></code>. <code>StreamingServiceItemRenderer</code> is the item renderer for each cell in the DataGrid.</p>
<p><code>StreamingServiceItemRenderer</code> takes advantage of the fact that the DataGrid reuses its itemRenderers. When the user scrolls, the DataGrid sets in a new value for <code>data</code>. When that happens, the itemRenderer dispatches its <code>updateStreamInfo</code> member Signal.</p>
<p>When the UI dispatches its <code>updateStreamInfo</code> Signal, <code>StreamingServiceUIMediator</code> tells the service instance the new channel name. Then, the service closes its current channel, and opens a stream with the new channel name.</p>
<p>Because the DataGrid reuses its itemRenderers, and the itemRenderers dispatch changes in its data values to its Mediator, the Mediator can reuse its service instance, which manages opening and closing streams. </p>
<p><strong>This means we only keep open connections for visible cells.</strong></p>
<p>Pretty cool, huh?</p>
]]></content:encoded>
			<wfw:commentRss>http://guyinthechair.com/2011/07/design-by-contract-in-robotlegs-1-4/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The Flex Framework and Modularity: A Manifesto</title>
		<link>http://guyinthechair.com/2010/01/modularity-a-manifesto/</link>
		<comments>http://guyinthechair.com/2010/01/modularity-a-manifesto/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 08:33:46 +0000</pubDate>
		<dc:creator>Paul Taylor</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[bitching]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[LayoutManager]]></category>
		<category><![CDATA[modularity]]></category>
		<category><![CDATA[UIComponent]]></category>

		<guid isPermaLink="false">http://guyinthechair.com/?p=164</guid>
		<description><![CDATA[If you haven&#8217;t watched Greg Burch&#8217;s excellent presentation on Slider at RIAdventure, you freakin should. Seriously, stop right now and watch it. But don&#8217;t forget about this blog post. Make sure to come back here after you&#8217;re done. The Soul of Flex Ok, from this point on I am going to assume you&#8217;ve done exactly as I [...]]]></description>
			<content:encoded><![CDATA[<p><span style="font-weight: normal; font-size: 13px;">If you haven&#8217;t watched Greg Burch&#8217;s <a title="Greg Burch's RIAdventure Slider Presentation" href="http://www.screencast.com/users/jwilker/folders/RIAdventure%202009/media/03101638-66d9-4792-97de-77a8fc7c053f" target="_blank">excellent presentation on Slider</a> at RIAdventure, you freakin should. Seriously, stop right now and watch it. But don&#8217;t forget about this blog post. Make sure to come back here after you&#8217;re done.</span></p>
<h2>The Soul of Flex<br />
<span style="font-weight: normal; font-size: 13px;">Ok, from this point on I am going to assume you&#8217;ve done exactly as I told you and watched Greg Burch&#8217;s <a title="Greg Burch's RIAdventure Slider Presentation" href="http://www.screencast.com/users/jwilker/folders/RIAdventure%202009/media/03101638-66d9-4792-97de-77a8fc7c053f" target="_blank">excellent presentation on Slider</a> at RIAdventure. Around 6 minutes in, Greg starts talking about the Soul of Flex. I didn&#8217;t have much time to consider it, but two things immediately came to mind: invalidation and styles. In the video you&#8217;ll hear me mention CSS but later agree with Greg that CSS or not, styles is the true feature.</span></h2>
<p>Of course, I agree with all the others he said were also part of the Soul: MXML, databinding, states, skinning, item renderers, containers, and components. But as I watched him remove items that weren&#8217;t part of the Soul of Flex, I thought to myself:</p>
<p style="padding-left: 30px;">&#8220;Self, this list is completely subjective and based entirely on my opinion. It is extremely likely that someone else&#8217;s definition of the Soul of Flex is different than mine, and who&#8217;s to say which one is right? Is it Adobe&#8217;s job? Now that they&#8217;ve packed a framework full of features that real people use, they&#8217;re going to remove a good many features simply because the they do not fall into Adobe&#8217;s definition of what the Soul is? That&#8217;s not right. The community is too diverse for that. Flex is the Soul of Flex.&#8221;</p>
<div id="attachment_170" class="wp-caption alignleft" style="width: 160px"><a href="http://guyinthechair.com/wp-content/uploads/2010/01/soul-of-flex.png" target="_blank"><img class="size-thumbnail wp-image-170" title="Soul of Flex" src="http://guyinthechair.com/wp-content/uploads/2010/01/soul-of-flex-150x150.png" alt="The real Soul of Flex" width="150" height="150" /></a><p class="wp-caption-text">I say Flex is the Soul of Flex.</p></div>
<p>This got me thinking about Flex with regards to mobile development. At the time of this writing, every Flex 4 component has a base class that is <a title="UIComponent" href="http://opensource.adobe.com/svn/opensource/flex/sdk/trunk/frameworks/projects/framework/src/mx/core/UIComponent.as" target="_blank">13,246 lines of code</a> (<a title="CLOC parser in JS" href="http://dnalot.com/SparkDataGrids/clocFx.html" target="_blank"><strong>1412</strong> blank lines, <strong>6423</strong> comment lines, and <strong>5411</strong> actual code lines.</a>) If you&#8217;re using Flex 4 and skinning, you have two components, so multiply potentially everything the UIComponent does by 2:</p>
<ul style="list-style-position: inside;">
<li>2x the UIComponents to initialize</li>
<li>2x the UIComponents added to the inheriting style chain</li>
<li>2x the UIComponents to make validation passes on</li>
<li>2x the event listeners that are registered</li>
<li>2x the number of objects for the DragProxy (in the DragManager) to find in its getObjectsUnderPoint() implementation</li>
<li>and on and on&#8230;</li>
</ul>
<p>This means one of two things. Either the decision to abstract skins into another UIComponent was monumentally retarded, or the UIComponent is too large and tries to do to many things. Since I believe the new skins are wonderful from an architectural/modularity standpoint, I have to pick the second choice: <strong>the UIComponent is too damn big</strong>. It tries to do too damn much. Let me reiterate: <strong>I love the new Spark skins. I hate the size of the UIComponent.</strong></p>
<h2>The Question of Responsibility<br />
<span style="font-weight: normal; font-size: 13px;">Why is the UIComponent (at the time of this writing) 13,246 lines long? The answer is features. The UIComponent violates the <a title="Single Responsibility Principle" href="http://en.wikipedia.org/wiki/Single_responsibility_principle" target="_blank">Single Responsibility Principle</a>. Here&#8217;s a list of most of the features the UIComponent encapsulates:</span></h2>
<ul>
<li>Invalidation &#8211; Implements IInvalidating.
<ul>
<li>Defines invalidate properties/size/displayList functions.</li>
<li>Defines validateNow function.</li>
</ul>
</li>
<li>Validation &#8211; Implements ILayoutManagerClient.
<ul>
<li>Defines the initialized, nestLevel, processedDescriptors, and updateCompletePendingFlag properties.</li>
<li>Defines validate properties/size/displayList functions.</li>
</ul>
</li>
<li>Styles &#8211; Implements IAdvancedStyleClient, the only visual class that does.</li>
<li>States &#8211; Implements IStateClient and IStateClient2 to support the new Fx4 states syntax.</li>
<li>Tooltips &#8211; Implements IToolTipManagerClient, the only component class that does.</li>
<li>Constraint-based layout.</li>
<li>IRepeaterClient &#8211; UIComponents (and subclasses) can be created by Repeaters.</li>
<li>Databinding.</li>
<li>Related to Databinding, implements IPropertyChangeNotifier.</li>
<li>Embedded fonts.</li>
<li>Focus &#8211; Doesn&#8217;t implement IFocusManagerComponent by name, but does implement the functions. Has a reference to the FocusManager and a focusPane property.</li>
<li>Validators &#8211; Implements IValidatorListener, which allows the UIComponent to respond to ValidationResultEvents dispatched by the Validator classes.</li>
<li>Modules &#8211; Implements IFlexModule, so if created by an IFlexModuleFactory, the factory stores a reference of itself on the UIComponent.</li>
<li>Explicit/measured- max/min &#8211; widths/heights.</li>
<li>Percent widths/heights.</li>
<li>Enabled/disabled.</li>
<li>Effects.</li>
<li>Special logic for adding/removing children.</li>
<li>All the events it creates and dispatches, including
<ol>
<li>Initialization events, like preInit and creationComplete.</li>
<li>FlexEvents like show, hide, move, resize</li>
<li>StateChangeEvents.</li>
<li>DragEvents for the List classes. Not part of the public API.</li>
<li>ToolTipEvents</li>
<li>FlexMouseEvents</li>
</ol>
</li>
</ul>
<p>Man, the UIComponent does a ton of stuff. We haven&#8217;t even gotten into the measurement, layout, containment, children handling, skinning, item renderers, or graphics functionality of Flex yet!</p>
<h2>The UIComponent is the base class for&#8230; everything<br />
<span style="font-weight: normal; font-size: 13px;">At least, everything visual. Not including Flex 3 skins. It&#8217;s the base class for all non-Fx3-skin-related visual elements.</span></h2>
<p>The UIComponent has so many responsibilities because it is the base class for so many controls. You want a Label or TextArea? They&#8217;re UIComponents. You want a Button? It&#8217;s a UIComponent. You want a container of Buttons? It&#8217;s a UIComponent.</p>
<p><span style="font-weight: normal; font-size: 13px;">But there are a few things I can&#8217;t figure out about this configuration:</span></p>
<ol>
<li>Why does a container need logic in its base class that accesses embedded fonts, or creates UITextFields from embedded font contexts (I checked, UITextField is the only class passed into createInFontContext()).</li>
<li>Similarly, why does a Label require any knowledge about processedDescriptors, states, or validators? Labels can&#8217;t have children, states, or validators. In fact, Labels can&#8217;t do much except display text. Why do they need to be IFlexModule objects? I could go on, but I think you get the point.</li>
</ol>
<p>There is a trend of WTFs like this about the UIComponent that can only be explained by, &#8220;it&#8217;s <strong>the</strong> base class and we&#8217;re trying to keep our API super clean.&#8221;</p>
<h2>There is a better way: Composited Modularity<br />
<span style="font-weight: normal; font-size: 13px;">Most of the functionality is already segregated by the liberal use of interfaces&#8230; now lets actually implement it that way.</span></h2>
<p>Almost all the functionality that the UIComponent (and subclasses of UIComponent for that matter) contains can be grouped into smaller, more discreet classes. Lets call them modules. Once this is done, the UIComponent simply exists to provide a unified API to the developer and glue with which to assemble the modules. More complex controls and components are simply composed of more complex modules.</p>
<p>For example, the functionality for embedded fonts should be isolated into its own module. Then, only the Label, Text, TextArea, and maybe some other controls that require direct access to embedded fonts need to include the &#8220;EmbeddedFontsModule.&#8221; This way, the VBox doesn&#8217;t have to include functionality for accessing embedded fonts.</p>
<p>Similarly, a VBox would include special modules for adding children, measurement, and layout. See, this is easy.</p>
<p><span style="font-weight: normal; font-size: 13px;">And while we&#8217;re at it, let&#8217;s rewrite the LayoutManager.</span></p>
<h2>You heard me. The LayoutManager. It&#8217;s in my sights.<br />
<span style="font-weight: normal; font-size: 13px;">What&#8217;s so special about 3-phased validation that it has to be hard-coded into the Flex framework? Is it phase ordering? Nest-level ordering? That&#8217;s not special. That&#8217;s algorithms.</span></h2>
<p>First, for those who don&#8217;t know, the LayoutManager is what enables Flex components to do the awesome 3-phase component lifecycle that we&#8217;ve all come to know and love. When a component is invalidated for a phase (say, invalidateProperties()), he registers with the LayoutManager. The LayoutManager adds the component to the proper invalidation queue. There are 3 invalidation queues, one for each validation phase. The invalidation queues are a special PriorityQueue implementation. This is important, because there is an order to the validation process.</p>
<p>Components have a nestLevel, which is really just their position in the display list. The Application&#8217;s nestLevel is 3. The nestLevel increases from there, so the lowest component in the display tree has the highest number.</p>
<p>When a component is added to the invalidatePropertiesQueue, it is added at its nestLevel priority. When the invalidatePropertiesQueue is processed, components with the lowest nestLevel, the ones closest to 0, are processed first. This is called &#8220;top-down&#8221; processing, because it starts at the top of the display list and processes to the bottom.</p>
<p>The commit phase does top-down processing, because generally parents can commit properties on themselves and change properties on their children, which will then get committed. The measure phase is from the bottom-up (can you guess what that means? If not, put it in the comments.), because a parent will often make a decision on his size based on the sizes of his children. The update phase is another top-down queue, because now that the sizes and positions are calculated, it&#8217;s time to lay them out. This is what makes percent widths/heights, scrollbars, etc. possible.</p>
<h3>Rethinking validation<br />
<span style="font-weight: normal; font-size: 13px;">Does validation have to be hardcoded? Are the 3 phases all that are needed? How hard would it be to switch to just 2? 4? 100?</span></h3>
<p>The fact is, the developer should be able to hook into the power of phased updates for himself. Why have a manager that only validates 3 hard-coded phases, when you could have a manager that validates an unlimited number of dynamic phases?<br />
<span style="font-weight: normal; font-size: 13px;">Well sure Paul, that sounds great and all, but it&#8217;s kind of a tough problem.</span> &lt;&#8211; Shut up alternate text, I&#8217;ve got a solution.</p>
<p>Keep the idea of nestLevel. It&#8217;s clear, clean, and definite. Change the idea of 3 hard-coded phases into an unlimited number of injected phases, based on priorities. Similar to nestLevel for components, priorities are arbitrarily defined by the developer. A priority is an Array of ints. It can be as simple as &#8220;1&#8243; or as complex as &#8220;1.2.3.4.&#8221; It is only used as a basis for comparison between other priorities. For example, priority 1.2 is greater than priority 1.2.1, since it is assumed 1.2.1 is a subset of 1.2. However, 1.200 is much greater than 1.2, because 200 &gt; 2. Do priorities make sense? If the answer is no, put it in the comments.</p>
<p>The idea is that a developer can invalidate a component for a certain phase. The phase has a priority (like 1.3), and a direction (UP or DOWN). The ValidationManager keeps a Heap of Heaps, sorted by priority. If it doesn&#8217;t find a Heap at the priority specified, it creates a new Heap, adds the IValidationClient to it, sets the proper sort direction (UP or DOWN) on it, then adds the new Heap to the Heap of Heaps. If it does find a Heap, it adds the IValidationClient to the Heap that it found.</p>
<p>When it comes time to Validate (on the next enter_frame event), the ValidationManager dequeues each Heap from the Heap of Heaps. Then it dequeues each item from each individual Heap, validating it as it goes.</p>
<p>The beauty of this is that the Flex component lifecycle can be perfectly mimicked. Set the three phases to happen one after another, and you can get the same results. For APIs sake, lets add a gap between them, so a third party developer can come by later and inject his own phases between the 3 usual phases. If we set commit at 1.3, measure at 1.5, and update at 1.7, this should give us enough of a gap for developers to use.</p>
<p>Say Developer X wants to add some extra <strong>umph</strong> to his custom component, but needs this processing needs to happen in-phase and inbetween the measure and update phases. He simply needs to inject his own validation phase anywhere between measure at 1.5 and update at 1.7, lets keep it simple and say 1.6, and BAM. ValidationManager will validate his phase right after measure and right before update.</p>
<p>This allows the developers a whole new avenue of development, something that they would have had to tack onto one of Flex&#8217;s 3 hard-coded phases before.</p>
<p><span style="font-weight: normal; font-size: 13px;"> This is all words. Where are the actions. You fail.</span></p>
<h2>This is where I introduce FlashWorks<br />
<span style="font-weight: normal; font-size: 13px;">FlashWorks is a component set for Flash and FlashBuilder, designed around the concepts of a tiny core API and modularity.</span></h2>
<p>It includes the ValidationManager and validation scheme I&#8217;ve outlined here, as well as a few other things:</p>
<ul>
<li>MXML development</li>
<li>Skinning similar to Flex 4</li>
<li>Graphics primitives (extend EventDispatcher)</li>
<li>Measurement and Layout controllers with support for percent widths/heights</li>
<li>Injectable Validation phases</li>
<li>States, including support for FB4&#8242;s new States syntax</li>
<li>Styles, through a controller that interfaces with <a title="F*CSS Flash CSS parsing" href="http://fcss.flashartofwar.com/" target="_blank">Jesse Freeman&#8217;s F*CSS</a></li>
<li>Databinding</li>
<li>SystemManager that works with FB4&#8242;s code generation, so the FlashWorks movie is 2 frames, just like Flex.</li>
<li>A modular, pay-as-you-go system, so functionality is only included when it&#8217;s used. For instance, the States controller isn&#8217;t included unless States are defined on the component.</li>
</ul>
<h2>Alright, here&#8217;s the code and demos</h2>
<p><font color="#000099">[Edit]</font> I&#8217;ve since moved from this project onto <a href="http://github.com/guyinthechair/reflex">Reflex</a>. I feel it has more of a chance for success than a solo framework by me would.<br />
I&#8217;m keeping the demo and demo code here as a proof of concept:<br />
Demo: <a title="FlashWorks demo" href="http://guyinthechair.com/misc/flashworks/FlashWorks.html" target="_blank">http://guyinthechair.com/misc/flashworks/FlashWorks.html</a><br />
Source code for demo: <a title="FlashWorks Demo Source" href="http://guyinthechair.com/misc/flashworks/srcview/index.html" target="_blank">http://guyinthechair.com/misc/flashworks/srcview/index.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://guyinthechair.com/2010/01/modularity-a-manifesto/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

