MVC和MVP的区别

MVC vs. MVP

By now you should've heard of the Model-View-Controller design pattern. If you've read OOP with ActionScript by Branden and Sam then you're also somewhat familiar with the Model-View-Presenter design pattern. So what's the difference?

MVC came first. With the MVC pattern it's possible to separate your presentation information from your behind the scenes business logic. Think along the lines of XHTML/CSS and separating your content from your presentation. A brilliant concept that works quite well, but is not without it's faults.

In MVC, the model stores the data, the view is a representation of that data, and the controller allows the user to change the data. When the data is changed, all views are notified of the change and they can update themselves as necessary (think EventDispatcher).

MVP is a derivative of MVC, mostly aimed at addressing the "Application Model" portion of MVC and focusing around the observer implementation in the MVC triad. Instead of a Controller, we now have a Presenter, but the basic idea remains the same - the model stores the data, the view is a representation of that data (not necessarily graphical), and the presenter coordinates the application.

In MVP the Presenter gets some extra power. It's purpose is to interpret events and perform any sort of logic necessary to map them to the proper commands to manipulate the model in the intended fashion. Most of the code dealing with how the user interface works is coded into the Presenter, making it much like the "Application Model" in the MVC approach. The Presenter is then directly linked to the View so the two can function together "mo' betta".

Basically, in MVP there is no Application Model middle-man since the Presenter assumes this functionality. Additionally, the View in MVP is responsible for handling the UI events (like mouseDown, keyDown, etc), which used to be the Controllers job, and the Model becomes strictly a Domain Model.

Clear as mud, right?

If you're interested in this topic there's quite a bit of useful information out there, but you'll need to take the time to digest it. Check out the following links: (most of these are Smalltalk based, but should still be understandable)

  • Model View Controller History
  • Model-View-Controller framework
  • Model View Controller
  • (ootips) - Model-View-Contoller
  • Model View Presenter
  • Model-View-Presenter framework
  • Pattern: Model-View-Presenter
  • MVP: Model-View-Presenter (Java paper)
  • Twisting the Triad

Above all, remember that MVC and MVP are just patterns. They're more or less a set of guidelines to follow when building applications. In the end, the developer can implement the application however they see fit.

 

光看理论还不能掌握一个架构模式,得看看实际的例子才行,我在codeproject上发现一篇不错的文章《Model View Presenter with ASP.NET》,作者指出了ASP.NETcode-behind编程模型的各种缺点,然后讲解了如何将MVP应用到ASP.NET Application中,而无须使用复杂的Framework

更多的例子:

http://www.mattberther.com/2005/01/000589.html

http://codebetter.com/blogs/jeremy.miller/articles/129546.aspx

http://weblogs.asp.net/pgreborio/archive/2005/01/07/348021.aspx

http://msdn.microsoft.com/msdnmag/issues/06/08/DesignPatterns/default.aspx

如果不介意看smalltalk的话(其实关键是思想):

http://www.object-arts.com/docs/index.html?modelviewpresenter.htm

http://www.mimuw.edu.pl/~sl/teaching/00_01/Delfin_EC/Overviews/ModelViewPresenter.htm

另外还有一个Jean-Paul TV

你可能感兴趣的:(应用程序架构,mvc,application,smalltalk,asp.net,actionscript,events)