iOS中MVP架构

来源:互联网 发布:家有儿女知乎 编辑:程序博客网 时间:2024/05/20 01:38

浅谈一下MVP架构模式

M:即model,对于Model层也是数据层。它区别于MVC架构中的Model,在这里不仅仅只是数据模型。在MVP架构中Model它负责对数据的存取操作,例如对数据库的读写,网络的数据的请求等

V:即UIView、UIViewController..是显示数据(model)并且将用户指令(events)传送到presenter以便作用于那些数据的一个接口。View通常含有Presenter的引用。在Android开发中通常将Activity或者Fragment作为View层。

P:Presenter,对于Presenter层他是连接View层与Model层的桥梁并对业务逻辑进行处理。在MVP架构中Model与View无法直接进行交互。所以在Presenter层它会从Model层获得所需要的数据,进行一些适当的处理后交由View层进行显示。这样通过Presenter将View与Model进行隔离,使得View和Model之间不存在耦合,同时也将业务逻辑从View中抽离。

PS:P层需要持有M和V,并作逻辑的处理


实际上MVP是在MVC上发展而来,是MVC的变种。MVP主要是将UIViewController里面复杂的逻辑处理和网络请求等剥离出来,让数据和View分离,而P层作为一个协调,是整个程序有条不紊的工作。

在MVC中用户的请求会到controller,由controller从model中取出数据,选择合适的View,把处理结果呈现在View上;

在MVP上,用户的请求首先到达Viewview传递请求到特定的Presenter,Presenter从model获取数据后,再把处理结果通过接口返回View;

所以MVP在MVC的基础上将M和V的联系彻底断开,用P进行连接。

在提一下MVVM,MVVM也是MVC的一个加强版,我们正式连接了View 和View Controller,并将表示逻辑从Controller中移出,放到了一个新的对象里,即ViewModel中。

MVVM 模式将 Presenter 改名为 ViewModel,基本上与 MVP 模式完全一致。唯一的区别是,它采用双向绑定(data-binding):View的变动,自动反映在 ViewModel。

举例

下面是一个看一个简单的 Person Model 以及相应的 View Controller。

@interface Person : NSObject- (instancetype)initwithSalutation:(NSString *)salutation firstName:(NSString *)firstName lastName:(NSString *)lastName birthdate:(NSDate *)birthdate;@property (nonatomic, readonly) NSString *salutation;@property (nonatomic, readonly) NSString *firstName;@property (nonatomic, readonly) NSString *lastName;@property (nonatomic, readonly) NSDate *birthdate;@end

现在假设有一个 PersonViewController,在 viewDidLoad里,只需要基于它的 model属性设置一些Label即可。

- (void)viewDidLoad {     [super viewDidLoad];     if (self.model.salutation.length > 0) {         self.nameLabel.text = [NSString stringWithFormat:@"%@ %@ %@",self.model.salutation,self.model.firstName, self.model.lastName];    } else {         self.nameLabel.text = [NSString stringWithFormat:@"%@ %@", self.model.firstName,self.model.lastName];    }     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];     [dateFormatter setDateFormat:@"EEEE MMMM d, yyyy"];    self.birthdateLabel.text = [dateFormatter stringFromDate:model.birthdate];}

现在来看怎样通过一个ViewModel来增强它。

//ViewModel.h@interface PersonViewModel : NSObject- (instancetype)initWithPerson:(Person *)person;@property (nonatomic, readonly) Person *person;@property (nonatomic, readonly) NSString *nameText;@property (nonatomic, readonly) NSString *birthdateText;@end

ViewModel实现如下

//ViewModel.m@implementation PersonViewModel- (instancetype)initWithPerson:(Person *)person {     self = [super init];     if (!self) return nil;     _person = person;     if (person.salutation.length > 0) {     _nameText = [NSString stringWithFormat:@"%@ %@ %@", self.person.salutation, self.person.firstName, self.person.lastName];     } else {         _nameText = [NSString stringWithFormat:@"%@ %@", self.person.firstName, self.person.lastName];    }     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];    [dateFormatter setDateFormat:@"EEEE MMMM d, yyyy"];     _birthdateText = [dateFormatter stringFromDate:person.birthdate];     return self;}@end

现在我们已将viewDidLoad中的表示逻辑放入我们的 View Model 里了。此时,我们新的 viewDidLoad,就会非常轻量:

- (void)viewDidLoad { [super viewDidLoad];     self.nameLabel.text = self.viewModel.nameText;     self.birthdateLabel.text = self.viewModel.birthdateText;}
参考文章
被误解的 MVC 和被神化的 MVVM







0 0
原创粉丝点击