转载自:iOS架构模式MVC、MVP、MVVM(内附demo)
文章对比了MVC、MVP、MVVM,结构清晰,示例简单,容易让人接受。所以转载至此。以供自己赏读。

MVC

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

MVC结构如图所示:


模型层:
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

MVP

**MVP的实现思路是:**用户操作View,在Presenter层完成业务逻辑处理,更新Model层,通过Presenter将数据显示在View层,完全隔断Model和View之间的通信。
我们通过接口的方式来连接view和presenter层,这样导致的问题是,如果页面过于复杂,我们的接口就会很多,为了更好的处理类似的问题,需要定义一些基类接口,把一些公共的逻辑,比如网络请求,toast,提示框等放在里面。
因为MVP不依赖Model,所以可以更好的进行组件化,把它从特定的场景中脱离出来,做到高度复用。MVP中的Presenter更多的作为框架的控制者,承担了大量的逻辑操作。

MVP结构如图:


模型层:
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

Presenter层:
PersonViewProtocol.h

#import <Foundation/Foundation.h>@protocol PersonViewProtocol <NSObject>- (void)setNameText:(NSString *)nameText;@end

Presenter.h

#import <Foundation/Foundation.h>
#import "PersonViewProtocol.h"@interface Presenter : NSObject- (void)attachView:(id <PersonViewProtocol>)view;- (void)fetchData;@end

Presenter.m

#import "Presenter.h"
#import "Person.h"@interface Presenter()@property (nonatomic, strong) Person *person;
@property (nonatomic, weak) id<PersonViewProtocol> attachView;@end@implementation Presenter- (void)attachView:(id<PersonViewProtocol>)view {self.attachView = view;
}- (void)fetchData {self.person = [[Person alloc] initWithFirstName:@"赵丽颖" lastName:@"胡歌"];if (self.person.firstName.length > 0) {[self.attachView setNameText:self.person.firstName];} else {[self.attachView setNameText:self.person.lastName];}
}@end

ViewController.m

#import "ViewController.h"
#import "PersonViewProtocol.h"
#import "Presenter.h"
#import "TestView.h"@interface ViewController ()<PersonViewProtocol>@property (nonatomic, strong) TestView *testView;
@property (nonatomic, strong) Presenter *presenter;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];[self setupViews];self.presenter = [Presenter new];[self.presenter attachView:self];[self.presenter fetchData];
}- (void)setupViews {self.testView = [[TestView alloc] initWithFrame:CGRectMake(100, 100, CGRectGetWidth(self.view.bounds)-200, 50)];[self.view addSubview:self.testView];}#pragma PersonViewProtocol
- (void)setNameText:(NSString *)nameText {self.testView.nameLabel.text = nameText;
}@end

MVVM

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


模型层:
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

(MVC、MVP、MVVM)Demo下载)
密码:pmv6

