ios零碎记录Swift开发iOS程序猿

iOS中的MVP模式初探

2017-11-05  本文已影响698人  小冰山口

好久没更新简书了, 都忘了该怎么写了.

DEMO在这里

以前在项目中都写的是MVC模式, 由于现在在项目中要学习MVP模式, 所以找了一个Demo研究了一下. 就简单说说自己的看法吧.

先说一下MVC模式, 示意图如下:
MVC模式示意图

最简单的比方, 我们拿到一个模型数组了之后, 这个就相当于是一个数据源.

数据源经过简单地处理加工, 比如在tableView中, 我们可能会使用数据源方法, 将模型数组中的元素取出来, 传递给View层, 比如cell

cell展示模型中的数据

那么MVP模式又是怎样的呢?请看下图
MVP模式示意图

从上图可以看出, 从MVC中又抽象出了P层, 即Presenter层

关于C端和P端的循环引用的问题, 直接用weak关键字就可以解决了
利用代码来说明一下问题:
#import <UIKit/UIKit.h>

/**
  作为P : presenter 是管理 view viewController model这个三个中间人,负责UI刷新
  视图的交互总是和VC 关联着的
 */
@protocol TGPresenterProtocol <NSObject>

@optional

// 处理View视图相关操作 -- 协议的遵守者
- (void)setView:(NSObject *)view;
// 处理事件的相关响应
- (void)setViewController:(UIViewController *)viewController;

// 展示
- (void)present;
// 加载model 
- (void)presentWithModel:(id)model viewController:(UIViewController *)viewController;


@end

把需要管理的view传递给P,

- (instancetype)initWithTableView:(UITableView *)view{
    
    self = [super init];
    if (!self) {
        return nil;
    }
    _view = view;
    _view.delegate = self;
    _view.dataSource = self;
    _view.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 自适应高度
    _view.rowHeight = UITableViewAutomaticDimension;
    _view.estimatedRowHeight = 100;
    return self;
}

- (void)setView:(UITableView *)view{
    // 设置视图
    _view = view;
    _view.delegate = self;
    _view.dataSource = self;
    _view.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 自适应高度
    _view.rowHeight = UITableViewAutomaticDimension;
    _view.estimatedRowHeight = 100;
}
- (void)loadHPData{
    
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"testCellData" ofType:@"json"];
    NSData *jsonData = [NSData dataWithContentsOfFile:dataPath];
    NSError *error;
    NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
    if (error) {
        NSLog(@"error = %@",error.localizedDescription);
    }
    NSLog(@"dataDic = %@",dataDic);
    // model 要处理好数据的显示格式
    self.hpModel = [[CellSelfSizeModel alloc] initWithDic:dataDic];
    // 刷新
    [self present];
    
} 
- (void)present{
    
    [self.view reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.hpModel.data.listArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *ID = @"";
    cellType type;
    
    CelllSelfSizeListModel *newsList;
    if (indexPath.row > self.hpModel.data.listArray.count - 1) {
        newsList = nil;
    }else{
        newsList = self.hpModel.data.listArray[indexPath.row];
    }

    if (newsList.orginImg.length>0) {
        // 有图片
        type = NewsInListCellTypeHavePic;
        
    }else{
        // 无图片
        type = NewsInListCellTypeOnlyWord;
    }
    
    ID = [NSString stringWithFormat:@"reusId%ld",(long)type];
    
    SelfSizeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[SelfSizeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID cellType:type];
    }
    
    cell.cellModel = newsList;
    
    return cell;
}
这样就实现了Controller, View, Model的解耦. 给大家看看控制器做的事情:
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"MVP Demo";
//    布局
    [self initViews];
    [self setUpConstraints];
    
    self.hpPresenter = [TGHPPresenter new];
    // 视图对象
    self.hpPresenter.view = self.tableView;
    // 控制器对象
    self.hpPresenter.viewController = self;
    // 外边是要传入参进去的 -- 数据模型
    [self.hpPresenter loadHPData];
    
}

只需要初始化P层, 然后调P层的接口就可以了. 至于P层内部的逻辑, 我不需要知道

V层也只专注于视图的创建
M层只专注于模型的构建(字典->模型)
这样分层, 解耦的思想在程序设计中是极为重要的. 其实也可以看出MVP是对MVC模式的进一步抽象.

代码Demo是我们老大写的, 我只是分析了一波, 😓

我已经用Swift写了一遍, Demo见最上面. 谢谢关注
上一篇 下一篇

猜你喜欢

热点阅读