{"id":29639527,"url":"https://github.com/ryanleely/tinypart","last_synced_at":"2025-09-24T16:13:18.873Z","repository":{"id":56924306,"uuid":"128731003","full_name":"RyanLeeLY/TinyPart","owner":"RyanLeeLY","description":"TinyPart is an iOS modularization framework implemented by Ojective-C. It also supports URL-routing and inter-module communication.  TinyPart是一个由Objective-C编写的面向协议的iOS模块化框架，同时它还支持URL路由和模块间通信机制。","archived":false,"fork":false,"pushed_at":"2019-09-12T06:32:27.000Z","size":921,"stargazers_count":133,"open_issues_count":2,"forks_count":13,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-08-23T09:05:16.060Z","etag":null,"topics":["framework","ios","modular","module","objc","objective-c","registered-services","route","router","service","tinypart","url"],"latest_commit_sha":null,"homepage":"https://github.com/RyanLeeLY/TinyPart","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RyanLeeLY.png","metadata":{"files":{"readme":"README-zhCN.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-04-09T07:20:21.000Z","updated_at":"2025-08-07T10:23:50.000Z","dependencies_parsed_at":"2022-08-20T22:50:24.681Z","dependency_job_id":null,"html_url":"https://github.com/RyanLeeLY/TinyPart","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/RyanLeeLY/TinyPart","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RyanLeeLY%2FTinyPart","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RyanLeeLY%2FTinyPart/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RyanLeeLY%2FTinyPart/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RyanLeeLY%2FTinyPart/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RyanLeeLY","download_url":"https://codeload.github.com/RyanLeeLY/TinyPart/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RyanLeeLY%2FTinyPart/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":276778710,"owners_count":25703226,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-24T02:00:09.776Z","response_time":97,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["framework","ios","modular","module","objc","objective-c","registered-services","route","router","service","tinypart","url"],"created_at":"2025-07-21T20:38:05.272Z","updated_at":"2025-09-24T16:13:18.825Z","avatar_url":"https://github.com/RyanLeeLY.png","language":"Objective-C","readme":"# TinyPart\nTinyPart是一个由Objective-C编写的面向协议的iOS模块化框架，同时它还支持URL路由和模块间通信机制。\n## 安装\n### cocoapods\n\n```ruby\npod 'TinyPart'\n```\n\n## 特点\n* **面向协议**。TinyPart中模块和服务都是面向协议的。服务面向协议的好处在于对于接口维护比较友好，任何接口的变动在编译时都是可见的，但是大型项目有可能会面临大量需要维护的协议，这也是不可忽视的缺点。\n\n* **动态化**。TinyPart中模块和服务的注册都是在运行时完成的，可以根据具体需要调整模块的注册和启动时间，异步启动模块对于优化APP首屏加载时间会有帮助。\n\n* **路由服务**。我们知道面向协议的服务虽然对于接口维护比较方便，但是模块间相互调用各自服务，是需要知道对方协议的，这样最后可能会导致所有的协议需要暴露给所有的模块。比如现在有10个模块每个模块有1个服务协议，在上述情况下每个模块需要知道另外9个模块的协议，这相当于每个协议都被@import了9次，所有协议总共将会被@import 9*10=90次，这对于想做完全代码隔离的模块化来说是个噩梦。通过路由服务则很好地平衡了模块间调用和依赖问题，顺便也解决了跨APP跳转的问题。\n\n* **URL路由**。在路由基础上，只需要再增加简单的1到2行代码就可以实现通过APPScheme的URL路由机制。\n\n* **多级模块有向通信**。一般来说，完全去耦合的模块间通信方案大概是两种：URL和通知```NSNotification```。URL解决了模块间服务相互调用的问题，但是如果想要通过URL实现一个观察者模式则会变得非常复杂。这时候大家可能会偏向于选择通知，但是由于通知是全局性的，这样会导致任何一条通知可能会被APP内任何一个模块所使用，久而久之这些通知会变得难以维护。\u003cbr\u003e所谓**多级模块有向通信**，则是在```NSNotification```基础上对通知的传播方向进行了限制，底层模块对上层模块的通知称为**广播**```Broadcast```，上层模块对底层模块或者同层模块的通知称为**上报**```Report```。这样做有两个好处：一方面更利于通知的维护，另一方面可以帮助我们划分模块层级，如果我们发现有一个模块需要向多个同级模块进行```Report```那么这个模块很有可能应该被划分到更底层的模块。\n\n## 架构说明\n![架构图](https://github.com/RyanLeeLY/TinyPart/blob/master/TinyPart.jpeg)\n\n## 用法\n### 初始化\n* 继承 TPAppDelegate，初始化TPContext\n\n```Objective-C\n#import \"TinyPart.h\"\n\n@interface AppDelegate : TPAppDelegate\n@property (strong, nonatomic) UIWindow *window;\n@end\n\n@implementation AppDelegate\n@synthesize window;\n\n- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {\n    [TPMediator sharedInstance].deleagate = self;\n\n    [TPContext sharedContext].launchOptions = launchOptions;\n    [TPContext sharedContext].application = application;\n    context.configPlistFileName = @\"TinyPart.bundle/TinyPart.plist\";\n    context.modulePlistFileName = @\"TinyPart.bundle/TinyPart.plist\";\n    context.servicePlistFileName = @\"TinyPart.bundle/TinyPart.plist\";\n    context.routerPlistFileName = @\"TinyPart.bundle/TinyPart.plist\";\n    [TinyPart sharedInstance].context = [TPContext sharedContext];\n    \n    return [super application:application didFinishLaunchingWithOptions:launchOptions];\n}\n@end\n```\n\n* 新建plist文件'TinyPart.bundle/TinyPart.plist'\n\n![create_plist](https://github.com/RyanLeeLY/TinyPart/blob/master/Pics/create_plist.jpeg)\n\n### 模块Module\n* **定义并注册一个模块**\n\n```Objective-C\n#import \"TinyPart.h\"\n\n@interface TestModule : NSObject \u003cTPModuleProtocol\u003e\n@end\n\n@implementation TestModule\nTP_MODULE_AUTO_REGISTER // 自动注册模块，动态注册模块\n\nTP_MODULE_ASYNC         // 异步启动模块，优化开屏性能\n\nTP_MODULE_PRIORITY(1)   // 模块启动优先级，优先级高的先启动\n\nTP_MODULE_LEVEL(TPModuleLevelBasic)     // 模块级别：基础模块\n\n- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {\n    return YES;\n}\n\n- (void)moduleDidLoad:(TPContext *)context {\n    switch (context.env) {\n        case TPRunningEnviromentTypeDebug: {\n            NSLog(@\"%@\", @\"TestModule1 moduleDidLoad: debug\");\n            break;\n        }\n        case TPRunningEnviromentTypeRelease: {\n            NSLog(@\"%@\", @\"TestModule1 moduleDidLoad: release\");\n            break;\n        }\n    }\n}\n@end\n```\n\n如果没有使用`TP_MODULE_AUTO_REGISTER `自动注册宏的话，还需要将TestModule1加到`TinyPart.plist`配置文件中。\n\n![register_module](https://github.com/RyanLeeLY/TinyPart/blob/master/Pics/register_module.jpeg)\n\n### 服务Service用法\n* **定义并注册一个服务Service**。Service可自定义单例模式或多例模式。\n\n```Objective-C\n#import \"TPServiceProtocol.h\"\n\n@protocol TestModuleService1 \u003cTPServiceProtocol\u003e\n- (void)function1;\n@end\n\n@interface TestModuleService1Imp : NSObject \u003cTestModuleService1\u003e\n@end\n\n@implementation TestModuleService1Imp\nTPSERVICE_AUTO_REGISTER(TestModuleService1) // 自动注册服务\n\n- (void)function1 {\n    NSLog(@\"%@\", @\"TestModuleService1 function1\");\n}\n@end\n```\n\n如果没有使用`TPSERVICE_AUTO_REGISTER `自动注册宏的话，还需要将TestModuleService1加到`TinyPart.plist`配置文件中。\n\n![register_service](https://github.com/RyanLeeLY/TinyPart/blob/master/Pics/register_service.jpeg)\n\n* **访问服务Sevice**\n\n```Objective-C\nid\u003cTestModuleService1\u003e service1 = [[TPServiceManager sharedInstance] serviceWithName:@\"TestModuleService1\"];\n    [service1 function1];\n```\n\n### 路由Router用法\n* **定义并注册一个路由Router**\n\n```Objective-C\n#import \"TPRouter.h\"\n\n@interface TestRouter : TPRouter\n@end\n#import \"TinyPart.h\"\n\n@implementation TestRouter\nTPROUTER_AUTO_REGISTER  // 自动注册路由\n\n// APP身份验证，需要实现TPMediatorDelegate中的身份验证回调\nTPRouter_AUTH_REQUIRE(@\"action1\", @\"action2\")\n\nTPROUTER_METHOD_EXPORT(action1, {\n    NSLog(@\"TestRouter action1 params=%@\", params);\n    return nil;\n});\n\nTPROUTER_METHOD_EXPORT(action2, {\n    NSLog(@\"TestRouter action2 params=%@\", params);\n    return nil;\n});\n@end\n```\n\n如果没有使用`TPROUTER_AUTO_REGISTER`自动注册宏的话，还需要将TestModuleService1加到`TinyPart.plist`配置文件中。\n\n![register_router](https://github.com/RyanLeeLY/TinyPart/blob/master/Pics/register_router.jpeg)\n\n* **使用路由Router**\n\n```Objective-C\n[[TPMediator sharedInstance] performAction:@\"action1\" router:@\"Test\" params:@{}];\n```\n\n### URL路由\n\n* **新建**```TinyPart.bundle/TinyPart.plist```，并注册一条**URL Scheme**\n\n```\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\"?\u003e\n\u003c!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"\u003e\n\u003cplist version=\"1.0\"\u003e\n\u003cdict\u003e\n\t\u003ckey\u003eAPPURLSchemes\u003c/key\u003e\n\t\u003carray\u003e\n\t\t\u003cstring\u003etinypart\u003c/string\u003e\n\t\u003c/array\u003e\n\u003c/dict\u003e\n\u003c/plist\u003e\n```\n\n* **定义一条URL路由规则**。在已有的```TestRouter```基础上，我们只需要新建一个```TPMediator+Test```的扩展。\n\n```Objective-C\n@implementation TPMediator (Test)\n+ (void)load {\n    // 声明TestRouter对应的Host\n    TPURLHostForRouter(@\"com.tinypart.test\", TestRouter) // tinypart://com.tinypart.test\n    \n    // 声明TestRouter中action1对应的Path\n    TPURLPathForActionForRouter(@\"/action1\", action1, TestRouter);  // tinypart://com.tinypart.test/action1\n}\n@end\n```\n* **使用URL路由**\n\n```Objective-C\nNSURL *url = [NSURL URLWithString:@\"tinypart://com.tinypart.test/action1?id=1\u0026name=tinypart\"];\n[[TPMediator sharedInstance] openURL:url];\n```\n\n### 有向通信\n* **发送**。这里注意前面提到的，底层模块对上层模块的通知称为**广播**```Broadcast```，上层模块对底层模块或者同层模块的通知称为**上报**```Report```。模块级别分为**Basic、Middle、Topout**三个级别。\n\n```Objective-C\nTPNotificationCenter *center2 = [TestModule2 tp_notificationCenter];\n\n[center2 reportNotification:^(TPNotificationMaker *make) {\n    make.name(@\"report_notification_from_TestModule2\");\n} targetModule:@\"TestModule1\"];\n    \n[center2 broadcastNotification:^(TPNotificationMaker *make) {\n\tmake.name(@\"broadcast_notification_from_TestModule2\").userInfo(@{@\"key\":@\"value\"}).object(self);\n}];\n```\n\n* **接收**\n\n```Objective-C\nTPNotificationCenter *center1 = [TestModule1 tp_notificationCenter];\n// Observer销毁后自动释放\n[center1 addObserver:self selector:@selector(testNotification:) name:@\"report_notification_from_TestModule2\" object:nil];\n```\n\n## 参考项目\n[**BeeHive**](https://github.com/alibaba/BeeHive)\n\n[**ReactNative**](http://facebook.github.io/react-native/)\n\n## 开源许可证\nTinyPart is available under the MIT license. See the [LICENSE](https://github.com/RyanLeeLY/TinyPart/blob/master/LICENSE) file for more info.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryanleely%2Ftinypart","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fryanleely%2Ftinypart","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryanleely%2Ftinypart/lists"}