初探MVP(面向協(xié)議的架構(gòu))

文末附Demo

1、架構(gòu)圖

MVP.jpg
稍加解釋:
1、持有關(guān)系:View持有Presenter;Presenter持有Model
2、持有者一般需要接收被持有者的狀態(tài)變化,一般采用觀察者模式/代理模式。常用方式有代理/block/通知。
3、Presenter可以有多個(gè)
4、各部分職責(zé):
  - View負(fù)責(zé)組合UI,接收UI視圖的交互回調(diào),并把業(yè)務(wù)相關(guān)的處理傳遞給Presenter;
  - Presenter負(fù)責(zé)處理業(yè)務(wù)(case),包括交互邏輯,數(shù)據(jù)變化邏輯;接收Model的通知;
  - Model負(fù)責(zé)加工數(shù)據(jù),提供數(shù)據(jù)的獲取/更新/等等接口,以及數(shù)據(jù)變化的通知協(xié)議;

PS: 初次探討是的基本的MVP架構(gòu),在此基礎(chǔ)上還可以進(jìn)行更細(xì)粒度的拆分,比如路由層、服務(wù)層等

2、實(shí)現(xiàn)(附demo)

1、View層

1、持有Presenter并遵守&實(shí)現(xiàn)其協(xié)議
@interface MVPListViewController () <MVPListPresenterDelegate>

/// UI
@property (nonatomic, strong) UITableView *tableView;

@end

// 在MVPListViewController+Private.h文件中
@interface MVPListViewController (/* Private */)

/// presenter
@property (nonatomic, strong) MVPListPresenter *listPresenter;

@end

@implementation MVPListViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        // 持有Presenter并遵守協(xié)議
        self.listPresenter = [[MVPListPresenter alloc] init];
        self.listPresenter.delegate = self;
    }
    return self;
}

// 實(shí)現(xiàn)協(xié)議, 接收數(shù)據(jù)更新的通知
- (void)updateDataWithCompletion:(void (^)(BOOL))completion
{
    [self.tableView reloadData];
    // handle status if presenter need it. here only a easy example.
    !completion ?: completion(YES);
}

2、Presenter層 (業(yè)務(wù)層)

1、持有Model層,并遵守協(xié)議
@interface MVPListPresenter () <MVPListDatasourceDelegate>

///
@property (nonatomic, strong) MVPListDatasource *datasource;
///
@property (nonatomic, strong) NSArray<MVPListMoel *> *dataArray;

@end

@implementation MVPListPresenter

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.datasource = [[MVPListDatasource alloc] init];
        self.datasource.delegate = self;
    }
    return self;
}

#pragma mark - DatasourceDelegate
- (void)updateWithDataArray:(NSArray *)dataArray
{
    self.dataArray = dataArray.copy;
    [self update];
}

3、Model層

1、處理數(shù)據(jù),并對(duì)數(shù)據(jù)進(jìn)行加工
.h文件
@interface MVPListDatasource : NSObject

/*
 這里可以采用block的方式代替delegate, 具體看個(gè)人或團(tuán)隊(duì)喜好.
 */
@property (nonatomic, weak) id<MVPListDatasourceDelegate> delegate;
// 將獲取的數(shù)據(jù)加工成MVPListModel類型的數(shù)據(jù) (數(shù)據(jù)模型化,規(guī)范使用)
- (void)requestDataWithCompletionHandler:(void (^)(NSArray<MVPListMoel *> *data))completionHandler;

@end
.m文件
#pragma mark - Data
- (void)requestDataWithCompletionHandler:(void (^)(NSArray<MVPListMoel *> * _Nonnull))completionHandler
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        !completionHandler ?: completionHandler(self.dataArray);
    });
}
// 提供協(xié)議,以便數(shù)據(jù)發(fā)生變化及時(shí)通知到Presenter
@protocol MVPListDatasourceDelegate <NSObject>

- (void)updateWithDataArray:(NSArray *)dataArray;

@end

Demo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容