<?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>Paul Williams</title>
	<atom:link href="http://blogs.adobe.com/paulw/feed" rel="self" type="application/rss+xml" />
	<link>http://blogs.adobe.com/paulw</link>
	<description></description>
	<lastBuildDate>Fri, 30 Jul 2010 07:48:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Unit Testing User Interfaces &#8211; Autonomous View</title>
		<link>http://blogs.adobe.com/paulw/archives/2008/09/unit_testing_us_3.html</link>
		<comments>http://blogs.adobe.com/paulw/archives/2008/09/unit_testing_us_3.html#comments</comments>
		<pubDate>Tue, 30 Sep 2008 15:12:01 +0000</pubDate>
		<dc:creator>Paul Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blogs.adobe.com/paulw/2008/09/unit_testing_us_3.html</guid>
		<description><![CDATA[
 <a href="http://blogs.adobe.com/paulw/archives/2008/09/unit_testing_us_3.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>So far we&#8217;ve looked at possible unit test strategies for <a href="http://blogs.adobe.com/paulw/archives/2008/04/unit_testing_us_1.html">presentation models</a> and <a href="http://blogs.adobe.com/paulw/archives/2008/05/unit_testing_us_2.html">supervising presenters</a>. Now I want to turn my attention to the <a href="http://blogs.adobe.com/paulw/archives/2007/09/presentation_pa_1.html">Autonomous View</a> pattern. By leaving state and logic in the view, the Autonomous View pattern is arguably one of the simplest presentation patterns to understand and implement. But is it so easy to unit test?</p>
<p><span id="more-20"></span></p>
<h2>Challenges </h2>
<p>When attempting to unit test views directly, there are numerous challenges to overcome. </p>
<p><strong>Challenge 1 &#8211; Isolating the views:</strong> Strictly speaking, to unit test an autonomous view class we should be creating stub or mock objects for all of the classes it collaborates with. But a typical autonomous view in Flex could collaborate with numerous Flex framework classes. In my example, the AlbumForm collaborates with three TextInputs, one Checkbox, and two Buttons; not to mention container classes like Form and FormItem. Stubbing all of these is no trivial task and not something I&#8217;m going to attempt. So strict unit testing of an autonomous view application is out. </p>
<p>A more sensible approach  would be to treat each distinct autonomous view class as a &quot;unit&quot; for testing purposes, and assume that the Flex controls it contains are part of that unit. So I&#8217;ll create a unit test suite for AlbumForm and AlbumBrowser since these are distinct autonomous views, and when testing AlbumBrowser I&#8217;ll create a stub for AlbumForm.</p>
<p><strong>Challenge 2 &#8211;  Creating the view:</strong> When testing presentation models and supervising presenters I instantiated the &quot;class under test&quot; in my test suite&#8217;s setup method. But if I do the same for autonomous views, when I try to access a child of my view instance I get a null pointer exception. This is because my test code is being executed before the view&#8217;s CreationComplete event has fired. So to ensure that the CreationComplete event has been dispatched before my test runs, I have added an asynchronous setup method. This method instantiates the autonomous view, adds it to the display list and then uses the FlexUnit &quot;addAsync&quot; method to trigger the unit test code in response to the view&#8217;s CreationComplete method. </p>
<p><strong>Challenge 3 &#8211;  Simulating gestures:</strong> A further problem with unit testing my views is that much of their behaviour is driven directly by user activity. For example the change event of the TextInput class can only occur in response to user input. So I have the following options:</p>
<ul>
<li>Fake the event by calling dispatchEvent on the control and passing it a suitable Event instance that I&#8217;ve created and populated myself..</li>
<li>Drive the control using the Flex automation library</li>
<li>Bypass event dispatch and call the event handler directly from my test.</li>
</ul>
<p>The first of these appears to be the easiest, so that&#8217;s the option I&#8217;m going with.</p>
<p><strong>Challenge 4 &#8211; Stubbing the Alert class:</strong> The AlbumBrowser class displays an alert box when the user navigates between albums without saving their changes. I want to test this behaviour, but I don&#8217;t want to see pop-ups appearing during a test run. So I&#8217;ve extracted the pop-up behaviour into a separate class (called YesNoDialogue), and added a stub class for use during test runs.  </p>
<p> <strong>Challenge 5 &#8211; Stubbing child views:</strong> I&#8217;ve already said I&#8217;ll create a stub for AlbumForm in order to test AlbumBrowser in isolation of AlbumForm&#8217;s logic. I&#8217;ve done something similar in my previous entries for Presentation Model and Supervising Presenter. In those examples I used a factory method to allow real classes to be substituted with stubs. This works great in ActionScript, but is there a way to swap a real view with a stub when the real view is declared in MXML? Well I gave myself a full 15 minutes to think of one, but nothing came to mind. So in order to stub my AlbumForm, I&#8217;m forced to abandon MXML and create my child view in ActionScript. This is a significant compromise.</p>
<h2>Example Application </h2>
<p>With these challenges overcome or worked around, I could successfully unit test the Autonomous View demo. The unit tested version of the application is <a href="http://examples.pmwilliams.co.uk/adobeblog/unittestingui/autonomousview/UnitTestingAutonomousView.html" target="_blank">available here</a>; right-click to view the source. The test   runner for the unit tests is <a href="http://examples.pmwilliams.co.uk/adobeblog/unittestingui/autonomousview/AllUnitTests.html" target="_blank">available here</a>.</p>
<h2>Thoughts</h2>
<p>This isn&#8217;t so much a tutorial on how to unit test an autonomous view application as an example of why you should avoid trying to. Although many of the challenges can be resolved or worked-around, one in particular presents a real problem. The autonomous view pattern is attractive because of its simplicity; and a large part of that comes from the convenience of MXML for declarative layout. By abandoning MXML in order to unit test,  I am undermining the simplicity of the pattern. </p>
<h2>Further work </h2>
<p>There may be clever ways to stub views in MXML and I admit I didn&#8217;t spend  much time looking at how to achieve this. If you&#8217;ve found an elegant approach for testing this pattern without abandoning MXML I&#8217;d be very interested to hear about it, but I don&#8217;t intend to investigate the topic any further myself. </p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.adobe.com/paulw/archives/2008/09/unit_testing_us_3.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Unit Testing User Interfaces &#8211; Supervising Presenter</title>
		<link>http://blogs.adobe.com/paulw/archives/2008/05/unit_testing_us_2.html</link>
		<comments>http://blogs.adobe.com/paulw/archives/2008/05/unit_testing_us_2.html#comments</comments>
		<pubDate>Tue, 27 May 2008 14:57:59 +0000</pubDate>
		<dc:creator>Paul Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blogs.adobe.com/paulw/2008/05/unit_testing_us_2.html</guid>
		<description><![CDATA[
 <a href="http://blogs.adobe.com/paulw/archives/2008/05/unit_testing_us_2.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>In my <a href="http://blogs.adobe.com/paulw/archives/2008/04/unit_testing_us_1.html">last entry</a> I took a look at unit testing the classes of a simple Presentation Model application. In this entry I&#8217;m going to turn my attention to a functionally-identical <a href="http://blogs.adobe.com/paulw/archives/2007/10/presentation_pa_2.html">Supervising Presenter</a> application. If you cast your mind back to my <a href="http://blogs.adobe.com/paulw/archives/2007/09/presentation_pa.html">original investigation</a> of these patterns you&#8217;ll remember that the main difference between <a href="http://blogs.adobe.com/paulw/archives/2007/10/presentation_pa_3.html">Presentation Model</a> and Supervising Presenter is that the former extracts both state and logic from the view whereas the latter only extracts logic. So in order to receive user events and manipulate view state in response to these events,  supervising presenters maintain a reference to their corresponding view.</p>
<p><span id="more-6"></span></p>
<h2><strong>Challenges</strong></h2>
<p>The first challenge when unit testing supervising presenters is the dependency they have on the view. Since I am trying to follow a strict unit testing strategy, I want my supervising presenters to be tested independently of other classes, including view classes. So I need to find some way to isolate my  presenters from their corresponding views. Besides this unit-testing purism, there are other reasons for isolating my tests from the view:</p>
<ul>
<li>Views are generally relatively slow to instantiate and require a disproportionate amount of resources. Having to create views in order to test classes that depend on them may slow down the running of my test suites.</li>
<li>Animation, deferred instantiaion and modal behaviour could make unit testing challenging. </li>
</ul>
<p>There are probably many ways to decouple presenters from views, but I&#8217;ve decided to follow <a href="http://www.martinfowler.com/eaaDev/SupervisingPresenter.html" target="_blank">Martin Fowler&#8217;s suggestion</a> of creating a &quot;Gateway&quot; between the view and the presenter. Each gateway will have two responsibilities:</p>
<ul>
<li>Act as a <a href="http://en.wikipedia.org/wiki/Facade_pattern" target="_blank">facade</a> for a view. Presenters will access and manipulate the view via the facade.</li>
<li>Observe the view for user events and invoke operations on the presenter.</li>
</ul>
<p>Each view-dependent gateway will implement an interface to allow it to be substituted with a view-independent stub class for use during testing. This <a href="http://weblogs.macromedia.com/paulw/archives/unittestingui/supervisingpresenter/demo/srcview/source/diagram.png" target="_blank">UML diagram</a> illustrates the impact of the gateway approach on the original design. </p>
<p>The second challenge is the collaborative relationship between supervising presenters. In order to isolate one supervising presenter from another I&#8217;m going to use the factory method approach described in my previous entry.</p>
<h2>Unit testing AlbumFormPresenter </h2>
<p>The presenter for AlbumForm does not collaborate with any other presenter objects, but I do need to create a gateway class in order to isolate it from the view. I had to make a few changes to the architecture in order to slot in my gateway, and the end result is a gateway object between the view and the presenter called AlbumFormGateway. This gateway class implements the interface IAlbumFormGateway, so that it can be replaced with a test double called StubAlbumFormGateway. The test double does not require the real view, and the AlbumFormPresenter does not differentiate between the real gateway and the stub gateway because they both implement the same interface. So now I can unit test my AlbumFormPresenter class without needing an instance of AlbumForm.</p>
<p><strong>Unit testing AlbumBrowserPresenter</strong> </p>
<p>I also need to create a gateway object for AlbumBrowserPresenter to decouple it from the corresponding view class. This is achieved in precisely the same way as for AlbumFormPresenter. So I&#8217;ve added an AlbumPresenterGateway, IAlbumPresenterGateway and StubAlbumPresenterGateway. </p>
<p>In addition to creating the gateway class, I also need to isolate the functionality of AlbumBrowserPresenter from AlbumFormPresenter. If you recall from my previous entry, a similar relationship existed between the AlbumBrowser and AlbumForm presentation models. So I&#8217;m applying the same factory method approach approach to allow the real AlbumFormPresenter to be replaced by a stub. The result is a new interface IAlbumFormPresenter, and a new stub class StubAlbumFormPresenter. </p>
<p>A further  complexity for AlbumBrowserPresenter is the dependency it has on the PopUpManager. Rather than stub the PopUpManager class I have placed it on the &#8216;view-side&#8217; of the gateway; so the presenter displays the pop-up by calling a method on the gateway. </p>
<p><strong>Unit testing Album</strong> </p>
<p>Unlike the Presentation Model applicaton, the Album VO in the Supervising Presenter example doesn&#8217;t have any methods, so it doesn&#8217;t need to be tested. Or does it?</p>
<p>In order for my application to work the Album VO class does need to do one very important job: It needs to support binding. Should I write unit tests for bindability? Well strictly speaking I should, and perhaps I could use <a href="http://blogs.adobe.com/tomsugden/2008/01/post.html" target="_blank">Tom&#8217;s EventfulTestCase</a> for this. But I think I&#8217;ll look more closely at this another day.</p>
<p><strong>Unit testing AlbumDelegate</strong></p>
<p>As with the Presentation Model example, I&#8217;m not testing this class. </p>
<h2>Example Application </h2>
<p>The unit tested version of the supervising presenter demo application is <a href="http://examples.pmwilliams.co.uk/adobeblog/unittestingui/supervisingpresenter/UnitTestingSupervisingPresenter.html" target="_blank">available here</a>; right-click to view the source. The test   runner for the unit tests is <a href="http://examples.pmwilliams.co.uk/adobeblog/unittestingui/supervisingpresenter/AllUnitTests.html" target="_blank">available here</a>.</p>
<h2>Thoughts</h2>
<p>The supervising presenter&#8217;s view dependency does make unit testing more challenging. The gateway approach appears to be a robust strategy for isolating supervising presenters from views, but it comes at a price. The gateway layer adds extra complexity to the application, and the gateways themselves cannot be unit-tested because they still have dependencies on views. </p>
<h2>Further work </h2>
<p>After going to the trouble of creating gateways I&#8217;m left wondering whether it would be simpler to create test doubles for my actual view classes and dispense with gateways altogether. To do this I would have to extract an interface for each view and then create standalone test doubles that contain identical framework controls. For this to work I would also need to find some way to create fake framework events for my presenters to respond to. I&#8217;m not going to attempt this because I now want to turn my attention to unit testing autonomous views. </p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.adobe.com/paulw/archives/2008/05/unit_testing_us_2.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit Testing User Interfaces &#8211; Presentation Model</title>
		<link>http://blogs.adobe.com/paulw/archives/2008/04/unit_testing_us_1.html</link>
		<comments>http://blogs.adobe.com/paulw/archives/2008/04/unit_testing_us_1.html#comments</comments>
		<pubDate>Tue, 15 Apr 2008 14:54:17 +0000</pubDate>
		<dc:creator>Paul Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blogs.adobe.com/paulw/2008/04/unit_testing_us_1.html</guid>
		<description><![CDATA[
 <a href="http://blogs.adobe.com/paulw/archives/2008/04/unit_testing_us_1.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>As mentioned in the <a href="http://blogs.adobe.com/paulw/archives/2008/03/unit_testing_us.html">introduction</a>, I&#8217;m going to attempt to unit test my <a href="http://blogs.adobe.com/paulw/archives/2007/10/presentation_pa_3.html">presentation model</a> example application first. Of all the presentation patterns I&#8217;ve looked at, I&#8217;m expecting the presentation model to be the easiest to unit test. </p>
<h2>Challenges</h2>
<p>By extracting view state and logic from the view, presentation model classes do not have any dependencies on  view classes. They do sometimes have dependencies on each other, so test doubles may be required to isolate the functionality in one presentation model from functionality in another.</p>
<p><span id="more-7"></span></p>
<h2>Unit testing AlbumForm</h2>
<p>The AlbumForm class is quite straightforward to unit test as it only collaborates with the Album value object. I&#8217;ve decided not to create a test double for the Album VO because of its simplicity. It only has too methods, and I suspect they will be difficult to simulate without duplicating their behaviour.</p>
<p>In the unit test for AlbumForm I have created tests for all of the public methods of the AlbumForm class. Many of the public methods require more than one test in order to exercise every path of execution. </p>
<h2>Unit testing AlbumBrowser</h2>
<p>The AlbumBrowser class is a little harder to unit test because it collaborates with the AlbumForm class. In order to isolate my AlbumBrowser unit tests from the functionality of AlbumForm, I&#8217;ve decided to create a test double for the AlbumForm class. To do this I&#8217;ve made the following changes: </p>
<ul>
<li><strong>Extracted an  interface from AlbumForm</strong> &#8211; In order to create a test double for the AlbumForm, I&#8217;ve created an IAlbumForm interface which the AlbumForm now implements. The test double &#8216;StubAlbumForm&#8217; also implementes IAlbumForm allowing it to be used as a substitute for AlbumForm. </li>
<li><strong>Modified AlbumBrowser to work with any IAlbumForm implementation </strong>- Now there&#8217;s more than one way to do this, and I&#8217;ve used a factory method. The AlbumBrowser is modified so that it creates its instance of AlbumForm in a <a href="http://en.wikipedia.org/wiki/Factory_method_pattern" target="_blank">factory method </a>(that&#8217;s just an instance method that is reponsible for constructing the AlbumForm). The factory method is marked protected so that it can be overriden in a subclass. In my AlbumBrowser unit test I&#8217;ve created a private subclass of AlbumBrowser that overrides the factory method to return an instance of StubAlbumForm*. My unit test for AlbumBrowser tests an instance of the private sub-class rather than AlbumBrowser itself. This allows me to test AlbumBrowser functionality in isolation of the AlbumForm class. </li>
<li><strong>Added a property to StubAlbumForm to allow me to control the flow of tests</strong> &#8211; The AlbumBrowser takes different actions depending on the result from the &#8216;changesToSave&#8217; method on the AlbumForm class. In order to test these different paths I&#8217;ve added a &#8216;pendingChanges&#8217; property to the StubAlbumForm that allows me to specify the return value of the &#8216;changesToSave&#8217; method.</li>
</ul>
<p>I&#8217;ve also captured these modifications in the form of a UML diagram, so <a href="http://weblogs.macromedia.com/paulw/archives/unittestingui/presentationmodel/demo/srcview/source/diagram.png" target="_blank">take a look</a> if none of the above makes any sense.</p>
<p>Together, these changes allow me to test AlbumBrowser in isolation of the AlbumForm class. Doing all of this may be considered overkill for such a small application, but it becomes important for larger applications with deeper hierarchies of presentation model instances and more complex collaborations. </p>
<p><strong>Unit testing Album</strong> </p>
<p>The Album VO doesn&#8217;t collaborate with any other objects, so testing it is very easy. The unit test for the Album class covers the two public methods: clone() and equals(). 
</p>
<p><strong>Unit testing AlbumDelegate</strong></p>
<p>Since this class is a fake delegate, I&#8217;m not going to test it. In real applications it will be necessary to create test doubles for delegates. </p>
<h2>Example Application </h2>
<p>The unit tested version of the presentation model demo application is <a href="http://examples.pmwilliams.co.uk/adobeblog/unittestingui/presentationmodel/UnitTestingPresentationModel.html" target="_blank">available here</a>; right-click to view the source. The test runner for the unit tests is <a href="http://examples.pmwilliams.co.uk/adobeblog/unittestingui/presentationmodel/AllUnitTests.html" target="_blank">available here</a>. </p>
<h2>Thoughts</h2>
<p>The presentation model&#8217;s independence from the view simplifies the unit testing of application-specific user interface logic. The main challenge is isolating the functionality of one presentation model from that of another. I&#8217;ve chosen to use a factory method approach in my application, but other <a href="http://en.wikipedia.org/wiki/Creational_pattern" target="_blank">creational patterns</a> could also be applied, as could some form of <a href="http://en.wikipedia.org/wiki/Dependency_injection" target="_blank">dependency injection</a>.</p>
<p><strong>Further work</strong></p>
<p>The presentation model classes in this application are still not very well encapsulated. Many of the properties the view binds to are public properties; this simplifies binding, but undermines encapsulation. I&#8217;d like to tighten up the encapsulation on these classes, but that&#8217;s a task for another day.</p>
<p><em>*A similar approach in Java would be to use Anonymous Inner Classes inside tests or fixtures to create test-specific sub-classes (here&#8217;s <a href="http://www.ibm.com/developerworks/library/j-mocktest.html" target="_blank">an example</a> from IBM). EcmaScript and consequently ActionScript do not support this language feature, so I&#8217;ve used a private class instead. </em></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.adobe.com/paulw/archives/2008/04/unit_testing_us_1.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Unit Testing User Interfaces &#8211; Introduction</title>
		<link>http://blogs.adobe.com/paulw/archives/2008/03/unit_testing_us.html</link>
		<comments>http://blogs.adobe.com/paulw/archives/2008/03/unit_testing_us.html#comments</comments>
		<pubDate>Mon, 24 Mar 2008 13:25:53 +0000</pubDate>
		<dc:creator>Paul Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blogs.adobe.com/paulw/2008/03/unit_testing_us.html</guid>
		<description><![CDATA[
 <a href="http://blogs.adobe.com/paulw/archives/2008/03/unit_testing_us.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>You may  have caught my <a href="http://blogs.adobe.com/paulw/archives/2007/09/presentation_pa.html">last series</a> in which I looked at six different patterns for  organising view logic in a Flex client. In this series I&#8217;m going to take the  demonstration applications for three of those patterns and look at the changes  required to effectively unit test them. The aim of the exercise is to gain an  insight into how amenable each pattern is to unit testing and hopefully  wrestle with some common unit testing challenges along the way.</p>
<p><span id="more-8"></span></p>
<h2>What is  unit testing?</h2>
<p>This  isn&#8217;t an introduction to unit testing or a tutorial on how to get started with  unit testing. So if you&#8217;re asking this question you may want to have a look  at some of the links below. The test tool of choice for this series is <a href="http://code.google.com/p/as3flexunitlib/">Flex  Unit</a> as it is the one we use on our consulting projects. There are others out  there, so feel free to shop around.</p>
<ul>
<li><a href="http://www.adobe.com/devnet/flex/articles/unit_testing.html" target="_blank">Unit testing and Test Driven Development (TDD) for Flex and ActionScript 3.0</a></li>
<li><a href="http://www.darronschall.com/weblog/archives/000216.cfm" target="_blank">How to use FlexUnit with FlexBuilder 2</a></li>
<li><a href="http://ammonlauritzen.com/blog/2006/11/02/flexunit_basics/" target="_blank">FlexUnit Basics</a></li>
<li><a href="http://blog.iconara.net/2007/02/06/flexunit/" target="_blank">The lost FlexUnit documentation </a></li>
<li><a href="http://www.adobe.com/cfusion/communityengine/index.cfm?event=dogooglesearch&amp;searchTagName=flexunit&amp;productId=2&amp;startRow=1&amp;loc=en_US" target="_blank">FlexUnit articles in the Flex Cookbook</a></li>
</ul>
<h2>The plan</h2>
<p>When  looking at Presentation Patterns I started with the simplest pattern to  implement, but for this series I&#8217;m going to start with the pattern that I  believe will be simplest to unit test. So here&#8217;s the order of business:</p>
<ol>
<li>Presentation  Model </li>
<li>Supervising  Presenter</li>
<li>Autonomous  View</li>
</ol>
<h2>Why  test view logic?</h2>
<p>Applying a unit testing strategy to your user interface logic can have more than one advantage:</p>
<ul>
<li><strong>Helps uncover defects early </strong> &#8211; This is the obvious one, unit testing should help you detect bugs as you develop your code. It won&#8217;t detect every bug, but it should provide an extra line of defence. </li>
<li><strong>Facilitates refactoring</strong> &#8211; Refactoring is something of a buzzword these days and you&#8217;re far more likely to hear people talking about refactoring their code than unit testing it. Refactoring is a structured technique for reducing duplication and redundancy in your code, but without automated unit tests it can also be a great way to add defects. </li>
<li><strong>Helps to communicate intent</strong> &#8211; Unit tests can help to describe the intended behaviour of a class. This is of particular importance in team-based software development, or for applications with long maintenance phases. Code commenting and technical documentation are still often favoured over unit testing, but it is debatable whether these are more reliable than a comprehensive suite of unit tests. </li>
<li><strong>Increases the </strong><strong> focus on architecture </strong> &#8211; Adopting a unit test strategy forces you to think more about the architecture of your application. Perhaps most significantly, the need to test each class in isolation of its collaborators encourages a developer to code against interfaces rather than concrete classes. You&#8217;re also likely to see fewer global singletons and <a href="http://en.wikipedia.org/wiki/Law_of_Demeter" target="_blank">law of demeter</a> violations in a unit tested application because of the increased emphasis on modularity. </li>
</ul>
<h2>Is this  Test Driven Development?</h2>
<p>Test Driven Development (TDD) or test-first development is a development practice in which unit tests are written before the code they test. The advantage of TDD is that the test is seen to fail before the code is written, and the code is then written to make the test pass. I&#8217;m not following a TDD approach in these entries because the applications I&#8217;m testing have already been written.</p>
<p>In my <a href="http://blogs.adobe.com/paulw/archives/2008/04/unit_testing_us_1.html">next entry</a> I&#8217;ll take a look at unit testing my Presentation Model example. </p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.adobe.com/paulw/archives/2008/03/unit_testing_us.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Presentation Patterns &#8211; Conclusions</title>
		<link>http://blogs.adobe.com/paulw/archives/2008/02/presentation_pa_7.html</link>
		<comments>http://blogs.adobe.com/paulw/archives/2008/02/presentation_pa_7.html#comments</comments>
		<pubDate>Sun, 03 Feb 2008 23:55:17 +0000</pubDate>
		<dc:creator>Paul Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blogs.adobe.com/paulw/2008/02/presentation_pa_7.html</guid>
		<description><![CDATA[
 <a href="http://blogs.adobe.com/paulw/archives/2008/02/presentation_pa_7.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Towards the end of last year I took a look at six contrasting patterns for  organising presentation logic. Some of these patterns are  well known and popular in the Flex community, others are very rarely discussed.  If you know of any other presentation patterns that you feel are worth looking  at more closely it would be great to hear about them. </p>
<p><span id="more-9"></span></p>
<h2>Why did I want to do this? </h2>
<h2><em>Establish  a base-line of terminology for future discussions </em></h2>
<p>Over  time, terms like View Helper and Code Behind have become somewhat vague.  Discussions involving them often go awry because participants do not have a  common understanding of these concepts. Having a &quot;pattern language&quot;  is beneficial when investigating architecture, but only if the patterns are  unambiguously defined. So in future discussions I want to be able to link back  to this series if I happen to mention a presentation pattern in passing.</p>
<h2><em>Increase  my own understanding</em></h2>
<p>Some of  the patterns in this discussion have been applied on consulting projects past  and present, while others are architectural curiosities that have piqued my  interest. Building a simple application with each one has helped me to come to  a better understanding of how they contrast. Before starting to develop the  <a href="http://blogs.adobe.com/paulw/archives/2007/10/presentation_pa_2.html">Supervising Presenter</a> I thought it was very similar to the <a href="http://blogs.adobe.com/paulw/archives/2007/11/presentation_pa_4.html">View Helper</a>, but it  turned out to be quite different. And despite never being entirely comfortable  with the <a href="http://blogs.adobe.com/paulw/archives/2007/11/presentation_pa_5.html">Code Behind</a> pattern, it wasn&#8217;t until I saw it side-by-side with  patterns like <a href="http://blogs.adobe.com/paulw/archives/2007/11/presentation_pa_6.html">Presentation Model</a> and <a href="http://blogs.adobe.com/paulw/archives/2007/11/presentation-pa-6.html">Passive View</a> that I started to understand  the benefits of a more compositional approach.</p>
<h2><em>Explore  the possibilities with Flex </em></h2>
<p>Knowing  about architectural possibilities is only useful if your target platform is  able to support them. Flex has proved itself more than capable of supporting  all six of these contrasting patterns. Having said that, some of the patterns  do make better use of Flex&#8217;s features than others. Both the Presentation Model  and Supervising Presenter combine extract-class refactorings with elegant use  of MXML bindings and declarative event handlers. On the hand, Passive View and  Code Behind sacrifice these features in order to achieve a greater degree of  ActionScript / MXML separation. </p>
<h2>What  Next?</h2>
<h2><em>Take a  closer look at unit testing </em></h2>
<p>Although  I talked about unit testing throughout these discussions, you may have noticed  that I didn&#8217;t actually do any. The very need to unit test could make or break a  pattern, and can become a driving force for architectural decisions.  Furthermore, the subject of unit testing is broad enough and important enough  to warrant a series of blog entries to itself. So <a href="http://blogs.adobe.com/paulw/archives/2008/03/unit_testing_us.html">my next job</a> is to look at how amenable some of these patterns are to unit testing. </p>
<h2><em>Further  exploration of presentation patterns </em></h2>
<p>I kept  the example application for this series very simple because I knew I would have  to implement it six times, this is why many aspects of presentation behaviour  mentioned in the <a href="http://blogs.adobe.com/paulw/archives/2007/09/presentation_pa.html">introduction</a> were not demonstrated. I can see real advantages of moving non-graphical concerns such as navigation, deep-linking and localisation out of the view, so I&#8217;d like to take a closer look at this in the future. </p>
<h2><em>Take a look at integrations patterns</em> </h2>
<p>In RIA  development projects, finding a way to organise your presentation logic in a  way that meets your objectives is a great step forward, but not the only  challenge. The way in which your application interacts with remote services is  also extremely important. So what are the common integration patterns in the  Flex world?</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.adobe.com/paulw/archives/2008/02/presentation_pa_7.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Presentation Patterns &#8211; Passive View</title>
		<link>http://blogs.adobe.com/paulw/archives/2007/11/presentation_pa_6.html</link>
		<comments>http://blogs.adobe.com/paulw/archives/2007/11/presentation_pa_6.html#comments</comments>
		<pubDate>Thu, 29 Nov 2007 07:20:21 +0000</pubDate>
		<dc:creator>Paul Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blogs.adobe.com/paulw/2007/11/presentation_pa_6.html</guid>
		<description><![CDATA[
 <a href="http://blogs.adobe.com/paulw/archives/2007/11/presentation_pa_6.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Like the <a href="http://blogs.adobe.com/paulw/archives/2007/10/presentation_pa_2.html">Supervising Presenter</a>, the Passive View pattern is a derivative of <a href="http://martinfowler.com/eaaDev/ModelViewPresenter.html" target="_blank">Model View Presenter</a> (MVP). Martin Fowler split MVP into separate patterns to highlight two common approaches for realising MVP. </p>
<p>The Passive View pattern also has some similarities with <a href="http://blogs.adobe.com/paulw/archives/2007/11/presentation_pa_5.html">Code Behind</a> since both  achieve a complete separation of ActionScript and MXML.</p>
<p><span id="more-10"></span></p>
<h2>Key Features</h2>
<ul>
<li>State is in the view </li>
<li>Logic is in the Presenter </li>
<li>Presenter observes the view </li>
<li>Presenter updates the view </li>
<li>Presenter &lsquo;knows&rsquo; about the view </li>
<li>View does not &lsquo;know&rsquo; about the Presenter </li>
</ul>
<p>The key features of  Passive View are identical to those of Supervising Presenter. The difference between the two patterns is the degree to which logic is extracted from the view. Views in a Supervising Presenter application are allowed to &#8216;observe&#8217; domain or value objects in order to display information, but  views in a Passive View application are managed entirely by their presenter object. </p>
<p>The Supervising Presenter pattern lends itself to User Interface frameworks that have synchronisation features (eg. bindings in Flex), by allowing controls in the view to synchronise directly with underlying domain or value objects. The Passive View pattern prohibits this kind of behaviour in the view, so synchronisation between the view and the data model is managed by the presenter class instead.</p>
<h2>Motivations</h2>
<p>The motivations for the Passive View are similar to those of the Supervising Presenter and <a href="http://blogs.adobe.com/paulw/archives/2007/10/presentation_pa_3.html">Presentation Model</a>:</p>
<h2><em>Easier to test</em></h2>
<p>By moving all behaviour out of the view, the Passive View pattern is likely to afford the maximum unit test coverage of all the patterns I&#8217;ve looked at. The presenter class has a reference back to the view; so mock objects will be required to test it. </p>
<h2><em>Improved Separation of Concerns</em></h2>
<p>As with the other extract class refactorings, the Passive View pattern can help improve the separation of concerns in an application. By favouring composition over inheritance, Passive View achieves a greater degree of decoupling from the Flex framework than the Code Behind pattern. Presenter classes do not need to extend from Flex containers.</p>
<h2><em>Language Segregation</em></h2>
<p>By moving logic out of the view, the Passive View pattern can achieve similar developer / designer workflow objectives as Code Behind. The resulting view class is free from event handler specifications, binding statements and script blocks.</p>
<h2>Issues</h2>
<p>I think all of the issues identified for Supervising Presenter also apply to the Passive View. Rather than repeating myself, I&#8217;ll just refer you <a href="http://blogs.adobe.com/paulw/archives/2007/10/presentation_pa_2.html">back to that discussion</a>.</p>
<h2><em>Reduced role for MXML</em></h2>
<p>As I suggested when discussing the Code Behind pattern, I don&#8217;t believe MXML is <em>just</em> for designers. Although it is primarily targetted at describing the look of an application, it also offers features that support developers. Keeping MXML for designers means developers do not have access to MXML bindings and declarative event handler specifications. This isn&#8217;t the end of the world and developers will adapt, but it does mean that some of Flex&#8217;s more powerful features are no longer being used. </p>
<p><em><strong>What about declarative behaviour</strong></em>?</p>
<p>Some presentation behaviour can be achieved  declaratively in MXML;  effects, transitions and validators are all examples. Whether Passive View permits this kind of declarative logic in the view isn&#8217;t clear. It is probably safe to leave graphical behaviour in the view  and drive state changes from the presenter if necessary. Non-graphical behaviour such as validation is probably best moved into the presenter class since it is likely to be more closely related to the underlying data model.</p>
<h2>Example application</h2>
<p> The <a href="http://examples.pmwilliams.co.uk/adobeblog/presentationpatterns/passiveview/PassiveView.html" target="_blank">example application</a> demonstrates the Passive View pattern; right-click to access the source. </p>
<h2>Final thoughts </h2>
<p>The Passive View pattern goes a step further than Supervising Presenter by placing additional constraints on the autonomy of the view. But what interests me most about Passive View is the way in which it achieves similar goals to the Code Behind pattern through the use of composition rather than inheritance. For me this is a reminder that there is usually more than one way to solve a particular problem, and that the popular or mainsteam approach may not always be the best option.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.adobe.com/paulw/archives/2007/11/presentation_pa_6.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Presentation Patterns &#8211; Code Behind</title>
		<link>http://blogs.adobe.com/paulw/archives/2007/11/presentation_pa_5.html</link>
		<comments>http://blogs.adobe.com/paulw/archives/2007/11/presentation_pa_5.html#comments</comments>
		<pubDate>Thu, 15 Nov 2007 07:04:38 +0000</pubDate>
		<dc:creator>Paul Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blogs.adobe.com/paulw/2007/11/presentation_pa_5.html</guid>
		<description><![CDATA[
 <a href="http://blogs.adobe.com/paulw/archives/2007/11/presentation_pa_5.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Code Behind is a somewhat vague term that means different things to different people. I believe it was originally <a href="http://support.microsoft.com/kb/303247" target="_blank">coined by Microsoft</a> to describe any strategy for separating logic from content in ASP pages. Using this broad definition it could be argued that the <a href="http://blogs.adobe.com/paulw/archives/2007/10/presentation_pa_2.html">Supervising Presenter</a>, <a href="http://blogs.adobe.com/paulw/archives/2007/10/presentation_pa_3.html">Presentation Model</a> and even the <a href="http://blogs.adobe.com/paulw/archives/2007/11/presentation_pa_4.html">View Helper</a> are all examples of the technique. So this interpretation of code-behind lacks the sort of precision we require to discuss it as a pattern. </p>
<p> However, one of the most common methods for achieving code-behind in ASP and .NET uses inheritance: Presentation logic is extracted out of the view class, and into the view&#8217;s superclass. The technique has been brought over to the Flex world and is arguably one of the most talked-about and advocated presentation patterns. It is this &quot;code-behind pattern&quot; that I am going to look at today. </p>
<p><span id="more-11"></span></p>
<h2>Key features </h2>
<ul>
<li>Logic (and other &quot;developer&quot; stuff) is in an ActionScript superclass </li>
<li>Layout (and other &quot;designer&quot; stuff) is in an MXML subclass </li>
</ul>
<h2>Motivations</h2>
<p><strong><em>Language Segregation</em></strong></p>
<p>I think the primary motivation for the Code Behind pattern is the segregation of ActionScript from MXML in order to support a developer/designer workflow. The precise division of responsibilities between ActionScript and MXML varies across implementations. Event handler specifications are usually extracted into the ActionScript class, but there seems to be some difference of opinion on whether binding statements can remain in MXML. It is also unclear whether designers  can be expected to configure declarative validation in the MXML file. In my example I have extracted event handlers, binding statements and validation into the code-behind class. </p>
<p><strong><em>Unit Testing</em></strong></p>
<p>I&#8217;ve not seen much discussion of unit testing Code Behind classes, but I suspect they may be more &#8216;testable&#8217; than equivalent <a href="http://blogs.adobe.com/paulw/archives/2007/09/presentation_pa_1.html">Autonomous Views</a>. </p>
<p>Code-behind classes contain references to  sub-views; so unit testing them is likely to require mock objects. Since instances of sub-views are instantiated by the MXML subclass, it will be necessary to find a way to inject suitable mock instances when unit testing the base-class.</p>
<p>In addition to having references to sub-views, code-behind classes also contain references to UI controls. Since Flex controls do not implement interfaces, it is unclear to me how they could be mocked in the traditional sense. I think further investigation is required to understand the issues here. </p>
<h2>Issues</h2>
<p><strong><em>Why extract superclass?</em></strong></p>
<p>With the exception of the Autonomous View pattern, all of the patterns I&#8217;ve looked at so far attempt to extract logic from view classes into a  separate set of ActionScript classes. This kind of refactoring is often referred to as &#8216;extract class&#8217;. The Code Behind pattern is unique in that it attempts to extract logic into a superclass, a variety of refactoring  known (somewhat unsurprisingly) as &#8216;extract superclass&#8217;. Generally speaking the extract superclass refactoring is applied to move functionality shared by two classes into a base class in order to reduce code duplication. But in the case of Code Behind, we only have one view class. So why use extract superclass?</p>
<p><strong><em>Poor separation of concerns</em></strong></p>
<p>By performing an extract superclass refactoring, the resulting ActionScript class is still very likely to extend one of the Flex framework components such as HBox, VBox, or Panel. So it is difficult to see any real separation of concerns. As with the Autonomous View pattern, the layout concern is still central to the class hierarchy and code-behind classes are unlikely to have a common editable base class. </p>
<p><strong><em>Tight coupling</em></strong></p>
<p>The two classes resulting from the code behind pattern are very tightly coupled. An inheritance relationship is arguably the most intimate of associations. In advanced architectures it may be desirable to have more than one logic class for each view (for example to provide different view behaviour for different user roles); this isn&#8217;t possible when the view extends the logic class.</p>
<p><strong><em>Is MXML only for designers?</em></strong></p>
<p>Perhaps I&#8217;m being selfish, but there are two very useful features of Flex that are only available in MXML:</p>
<ul>
<li>Bindings with curly braces &#8216;{}&#8217; </li>
<li>Declarative event handlers</li>
</ul>
<p>Whilst I could probably live without these two features, I&#8217;d prefer not to.</p>
<h2>Example application</h2>
<p> The <a href="http://examples.pmwilliams.co.uk/adobeblog/presentationpatterns/codebehind/CodeBehind.html" target="_blank">example application</a> demonstrates the Code Behind pattern; right-click to access the source. </p>
<h2>Final thoughts</h2>
<p>It isn&#8217;t clear to me why the Code Behind pattern favours inheritance over composition, and I think this is the main drawback to the approach. Although logic is extracted out of the view, the resulting code-behind class is still tightly coupled to the Flex framework. Furthermore by having the view extend the code-behind class, the view becomes very tightly coupled to the logic.</p>
<p>By favouring composition over inheritance, the Supervising Presenter and Presentation Model patterns offer improved separation of concerns and consequently greater flexibility. Neither of these patterns achieves the same degree of separation between MXML and ActionScript as the Code Behind  pattern, but the next pattern I&#8217;m going to look at certainly does. It&#8217;s another one of Martin Fowler&#8217;s and it&#8217;s called Passive View. </p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.adobe.com/paulw/archives/2007/11/presentation_pa_5.html/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Presentation Patterns &#8211; View Helper</title>
		<link>http://blogs.adobe.com/paulw/archives/2007/11/presentation_pa_4.html</link>
		<comments>http://blogs.adobe.com/paulw/archives/2007/11/presentation_pa_4.html#comments</comments>
		<pubDate>Mon, 05 Nov 2007 09:32:28 +0000</pubDate>
		<dc:creator>Paul Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blogs.adobe.com/paulw/2007/11/presentation_pa_4.html</guid>
		<description><![CDATA[
 <a href="http://blogs.adobe.com/paulw/archives/2007/11/presentation_pa_4.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>While looking at the <a href="http://blogs.adobe.com/paulw/archives/2007/10/presentation_pa_2.html">Supervising Presenter</a> and <a href="http://blogs.adobe.com/paulw/archives/2007/10/presentation_pa_3.html">Presentation Model</a> patterns it occurred to me that some people may find these concepts reminiscent of the View Helper pattern that enjoyed a spell of popularity in some Cairngorm architectures. Since the View Helper is now regarded as &#8216;not recommended&#8217;, I want to take a closer look at the pattern and try to understand why it may have fallen from favour.</p>
<p><span id="more-12"></span></p>
<p>The View Helper is loosely based on a similarly named <a href="http://java.sun.com/blueprints/corej2eepatterns/Patterns/ViewHelper.html" target="_blank">J2EE pattern</a>, but the purpose and realisation of the pattern within Flex applications is somewhat different to its server-side counterpart.</p>
<h2>Key features </h2>
<ul>
<li>State is (mainly) in the view</li>
<li>Logic is (mainly) in the view helper </li>
<li>The view &#8216;knows&#8217; about the view helper</li>
<li>The view helper &#8216;knows&#8217; about the view</li>
</ul>
<p>As with Supervising Presenter and Presentation Model, the  View Helper pattern extracts logic from view components into ActionScript classes. </p>
<h2>Motivations</h2>
<p>Tracking down the original motivations for the View Helper pattern is more an exercise in historical research than technical investigation, but here are a  few possibilities:</p>
<p><strong><em>Mediation</em></strong></p>
<p><a href="http://blogs.adobe.com/swebster/" target="_blank">Steven</a> tells me the View Helper has its roots in RIA development using Flash. It was introduced to act as a <a href="http://en.wikipedia.org/wiki/Mediator_pattern" target="_blank">mediator</a> for views in an application. Back in those days, views were constructed as a hierarchy of movie clips. For these movie clips to find each other and collaborate directly they would need to know quite a lot about the structure of the view. Rather than duplicating this &#8216;knowledge&#8217; around the view hierarchy, it was encapsulated within the view helper class. So when the structure of the view changed, only the view helper needed to be updated.</p>
<p>The problem the View Helper pattern addresses in Flash is less of a problem in Flex, because of the &quot;flattening&quot; effect composite view components have on the view hierarchy. But the mediator pattern is certainly relevant to Flex applications: both the View Helper and Supervising Presenter patterns perform mediation between UI components. </p>
<p><strong><em>Language Segregation</em></strong></p>
<p>Another justification for the View Helper pattern is to achieve a segregation of MXML and ActionScript. For some, this is to enhance readability or tidyness of the code, and for others it is to support some kind of designer / developer workflow.</p>
<p><strong><em>Decoupling</em></strong></p>
<p>In the days before the miracle of Flex bindings and the omniscient ModelLocator, it was necessary for Cairngorm commands to directly update views with the results of remote operations. In order to avoid coupling the commands to the views, the View Helper was used as an intermediary. The View Locator pattern was also employed to allow the view helpers to be &#8216;located&#8217; by the commands.</p>
<p><strong><em>Unit testing </em></strong></p>
<p>As we&#8217;ve seen with two of our previous patterns (Supervising Presenter and Presentation Model), one objective is improved &#8216;testability&#8217;. From the perspective of the view helper, the reference back to the view will cause the same unit testing difficulties as apply to the Supervising Presenter. Dependencies in this direction could be loosened by having the view implement an interface, and coupling the view helper to the interface instead of  the concrete view class. This would allow testing of the view helper with mock objects.</p>
<h2>Issues</h2>
<p><strong><em>Libraries instead of classes</em></strong></p>
<p>View Helpers are usually little more than  ActionScript libraries for their corresponding views. In most cases they don&#8217;t form their own structure of collaborating classes or exist within a broader class hierarchy, and they cannot be really described as an architectural layer. Each  one is just a file in which to hide away the logic for a particular view.</p>
<p><strong><em>View helpers are lonely </em></strong></p>
<p>If you take a look at the class diagram for the demo application, you may notice there are no direct associations between view helpers. So although the logic has been extracted from the view into a separate class, this new class must still rely on the view in order to collaborate with other classes. The lack of associations between view helpers severely limits their value as a means to separate concerns. This caused me some problems when coding the example, and I found myself with awkward statements like:</p>
<blockquote>
<p> view.form.helper.album = album;</p>
</blockquote>
<p>One way to avoid this issue would be to move some of the &#8216;setter&#8217; logic back into the view. Another approach would be to introduce a singleton that would allow view helpers to be &#8216;located&#8217; by other view helpers. Neither option is particularly satisfactory.</p>
<p><strong><em>View creates the helper</em></strong></p>
<p>Should the view be creating the helper? Giving this responsibility to the view reinforces the notion that the view is still in charge, and the helper is a subordinate. I tried to avoid this for the Supervising Presenter and Presentation Model patterns, favouring injection instead.</p>
<h2>Example application</h2>
<p>The <a href="http://examples.pmwilliams.co.uk/adobeblog/presentationpatterns/viewhelper/ViewHelper.html" target="_blank">example application</a> demonstrates the View Helper pattern; right-click to access the source. </p>
<h2>Final thoughts</h2>
<p>The View Helper pattern isn&#8217;t a great distance from either of the Supervising Presenter or the Presentation Model patterns discussed previously. But it doesn&#8217;t seem to achieve the same benefits, and this may be because the motivations  for the pattern have been forgotten or have gradually lost their relevance. As a mediator, the View Helper does not achieve the same separation of concerns as the Supervising Presenter: there is a bi-directional coupling between the view and the helper, and the helper must rely on the view in order to find and collaborate with other view helpers. By improving testability, the View Helper could be regarded as a step in the right direction, but as a refactoring it just didn&#8217;t quite go far enough. </p>
<p>Next up: <a href="http://blogs.adobe.com/paulw/archives/2007/11/presentation_pa_5.html">Code Behind</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.adobe.com/paulw/archives/2007/11/presentation_pa_4.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Presentation Patterns &#8211; Presentation Model</title>
		<link>http://blogs.adobe.com/paulw/archives/2007/10/presentation_pa_3.html</link>
		<comments>http://blogs.adobe.com/paulw/archives/2007/10/presentation_pa_3.html#comments</comments>
		<pubDate>Mon, 22 Oct 2007 08:15:09 +0000</pubDate>
		<dc:creator>Paul Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blogs.adobe.com/paulw/2007/10/presentation_pa_3.html</guid>
		<description><![CDATA[
 <a href="http://blogs.adobe.com/paulw/archives/2007/10/presentation_pa_3.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>The next pattern I want to look at goes a step further than the <a href="http://blogs.adobe.com/paulw/archives/2007/10/presentation_pa_2.html">Supervising Presenter</a> by moving logic <em>and</em> state out of the view. Once again discussed by <a href="http://martinfowler.com/eaaDev/PresentationModel.html">Martin Fowler</a>, the Presentation Model has its roots in the Model-View-Controller (MVC) pattern. </p>
<p>A presentation model is an &lsquo;abstraction&rsquo; of a view. In other words, the model represents the behaviour and state of a view, without getting involved with any of that messy graphical stuff. The view represents a projection or rendering of the model onto the screen. When changes occur in the presentation model, the view updates itself on-screen.</p>
<p><span id="more-13"></span></p>
<p><em>Martin Fowler describes two variants of the Presentation Model, one in which the view observes the model, and another in which the model observes the view. I will be looking at the former in this discussion because in Flex it is far easier to set-up observer relationships from MXML views to ActionScript models.</em> </p>
<h2>Key Features</h2>
<ul>
<li>State is in the presentation model</li>
<li>Logic is in the presentation model</li>
<li>View observes the model and updates accordingly</li>
<li>The view &ldquo;knows&rdquo; about the presentation model</li>
<li>The presentation model does not &ldquo;know&rdquo; about the view</li>
</ul>
<p>Model-view synchronization is usually achieved by applying the observer pattern, and in Flex this can be realised with MXML bindings. Although the view &lsquo;knows&rsquo; about the underlying model, the model is unaware of the view; compare this with the Supervising Presenter, where the opposite applies.</p>
<p>Furthermore, in a Presentation Model application, view state and logic are both extracted from the view into the model. Again this is in contrast to the Supervising Presenter, in which only logic is moved out of the view; and the Autonomous View, in which logic and state remain in the view.</p>
<h2>Motivations </h2>
<p>As with the Supervising Presenter, the motivation behind the Presentation Model is to address issues with the Autonomous View approach:</p>
<p><strong><em>Testability</em></strong></p>
<p>Since Presentation Model classes are fully decoupled from the view, the logic they contain is much easier to test. This puts the Presentation Model ahead of the Supervising Presenter, since the latter has a reference back to the view. Writing FlexUnit tests for presentation models should be no more difficult than for other kinds of model classes.</p>
<p><strong><em>Improved separation of concerns</em></strong></p>
<p>If each presentation model represents an abstraction of a view, then together they can be thought of as a model for an entire user interface. Common presentation behaviour can be refactored into  base classes and shared throughout the model. I can see enormous potential here, which I hope to explore in the future. </p>
<h2>Issues </h2>
<p><strong><em>How much logic and state should be extracted?</em></strong></p>
<p>As much as possible. Most non-graphical presentation concerns are candidates for extraction into the model. Presentation concerns that have a strong graphical component (such as animation) and concerns that are closely coupled to the underlying framework (such as drag-and-drop) may be best left in the view. Coding this logic into the model would require too much knowledge of screen size, layout and the kinds of UI widgets being used.</p>
<p><strong><em>Some logic is likely to remain in the view</em></strong></p>
<p>As mentioned above, logic that is directly coupled to graphical concerns will need to stay in the view. The Supervising Presenter does not have this restriction, because it is allowed to &lsquo;know&rsquo; about the view. </p>
<p>The view also needs to be &#8216;smart&#8217; enough to know which methods to call on the presentation model in response to input events. </p>
<h2>Example application</h2>
<p>The <a href="http://examples.pmwilliams.co.uk/adobeblog/presentationpatterns/presentationmodel/PresentationModel.html" target="_blank">example application</a> demonstrates the Presentation Model pattern; right-click to access the source. </p>
<h2>Final thoughts </h2>
<p>Both the Supervising Presenter and Presentation Model allow you to create a separate class hierarchy for your application-specific behaviour. The former uses the view to store UI state, the latter places the state in the same class as the logic. By being decoupled from the view, the Presentation Model pattern offers more convenient unit testing and improved decoupling from the view. However, the model&#8217;s total ignorance of the view means that some of the more &#8216;graphical&#8217; behaviour cannot be extracted.</p>
<p>Up next: <a href="http://blogs.adobe.com/paulw/archives/2007/11/presentation_pa_4.html">View Helper</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.adobe.com/paulw/archives/2007/10/presentation_pa_3.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Presentation Patterns &#8211; Supervising Presenter</title>
		<link>http://blogs.adobe.com/paulw/archives/2007/10/presentation_pa_2.html</link>
		<comments>http://blogs.adobe.com/paulw/archives/2007/10/presentation_pa_2.html#comments</comments>
		<pubDate>Sat, 13 Oct 2007 09:10:52 +0000</pubDate>
		<dc:creator>Paul Williams</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blogs.adobe.com/paulw/2007/10/presentation_pa_2.html</guid>
		<description><![CDATA[
 <a href="http://blogs.adobe.com/paulw/archives/2007/10/presentation_pa_2.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Like the <a href="http://blogs.adobe.com/paulw/archives/2007/09/presentation_pa_1.html">Autonomous View</a>, this is another one of <a href="http://martinfowler.com/eaaDev/SupervisingPresenter.html" target="_blank">Martin Fowler&#8217;s patterns</a>. He calls it Supervising Controller, but suggests the alternative name of Supervising Presenter. I&#8217;m voting for the latter for the following reasons:</p>
<ul>
<li>This pattern is a derivative of the Model-View-Presenter pattern rather than Model-View-Controller, so &#8216;Supervising Presenter&#8217; seems a more suitable name.</li>
<li>Due to excessive overloading, the meaning of the term &#8216;controller&#8217; in UI architecture discussions has become somewhat vague.</li>
<li>The &#8216;Presenter&#8217; object is neither a controller in the J2EE / <a href="http://en.wikipedia.org/wiki/Model_2" target="_blank">Model-2 </a>/ Cairngorm sense of the word, nor does it have much in common with the controllers from the original <a href="http://www.ccmrc.ucsb.edu/~stp/PostScript/mvc.pdf" target="_blank">Smalltalk-MVC</a> architecture.</li>
</ul>
<p><span id="more-14"></span></p>
<h2><em>Key features</em></h2>
<ul>
<li>State is in the view</li>
<li>Logic is in the Presenter</li>
<li>Presenter observes the view</li>
<li>Presenter updates the view</li>
<li>Presenter &lsquo;knows&rsquo; about the view</li>
<li>View does not &lsquo;know&rsquo; about the Presenter</li>
</ul>
<p>Unlike the Autonomous View, the Supervising Presenter aims to extract non-trivial presentation logic out of the view into a separate class known as a Presenter. The view retains some autonomy, and controls in the view can still bind directly to underlying domain or value objects.</p>
<p>One very important feature of the Supervising Presenter pattern is that the view <em>is unaware</em> of its associated Presenter, but the Presenter <em>is aware</em> of the view. This is a trait inherited from Model-View-Presenter and is a principal difference between MVP and MVC. Each Supervising Presenter must observe its associated view for user events and coordinate the necessary view / model updates in response.</p>
<h2><em>Motivations</em></h2>
<p>Two reasons for adopting the Supervising Presenter pattern would be to address the drawbacks of the Autonomous View pattern:</p>
<p><strong>Easier to test</strong></p>
<p>Separating complex presentation logic into a separate class should facilitate unit testing. But there is one big fly in the ointment: The Supervising Presenter has a dependency on the associated view. So to test a Supervising Presenter you either need instances of your actual views, or &#8216;mock&#8217; objects that simulate the behaviour of the views. If you use your actual view components, you&rsquo;ll probably experience the same unit testing problems as you would with an Autonomous View</p>
<p><strong>Improved separation of concerns</strong></p>
<p>Following the Supervising Presenter pattern should yield an application-specific class hierarchy that is separated from the view classes, but coupled to them. Common presentation concerns can be refactored into Presenter base classes, and this should help to reduce code-duplication and improve consistency across an application.</p>
<h2><em>Issues</em></h2>
<p><strong>How much logic do we extract?</strong></p>
<p>The question of how much &#8216;supervision&#8217; a view needs is difficult to answer. The Supervising Presenter pattern exists half-way between Autonomous View (which we have already seen) and a pattern known as <a href="http://martinfowler.com/eaaDev/PassiveScreen.html" target="_blank">Passive View</a>. The Passive View pattern extracts all logic from the view into a Presenter class, including bindings. For my own example, I have extracted any logic that cannot be expressed by a simple binding statement. I have also left the validators in the view, as they can be configured declaratively. But the lack of clarity on how much logic to extract may lead to inconsistencies on larger development projects.</p>
<p><strong>Mock objects? What mock objects?</strong></p>
<p>The use of <a href="http://en.wikipedia.org/wiki/Mock_object" target="_blank">mock objects</a> is a common strategy when unit testing, but creating mock objects to simulate Flex views may not be a trivial task. Then again, once it is done the resulting mock-object library could be reused on other projects.</p>
<p><strong>Logic is extracted but &#8216;state&#8217; remains in the view</strong></p>
<p>The Supervising Presenter pattern extracts presentation logic from the view, but leaves presentation state in the view. A purist may argue that this is only a partial refactoring: In an OO architecture, state and associated logic are usually encapsulated within the same class. Dogma perhaps, but in my experience strong encapsulation does facilitate refactoring and reuse.</p>
<h2><em>Example application</em></h2>
<p>The <a href="http://examples.pmwilliams.co.uk/adobeblog/presentationpatterns/supervisingpresenter/SupervisingPresenter.html" target="_blank">example application</a> demonstrates my take on the Supervising Presenter pattern. As with the Autonomous View demo, there&#8217;s additional information accessible via the View Source option. </p>
<h2><em>Final thoughts</em></h2>
<p>Despite the issues described above, I do think the Supervising Presenter deserves more attention. It holds the promise of improved unit test coverage, and may help to decrease code-duplication in an application. So far I haven&rsquo;t encountered a Flex application that uses the Supervising Presenter pattern, so I guess my example must be the first. Perhaps you know different.</p>
<p>Up next: <a href="http://blogs.adobe.com/paulw/archives/2007/10/presentation_pa_3.cfm">Presentation Model</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.adobe.com/paulw/archives/2007/10/presentation_pa_2.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
