使用组件化是为了解耦处理,多个模块之间经过协议进行交互。而负责解析协议,找到目的控制器,或者是返回对象给调用者的这个组件就是路由组件。本文讲解如何使用核心的50行代码实现一个路由组件。html
以前看过挺多的关于路由管理、路由处理的文章,经常会和组件化出如今一块儿,一开始不知道为什么路由和组件化出如今一块儿,后来公司的项目中使用了路由组件(他自己也是一个组件,确切的说是一个中间人或者中介者),才忽然想明白了,原来如此。
使用组件化是为了解耦处理,多个模块之间经过协议进行交互。而负责解析协议,找到目的控制器,或者是返回对象给调用者的这个组件就是路由组件。ios
路由组件的职责主要是:git
下面会会在以上分析的基础上实现一个简单的路由组件,对应的代码能够在YTRouterDemo这里找到数据库
路由的实现包括两部分:路由注册实现以及路由使用实现数组
路由注册实现时序图:
 缓存
如上图所示,步骤很简单:架构
YTRouterActionObject
对象,用于保存path和对应的blokYTRouterActionObject
对象保存在上一步找到的位置中以上步骤对应的代码以下:组件化
- (void)registerPath:(NSString *)path actionBlock:(RouterActionBlock)actionBlock {
YTRouterActionObject *actionObject = [YTRouterActionObject new];
actionObject.path = path;
actionObject.actionBlock = actionBlock;
NSMutableDictionary *subRouter = [self subRouterMapWithPath:path];
subRouter[YTRouterActionObjectKey] = actionObject;
}
- (NSMutableDictionary *)subRouterMapWithPath:(NSString *)path {
NSArray *components = [path componentsSeparatedByString:@"/"];
NSMutableDictionary *subRouter = self.routerMap;
for (NSString *component in components) {
if (component.length == 0) {
continue;
}
if (!subRouter[component]) {
subRouter[component] = [NSMutableDictionary new];
}
subRouter = subRouter[component];
}
return subRouter;
}
复制代码
在Demo中注册的几个路由最终的配置以下,好比home/messagelist
对应的路由配置保存在<YTRouterActionObject: 0x6040000365e0>
对象中性能
Printing description of self->_routerMap:
{
home = {
"_" = "<YTRouterActionObject: 0x60c00003b040>";
messagelist = {
"_" = "<YTRouterActionObject: 0x6040000365e0>";
detail = {
"_" = "<YTRouterActionObject: 0x600000038ec0>";
};
getmessage = {
"_" = "<YTRouterActionObject: 0x600000038e80>";
};
};
};
}
复制代码
路由使用实现时序图:
 测试
