目錄
簡述
組件化路由的設(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對象 - 傳參只能通過
query和parameters,舍棄了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的擴展,添加了absolutePath為host+path
MTRouterNoFoundController
- 當找不到路由對應(yīng)的Controller時,返回此類,提示開發(fā)者路由未找到
補充
1.當url不存在時,返回MTRouterNoFoundController的實例。
2.當url已經(jīng)注冊時,通過 NSAssert 拋出異常
使用方式
- pod導(dǎo)入
pod 'MTTheme'
- 路由注冊
大家可以將路由注冊寫在+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;
}];
- 路由使用
// 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]
}];
路由性能評測
- 環(huán)境:
設(shè)備:iphone6 plus
系統(tǒng):iOS 11.3
- 添加2000個路由
- 通過添加
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));
- 測試結(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.
- 結(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",
}
}
}