简单介绍GUI设计模式(MVP)

看到这个题目,我估计绝大部分朋友会第一时间想到 model-view-controller ( MVC )模式。的确这个是在 GUI 设计领域里应用最为广泛的模式了, 3 个模块相互之间交互,大家耳熟能详,我就不多费笔墨了,不了解的朋友请察看 MVC简介

在进行 view 和逻辑分离的工作中,大家知道最多的是MVC, Model-View-Presenter (MVP) 知道的并不多

MVC

   可以参看 MVC简介Castle.MVC框架介绍

MVP

   由 Martin Fowler 发现并进行研究, Martin Flowler 的文章 Model View Presenter 进行详细的介绍。 从根本上来说,她只是个 MVC 的变种。在 MVC 中 view 直接处理相关的 GUI event ,比方说,键盘鼠标事件, checkBox 被选中,按钮被按等等。而在 MVP 中 view 接收到事件,然后会将它们传递到 Presenter, 如何具体处理这些事件,将由 Presenter 来完成。从 class diagram 上来看,就是 Presenter 有 View 和 Model 的引用, Presenter 负责来管理其他两个模块。跟据两者不同来看, MVC 比较适合用来开发 components, 而 MVP 比较适合进行 applications 的开发 , 因为使用 MVP 导致绝大部分逻辑代码集中在 Presenter, 而 view 变得非常简单 , 适当采用良好的编码风格,可以让毫无经验的编码人员稍加培训立刻上岗,大大加速开发 view 的速度, Asp.net 2.0 的 Webform 模型很容易的使用 MVP 模式。

这里有一篇 CAB 与 MVP 模式

下面是一篇文章,来自 http://www.darronschall.com/weblog/archives/000113.cfm 附: 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.

http://www.codeproject.com/dotnet/tdd_in_dotnet.asp

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Android小菜鸡

助你快速搭建一个健壮可控的WebApp

1293
来自专栏指尖下的Android

Android WebView 踩坑之路

2313
来自专栏听雨堂

Android新手之旅(3) 信息的输出

  不管什么语言,了解信息的输出可谓紧要的事情,如vb的msgbox,js的alert,c#的MessageBox.Show,这个对于调试意义重大。Androi...

22710
来自专栏移动开发

Glide 如何实现正确加载图片而没有错位

当我们在常见的列表界面中(如 recycleview 实现的列表),使用上面的代码,在我们快速滑动中,glide 是如何实现正确加载图片,而没有导致图片内容的错...

3313
来自专栏Android开发指南

4.json解析

4658
来自专栏沃趣科技

Oracle 12c ASM专题|Flex Diskgroup相关概念

原文链接 https://martincarstenbach.wordpress.com/2017/07/11/12-2-new-feature-the-fle...

3727
来自专栏Android机动车

『Android Tip』-- 模拟手势操作

这个版本终于快结束了,历时一个月的时间,这段时间里重复着开发、找 BUG 和解 BUG 的工作,人已经快麻木了,不过最后看到 自己的开发成果还是蛮欣慰的,这可能...

931
来自专栏Android中高级开发

Android使用百度地图定位并显示手机位置后使用前置摄像头“偷拍”

拿到这个需求后,对于摄像头的使用不太熟悉,于是我先做了定位手机并在百度地图上显示的功能

3972
来自专栏Android源码框架分析

Android后台杀死系列之一:FragmentActivity及PhoneWindow后台杀死处理机制

App在后台久置后,再次从桌面或最近的任务列表唤醒时经常会发生崩溃,这往往是App在后台被系统杀死,再次恢复的时候遇到了问题,而在使用FragmentActiv...

2183
来自专栏Android中高级开发

Android开发之漫漫长途 Ⅲ——Activity的显示之Window和View(2)

该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列。该系列引用了《Android开发艺术探索...

1193

扫码关注云+社区

领取腾讯云代金券