iOS组件化路由的设计方案,用于组件化各业务模块的解耦

2018-05-09  本文已影响0人  ireliaBR

目录

简述

组件化路由的设计方案,用于组件化各业务模块的解耦

您的star,是我前进的动力 😊
MTRouter地址

功能:

设计结构

NSURLComponent介绍

首先来熟悉一下NSURLComponent的各个字段:


URL结构

路由对象存储设计方案

{
    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";
    };
}

类图结构

类图

MTRouter

MTRequest

MTURL

MTRouterNoFoundController

补充

1.当url不存在时,返回MTRouterNoFoundController的实例。

2.当url已经注册时,通过 NSAssert 抛出异常

使用方式

  1. pod导入
    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. 环境:
    设备:iphone6 plus
    系统: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. 测试结果

使用路由时测试结果

[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的测试结果

[During]普通push测试 during in -0.009095 seconds.
[During]普通push测试 during in -0.010562 seconds.
[During]普通push测试 during in -0.009745 seconds.
  1. 结论

未来优化

1.路由服务端配置传入

2.当Controller出现Bug时,通过后台配置对应的type为MTRouterModelTypeH5,转到H5页面

3.json结构如下

{
    "module1": {
        "firstCtrl": {
            "identifier": "FirstViewController",
            "type": "MTRouterModelTypeController",
        },
        "secondCtrl": {
            "identifier": "http://www.meitu.com",
            "type": "MTRouterModelTypeH5",
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读