一、典型的iOS构架——MVC

 

  在典型的MVC设置中,Model呈现数据,Vie呈现用户界面,而ViewController调节它两者之间的交互。

  虽然View和View Controller是技术上不同的组件,但他们总是手牵手在一起,成对的,View不能和不同的View Controller配对,反之亦然。View和View Controller之间的关系如下图所示:

  在典型的MVC应用里,许多逻辑被放在ViewController里。它们中的一些确实属于View Controller,但更多的是“表示逻辑”(presentation logic),就是那些将Model数据转换为View可以呈现的东西的事情,例如将一个NSData转换为一个格式化过的NSString。典型的MVC设计模式没有做太多的事情来解决iOS应用中日益增长的重量级视图控制器的问题。

二、MVC的增强版——MVVM  

  在MVC中增加一个View Model,把所有表示逻辑放进去,它位于Model和View Controller之间,如下图所示:

  在MVVM中,正式链接了视图(View)和控制器(View Controller),并将表示逻辑从ViewController移出放到一个新的对象里,即View Model。

  MVVM的优势:

    *能减少ViewController的复杂性,并使得表示逻辑更易于测试;

    *可以兼容MVC架构;

    *增加应用程序的可测试性;

    *配合一个绑定机制效果最好。

  MVVM基本上就是MVC的改进版,所以很容易整合到现有使用典型MVC架构的应用中。

  下面看一个简单的Person Model以及相应的View Controller:

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property (nonatomic,readonly) NSString *salutation;
@property (nonatomic,readonly) NSString *firstname;
@property (nonatomic,readonly) NSString *lastname;
@property (nonatomic,readonly) NSDate *birthDate; -(instancetype)initWithSalutation:(NSString *)salutation firstname:(NSString *)firstname lastname:(NSString *)lastname birthDate:(NSDate *)birthDate;
@end

下面有一个PersonViewController,在ViewDidLoad里,只需要基于他的model属性设置一些Label即可:

-(void)viewDidLoad{
[super viewDidLoad];
if (self.moedl.salutation.length > ) {
self.nameLabel.text = [NSString stringWithFormat:@"%@.%@.%@",self.moedl.salutation,self.moedl.firstname,self.moedl.lastname];
}
else{
self.nameLabel.text = [NSString stringWithFormat:@"%@.%@",self.moedl.firstname,self.moedl.lastname];
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"EEEE MMMM d,yyyy"];
self.birthdateLabel.text = [dateFormatter stringFromDate:self.moedl.birthDate];
}

  

  这全都直接了当了,典型的MVC。现在来看看如何用一个ViewModel来增强它,创建一个PersonViewModel:

PersonViewModel.h

#import <Foundation/Foundation.h>
#import "Person.h" @interface PersonViewModel : NSObject
@property (nonatomic,readonly) Person *person;
@property (nonatomic,readonly) NSString *nameText;
@property (nonatomic,readonly) NSString *birthdateText;
-(instancetype)initWithPerson:(Person *)person;
@end

PersonViewModel.m

-(instancetype)initWithPerson:(Person *)person{
self = [super init];
if (!self) {
return nil;
}
_person = person;
if (person.salutation.length > ) {
_nameText = [NSString stringWithFormat:@"%@.%@.%@",self.person.salutation,self.person.firstname,self.person.lastname];
}
else{
_nameText = [NSString stringWithFormat:@"%@.%@",self.person.firstname,self.person.lastname];
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"EEEE MMMM d,yyyy"];
_birthdateText = [dateFormatter stringFromDate:person.birthDate]; return self;
}

  这样就将viewDidLoad中的表示逻辑加入viewModel里。此时,新的viewDidLoad就会非常轻量:

-(void)viewDidLoad{
[super viewDidLoad];
self.nameLabel.text = self.viewModel.nameText;
self.bithdateLabel.text = self.viewModel.birthdatetext;
}

  因此,我们并没有对MVC架构做太多改变。还是同样的代码,只是移动了位置。它与MVC兼容,带来更轻量级的ViewControllers。

  在MVVM里,我们试着尽可能多的将代码移入View Model里。测试ViewController就变得容易多了,因为它们不再做一大堆事情,并且View Model也非常易于测试。现在看下面的代码:

SpecBegin(Person)
NSString *salutation = @"Dr.";
NSString *firstName = @"first";
NSString *lastName = @"last";
NSDate *birthdate = [NSDate dateWithTimeIntervalSince1970:]; it (@"should use the salutation available. ", ^{
Person *person = [[Person alloc] initWithSalutation:salutation firstName:firstName lastName:lastName birthdate:birthdate];
PersonViewModel *viewModel = [[PersonViewModel alloc] initWithPerson:person];
expect(viewModel.nameText).to.equal(@"Dr. first last");
}); it (@"should not use an unavailable salutation. ", ^{
Person *person = [[Person alloc] initWithSalutation:nil firstName:firstName lastName:lastName birthdate:birthdate];
PersonViewModel *viewModel = [[PersonViewModel alloc] initWithPerson:person];
expect(viewModel.nameText).to.equal(@"first last");
}); it (@"should use the correct date format. ", ^{
Person *person = [[Person alloc] initWithSalutation:nil firstName:firstName lastName:lastName birthdate:birthdate];
PersonViewModel *viewModel = [[PersonViewModel alloc] initWithPerson:person];
expect(viewModel.birthdateText).to.equal(@"Thursday January 1, 1970");
});
SpecEnd

  如果没有将这个逻辑移入View Model,我们将不得不实例化一个完整的View Controller以及伴随着的View,然后去比较View中Label值。这样做不只是会变成一个麻烦的间接层,而且它只代表了一个十分脆弱的测试。现在,可以按自己的意愿自由地修改视图层级而不必担心破坏单元测试。使用MVVM对测试带来的好处是显而易见的。

  注意到这个简单的例子中,Model是不可变的,所以可以只在初始化的时候指定View Model的属性。对于可变Model,还需要使用一些绑定机制,这样View Model就能在背后的Model改变时更新自身的属性。此为,一旦View Model上的Model发生改变,那View的属性也需要更新。Model的改变应该级联向下通过View Model进入View。

  在OS X上,可以使用cocoa绑定,但在iOS上并没有这样好的配置可用。但是,KVO为在iOS上实现提供了可能。

