I’m writing a bit of code using PureMVC, so I thought I would share this experience – gone all my merry way doing just that.
In this article, I’m covering basic separation principles. Then I’m introducing MVC. I describe PureMVC, explain what it’s about in fairly abstract terms, and try to help you figure out whether it’s worth your while.
If you’ve never seen PureMVC code, I’m afraid you’ll have to look elsewhere (especially if you’re into ‘code makes more sense than theory’).
First things first
MVC is a meta-pattern mainly used in developing applications and application components. MVC separates applications into three tiers: Model, View and Controller. The model is your data, the view represents your widgets or components, and the controller determines how the model and view affect each other.
Separation
Like with many architectural patterns, separation is essential to MVC. There are many ways into which you might either need or wish to ‘separate’ application tiers:
- Logistic separation: concurrent tiers may be deployed on different computers.
- Physical separation: concurrent tiers may be separately bundled – you store the source code for each tier in different folders.
- Logical separation: concurrent tiers can be compiled independantly from each other.
- Runtime separation: at runtime, concurrent tiers reside and hold resources in separate areas of memory, and may run asynchronously
- Socio-Professional separation: different people (or different companies) each write a part of the same application
- Conceptual separation: concurrent tiers use concepts from different domains
- Cybernetic separation: there is less communication across tiers than there is within any given application tier.
Depending on where you stand, you may have no choice – for example you may need to keep a database on a server and present information in a web browser – or you may simply wish to keep things separate in some way or other for the sake of clarity.
In the meantime, we’ve already agreed that MVC is mainly for writing applications. This surely implies that application tiers, however separate, need to communicate. It is useful to remember that, although you can achieve absolute separation in some respects (e.g., Physical) you can never (nor should you want to) achieve complete cybernetic or conceptual separation – all components within an application contribute towards the same purpose (business logic) and in order to do so, they will communicate – except to the extent that digital communications are replaced by human communication.
Maybe another, essential consideration is that separation is flexibility at a cost. Such cost will generally include additional design efforts as well as areduction in quality and/or efficiency. Also, you will incur this cost while you are writing and maintaining your application, not just while deciding how components should be separated.
MVC as a Meta-Pattern
MVC is a high level meta-pattern based on a special form of conceptual separation. The idea is that the ‘factual/transactional’ aspects of your application are held on one side, while the ‘visual/interactive’ aspects are held at another side. Another layer (Controller) mediates between the two.
You bet such a high level, conceptual degree of separation would be open to debate and interpretation – how about the age old debate between form and content? For this reason, and because there are least 6 other ways to separate your applications, MVC is not a software pattern, just a meta-pattern.
This means that you’ll likely run in trouble if you just ‘try to use it’ without a little more design and preparation.
This also means that there are many ways to define an ‘instance’ of MVC. Such instances include both strategies and frameworks; PureMVC is an MVC framework – on this note, don’t expect PureMVC to resolve all your problems – depending on what you are trying to do, PureMVC will either be supportive, or unsupportive.
What is PureMVC?
PureMVC is two things:
- A flexible, comprehensive framework ported across several languages.
- An open source software product, developed, supported and documented and promoted by a company.
From the above, there are immediate consequences:
- Flexibility means that PureMVC can be supportive and unsupportive in a variety of ways – Supportive because it can be tailored to some of your needs; unsupportive because not all the design effort involved in defining a framework has been done for you, and you’ll get hurt if you don’t finish the job.
- Comprehensive means that PureMVC covers, at framework level, most of the aspects involved in instanciating the MVC meta-pattern. This means that development using PureMVC is less error prone (than if it relied on more conventions) and results in code that often lacks concision and elegance.
- Because PureMVC has been designed to be portable, it relies on a low common denominator between several languages. For this reason, it is probably a good investment if you’re navigating between languages and somehow despicable in consideration of facilities offered by specific languages.
- Because PureMVC is actively promoted by a profit driven organization, it is hyped and tends to make promises that it doesn’t always fulfill. Business orientation may also be the reason why the PureMVC documentation combines sophisticated, obfuscated terminology with lengthy, uninformative explanations. So far, this may have proved to be a fair marketing choice, because PureMVC seems increasingly successful – which in the end benefits all its users.
- Because PureMVC is open source, you can modify it in some ways and make it more suited to your needs if you really have to.
OK, but what is it?
PureMVC is a ’5 tier 3 tier framework with a facade’:
- Proxies are your model objects.
- Mediators are your view objects.
- Commands realise your controller as a collection of short lived, single responsibility classes (first good thing)
- Components are platform level, view side delegates typically shipping as part of a UI toolkit (Swing, Flash components etc…) and independant from PureMVC.
- Data objects are… …just data. They are also independant from PureMVC and originate either in your efforts at modelling your business domain or into an API provided by a third party. Data objects are, like proxies, part of your model.
- The Facade is a kind of registry/communication interface between your model, view and controller.
Proxies, Mediators and Commands communicate in two ways:
- Notifications. These are (potentially) heavyweight messages holding untyped content transiting via the facade. Basically you wrap data into an instance of an IMessage interface, pass it to the facade it gets dispatched to other actors in your application. Then you unwrap your message, type content back (if you’re using a typed language) to whatever you expect it originally typed to, and do some processing. I dare hope you’re not thrilled.
- Function calls. You retrieve an actor from the facade (any actor) and invoke functions on it. If you’re using a typed language, you can use typed arguments (but you lose logical separation).
Now is a good time to visit the PureMVC website and fetch their pretty diagram.
OK, since you’re back I’ll point out right away that ’5 tier 3 tier’ should arouse suspicion. Let’s get sober and clarify this bit:
- The model consists in Proxies and Data objects.
- The view consists in Mediators and Components
- Commands are mmh… just commands, and realise your controller.
Right, what’s going on here is additional separation, at a cost and with potential benefits. Some of the benefits go to the people that designed the framework; you maybe interested by the remaining benefits:
- Since PureMVC aims at being comprehensive and portable, it provides interfaces and default implementations for proxies, mediators and commands. Providing default implementations atop existing library components would be difficult or impossible, although developers may do that on a case basis. So 5 tiers allows to provide a product that is more complete and thus appears more attractive.
- Given that a lot of your application level code is separated from language/platform/domain specific entities, your application is more portable. This means you can migrate it fairly easily to a different platform – this advantage is (to a limited extent) symmetric with the fact that the framework is easy to port across languages – but then, whereas it is definitely a strength for the framework (as a product) to easily port across languages, how likely is it that we’ll want to port our application to another platform? How often does this converge with a genuine business concern?
- It is probably the case that maintaining a level of separation between library/platform and application specific code increases clarity.
What are the benefits of PureMVC?
- Commands – since I started writing commands, I stopped asking ‘whatever control might be’. Whenever you want an interaction between view and model, you instanciate a command, retrieve view and model actors (or fire notifications at them) and ‘do a job’. Once that is done your command gets garbaged collected. While this last bit, per se, may prohibit your using the framework as is (high rates of garbage collection may kill your application), it warrants that you won’t be holding references to data or view objects for longer than reason, or worse, storing information in controllers and nowhere else, which ‘sort of naturally’ happens if you don’t have commands. Commands also make it very clear, at project browser level, what is being done by your application – kind of silly but commands and IDEs, which aren’t great at presenting information visually and concisely, seem to fare well together.
- 5 tier 3 tier. If… (a)you’re writing an application on your own, (b)you know the platform well, (c)you won’t reuse your business model (‘data’), (d)have no intention to use concurrent UI presentations of your data and (e)aren’t planning on porting your application to several languages and platforms, then 5/3 isn’t for you. But as you can see that’s many conditions. 5/3, originally, really put me off. Practically, this currently saves me a lot of time and worry as I’m specifying the ’3 tier core’ (proxies, mediators, commands) of a medium size application, atop a platform I know little, using a language variant I have never used before.
- Interfaces versus conventions. If you naturally lack discipline, are involved in teamwork and/or cannot enforce weaker conventions mechanically, interfaces warrant integrity at the cost of concision and elegance. This may slow you down (to jogging versus thrashing), irritate you and, overall define a day job versus programming fun. Mind that programming fun today might turn into a nightmare tomorrow.
What are the drawbacks of PureMVC?
- Untyped notification content. Providing a fully fledged ‘catch-all’ observer pattern implementation comes at a heavy cost. Event specific notification semantics (as provided by listener interfaces) are a nice to have. Typing rules maintainable applications versus wild mandragoras; casting is a pain.
The boiler plate involved in registering listeners and writing notification interfaces makes me cringe every time, wishing I weren’t using a mainstream object language, and so forth but… I like to implement notification methods that deliver me explicit, typed arguments instead of a single, blackboxed instance. If you’re APIs are customer facing, I’d rather advise you don’t throw these notifications at them. - Broadcast messages. I hear that PureMVC notifications are channelled, but can’t really see the benefits. Increased flexibility versus reduced maintainability is integral to broadcast systems, and whether you want to use broadcast, unicast or a mix of both is a decision that should affect your choice of a framework
- Asynchronous control. If your application is ‘mostly modeless’ or you can’t afford multi-threading anyway, you’ll have to live with a lot of book-keeping.
Multi-threaded or not, most UI frameworks nowadays are event driven (enforcing modal controllers against and event driven UI framework is a special skill). The problem isn’t there. The problem is that we tend to store transient data into semi permanent controllers. So yes, commands are safe, but they do require re-thinking control. - Singleton facade. Unless you’re writing applications too small to consider feature oriented modularity (not the common case) you’ll have to either (a)unravel the mysteries of Multiton PureMVC or (b)find a workaround (there are some) or (c)experiment with a crowded broadcast messaging context (please don’t).
Is it worth it?
That depends mostly on the professional context you are evolving into, and a little bit on your programming experience:
- If you’re not doing a lot of team work and can demonstrate some discipline, maybe you just don’t need a framework – especially not a low common denominator framework at the language level. This is because frameworks tend to kill concision and elegance and you can instanciate the MVC meta-pattern using conventions.
- In my office, we chose PureMVC in part because we felt it would warrant increased code integrity (PureMVC is a framework and we are a 20 strong team with a lot of thrashing and no TDD), in part because we came to like 5/3, and in part because our guys are enthusiastic about the framework – plus it’s fairly popular in our waters, so it’s good for professional development.
- At home – at OOgtech – I won’t stand lack of concision and elegance. Putting the emphasis on feature separation versus layering is proving to support a very fast development/release cycle, while PureMVC not only emphasizes layering, but also demotes modularity in its singleton implementation.
- Let’s face it – writing a separation framework requires expertise, not time or staff resources. The best framework, from a technical point of view, is probably an adaptation of something that already exists. So if you’re not worried about fostering the professional development of your employees and you’re after high productivity versus fads, PureMVC will just be one of several choices – and the best choice may often involve developing your own framework.
Summary
PureMVC is a well structured MVC framework. It boasts a couple of serious advantages – 5/3 tier structure, excellent separation using commands. It may not be the best choice if you’re after a very fast development/release cycle and could have benefited templated, typed notifications versus a catch-all observer pattern implementation.
This framework is commendable for small to large applications involving few modules and many loosely coupled interactions across a small number of interactors.
PureMVC may seduce developers looking for an MVC instance available across several languages and (as a front end developer) may look good on your CV (at least for now). While it may be too heavy for novice developers to learn, it is fairly integer and I would recommend it to developers with prior, untutored experience in instanciating the MVC meta-pattern.