美文网首页
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基础 1.mvc、mvvm iOS 关于MVC和MVVM设计模式的那些事 2.autoReless 自动释放...

  • iOS MVVM架构总结

    参考:iOS 中MVC设计模式iOS MVVM架构iOS MVVM-框架介绍iOS 架构模式MVVM的实践总结iO...

  • iOS-19 MVC和MVVM

    1 iOS MVC、MVVM、MVP详解 - 简书 2 浅谈 MVC、MVP 和 MVVM 架构模式 - Coco...

  • iOS 设计模式

    MVC和MVVM?它是iOS开发中阻力最低的架构模式。MVC代码量最小,设计开销最小的模式。MVC常见的问题: 在...

  • MVC、MVP、MVVM

    MVC MVP MVVM 各种大神博客介绍:唐巧:被误解的 MVC 和被神化的 MVVM 组件化讨论《iOS应用架...

  • iOS-MVC,MVP,MVVM及VIPER简介

    iOS中MVC,MVP,MVVM及VIPER设计模式介绍的文章有很多,开发过程MVC最常见的模式,MVVM也经常被...

  • MVVM

    1.iOS 关于MVC和MVVM设计模式的那些事

  • 设计模式概览

    iOS MVC中的设计模式 MVC是一种用户界面架构模式,同样的MVVM、MVP等都是MVC的变种,iOS平台中用...

  • iOS开发学习笔记:对MVC、MVVM建立认识

    本文参考: iOS 架构模式 - 简述 MVC, MVP, MVVM 和 VIPER (译) 浅谈 MVC、MVP...

  • iOS中的MVC和MVVM

    MVC MVC的实现思路是:用户操作View,在Controller层完成业务逻辑处理,更新Model层,将数据显...

网友评论

      本文标题:iOS中的MVC和MVVM

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