iOS——MVVM设计模式的更多相关文章

  1. 你真的了解iOS代理设计模式吗?

    在项目中我们经常会用到代理的设计模式,这是iOS中一种消息传递的方式,也可以通过这种方式来传递一些参数.这篇文章会涵盖代理的使用技巧和原理,以及代理的内存管理等方面的知识.我会通过这些方面的知识,带大 ...

  2. 【转】你真的了解iOS代理设计模式吗?

    转自:http://www.cocoachina.com/ios/20160317/15696.html 在项目中我们经常会用到代理的设计模式,这是iOS中一种消息传递的方式,也可以通过这种方式来传递 ...

  3. MVVM设计模式和WPF中的实现(四)事件绑定

    MVVM设计模式和在WPF中的实现(四) 事件绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...

  4. 浅谈 MVVM 设计模式在 Unity3D 中的设计与实施

    初识 MVVM 谈起 MVVM 设计模式,可能第一映像你会想到 WPF/Sliverlight,他们提供了的数据绑定(Data Binding),命令(Command)等功能,这让 MVVM 模式得到 ...

  5. iOS - MVVM 架构模式

    1.MVVM 从字面意思来理解,MVVM 即 Modal View ViewModel(模型 视图 视图模型).MVC 是一个用来组织代码的权威范式,也是构建 iOS App 的标准模式.Apple ...

  6. 设计模式笔记之三:Android DataBinding库(MVVM设计模式)

    本博客转自郭霖公众号:http://mp.weixin.qq.com/s?__biz=MzA5MzI3NjE2MA==&mid=2650236908&idx=1&sn=9e53 ...

  7. 包建强的培训课程(8):iOS与设计模式

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  8. 通过TodoList案例对比Vue.js的MVVM设计模式与JQuery的MVP设计模式

    Vue MVVM设计模式: 在使用vue进行编程时,不会再涉及到DOM的操作,取而代之的是修改数据层,当把数据进行变更的时候,vue之中它的底层会自动的根据数据的不同帮助我们去重新渲染页面. 编码时不 ...

  9. 使用MVVM设计模式构建WPF应用程序

    使用MVVM设计模式构建WPF应用程序 本文是翻译大牛Josh Smith的文章,WPF Apps With The Model-View-ViewModel Design Pattern,译者水平有 ...

随机推荐

  1. Oracle字符集设置

    客户端与服务端字符集不一致会造成乱码问题. 在服务端: sql>SELECT * FROM NLS_DATABASE_PARAMETERS; 在查询结果中关注如下参数: nls_language ...

  2. jar的下载地址及其使用说明

    有时候会苦于jar的搜索.这里我就给出我平时用到的吧,方便大家.后期会不断添加. 1.dom4j-1.6.1.jar 主要用于解析xml的jar包.下载地址:   http://pan.baidu.c ...

  3. c# Beginlnvoke 委托

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. JSTL,自定义一个标签的功能案例

    1.自定义一个带有两个属性的标签<max>,用于计算并输出两个数的最大值: 2.自定义一个带有一个属性的标签<lxn:readFile  src=“”>,用于输出指定文件的内容 ...

  5. Unity脚本在层级面板中的执行顺序测试4-附加整理

    测试4为一些附加内容,后续的各种tips都加在此. 前几篇测试的链接: Unity脚本在层级面板中的执行顺序测试1 http://www.cnblogs.com/hont/p/4298110.html ...

  6. Python学习笔记-Day3-python内置函数

    python内置函数 1.abs    求绝对值 2.all 判断迭代器中的所有数据是否都为true 如果可迭代的数据的所有数据都为true或可迭代的数据为空,返回True.否则返回False 3.a ...

  7. jar 文件

    使用类库中的类: 1.当前应用程序所在目录中没有包名的类,直接用. 2.java库. import语句导入. 3.当前目录的子孙目录有报名的类,set classpath,之前有写. 4.扩展ext文 ...

  8. WPFの布局中Panel的选用

    一.Canvas 这个容器能够对元素做准确的定位,但同时也是其创建的页面不够灵活. 二.StackPanel 最大的优点是:他会顺序的对他的子元素进行排列显示.(没有任何附加属性) 要注意的是:他有两 ...

  9. Hashtable与ConcurrentHashMap区别

    Hashtable与ConcurrentHashMap区别 ConcurrentHashMap融合了hashtable和hashmap二者的优势. hashtable是做了同步的,是线性安全的,(2) ...

  10. VC++ 工程添加 Unicode Debug和Unicode Release编译支持

    转载:http://blog.csdn.net/djhdu/article/details/171766 转载:http://blog.163.com/long_lh/blog/static/2769 ...