MVVM介绍

2018-04-23  本文已影响14人  你weixiao的时候很美

本篇参考objc.io文章 MVVM介绍
还有这一篇 Model-View-ViewModel for iOS

1. 介绍MVVM

![mvvm.png](https://upload-
images.jianshu.io/upload_images/2493548-2a8492613b3b7c72.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
这个图解准确地描述了什么是 MVVM:

2. MVVM的特性

通过例子1. 解析MVVM有3个重要的要点:

// Model
@interface Person : NSObject
- (instancetype)initwithSalutation:(NSString *)salutation firstName:(NSString *)firstName lastName:(NSString *)lastName birthdate:(NSDate *)birthdate;
@property (nonatomic, readonly) NSString *salutation;
@property (nonatomic, readonly) NSString *firstName;
@property (nonatomic, readonly) NSString *lastName;
@property (nonatomic, readonly) NSDate *birthdate;
@end

//在controller中显示model
// namelabel 和birthdateLabel显示model中的数据
- (void)viewDidLoad {
    [super viewDidLoad];

    if (self.model.salutation.length > 0) {
        self.nameLabel.text = [NSString stringWithFormat:@"%@ %@ %@", self.model.salutation, self.model.firstName, self.model.lastName];
    } else {
        self.nameLabel.text = [NSString stringWithFormat:@"%@ %@", self.model.firstName, self.model.lastName];
    }

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEEE MMMM d, yyyy"];
    self.birthdateLabel.text = [dateFormatter stringFromDate:model.birthdate];

// 我们使用ViewModel实现如下

//viewModel.h
@interface PersonViewModel : NSObject
- (instancetype)initWithPerson:(Person *)person;

@property (nonatomic, readonly) Person *person;
@property (nonatomic, readonly) NSString *nameText;
@property (nonatomic, readonly) NSString *birthdateText;
@end

//viewModel.m
@implementation PersonViewModel
- (instancetype)initWithPerson:(Person *)person {
    self = [super init];
    if (!self) return nil;

    _person = person;
    if (person.salutation.length > 0) {
        _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;
}
@end

//在controller中显示如下:
- (void)viewDidLoad {
    [super viewDidLoad];

    self.nameLabel.text = self.viewModel.nameText;
    self.birthdateLabel.text = self.viewModel.birthdateText;
}

//如下是我们的单元测试:

SpecBegin(Person)
    NSString *salutation = @"Dr.";
    NSString *firstName = @"first";
    NSString *lastName = @"last";
    NSDate *birthdate = [NSDate dateWithTimeIntervalSince1970:0];

    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

// 由上边的例子1可以看出:

  1. 首先MVVC是从MVC优化来的,兼容MVC。是将MVC中,controller中的表示逻辑的代码移动到ViewModel中。

2.对于容易测试这点: 首先,controller中的代码少了,容易测试。 其实viewModel中的代码容易测试,我们可以按意愿自由地修改视图层级而不必担心破坏我们的单元测试。

  1. 本例中,Model生成后就是不可变的,对于可变的model,我们需要一些绑定机制,Model 的改变应该级联向下通过 View Model 进入 View。 我们可以使用KVO,但是KVO代码量过多,我们可以使用ReactiveCocoa来绑定。
总结:

使用rac是因为对于view和Model的变化,使用KVO等绑定通知和更新比较麻烦。使用ReactiveCocoa可以方便的进行绑定和粘合。

上一篇 下一篇

猜你喜欢

热点阅读