iOS进阶之架构设计MVC、MVP、MVVM(12)相关推荐

  1. iOS进阶之架构设计MVC(1)

    做iOS开发已经有5个年头了,应用开发已经比较熟练了,也来到了一个迷茫的阶段,彷佛触到了技术天花板.其实心知肚明,业务功能实现是比较得心应手了,但是在更高级的顶层设计方面还欠缺的很多.因此,必须转变思 ...

  2. iOS进阶之架构设计MVVM的理解(3)

    iOS进阶之架构设计MVC(1) iOS进阶之架构设计MVP(2) 前言: 前两篇文章已经理解MVC.MVP的设计模式.特别是MVP,比较难以理解,不好把握.需要多多实践,对比.来优化P段,找到最适合 ...

  3. Android App的设计架构:MVC,MVP,MVVM与架构经验谈

    本文转载自https://www.tianmaying.com/tutorial/AndroidMVC,原文作者周鸿博. 和MVC框架模式一样,Model模型处理数据代码不变在Android的App开 ...

  4. MVC---Android App的设计架构:MVC,MVP,MVVM与架构经验谈

    转载自: http://www.tianmaying.com/tutorial/AndroidMVC 1.架构设计的目的 通过设计使程序模块化,做到模块内部的高聚合和模块之间的低耦合.这样做的好处是使 ...

  5. iOS进阶之架构设计MVVM模式仿新闻项目(6)

    这是MVVM的第三篇文章了,之所以花这么多文章来介绍MVVM,就是为了加深对MVVM的理解,以及从不同demo的角度,对比分析那种是最适合自己的模式. 转自文章 iOS使用MVVM模式仿新闻项目 一. ...

  6. iOS进阶之架构设计MVVM的实现示例(4)

    实践是检验真理的唯一真理.让我们来看个简单的实现MVVM设计的demo例子吧. MVVM加深理解 MVVM模式将Presenter改名为ViewModel,基本上与MVP模式完全一致.唯一的区别是,它 ...

  7. iOS进阶之架构设计MVVM的实现示例(5)

    MVVM的核心在于:(个人意见) 1.MVVM的双向绑定: 2.Model与View解耦: 选用RAC实现MVVM架构,不是必要的,重要的实现架构,也可以自己用KVO实现,这里推荐使用Facebook ...

  8. iOS进阶之架构设计MVVM模式实践(11)

    1.下面通过一个实例来体会一下MVVM架构模式,下面是该工程的一级目录如下,每层之间的交互是用Block的形式来实现的 工程目录说明: 1.Request:文件夹下存储网络请求的类,下面会给出具体的实 ...

  9. mvvm模式和mvc的区别_Android 开发中的架构模式 -- MVC / MVP / MVVM

    预备知识 了解 Android 基本开发 看完本文可以达到什么程度 了解如何分析一个架构模式 掌握 MVC,MVP,MVVM 架构定义和实现 更多面试内容,面试专题,flutter视频 全套,音视频从 ...

最新文章

  1. 使用Mono管理Coyote Linux
  2. php和python哪个做第二语言-php之后如何选择第二语言?
  3. import cv2找不到模块的解决方法
  4. 太原理工大学linux与python编程r实验报告_太原理工大学算法设计与分析实验报告...
  5. 2017 php7 使用率,让PHP7达到最高性能的几个Tips
  6. 高级GIT教程-如何使用GIT调试代码?
  7. 读取.Properties配置文件
  8. 自己写的微信小程序炸金花简单版
  9. atitit.XML类库选型及object 对象bean 跟json转换方案
  10. 2N点实数序列为 N=64。用一个复数FFT程序,一次算出,并绘出。
  11. 手把手教你整合SpringCloud微服務框架-dubbo框架+zookeeper服务的注册发现+druid数据源管理
  12. c programe language learn notes 3
  13. 简单破解闪电视频转换王
  14. 计算机平均成绩等级公式,全国高校计算机等级考4.doc
  15. 如何让自己时刻冷静的方法_如何做到时刻保持冷静
  16. c语言中 输出操作是由库函数,【判断题】在 C语言中,输入操作是由库函数scanf完成,输出操作是由库函数printf完成 。...
  17. 赵小楼《天道》《遥远的救世主》深度解析(116)论天国的女人
  18. lyx安装(需先安装texlive)2021-03-14
  19. win10系统安装到服务器失败怎么解决,重装系统win10失败怎么办 重装系统失败的常见原因和解决方法...
  20. 有关刚开始学习Unity的心得

热门文章

  1. WebView的截屏实现
  2. runtime objc_msgSend
  3. android的WindowManager.addView弹窗添加
  4. PostgreSQL9.3中文手册的在线纠错
  5. [转帖]Docker里运行Docker docker in docker(dind)
  6. 三 volatile关键字
  7. edx错误的地方开始安装
  8. 安装Ubuntu 14.04后要做的5件事情
  9. 何时使用自定义HTTP 方法
  10. 关于软件外包的一些看法(转)