作者丨changsanjianghttps://www.jianshu.com/p/13c42f4e9af1
平常开发中用户点击头像, 进入个人主页,这看似平常的操作, 背后极有可能会牵扯到多个模块。 再如: 视频模块的播放页, 有与视频相关的音乐,点击这些音乐,需要跳转到音乐模块的播放页, 这样视频与音乐模块之间,不可避免的会产生依赖或耦合。 这个问题让人脑壳疼,相信很多朋友都这样做过,写一些代理或通知, 不停的传递事件; 有时干脆直接导入另一个模块。
因为我在公司独立开发, 顾及少一点,可以拿公司项目做实践,在尝试组件化的过程中, 了解到了路由, 对于解决上述问题, 有极大的帮助。因此我想总结并与大家分享一下。
Router
NS_ASSUME_NONNULL_BEGIN@interface SJRouter : NSObject+ (instancetype)shared;- (void)handleRequest:(SJRouteRequest *)request completionHandler:(SJCompletionHandler)completionHandler;@endNS_ASSUME_NONNULL_ENDRouteRequest
NS_ASSUME_NONNULL_BEGIN@interface SJRouteRequest : NSObject- (instancetype)initWithURL:(NSURL *)URL;- (instancetype)initWithPath:(NSString *)requestPath parameters:(nullable SJParameters)parameters;@property (nonatomic, strong, readonly) NSString *requestPath;@property (nonatomic, strong, readonly, nullable) SJParameters prts;- (instancetype)init NS_UNAVAILABLE;+ (instancetype)new NS_UNAVAILABLE;@endNS_ASSUME_NONNULL_ENDRouteHandlerProtocol
NS_ASSUME_NONNULL_BEGINtypedef id SJParameters;@protocol SJRouteHandler+ (NSString *)routePath;+ (void)handleRequestWithParameters:(nullable SJParameters)parameters topViewController:(UIViewController *)topViewController completionHandler:(nullable SJCompletionHandler)completionHandler;@endNS_ASSUME_NONNULL_END 流程简单的讲,app应用中,路由识别一个请求, 将它分派给对应的handler进行处理。 这个流程非常像发送一个网络请求(拼接参数=>发起请求=>回调)。
同样的,当Router收到下面的请求时(请求视频播放页):
- (void)push:(id)sender { SJRouteRequest *request = [[SJRouteRequest alloc] initWithPath:@"video/playbackInfo" parameters:@{@"video_id":@(111)}]; [SJRouter.shared handleRequest:request completionHandler:^(id _Nullable result, NSError * _Nullable error) {#ifdef DEBUG NSLog(@"%d - %s", (int)__LINE__, __func__);#endif }];}
会尝试识别路由, 找到匹配的handler,传递必要参数:
@implementation SJRouter- (void)handleRequest:(SJRouteRequest *)request completionHandler:(SJCompletionHandler)completionHandler { NSParameterAssert(request); if ( !request ) return; Class handler = _handlersM[request.requestPath]; if ( handler ) { [handler handleRequestWithParameters:request.requestPath topViewController:_sj_get_top_view_controller() completionHandler:completionHandler]; } else { printf("\n (-_-) Unhandled request: %s", request.description.UTF8String); }}@end
最后handler进行处理。
@implementation TestViewController+ (NSString *)routePath { return @"video/playbackInfo";}+ (void)handleRequestWithParameters:(nullable SJParameters)parameters topViewController:(UIViewController *)topViewController completionHandler:(nullable SJCompletionHandler)completionHandler { TestViewController *vc = [TestViewController new]; vc.completionHandler = completionHandler; [topViewController.navigationController pushViewController:vc animated:YES];}@end
至此, 我们再回过头看刚开始举的那个例子:
视频模块的播放页, 有与视频相关的音乐,点击这些音乐,需要跳转到音乐模块的播放页。
此时,可以让视频模块依赖Router, 进行跳转请求。这看起来都是依赖,实则两者差别很大了。
路由不止能处理跳转音乐模块的请求, 依赖也从多个变成只依赖Router即可。。。
在删除某个依赖模块时, 需要删除依赖的代码, 很烦的, 对吧。
吧啦吧啦吧啦吧啦吧啦。。。
所以点击跳转音乐模块,可以替换成如下操作, 发起请求:
SJRouteRequest *request = [[SJRouteRequest alloc] initWithPath:@"audio/playbackInfo" parameters:@{@"audio_id":@(232)}]; [SJRouter.shared handleRequest:request completionHandler:^(id _Nullable result, NSError * _Nullable error) {#ifdef DEBUG NSLog(@"%d - %s", (int)__LINE__, __func__);#endif }];
router找到对应的handler, 让其进行处理。
Hander
从开始到现在,可以看出Handler就是最终执行请求的那个家伙。 相信大家都有疑问, 如何成为一个Handler?
很简单,它是自动的(参见Router), 只要某个类遵守了SJRouteHandlerProtocol, 它便成为了一个Handler。再来看一遍协议吧。
NS_ASSUME_NONNULL_BEGINtypedef id SJParameters;@protocol SJRouteHandler+ (NSString *)routePath;+ (void)handleRequestWithParameters:(nullable SJParameters)parameters topViewController:(UIViewController *)topViewController completionHandler:(nullable SJCompletionHandler)completionHandler;@endNS_ASSUME_NONNULL_END
routePath: 即路径, 表示handler能够处理的路径。当发起请求时, Router会通过路径获取到对应的handler, 交给其进行处理。
handleRequestWithParameters。。。: handler进行的处理。
在整个请求过程中,Router做的事情实质上就是在众多Handler中寻找命中注定的那一个。如何寻找呢?为什么遵守了SJRouteHandlerProtocol便自动成为了Handler呢?这自然要归功于Runtime的强大力量,我们先看如何实现吧。
@implementation SJRouter - (instancetype)init { self = [super init]; if ( !self ) return nil; _handlersM = [NSMutableDictionary new]; int count = objc_getClassList(NULL, 0); Class *classes = (Class *)malloc(sizeof(Class) * count); objc_getClassList(classes, count); Protocol *p_handler = @protocol(SJRouteHandler); for ( int i = 0 ; i < count ; ++ i ) { Class cls = classes[i]; for ( Class thisCls = cls ; nil != thisCls ; thisCls = class_getSuperclass(thisCls) ) { if ( !class_conformsToProtocol(thisCls, p_handler) ) continue; if ( ![(id)thisCls respondsToSelector:@selector(routePath)] ) continue; if ( ![(id)thisCls respondsToSelector:@selector(handleRequestWithParameters:topViewController:completionHandler:)] ) continue; _handlersM[[(id)thisCls routePath]] = thisCls; break; } } if ( classes ) free(classes); return self;}@end
objc_getClassList: 很明显了, 获取App所有类
。
class_conformsToProtocol: 该类是否遵守某个协议。
得益于Runtime的这两个函数,即可获取到众多的Handler。 当发起请求时, 在众多Handler中寻找注定的那一个, 岂不是易如反掌。
App发起的跳转请求,更多到是内部页面之间的跳转, 我们最需要关注的就是它的路径,所以整个路由都是围绕着路径去跳转的, 而像URL中的scheme和host,体现出来的作用倒是不大。
因此, Request持有每个请求的路径, 以及必要的参数, 之后再无多余操作。
最后,文章难免会有疏漏与考虑不周之处, 望请大家指正。
推荐↓↓↓
长
按
关
注
?【16个技术公众号】都在这里!
涵盖:程序员大咖、源码共读、程序员共读、数据结构与算法、黑客技术和网络安全、大数据科技、编程前端、Java、Python、Web编程开发、Android、iOS开发、Linux、数据库研发、幽默程序员等。
