iOSiOS架构模式ios

iOS MVVM架构总结

2017-09-04  本文已影响1053人  differ_iOSER

参考:
iOS 中MVC设计模式
iOS MVVM架构
iOS MVVM-框架介绍
iOS 架构模式MVVM的实践总结
iOS MVVM+RAC 从框架到实战

为什么使用MVVM

iOS中,我们使用的大部分都是MVC架构。虽然MVC的层次明确,但是由于功能日益的增加、代码的维护,使得更多的代码被写在了Controller中,这样Controller就显得非常臃肿。
为了给Controller瘦身,后来又从MVC衍生出了一种新的架构模式MVVM架构。

MVVM分别指什么

MVVM就是在MVC的基础上分离出业务处理的逻辑到ViewModel层,即:

Model层:请求的原始数据
View层:视图展示,由ViewController来控制
ViewModel层:负责业务处理和数据转化

简单来说,就是API请求完数据,解析成Model,之后在ViewModel中转化成能够直接被视图层使用的数据,交付给前端(View层)。

MVVM与MVC的不同

首先我们简化一下MVC的架构模式图:

在这里,Controller需要做太多得事情,表示逻辑、业务逻辑,所以代码量非常的大。而MVVM:

MVVM的实现

比如我们有一个需求:一个页面,需要判断用户是否手动设置了用户名。如果设置了,正常显示用户名;如果没有设置,则显示“简书0122”这种格式。(虽然这些本应是服务器端判断的)
我们看看MVC和MVVM两种架构都是怎么实现这个需求的

MVC:

Model类:

#import <Foundation/Foundation.h>

@interface User : NSObject

@property (nonatomic, copy) NSString *userName;
@property (nonatomic, assign) NSInteger userId;

- (instancetype)initWithUserName:(NSString *)userName userId:(NSInteger)userId;

@end
@implementation User

- (instancetype)initWithUserName:(NSString *)userName userId:(NSInteger)userId {
    self = [super init];
    if (!self) return nil;
    _userName = userName;
    _userId   = userId;
    return self;
}

@end

ViewController类:

#import "HomeViewController.h"
#import "User.h"

@interface HomeViewController ()

@property (nonatomic, strong) UILabel *lb_userName;
@property (nonatomic, strong) User *user;

@end
@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //创建User实例并初始化
    if (_user.userName.length > 0) {
        _lb_userName.text = _user.userName;
    } else {
        _lb_userName.text = [NSString stringWithFormat:@"简书%ld", _user.userId];
    }
}

@end

这里我们需要将表示逻辑也放在ViewController中。

MVVM:

Model类:

#import <Foundation/Foundation.h>

@interface User : NSObject

@property (nonatomic, copy) NSString *userName;
@property (nonatomic, assign) NSInteger userId;

@end

ViewModel类:

声明:

#import <Foundation/Foundation.h>
#import "User.h"

@interface UserViewModel : NSObject

@property (nonatomic, strong) User *user;
@property (nonatomic, copy) NSString *userName;

- (instancetype)initWithUserName:(NSString *)userName userId:(NSInteger)userId;

@end

实现:

#import "UserViewModel.h"

@implementation UserViewModel

- (instancetype)initWithUserName:(NSString *)userName userId:(NSInteger)userId {
    self = [super init];
    if (!self) return nil;
    _user = [[User alloc] initWithUserName:userName userId:userId];
    if (_user.userName.length > 0) {
        _userName = _user.userName;
    } else {
        _userName = [NSString stringWithFormat:@"简书%ld", _user.userId];
    }
    return self;
}

@end

Controller类:

#import "HomeViewController.h"
#import "UserViewModel.h"

@interface HomeViewController ()

@property (nonatomic, strong) UILabel *lb_userName;
@property (nonatomic, strong) UserViewModel *userViewModel;

@end
@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _userViewModel = [[UserViewModel alloc] initWithUserName:@"liu" userId:123456];
    _lb_userName.text = _userViewModel.userName;
}

@end

可见,Controller中我们不需要再做多余的判断,那些表示逻辑我们已经移植到了ViewModel中,ViewController明显轻量了很多。说白了,就是把原来ViewController层的业务逻辑和页面逻辑等剥离出来放到ViewModel层。

总结:

写到这里,MVVM基本上就算结束了。重要的还是去实践,在实践中检验真理。

MVVM的缺点

关于MVVM的缺点,Casa Taloyum大神在他的博客《iOS应用架构谈 网络层设计方案》中已经阐述的很详细了。这里简单的引用原文做一个回顾。

这种做法是能够提高后续操作代码的可读性的。在比较直觉的思路里面,是需要这部分转化过程的,但这部分转化过程的成本是很大的,主要成本在于:
数组内容的转化成本较高:数组里面每项都要转化成Item对象,如果Item对象中还有类似数组,就很头疼。
转化之后的数据在大部分情况是不能直接被展示的,为了能够被展示,还需要第二次转化。
只有在API返回的数据高度标准化时,这些对象原型(Item)的可复用程度才高,否则容易出现类型爆炸,提高维护成本。
调试时通过对象原型查看数据内容不如直接通过NSDictionary/NSArray直观。
同一API的数据被不同View展示时,难以控制数据转化的代码,它们有可能会散落在任何需要的地方。

针对这些缺点,Casa Taloyum大神也提出了相应的解决方法,即用一个类似reformer
的对象进行数据过滤,根据不同的reformer
对象过滤出不同的数据。这种方法,目前我也在进行尝试,之后我也会分享我的使用心得,敬请关注。

谢谢!

上一篇 下一篇

猜你喜欢

热点阅读