`
wantsong
  • 浏览: 38087 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

MVP vs MVC[ZT]

阅读更多

从来没想了解过MVP。

最近做一个项目的设计,发现View和Control变成了双向关联,怎么都理不清楚。 后来高人说这是“Passive-MVC”。

转一篇帖子,看看什么是MVP。原帖: http://ameleta.spaces.live.com/blog/cns!5F6316345A821420!163.entry

 

Model View Presenter vs Model View Controller 

Intro
In my work I often deal with the situation when people use MVС/MVP patters without clear understanding the really difference between them. In this article I will try to explain my view on the issue.

 It’s important to understand that in n-tier systems MVP/C patterns are responsible for the presentation layer only. It’s not about how you build data or services layers, it’s about separating the data (model) and the user interface(view), telling you how the user interface communicates with data. Using these patterns gives your application independence on changing presentation without depending on data and controlling logic.

 Generally:

1.       Model means data & business logic model. It’s not always a DataSet, DataTable or such stuff. It’s kind of components or classes which can provide data and can receive the data to store it somewhere else. To simplify understanding of Model just think about it as “Façade” class.
2.       View represents data for the user. Generally it’s just the UI, and not always UI logic. For example in ASP.NET the page with controls is View.  The View can receive data directly from Model, but View never updates Model.
3.       The Presenter/Controller contains the logic to update Model regarding the user’s actions into the View. View only notifies Controller about user’s actions. Controller extracts data from View and sends it to Model. 

The main point of the MVC/P pattern is to split Model from View/Controller to make Model independent from them. So, Model cannot contain the references to the V/C.

 What is the MVC (model-view-controller)?
1.       Controller initializes the events of View interface to interact with model and controller.
2.       The user interacts with View (UI).
3.       Controller handles user’s events (can be the “observer” pattern) and asks Model to update.
4.       Model raises events, informing subscribers (View) about changes.
5.       View (UI) (subscribes to model events) handles Model’s evens and shows new Model’s data.
6.       The User Interface waits for the further user actions.

Primary points are:

1.       View does not use Controller to update Model. Controller handles the events from View to manage user’s interaction and data (via interaction with Model)
2.       Controller can be combined with the View. It’s what the Visual Studio do for the Windows Forms by default. The primary point is logical separation of Model from the View.
3.       The Controller do not contains the rendering logic.

The example below illustrates the “Active-MVC” pattern, also known as “the original MVC pattern”.
                                                                                                                    

 There is also the “Passive-MVC” pattern.

 The differences are that:
-         Model knows nothing about Controller and View, it only used by them both.
-         Controller uses View asking it to render new data.
-         View uses Model to get the data only when Controller ask View about it (no subscription between View and Model).
-         Controller handle Model data changes.
-         Controller may contain the rendering logic for the View.

And now we are close to MVP pattern.
MVP is like an MVC, but View doesn’t use Model.

In the MVP View and Model are utterly separated from each other using the Presenter. Any interaction between View and Model take place in Presenter. 

Presenter is like the Controller, but which:
1.       handles the user events from View (in MVC View handles this events);
2.       updates Model using updated data from View (MVC passive just informs View to get/set new data from Model and MVC active takes no role in it, because Model informs View);
3.       examines Model for changes (like the MVC passive);
4.       (the main difference from MVC) gets Model data and stores them into View;
5.       (the main difference from MVC active) notifies View about updates;
6.       (difference from MVC) renders View using the Presenter

So, MVP has following pros:
1.       Model is separated from View and we can change View independently from Model
2.       We using Model more effective, because all interactions are now in one place –in the Presenter(Controller)
3.       We can use one Presenter(Controller) for many Views without changing the Presenter(Controller) logic, it’s can be useful because View changes more often than the Presenter(Controller)
4.       If we are storing View logic in the Presenter(Controller) we can test the logic independent of user interaction (Unit Testing)

But there are cons:
1.       Too many interactions between View and Presenter because rendering View data is in the Presenter;

Also you need to understand that if you render too much data for View you are tied up on the particular View. So if  View needs to be changed you need to update the Presenter either, For example, if you rendered html and need to render the pdf, there is a big possibility that View need to be changed too. 

Mil thanks to Michael Nemtsev for coop


 

分享到:
评论

相关推荐

    mvp-mvc_demo

    **MVP(Model-View-Presenter)与 MVC(Model-View-Controller)是两种常见的软件设计模式,尤其在Android和Web开发中广泛使用。这两种模式都旨在实现业务逻辑、用户界面和数据之间的分离,提高代码的可维护性和可...

    Android MVP和MVC模式比较Demo

    本项目通过一个登录Demo对比分析了两种常见的架构模式:Model-View-Presenter(MVP)和Model-View-Controller(MVC)。这两种模式在处理业务逻辑、数据流和界面展示方面有着各自的特点。 **MVP(Model-View-...

    MVP与MVC的区别

    MVP 是从经典的模式MVC演变而来,它们的基本思想有相通的地方:Controller/Presenter负责逻辑的处理,Model提供数据,View负责显示.

    设计模式学习:Model View Presenter (MVP) mvc mvp

    **设计模式学习:Model View Presenter (MVP)与MVC** 在软件开发中,设计模式是一种通用解决方案,用于解决常见的设计问题,以提高代码的可读性、可维护性和复用性。MVP(Model View Presenter)和MVC(Model View ...

    MVC与MVP代码实现在android上

    在Android开发中,MVC(Model-View-Controller)和MVP(Model-View-Presenter)是两种常见的架构模式,用于组织应用程序的结构和逻辑。这两种模式有助于提高代码的可维护性和可测试性,降低组件之间的耦合度。 **...

    浅谈三大模式(mvc,mvp,mvvm)

    本文将深入探讨三种常见的设计模式:Model-View-Controller(MVC)、Model-View-Presenter(MVP)和Model-View-ViewModel(MVVM)。这些模式在构建用户界面时起着至关重要的作用,尤其是在Web和移动应用开发中。 ...

    MVC和MVP深度对比

    **MVC(Model-View-Controller)和MVP(Model-View-Presenter)是两种广泛应用于软件开发,尤其是Web和移动应用开发中的设计模式。它们都旨在实现业务逻辑与用户界面的分离,提高代码的可维护性和可测试性。本文将...

    几张图看明白MVC MVP MVVM

    MVC、MVP和MVVM是三种常见的软件架构设计模式,它们主要用于分离用户界面(UI)与业务逻辑,以提高代码的可维护性和可测试性。下面将详细地介绍这三种模式的工作原理、组成部分以及它们之间的异同。 **MVC(Model-...

    Android mvc、mvp、mvvm示例

    在Android应用开发中,模型-视图-控制器(MVC)、模型-视图- presenter(MVP)和模型-视图-ViewModel(MVVM)是常见的设计模式,用于组织代码结构,提高可维护性和可测试性。让我们逐一深入探讨这三种架构模式。 ##...

    MVC、MVP和MVVM

    本示例着重探讨了三种常见的架构模式:MVC(Model-View-Controller)、MVP(Model-View-Presenter)和MVVM(Model-View-ViewModel),以及DataBinding的基本运用。下面将对这些知识点进行详细解释。 首先,我们来...

    MVC和MVP的区别

    **MVC(Model-View-Controller)模式与MVP(Model-View-Presenter)模式是两种常见的软件架构设计模式,主要用于构建用户界面。这两种模式在软件开发中扮演着至关重要的角色,帮助开发者组织代码,提高代码的可维护...

    MVC_MVP_MVVM_demos

    "MVC_MVP_MVVM_demos"这个压缩包文件显然包含了关于三种常见的UI架构模式的示例:Model-View-Controller (MVC),Model-View-Presenter (MVP) 和 Model-View-ViewModel (MVVM)。下面我们将详细探讨这三个架构模式,...

    MVP模式与mvc

    ### MVP模式与MVC模式详解 #### 一、MVP模式简介 MVP(Model-View-Presenter)模式是一种软件架构设计模式,主要用于构建用户界面。它是从经典的MVC(Model-View-Controller)模式演变而来的。MVP模式的核心在于它...

    mvc,mvp,mvvm简单框架

    1. **模型(Model)**:仍然负责业务逻辑和数据管理,与MVC和MVP中的模型类似。 2. **视图(View)**:用户界面,但其状态和行为直接绑定到ViewModel的属性和命令。 3. **视图模型(ViewModel)**:作为视图和模型...

    android mvc、mvp、mvvm项目源码

    本文将深入探讨三种常见的架构模式:MVC(Model-View-Controller)、MVP(Model-View-Presenter)以及MVVM(Model-View-ViewModel),并结合提供的项目源码进行分析。 **MVC(Model-View-Controller)**: MVC是最...

    MVCMVPMVVM实例demo.zip

    压缩包中是6个实例demo,包括MVC,MVC的变种,MVP,MVP_login(实际开发中的使用),MVVM,FBKVO;demo对应着简书文章:https://www.jianshu.com/p/dc353e332b0e,如果你没有积分下载,也可以去简书给我留言,可以给你发

    Android中的MVC、MVP和MVVM

    Android中MVC、MVP和MVVM的使用,区别,以及使用场景

    MVC和MVP模式项目实现

    在软件开发领域,MVC(Model-View-Controller)和MVP(Model-View-Presenter)是两种常见的设计模式,用于构建用户界面。这两种模式都致力于分离关注点,提高代码的可测试性和可维护性。这里我们将深入探讨这两种...

    移动端MVC-MVP架构简单示例

    "移动端MVC-MVP架构简单示例"是一个很好的学习资源,它涵盖了两种常见的Android应用程序架构模式:Model-View-Controller (MVC) 和 Model-View-Presenter (MVP)。 **MVC架构** MVC是一种经典的软件设计模式,它将...

    MVC教程。微软MVP翻译

    MVC教程。国外翻译过来的,简单易懂,初学者的最好教程,

Global site tag (gtag.js) - Google Analytics