iOS-基于面向協(xié)議MVP模式下的軟件設(shè)計(jì)--架構(gòu)設(shè)計(jì)

用慣了MVC模式,是不是覺得ViewContro'l'le'r層的東西太多了,太亂了,隨隨便便就幾百行代碼,現(xiàn)在給大家?guī)硪粋€(gè)在Android軟件開發(fā)中最常使用的MVP面向協(xié)議編程模式,我也是覺得這種模式好用,才將這種思想轉(zhuǎn)移過來的,設(shè)計(jì)模式不是指單純的應(yīng)用在某一種語言當(dāng)中,他可以試用任何一種開發(fā)語言,所以好用的東西就要分享給大家,為了便于大家理解,Demo只是最基本的完成了MVP的架構(gòu)模式,感興趣的可以下來看一看。

MVP 模式

Model-View-Presenter(MVP)是(MVC)體系結(jié)構(gòu)模式的一種變體,并主要用于構(gòu)建用戶界面。在iOS,這個(gè)模式是使用一個(gè)協(xié)議實(shí)現(xiàn)的,協(xié)議定義了接口實(shí)現(xiàn)的委托。

Presenter

在MVP模式中,協(xié)議是假定中間人的功能,所有表示邏輯被推到中間人中。

Controller v/s Presenter

  • V層

UIView和UIViewController以及子類

  • P層

中介(關(guān)聯(lián)M和V)

  • M層

數(shù)據(jù)層(數(shù)據(jù):數(shù)據(jù)庫,網(wǎng)絡(luò),文件等等)

mvp.png
mvp-delegate.png

第一步:實(shí)現(xiàn)M層

#import "LoginModel.h"
//M層
@implementation LoginModel
- (void)loginWithName:(NSString*)name pwd:(NSString*)pwd callback:(Callback)callback{
    //實(shí)現(xiàn)功能
    //例如:訪問網(wǎng)絡(luò)?訪問數(shù)據(jù)庫?
    //數(shù)據(jù)曾劃分了模塊()
    [HttpUtils postWithName:name pwd:pwd callback:^(NSString *result) {
        //解析json ,xml數(shù)據(jù)
        //然后保存數(shù)據(jù)庫
        //中間省略100行代碼
        callback(result);//返回?cái)?shù)據(jù)回調(diào)
    }];
}

@end

第二步:實(shí)現(xiàn)V層

#import <Foundation/Foundation.h>
//V層
@protocol LoginView <NSObject>

- (void)onLoginResult:(NSString*)result;

@end

第三步:實(shí)現(xiàn)P層

// P層
#import <Foundation/Foundation.h>
#import "LoginView.h"
#import "LoginModel.h"
//中介(用于關(guān)聯(lián)M層和V層)
@interface LoginPresenter : NSObject
//提供一個(gè)業(yè)務(wù)方法
- (void)loginWithName:(NSString*)name pwd:(NSString*)pwd;
- (void)attachView:(id<LoginView>)loginView;
- (void)detachView;
@end
#import "LoginPresenter.h"

//P是中介(職責(zé)是用于關(guān)聯(lián)M和V)
//P層需要:持有M層的引用和V層的引用(OOP)思想
@interface LoginPresenter ()
@property (nonatomic,strong) LoginModel *loginModel;
@property (nonatomic,strong) id<LoginView> loginView;
@end


@implementation LoginPresenter
- (instancetype)init{
    self = [super init];
    if (self) {
        //持有M層的引用
        _loginModel = [[LoginModel alloc]init];
    }
    return self;
}
//提供綁定V層方法
//綁定
- (void)attachView:(id<LoginView>)loginView{
    _loginView = loginView;
}
//解除綁定
- (void)detachView{
    _loginView = nil;
}
//實(shí)現(xiàn)業(yè)務(wù)方法
- (void)loginWithName:(NSString*)name pwd:(NSString*)pwd{
    [_loginModel loginWithName:name pwd:pwd callback:^(NSString *result) {
        if (_loginView != nil) {
            [_loginView onLoginResult:result];
        }
    }];
    
}

@end

第四步:在VIewController中使用

#import "ViewController.h"
#import "LoginView.h"
#import "LoginPresenter.h"
@interface ViewController ()<LoginView>
@property (nonatomic,strong) LoginPresenter* presenter;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _presenter = [[LoginPresenter alloc ]init];
    [_presenter attachView:self];
    //程序一旦運(yùn)行立馬執(zhí)行請(qǐng)求(測(cè)試)(按鈕或者事件)
    [_presenter loginWithName:@"188*****8*8" pwd:@"123456"];
}

- (void)onLoginResult:(NSString *)result{
    
    NSLog(@"返回結(jié)果%@",result);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    [_presenter detachView];
}
@end

這樣一個(gè)簡(jiǎn)單的MVP模式的邏輯就完成了,這里有寶藏Demo,如果覺得有幫助,不要忘記點(diǎn)個(gè)star哦!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容