设为首页 加入收藏

TOP

如何正确地写好一个界面(四)
2017-10-12 13:04:27 】 浏览:10097
Tags:如何 正确 一个 界面
tps://msdn.microsoft.com/en-us/magazine/dn463786.aspx)

而苹果官方给的MVC的设计模式图却是这样的:


iOS UI Arch

到底哪一副图才是真正的MVC?我的答案只能是:都是。

MVC从施乐帕克实验室提出至今,已经应用到各种应用开发领域中:Web App可以用MVC,iOS/Android/Windows客户端应用也用MVC,Web前端也在用MVC,等等;这些几乎涵盖了我们常见的开发领域, 所以MVC其实已经超越了他原本最初的设计,基于所有涉及展示的应用都能套上MVC,只不过不同的平台在设计上略有差别。而MVP和MVVM,也不过是 MVC的衍生变种,除这两者之外,还有我们没怎么见过的HMVCMVA等。

 

4.2 Model Layer

在讨论MVP和MVVM之前,我想先明确一个经常被误解的概念:Model。由于Model这个词太通用化,如数据Model,数据库Model,这就导致了Model这一概念理解差异化,简单的说,就是被玩坏。抛开其他,我们来看看常见的定义:

Wikipedia的定义

The central component of MVC, the model, captures the behavior of the application in terms of its problem domain, independent of the user interface.[11] The model directly manages the data, logic and rules of the application.

MSDN(https://msdn.microsoft.com/en-us/library/ff649643.aspx)中的定义

Model. The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller).

上面两个定义基本一致:Model,管理应用的行为和数据。

再来看看Apple官方文档Model-View-Controller的定义

Model Objects

Model objects encapsulate the data specific to an application and define the logic and computation that manipulate and process that data. For example, a model object might represent a character in a game or a contact in an address book. A model object can have to-one and to-many relationships with other model objects, and so sometimes the model layer of an application effectively is one or more object graphs. Much of the data that is part of the persistent state of the application (whether that persistent state is stored in files or databases) should reside in the model objects after the data is loaded into the application. Because model objects represent knowledge and expertise related to a specific problem domain, they can be reused in similar problem domains. Ideally, a model object should have no explicit connection to the view objects that present its data and allow users to edit that data—it should not be concerned with user-interface and presentation issues.

Communication: User actions in the view layer that create or modify data are communicated through a controller object and result in the creation or updating of a model object. When a model object changes (for example, new data is received over a network connection), it notifies a controller object, which updates the appropriate view objects.

虽然Apple的官方文档是定义Model Objects,但它的含义还是封装数据以及管理数据相关的逻辑计算;

所以这里需要明确的一个概念是:在MVC的设计模式中,Model是一个Layer,而不只是一个数据模型(Data Model)类。总体来说,Model Layer 包含了数据模型,以及管理这些数据相关的逻辑计算,如本地数据变化、数据缓存、从网络请求数据等业务逻辑。关于这个问题,还可以参考这篇文章:《iOS应用架构谈 view层的组织和调用方案》。 但有一点需要说明:该文章更倾向于从Model Object上思考Model的定义,因为里面的关于Model的示例是从数据模型中扩展出业务接口;而本人则更倾向于从Model Layer来思考Model,即Model并不限于数据模型,可以是数据管理类(各种Manager)、请求队列管理等等。

 

4.3 MVP VS MVVM

上一节关于Model Layer中推荐的文章《iOS应用架构谈 view层的组织和调用方案》对MVC和MVVM都做了非常详细的讨论,是一篇非常不错的文章,推荐各位阅读,那么本节就来说说MVP,以及我为什么更倾向于选择MVP作为App架构设计中的设计框架。

回顾下在本章一开始祭出的MVP以及MVVM两张图,两者之间有什么不同?

MVVM的VM(View Model)到V(View),比MVP的P(Presenter)到V(View),多了数据绑定。也就是

MVP: 是MVC的变种,其中Model和View的定义与MVC的一致,不同点在于:MVC的Controller是管理一组Model与View之间交互逻 辑,是一个管理者;而Presenter(展示者)则是Model于View之间的连接者,针对特定模块的View提供对应的格式化的Model数据,将 View中的行为反馈到Model中。所以MVC中的Controller一般会管理一个或多个Model和一个或多个View,而Presenter则 是 M-P-V 一对一,有更细的粒度和更好的解耦。

从MVP的定义,你会发现MVP与MVV

首页 上一页 1 2 3 4 5 下一页 尾页 4/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇OpenGL ES: iOS 自定义 UIView 响.. 下一篇如何正确地写好一个界面

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目