iOS組件化路由的設(shè)計方案,用于組件化各業(yè)務(wù)模塊的解耦

目錄

簡述

組件化路由的設(shè)計方案,用于組件化各業(yè)務(wù)模塊的解耦

您的star,是我前進的動力 ??
MTRouter地址

功能:

  • 直接配置Controller,實現(xiàn)Controller的創(chuàng)建,push,present
  • 配置Handler,用戶模塊個性化配置、對已有Controller實例參數(shù)傳遞

設(shè)計結(jié)構(gòu)

NSURLComponent介紹

首先來熟悉一下NSURLComponent的各個字段:


URL結(jié)構(gòu)

路由對象存儲設(shè)計方案

{
    module1 =     {
        firstCtrl = "type: MTRouterModelTypeController, ctrlCls: FirstViewController";
        moduleInit = "type: MTRouterModelTypeHandler";
        secondCtrl = "type: MTRouterModelTypeController, ctrlCls: SecondViewController";
    };
    module2 =     {
        firstCtrl/ttt = "type: MTRouterModelTypeController, ctrlCls: FirstViewController";
        moduleInit = "type: MTRouterModelTypeHandler";
        secondCtrl/abc = "type: MTRouterModelTypeController, ctrlCls: SecondViewController";
    };
}
  • 分為兩層的 dictionary ,第一層Key為 Scheme ,第二層Key為 host+path , 存放 MTRouterModel 對象
  • 傳參只能通過 queryparameters ,舍棄了 module://ctrl/:id 的傳遞方式,提高查詢效率

類圖結(jié)構(gòu)

類圖

MTRouter

  • 用于路由注冊
  • registerUrl:handler: 注冊一個路由,觸發(fā)handler事件,通常來說用于模塊初始化設(shè)置,模塊Controller實例之間參數(shù)傳遞, 不建議亂用
  • registerUrl:controllerCls: 注冊一個controller的路由
  • executeUrl:parameters: 、 executeUrl: 觸發(fā)一個路由,當是Controller的路由時,會返回Controller
  • pushUrl:parameters:animated:pushNavCtrl: 、pushUrl:animated:pushNavCtrl: push出url對應(yīng)的Controller
  • presentUrl:parameters:animated:presentCtrl: 、 presentUrl:animated:presentCtrl: present出url對應(yīng)的Controller
  • dictPrint 打印存儲結(jié)構(gòu)

MTRequest

  • 自定義request對象,用于觸發(fā)路由時,解析url

MTURL

  • NSURL 的擴展,添加了 absolutePathhost+path

MTRouterNoFoundController

  • 當找不到路由對應(yīng)的Controller時,返回此類,提示開發(fā)者路由未找到

補充

1.當url不存在時,返回MTRouterNoFoundController的實例。

2.當url已經(jīng)注冊時,通過 NSAssert 拋出異常

使用方式

  1. pod導(dǎo)入
    pod 'MTTheme'
  1. 路由注冊
    大家可以將路由注冊寫在 +load 中, 這樣在 pre-main 時就會自動注冊路由
    // controller路由注冊
    [MTRouter.router registerUrl:@"module://firstCtrl" controllerCls:FirstViewController.class];
    [MTRouter.router registerUrl:@"module://secondCtrl" controllerCls:SecondViewController.class];
    
    //模塊個性化路由注冊
    [MTRouter.router registerUrl:@"module://moduleInit#user" handler:^id(NSDictionary *parameters) {
        Initialization *initObj = Initialization.initObj;
        initObj.firstCtrlBackgroundColor = parameters[@"firstCtrlBackgroundColor"];
        initObj.secondCtrlBackgroundColor = parameters[@"secondCtrlBackgroundColor"];
        return nil;
    }];
  1. 路由使用
    // controller路由的使用
    [MTRouter.router pushUrl:@"module://firstCtrl" animated:YES pushNavCtrl:self.navigationController];
    [MTRouter.router presentUrl:@"module://secondCtrl" animated:YES presentCtrl:self];
    
    // 事件路由的使用
    [MTRouter.router executeUrl:@"module://moduleInit"
                     parameters:@{
                                  @"firstCtrlBackgroundColor": [UIColor purpleColor],
                                  @"secondCtrlBackgroundColor": [UIColor orangeColor]
                                                                    }];

