@zhongzilu
2016-04-02T17:26:07.000000Z
字数 6914
阅读 3197
MVP
MVC
本文是翻译国外的一篇文章,英语水平有限,翻译有误的地方希望提出来,以方便修改,谢谢
Over the years I have mentored many developers on using design patterns and best practices.
多年来我有辅导过很多开发者使用设计模式和最佳实践One question that keeps coming up over and over again is:What are the differences between the Model View Controller (MVC) and Model View Presenter (MVP) patterns?
一遍又一遍出现的一个问题是: 模型 视图 控制器(MVC模式)和模型 视图 主持人(MVP模式)之间有哪些不同?Surprisingly the answer is more complex than what you would suspect.
令人惊讶的是答案比你想象中的更复杂Part of reasons I think many developers shy away from using either pattern is the confusion over the differences.
我认为一部分原因是因为许多开发者回避使用两种模式带来的混乱Before we dig into the differences let’s examine how the patterns work and the key benefits to using either one.
在我们深入研究两种模式如何工作和使用其中一种带来的关键利益之前Both (MVC & MVP) patterns have been use for several years and address a key OO principal namely separation of concerns between the UI and the business layers.
这两种模式(MVC & MVP)已经使用了好几年,并且处理面向对象的关键就是分离界面UI层和业务层.There are a number of frameworks is use today that based on these patterns including: JAVA Struts, ROR, Microsoft Smart Client Software Factory (CAB), Microsoft Web Client Software Factory, and the recently announced ASP.Net MVC framework.
这里有一些如今正在被使用的,基于这些模式的框架,包括:Java Struts, ROR, Microsoft Smart Client Software Factory (CAB), Microsoft Web Client Software Factory,还有最近发布的ASP.Net MVC框架Model View Controller (MVC) Pattern
The MVC pattern is a UI presentation pattern that focuses on separating the UI (View) from its business layer (Model).
MVC模式是一个着重于从业务层(Model)分离UI(View)的UI表示模型The pattern separates responsibilities across three components: the view is responsible for rending UI elements, the controller is responsible for responding to UI actions, and the model is responsible for business behaviors and state management.
模型分离的重任横跨三个组件: 视图层负责渲染UI元素, 控制器负责响应UI操作, 模型层负责业务逻辑和状态管理.In most implementation all three components can directly interact with each other and in some implementations the controller is responsible for determining which view to display (Front Controller Pattern),
多数的实现方法,可以使这三个组件互相直接作用影响,有一些实现的方法里,控制器负责确定哪些试图的显示(Front Controller Pattern)Model View Presenter (MVP) Pattern
The MVP pattern is a UI presentation pattern based on the concepts of the MVC pattern.
MVP模型是基于MVC模型的UI表示模型。The pattern separates responsibilities across four components: the view is responsible for rending UI elements, the view interface is used to loosely couple the presenter from its view, the presenter is responsible for interacting between the view/model, and the model is responsible for business behaviors and state management.
模型分离的重任横跨四个组件:视图层负责渲染UI元素, 视图层接口被用于把中介者(Presenter)从视图中解耦,中介者负责视图(View)和模型(Model)之间的交互,模型层负责业务逻辑和状态管理.In some implementations the presenter interacts with a service (controller) layer to retrieve/persist the model.
在某些实现方法中,中介者会和服务层/控制器层交互来获取或持有model。The view interface and service layer are commonly used to make writing unit tests for the presenter and the model easier.
视图接口和服务层通常被用来编写中介者和模型之间的单元测试。
Before using any pattern a developers needs to consider the pros and cons of using it.
在使用任何开发模式之前,开发人员需要考虑使用它的优点和缺点。There are a number of key benefits to using either the MVC or MVP pattern (See list below).
这里有一些使用MVC和MVP模型的关键好处(请看下面的列表)。But, there also a few drawbacks to consider. The biggest drawbacks are additional complexity and learning curve.
但是,这里也有一些缺点需要考虑。最大的缺点就是它的复杂性和需要付出额外的学习成本。While the patterns may not be appropriate for simple solutions; advance solutions can greatly benefit from using the pattern.
这种模型可能不太适合一般的解决方案;好的解决方案可以从中获得更大的利益。I'm my experience a have seen a few solutions eliminate a large amount of complexity but being re-factored to use either pattern.
在我的经历中,我见过使用这些模式来重构项目,解决了不少的复杂的问题。
Loose coupling – The presenter/controller are an intermediary between the UI code and the model. This allows the view and the model to evolve independently of each other.
松散耦合 - presenter或控制器在UI代码和模型之间充当着中间媒介的角色,这就使得视图和控制器之间互相独立。Clear separation of concerns/responsibility
清晰分离关注点和职责(关于concerns的翻译请自行有道)
-- UI (Form or Page) – Responsible for rending UI elements
UI(表单或页面) - 负责渲染UI元素
-- Presenter/controller – Responsible for reacting to UI events and interacts with the model
中介者/控制器 - 负责对UI事件与模型进行交互
-- Model – Responsible for business behaviors and state management
模型 - 负责业务逻辑和状态管理.Test Driven – By isolating each major component (UI, Presenter/controller, and model) it is easier to write unit tests. This is especially true when using the MVP pattern which only interacts with the view using an interface.
测试驱动 - 通过分离每个主要组件(UI,Presenter/controller, model)更容易编写单元测试,当使用MVP模型且只通过接口来和视图层交互时,就更应该这样做。Code Reuse – By using a separation of concerns/responsible design approach you will increase code reuse. This is especially true when using a full blown domain model and keeping all the business/state management logic where it belongs.
代码复用 - 通过使用分离关注点和职责的设计可以增加你的代码复用。在使用全面的域模型和保持所属业务和状态管理的逻辑时,就更应该这样做。Hide Data Access – Using these patterns forces you to put the data access code where it belongs in a data access layer. There a number of other patterns that typical works with the MVP/MVC pattern for data access. Two of the most common ones are repository and unit of work. (See Martin Fowler – Patterns of Enterprise Application Architecture for more details)
隐藏数据访问 - 使用这种模式会强迫你使用数据接入层中的数据接入代码。这里有一些其他典型的使用MVP/MVC模型来进行数据访问的模型,最常见的两个就是库和工作单元。(想看更多细节请看Martin Fowler的企业应用架构模式【Martin Fowler - Patterns of Enterprise Application Architecture】)Flexibility/Adaptable – By isolating most of your code into the presenter/controller and model components your code base is more adaptable to change. For example consider how much UI and data access technologies have changed over the years and the number of choices we have available today. A properly design solution using MVC or MVP can support multi UI and data access technologies at the same time.
灵活/适用 - 通过分离Presenter/controller和model组件可以使你的代码更灵活适用.举个例子,这些年来有许多UI技术和数据接入技术都有所改变,如今我们有许多的选择.恰当的使用MVC或者MVP模式来设计解决方案,可以同时支持多种UI技术和数据接入技术.
So what really are the differences between the MVC and MVP pattern.
Actually there are not a whole lot of differences between them.
因此,MVC和MVP模式之间真正的区别.其实是没有太多区别.Both patterns focus on separating responsibility across multi components and promote loosely coupling the UI (View) from the business layer (Model).
这两者都是专注跨组件分离职责,促进UI层从业务层中解耦.The major differences are how the pattern is implemented and in some advanced scenarios you need both presenters and controllers.
主要的区别在于如何实现,在某些高级的应用场景中需要同时有Presenter和ControlerHere are the key differences between the patterns:
这里有些这两种模式之间的关键区别:
MVP模式
-- View is more loosely coupled to the model. The presenter is responsible for binding the model to the view.
视图和模型之间耦合更加松散.中介者负责把model绑定到视图层.
-- Easier to unit test because interaction with the view is through an interface
更加容易编写单元测试,因为都是通过接口和视图交互的.
-- Usually view to presenter map one to one. Complex views may have multi presenters.
通常情况下,视图和中介者是一一对应的.复杂的视图可能会有多个中介者.MVC模式
-- Controller are based on behaviors and can be shared across views
控制器是建立在行为上的,并且可以跨视图共享
-- Can be responsible for determining which view to display
能够负责决定要显示哪个视图Hopefully you found this post interesting and it helped clarify the differences between the MVC and MVP pattern.
希望你会觉得这篇文章是有趣的,这篇文章会帮助你理清MVC和MVP模式之间的区别.If not, do not be discouraged patterns are powerful tools that can be hard to use sometimes.
如果没理清,请不要泄气,模型是一个强大的工具,以至于有时在使用时会觉得很困难One thing to remember is that a pattern is a blue print and not an out of the box solutions.
你只需要记住,模型只是一个蓝图,并不是快速的解决方案.Developers should use them as a guide and modify the implementation according to their problem domain.
开发者应该把它们作为指导手册,并根据遇到的问题修正实现方案.
作者 : zhongzilu
本文为作者原创,转载时请写上转载地址:https://www.zybuluo.com/zhongzilu/note/328268