Monday, May 09, 2005

Model-View-Presenter [MVP] Pattern

Ever worried about what UI Pattern should you consider for your application?
Ever worried what pattern should be used to support alternate views?
Ever worried how to do UI Testing?
Ever worried about UI Class becoming more complex, as project grows ?
Ever worried about complex UI Screens with multiple nesting levels and interdependencies?
Here is a pattern for you.

Patterns are impressive, a Simple twist in pattern, gives amazing capabilities. Twist MVC Pattern a bit, you’ll get MVP Pattern, along with an array of flexibility. So, What’s MVP Pattern ? MVP is an acronym for Model-View-Presenter. In simple words, MVP Pattern separates the presentation functionality from the view while allowing the view to receive the user events.

Fine, but isn’t MVC already doing these? The answer is that MVC is bit old fashioned and the view handles all the user events. But in MVP, view still handles the user events, but instantly delegates the job to the presenter, and presenter does the real job of deciding what to do with the user events after due consideration of the Model.

So What are the Advantages?
-> Firstly, it helps to abstract the Rich, Fat and Thin clients
-> You can use Multiple Views with Different UI Controls on any of the above client forms, but a consistent behavior can be implemented.
-> Testing becomes breeze, as the functionality can be tested without the UI
-> You can create a Triads of MVP Horizontally a.k.a. hMVP, so that you can make complex UI Screens more manageable
-> Presenter becomes the true controller and more concentration is given to the behavior.

So How to implement a MVP Pattern, all you need to do is a simple change to the MVC Pattern.

1. Create a interface which is totally independent of the controls
2. Inherit the interface on view
3. Move all the behavioral code of view to a separate class called presenter
4. The interface, has now broken the dependency between view and presenter
(Hint: some of you might have done this in your existing applications already, but without understanding the full advantages of this pattern)

Here is a simple diagram to depict MVP Pattern

posted by Logu Krishnan : 7:36 AM

Comments: Post a Comment

This page is powered by Blogger. Isn't yours?