Wednesday, 3 November 2010

Most Valuable Pattern

I first came across the Model View Presenter pattern in Dolphin Smalltalk. Of all the patterns I've learnt over the years it remains enigmatic, beautiful and In my opinion misunderstood. Last night I implemented the MVP pattern again on a home brew project and I was struck again by the following elegant that can result, after your rewire your head.

Rewiring your head is painful and unnerving, but it's clearly something that Andy and Blair did when writing Dolphin Smalltalk. I remember the child like fascination as I traced through the very small amounts of code, wondering where the code that did anything was. When you check back and see the small collection of components. You begin to realise that it's the different composition of these components and the same pattern repeated down and down like a fractal that give rise to complex emergent properties.

What I mean by this, is that Models can themselves contain MVP patterns, as can Views and Presenters. Indeed in Dolphin Views can even act as their own default Presenters.

There's a problem though. The pattern is so loose, so decoupled so self similar and holonic that it's very easy to get lost. This isn't such a problem in Dolphin which has a great GUI designer, because the view hierarchy gives you a visual reference to which layer you're dealing with.

This being the second or third time that I've had a crack at making a simple MVP framework for the Web, and the second or third time I've tried to do it test first, I realised that that M, V & P are names for spaces that you need to fill in yourself. Having Interfaces or class hierarchies is a hindrance. The secret stars of the show, outlined in the original Taligent paper are to my mind Commands, Selections, Notifications and Interactions (or Interactors as the paper has them).

Commands, Selections, Notifications and Interactions are easy to unit test using mock objects. However Presenters appear to be largely about wiring.

I want to write some more about the circular feedback around from input view, through presenter to model and back to view.

I also want to write about the asynchronous nature of the different event or action types in MVP.

Back to the code it's too good to leave this long

No comments:

Post a Comment