美文网首页
iOS中的MVC和MVVM

iOS中的MVC和MVVM

作者: Edviin_2de8 | 来源:发表于2022-04-08 14:14 被阅读0次

    MVC

    MVC的实现思路是:用户操作View,在Controller层完成业务逻辑处理,更新Model层,将数据显示在View层。
    在MVC中,每个层之间都有关联,耦合比较紧,在大型项目中,维护起来比较费力。
    View把控制权交给Controller层,自己不执行业务逻辑;Controller层执行业务逻辑并且操作Model层,但不会直接操作View层;View和Model层的同步消息是通过观察者模式进行,而同步操作是由View层自己请求Model层的数据,然后对视图进行更新,观察者模式可以做到多视图同时更新。

    MVC结构如图所示:

    image.png
    模型层:

    Person.h

    #import <Foundation/Foundation.h>
    
    @interface Person : NSObject
    
    @property (nonatomic, readonly) NSString *firstName;
    @property (nonatomic, readonly) NSString *lastName;
    
    - (instancetype)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName;
    
    @end
    
    

    Person.m

    #import "Person.h"
    
    @implementation Person
    
    - (instancetype)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName {
        self = [super init];
        if (self) {
            _firstName = firstName;
            _lastName = lastName;
        }
        return self;
    }
    
    @end
    
    
    视图层:

    TestView.h

    #import <UIKit/UIKit.h>
    
    @interface TestView : UIView
    
    @property (nonatomic, strong) UILabel *nameLabel;
    
    @end
    
    

    TestView.m

    #import "TestView.h"
    
    @implementation TestView
    
    - (instancetype)initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];
        if (self) {
            self.nameLabel = [[UILabel alloc] initWithFrame:self.bounds];
            self.nameLabel.textAlignment = NSTextAlignmentCenter;
            [self addSubview:self.nameLabel];
        }
        return self;
    }
    
    @end
    
    
    控制器层:

    ViewController.m

    #import "ViewController.h"
    #import "Person.h"
    #import "TestView.h"
    
    @interface ViewController ()
    
    @property (nonatomic, strong) Person *personModel;
    @property (nonatomic, strong) TestView *testView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        [self setupViews];
    
        if (self.personModel.firstName.length > 0) {
            self.testView.nameLabel.text = self.personModel.firstName;
        } else {
            self.testView.nameLabel.text = self.personModel.lastName;
        }
    
    }
    
    - (void)setupViews {
        self.personModel = [[Person alloc] initWithFirstName:@"" lastName:@"胡歌"];
        self.testView = [[TestView alloc] initWithFrame:CGRectMake(100, 100, CGRectGetWidth(self.view.bounds)-200, 50)];
        [self.view addSubview:self.testView];
    }
    
    @end
    
    

    MVVM

    MVVM和MVP的最大区别是采用了双向绑定机制,View的变动,自动反映在ViewModel上。
    MVVM结构如图:


    image.png

    模型层:
    Person.h

    #import <Foundation/Foundation.h>
    
    @interface Person : NSObject
    
    @property (nonatomic, readonly) NSString *firstName;
    @property (nonatomic, readonly) NSString *lastName;
    
    - (instancetype)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName;
    
    @end
    
    

    Person.m

    #import "Person.h"
    
    @implementation Person
    
    - (instancetype)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName {
        self = [super init];
        if (self) {
            _firstName = firstName;
            _lastName = lastName;
        }
        return self;
    }
    
    @end
    
    

    视图层:
    TestView.h

    #import <UIKit/UIKit.h>
    
    @interface TestView : UIView
    
    @property (nonatomic, strong) UILabel *nameLabel;
    
    @end
    
    

    TestView.m

    #import "TestView.h"
    
    @implementation TestView
    
    - (instancetype)initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];
        if (self) {
            self.nameLabel = [[UILabel alloc] initWithFrame:self.bounds];
            self.nameLabel.textAlignment = NSTextAlignmentCenter;
            [self addSubview:self.nameLabel];
        }
        return self;
    }
    
    @end
    
    
    ViewModel层:

    PersonViewModel.h

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

    PersonViewModel.m

    #import "PersonViewModel.h"
    
    @implementation PersonViewModel
    
    - (instancetype)initWithPerson:(Person *)person {
        self = [super init];
        if (self) {
            _person = person;
            if (_person.firstName.length > 0) {
                _nameText = _person.firstName;
            } else {
                _nameText = _person.lastName;
            }
        }
        return self;
    }
    
    @end
    
    

    ViewController.m

    #import "ViewController.h"
    #import "PersonViewModel.h"
    #import "TestView.h"
    
    @interface ViewController ()
    
    @property (nonatomic, strong) TestView *testView;
    @property (nonatomic, strong) PersonViewModel *viewModel;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        [self setupViews];
    
        self.testView.nameLabel.text = self.viewModel.nameText;
    
    }
    
    - (void)setupViews {
        Person *person = [[Person alloc] initWithFirstName:@"" lastName:@"胡歌"];
        self.viewModel = [[PersonViewModel alloc] initWithPerson:person];
        self.testView = [[TestView alloc] initWithFrame:CGRectMake(100, 100, CGRectGetWidth(self.view.bounds)-200, 50)];
        [self.view addSubview:self.testView];
    }
    
    @end
    
    

    相关文章

      网友评论

          本文标题:iOS中的MVC和MVVM

          本文链接:https://www.haomeiwen.com/subject/lelysrtx.html