路由性能評測

  1. 環(huán)境:
    設(shè)備:iphone6 plus
    系統(tǒng):iOS 11.3
  1. 添加2000個路由
  2. 通過添加 CFAbsoluteTimeGetCurrent() 計算加載和查詢路由運行時間

     //路由注冊
    CFAbsoluteTime routerRegStartTime = CFAbsoluteTimeGetCurrent();
    [self registerRouter];
    CFAbsoluteTime routerRegEndTime = CFAbsoluteTimeGetCurrent();
    NSLog(@"[During]路由注冊事件 during in %f seconds.", (routerRegStartTime - routerRegEndTime));
    
    //路由查詢handler
    CFAbsoluteTime routerHandlerSearchStartTime = CFAbsoluteTimeGetCurrent();
    [self moduleInit];
    CFAbsoluteTime routerHandlerSearchEndTime = CFAbsoluteTimeGetCurrent();
    NSLog(@"[During]路由查詢handler事件 during in %f seconds.", (routerHandlerSearchStartTime - routerHandlerSearchEndTime));
    
    //路由查詢Ctrl
    CFAbsoluteTime routerCtrlSearchStartTime = CFAbsoluteTimeGetCurrent();
    [MTRouter.router pushUrl:@"module://firstCtrl" animated:YES pushNavCtrl:self.navigationController];
    CFAbsoluteTime routerCtrlSearchEndTime = CFAbsoluteTimeGetCurrent();
    NSLog(@"[During]路由查詢Ctrl事件 during in %f seconds.", (routerCtrlSearchStartTime - routerCtrlSearchEndTime));
  1. 測試結(jié)果

使用路由時測試結(jié)果

  • 第一次
[During]路由注冊事件 during in -0.051065 seconds.
[During]路由查詢handler事件 during in -0.000395 seconds.
[During]路由查詢Ctrl事件 during in -0.012069 seconds.
  • 第二次
[During]路由注冊事件 during in -0.051075 seconds.
[During]路由查詢handler事件 during in -0.000451 seconds.
[During]路由查詢Ctrl事件 during in -0.009753 seconds.
  • 第三次
[During]路由注冊事件 during in -0.050468 seconds.
[During]路由查詢handler事件 during in -0.000345 seconds.
[During]路由查詢Ctrl事件 during in -0.010102 seconds.

不使用路由時Push的測試結(jié)果

  • 第一次
[During]普通push測試 during in -0.009095 seconds.
  • 第二次
[During]普通push測試 during in -0.010562 seconds.
  • 第三次
[During]普通push測試 during in -0.009745 seconds.
  1. 結(jié)論
  • 路由注冊基本維持在50毫秒
  • 路由查詢handler基本維持在3毫秒
  • 不使用路由和使用路由時間基本相同

未來優(yōu)化

1.路由服務(wù)端配置傳入

2.當Controller出現(xiàn)Bug時,通過后臺配置對應(yīng)的type為MTRouterModelTypeH5,轉(zhuǎn)到H5頁面

3.json結(jié)構(gòu)如下

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

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

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,658評論 19 139
  • 前言 隨著用戶的需求越來越多,對App的用戶體驗也變的要求越來越高。為了更好的應(yīng)對各種需求,開發(fā)人員從軟件工程的角...
    一縷殤流化隱半邊冰霜閱讀 88,167評論 214 1,098
  • 原文鏈接:https://github.com/halfrost/Halfrost-Field/blob/mast...
    hament閱讀 5,825評論 1 31
  • 今天夜晚周圍安靜下來了,自己的心也靜下來了!回顧以后種種,忽然發(fā)覺活得好平淡、好失敗!如同一個不得志的古詩人,...
    執(zhí)著_ea98閱讀 316評論 0 0
  • 上帝關(guān)上一扇門,必然為你打開一扇窗 引導(dǎo)語:2016年10月12日零點起,從余額轉(zhuǎn)入余額寶的資金,轉(zhuǎn)出時只能轉(zhuǎn)回余...
    馬克地圖閱讀 1,469評論 0 0

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