Recently I've started refactoring a Cairngorm based Flex app that I have inherited. The reason I'm doing this refactoring is because there is a lot of functionality mixed in with the presentation. This mix of MXML and ActionScript reminds me of my old ColdFusion days when HTML was mixed in with CFQuery tags, something I never felt confortable with.
So as part of this refactoring I'm incorporating the Presentation Model pattern, the presentation model has other names, like MVVM from the Silverlight world or Mediation pattern or the Application Model.
The main idea behind the pattern is that you Presentation Model class sits between the application model and the view and it separates the UI from the view behaviour. So the UI does not manage its state or behaviour, this is all handled by the presentation model which simply updates the UI.
In my refactoring work I've created a new parent Presentation Model (PM) class that will hold any common functionality between my PM classes. From this I've created a PM for the view I'm refactoring, in here I'm basically taking out all the ActionScript code that is in the original MXML UI and adding it to the PM class.
This doesn't work straight away there are some design issues I had to think about, one of the main changes I had to do was create a set of flags that are switched on and off in the PM. These flags are used to show or hide components in the UI, usually by set the visible property of the component. Another change is linking all the event calls in the UI to events now in the PM.
So far this is going well, its tidying the code, allowing me to make the code more readable, find areas of common functionality that should be moved up to the parent level.
It will also allow me to add Unit Testing to an application which currently doesn't use testing. So when I add new functionality to the application I can test the entire application.
One of the reasons I decided to start this refactoring work was due to an article on the RIARockstars website, the article was on sharing the one code base between multiple screens.
This is a great article, really shows how to create multiple versions of an application using the one code base. While it's not using Cairngorm the Presentation Model can be used by other frameworks (Robotlegs for example, my favourite framework, though it uses the Mediator Pattern).
Anyway I like the separation that the Presentation Model pattern give and will continue refactoring this app so that it uses this model on all views.
No comments:
Post a Comment