经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » iOS » 查看文章
iOS MVVM架构总结
来源:cnblogs  作者:鸿鹄当高远  时间:2018/12/17 9:38:21  对本文有异议

 

为什么使用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类:

  1. #import <Foundation/Foundation.h>
  2.  
  3. @interface User : NSObject
  4. @property (nonatomic, copy) NSString *userName;
  5. @property (nonatomic, assign) NSInteger userId;
  6. - (instancetype)initWithUserName:(NSString *)userName userId:(NSInteger)userId;
  7. @end
  8. @implementation User
  9. - (instancetype)initWithUserName:(NSString *)userName userId:(NSInteger)userId {
  10. self = [super init];
  11. if (!self) return nil;
  12. _userName = userName;
  13. _userId = userId;
  14. return self;
  15. }
  16. @end

 

ViewController类:

  1. #import "HomeViewController.h"
  2. #import "User.h"
  3.  
  4. @interface HomeViewController ()
  5. @property (nonatomic, strong) UILabel *lb_userName;
  6. @property (nonatomic, strong) User *user;
  7. @end
  8. @implementation HomeViewController
  9. - (void)viewDidLoad {
  10. [super viewDidLoad];
  11. //创建User实例并初始化
  12. if (_user.userName.length > 0) {
  13. _lb_userName.text = _user.userName;
  14. } else {
  15. _lb_userName.text = [NSString stringWithFormat:@"博客园%ld", _user.userId];
  16. }
  17. }
  18. @end

 

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

MVVM:

Model类:

  1. #import <Foundation/Foundation.h>
  2.  
  3. @interface User : NSObject
  4. @property (nonatomic, copy) NSString *userName;
  5. @property (nonatomic, assign) NSInteger userId;
  6. @end

 

ViewModel类:

声明:

  1. #import <Foundation/Foundation.h>
  2. #import "User.h"
  3.  
  4. @interface UserViewModel : NSObject
  5. @property (nonatomic, strong) User *user;
  6. @property (nonatomic, copy) NSString *userName;
  7. - (instancetype)initWithUserName:(NSString *)userName userId:(NSInteger)userId;
  8. @end

 

实现:

  1. #import "UserViewModel.h"
  2.  
  3. @implementation UserViewModel
  4. - (instancetype)initWithUserName:(NSString *)userName userId:(NSInteger)userId {
  5. self = [super init];
  6. if (!self) return nil;
  7. _user = [[User alloc] initWithUserName:userName userId:userId];
  8. if (_user.userName.length > 0) {
  9. _userName = _user.userName;
  10. } else {
  11. _userName = [NSString stringWithFormat:@"博客园%ld", _user.userId];
  12. }
  13. return self;
  14. }
  15. @end

 

Controller类:

  1. #import "HomeViewController.h"
  2. #import "UserViewModel.h"
  3.  
  4. @interface HomeViewController ()
  5. @property (nonatomic, strong) UILabel *lb_userName;
  6. @property (nonatomic, strong) UserViewModel *userViewModel;
  7. @end
  8. @implementation HomeViewController
  9. - (void)viewDidLoad {
  10. [super viewDidLoad];
  11. _userViewModel = [[UserViewModel alloc] initWithUserName:@"liu" userId:123456];
  12. _lb_userName.text = _userViewModel.userName;
  13. }
  14. @end

 

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

总结:

  • MVVM同MVC一样,目的都是分离Model与View,但是它更好的将表示逻辑分离出来,减轻了Controller的负担;
  • ViewController中不要引入Model,引入了就难免会在Controller中对Model做处理;
  • 对于很简单的界面使用MVVM会增加代码量,但如果界面中内容很多、Cell样式也很多的情况下使用MVVM可以很好地将VC中处理Cell相关的工作分离出来。

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




https://www.jianshu.com/p/f1d0f7f01130

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号