Thursday, November 10, 2011

From MVC to MVP to...


MVC used to be the buzzword for client development.
Implement the model, bind it to the controller and the view, or get a framework to do that for you, and there you have it - A fast client, connected to the server, great way to deliver your application.

Or is it?
I have never seen 'clean', by the book, MVC implementation. The controller and view logic gets mixed and messed up after a couple of iterations, binding makes manipulation of data formats hard, view business logic gets implemented in controller, controller code gets implemented in the view (Sometimes because of binding), and after a while the whole code gets impossible to understand and maintain.

So what do you do?
Find a simpler paradigm, which would work!
MVP says a simple thing:
- The model stays the same. Handle data and communicate with the server/ container/ host/ whatever.
- The presenter does all the work. Business Logic, View Logic, Validation, string formatting, whatever the user does NOT do interact with.
- The view handles GUI. There's no relation between the view and the model, the presenter takes care of that.

Now let's take this a bit further, and create inheritance based MVP component:
The View extents the presenter, which extends the model, et voila!

Now you have a simpler paradigm which could actually work and expend, you can rewrite or use different views (Different devices, for example) and have your factory create the client class according to the device...

As for frameworks - There are a lot of frameworks which would do the trick, though most of them do not use the inheritance paradigm. And anyway, I like to program my own frameworks, and use 3rd party frameworks to simplify specific infrastructural tasks (Like jax-rs for REST servlet implementation, or jQuery for DOM manipulation).

So next time you hear MVC/ MVP - try to see if the inheritance model suits you. You might be surprised how simple things could get...

No comments:

Post a Comment