如上图所示,步骤很简单:
YTRouterActionObject
对象YTRouterActionObject
对象的actionBlock,会传递一个YTRouterActionCallbackObject
对象,若是调用者须要的是返回值,可使用YTRouterActionCallbackObject
对象的actionCallbackBlock
传递一个返回值,这个actionBlock是又业务方的注册者实现的以上步骤对应的代码以下:
- (BOOL)runWithActionCallbackObject:(YTRouterActionCallbackObject *)actionCallbackObject {
// 判断是否支持scheme
if (![self canAcceptScheme:actionCallbackObject.uri.scheme]) {
return NO;
}
// 获取path对应的ActionObject
YTRouterActionObject *actionObject = [self actionObjectWithPath:actionCallbackObject.uri.path];
// 执行Path注册的对应Block
!actionObject.actionBlock ?: actionObject.actionBlock(actionCallbackObject);
return YES;
}
- (YTRouterActionObject *)actionObjectWithPath:(NSString *)path {
NSMutableDictionary *subRouter = [self subRouterMapWithPath:path];
return subRouter[YTRouterActionObjectKey];
}
复制代码
以上讲到了核心的路由注册实现
和路由使用实现
,总共代码尚未50行,因此仍是很简单的,接下来会讲下客户端的使用步骤,包括
注册的时机须要比较找,考虑到集成的方便,选择在load方法中处理路由注册,以下代码所示,添加了几个测试的路由,分两种状况来讲明下使用
一、不须要返回值
以下注册"home/messagelist"
的是一个页面跳转的路由,actionBlock的参数是一个YTRouterActionCallbackObject
对象,能够从YTRouterActionCallbackObject
对象或者到参数,关于如何传递值,会在下面的客户端调用者使用
这里讲到。而后在actionBlock处理目的页面的初始化、参数设置等步骤,而后执行页面跳转。
二、须要返回值
以下注册"home/messagelist/getmessage"
的是一个提供返回值的路由,一样也能够从YTRouterActionCallbackObject
对象获取参数,另外YTRouterActionCallbackObject
对象还有一个actionCallbackBlock
属性是专门处理返回参数给调用者的,以下的代码只是简单返回一个字符串,在更加具体的业务场景中,这里会设置接口调用、数据库查询等任务,最后把结果返回。
@implementation ModuleAUriRegister
+ (void)load {
[[YTRouterManager sharedRouterManager] registerPath:@"home/messagelist" actionBlock:^(YTRouterActionCallbackObject *callbackObject) {
MessageListViewController *messageListVC = [MessageListViewController new];
NSString *title = callbackObject.uri.params[@"title"];
messageListVC.title = title;
[[UIViewController yt_currentViewControlloer].navigationController pushViewController:messageListVC animated:YES];;
}];
[[YTRouterManager sharedRouterManager] registerPath:@"home/" actionBlock:^(YTRouterActionCallbackObject *callbackObject) {
}];
[[YTRouterManager sharedRouterManager] registerPath:@"home/messagelist/detail" actionBlock:^(YTRouterActionCallbackObject *callbackObject) {
}];
[[YTRouterManager sharedRouterManager] registerPath:@"home/messagelist/getmessage" actionBlock:^(YTRouterActionCallbackObject *callbackObject) {
// 内容回调
!callbackObject.actionCallbackBlock ?: callbackObject.actionCallbackBlock(@"message content text demo");
}];
}
@end
复制代码
一、简单的path跳转调用
使用YTRouterManager
单例对象的runWithPath
方法,传递一个注册的path参数完成跳转。
[self addActionWithTitle:@"Router页面跳转" detailText:@"home/messagelist" callback:^{
[[YTRouterManager sharedRouterManager] runWithPath:@"home/messagelist"];
}];
复制代码
二、使用URL调用和有URL参数的调用
使用YTRouterManager
单例对象的runWithURLString
方法,传递一个完整的包含了scheme/path,或者有参数的会才有参数的URL,好比"YTRouter://home/messagelist"
和 "YTRouter://home/messagelist?title=Hello Message"
,路由组件会解析出里面的scheme、path、params,进行scheme过滤处理、path查询YTRouterActionObject
对象处理、参数传递处理。
[self addActionWithTitle:@"Router使用URL调用" detailText:@"YTRouter://home/messagelist" callback:^{
[[YTRouterManager sharedRouterManager] runWithURLString:@"YTRouter://home/messagelist"];
}];
[self addActionWithTitle:@"Router使用带参数的URL调用" detailText:@"YTRouter://home/messagelist?title=Hello Message" callback:^{
[[YTRouterManager sharedRouterManager] runWithURLString:@"YTRouter://home/messagelist?title=Hello Message"];
}];
复制代码
效果以下图所示:
三、简单的path跳转调用
使用YTRouterManager
单例对象的runWithActionCallbackObject
方法,传递一个YTRouterActionCallbackObject
类型的参数,设置YTRouterActionCallbackObject
对象的uri和结果回调actionCallbackBlock参数,在actionCallbackBlock中处理返回值。
[self addActionWithTitle:@"Router获取返回值" detailText:@"home/messagelist/getmessage" callback:^{
__block id message = nil;
YTRouterActionCallbackObject *actionCallbackObject = [YTRouterActionCallbackObject new];
actionCallbackObject.uri = [[YTUri alloc] initWithPath:@"home/messagelist/getmessage"];
actionCallbackObject.actionCallbackBlock = ^(id result) {
message = result;
};
[[YTRouterManager sharedRouterManager] runWithActionCallbackObject:actionCallbackObject];
NSLog(@"message = %@", message);
}];
复制代码
iOS应用架构谈 组件化方案
iOS组件化实践方案-LDBusMediator炼就
iOS组件化思路-大神博客研读和思考
iOS 组件化方案探索
蘑菇街 App 的组件化之路
NSRecursiveLock递归锁的使用