简单介绍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)

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

 

### 回答1: MVP(Model-View-Presenter)是一种用于构建用户界面的设计模式,它将界面分为三个主要部分:模型(Model)、视图(View)和展示者(Presenter)。在Qt框架中实现MVP设计模式的demo可以按照以下步骤进行: 1. 首先,创建一个数据模型类(Model),该类负责处理数据的获取和管理。可以定义一些成员变量用于保存数据,并提供相应的方法来操作这些数据。 2. 接下来,创建一个视图类(View),该类用于用户界面的展示。可以在视图类中使用Qt的控件来构建界面,并将用户输入的操作反馈给Presenter类处理。 3. 然后,创建一个展示者类(Presenter),该类作为Model和View之间的中介,处理业务逻辑和数据的交互。可以在Presenter中实例化Model和View对象,并定义一些方法用于处理用户界面的操作,同时更新数据模型。 4. 最后,在Qt的主函数中初始化Model、View和Presenter对象,并建立它们之间的连接。在实际的应用场景中,可以通过信号和槽机制来实现View和Presenter之间的通信,以及Model和Presenter之间的数据传递。 通过上述步骤的实现,就可以在Qt框架中创建一个简单MVP设计模式的demo。在这个demo中,View负责用户界面展示,Presenter处理业务逻辑和数据交互,Model负责数据的管理。这种设计模式的好处是使代码结构清晰,职责明确,易于扩展和维护,使得开发过程更加高效。 ### 回答2: MVP(Model-View-Presenter)是一种常用的设计模式,用于将业务逻辑和用户界面分离。Qt框架是一个功能强大的GUI开发框架,可以轻松地实现MVP设计模式。 首先,我们需要创建三个主要的组件:Model(模型)、View(视图)和Presenter(展示器)。 模型(Model)是应用程序的数据源。它负责处理数据的读取、写入和处理。在Qt中,我们可以使用QAbstractItemModel类或自定义的数据结构作为模型。 视图(View)是用户界面的呈现层。它负责展示数据,并且可以与用户进行交互。在Qt中,我们可以使用QWidget或QML作为视图。 展示器(Presenter)是模型和视图之间的桥梁。它负责接收视图的用户交互事件,并使用模型来处理业务逻辑。在Qt中,我们可以使用QObject类来实现展示器。 在实现MVP设计模式的Demo中,我们可以创建一个简单的待办事项列表应用程序。 首先,在模型中,我们可以定义一个代表待办事项的数据结构,并实现数据的增删改查操作。 接下来,在视图中,我们可以使用QWidget或QML来创建一个用于展示待办事项的列表的界面。用户可以添加、删除和更新待办事项。 最后,在展示器中,我们可以连接视图和模型,处理用户的交互事件,并将其反映到模型中。 例如,当用户点击添加按钮时,展示器将接收到该事件,并调用模型的添加方法。模型更新后,展示器将更新视图以反映最新的待办事项列表。 通过以上步骤,我们可以实现一个简单MVP设计模式的Demo,同时利用Qt框架的强大功能。这样做将使代码更易于维护和扩展,并促进良好的代码分离和可测试性。 ### 回答3: MVP(Model-View-Presenter)是一种软件设计模式,主要用于分离应用程序的业务逻辑和图形界面。在Qt中实现MVP设计模式的示例可以按照以下步骤进行: 1. 创建模型(Model):模型负责处理应用程序的业务逻辑和数据处理。可以使用Qt的数据结构,如QList、QMap等来表示数据。例如,在一个学生信息管理系统中,可以创建一个学生模型类(StudentModel),用于存储和处理学生的信息。 2. 创建视图(View):视图是用户界面的可视化部分,负责读取模型中的数据并展示给用户。可以使用Qt的界面设计工具(如Qt Designer)创建视图的UI界面。例如,在学生信息管理系统中,可以创建一个学生信息展示的窗口,包含姓名、年龄、性别等信息的文本框和标签。 3. 创建Presenter(Presenter):Presenter是连接模型和视图之间的桥梁,负责处理用户的输入操作,更新模型中的数据,并将更新后的数据传递给视图进行展示。可以使用Qt的信号与槽机制来实现Presenter的交互逻辑。例如,在学生信息管理系统中,可以创建一个学生信息展示的Presenter类(StudentPresenter),用于响应用户的操作,更新学生模型的数据,并将更新后的数据传递给学生信息展示的视图进行展示。 4. 其他辅助类:除了上述三个核心类外,还可以根据需要创建其他辅助类,用于处理一些通用的功能,如数据验证、数据转换等。 在实现MVP设计模式的Demo中,可以创建一个学生信息管理系统的应用程序。通过使用上述步骤创建模型、视图和Presenter,并在主函数中进行连接和初始化,以实现学生信息的展示和更新操作。用户可以通过视图输入学生信息,并通过Presenter将输入的信息更新到模型中,并实时反映在视图中。 通过以上步骤以及合理的类和模块的划分,可以实现MVP设计模式的Demo,并帮助开发人员更好地分离业务逻辑和图形界面,提高代码的可维护性和可扩展性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值