zoukankan      html  css  js  c++  java
  • 浅谈MVVM

    iOS MVVM架构

     

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

    MVVM分别指什么

    Model-数据层
    ViewController/View-展示层
    ViewModel- 数据模型

    MVVM与MVC的不同

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


    MVC.png


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


    MVVM.png

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

    MVC:

    Model类:

    #import <Foundation/Foundation.h>
    @interface User : NSObject
    @property (nonatomic, copy) NSString *userName;
    @property (nonatomic, assign) NSInteger userId;
    @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];
    
        if (_user.userName.length > 0) {
            _lb_userName.text = _user.userName;
        } else {
            _lb_userName.text = [NSString stringWithFormat:@"简书%ld", _user.userId];
        }
    }

    这里我们需要将表示逻辑也放在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)initWithUser:(User *)user;
    @end

    实现:

    #import "UserViewModel.h"
    
    @implementation UserViewModel
    
    - (instancetype)initWithUser:(User *)user {
        self = [super init];
        if (!self) return nil;
            _user = user;
        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];
            _lb_userName.text = _userViewModel.userName;
    }

    可见,Controller中我们不需要再做多余的判断,那些表示逻辑我们已经移植到了ViewModel中,ViewController明显轻量了很多。

    总结:

    • MVVM同MVC一样,目的都是分离Model与View,但是它更好的将表示逻辑分离出来,减轻了Controller的负担;
    • ViewController中不要引入Model,引入了就难免会在Controller中对Model做处理;
  • 相关阅读:
    [原][osg]osg程序窗口化、显示网格、状态信息
    [转][qt]QSignalMapper 的用法,识别QPushButton的传入参数
    [转]office2013密钥和office2013永久激活码
    [原][工具][C++]调用adobe reader打开pdf,通过命令行启动pdf
    [转]C/C++实现回调机制的几种方式(回调、槽、代理)
    [转][c++]关于构造函数不能有返回类型的错误
    [转]全国性的地理空间数据库管理平台
    [转]JsonCpp遍历json
    阿里开源分布式事务解决方案 Fescar
    MySQL 中基于 XA 实现的分布式事务
  • 原文地址:https://www.cnblogs.com/LGX3399577/p/3-.html
Copyright © 2011-2022 走看看