{"id":19243578,"url":"https://github.com/devdragonli/protocolservicekit","last_synced_at":"2025-04-21T09:33:16.572Z","repository":{"id":62451209,"uuid":"280585675","full_name":"DevDragonLi/ProtocolServiceKit","owner":"DevDragonLi","description":"iOS组件通信中间件(Protocol Service),Adapter Swift/Objective-C","archived":false,"fork":false,"pushed_at":"2021-08-16T06:04:57.000Z","size":153,"stargazers_count":143,"open_issues_count":0,"forks_count":16,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-19T07:48:02.252Z","etag":null,"topics":["module","objective-c","protocol-service","service","swift"],"latest_commit_sha":null,"homepage":"","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/DevDragonLi.png","metadata":{"files":{"readme":"README.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":"2020-07-18T05:03:34.000Z","updated_at":"2024-06-23T13:41:47.000Z","dependencies_parsed_at":"2022-11-01T23:34:02.367Z","dependency_job_id":null,"html_url":"https://github.com/DevDragonLi/ProtocolServiceKit","commit_stats":null,"previous_names":["devdragonli/protocolservicemanger"],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DevDragonLi%2FProtocolServiceKit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DevDragonLi%2FProtocolServiceKit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DevDragonLi%2FProtocolServiceKit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DevDragonLi%2FProtocolServiceKit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DevDragonLi","download_url":"https://codeload.github.com/DevDragonLi/ProtocolServiceKit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250032380,"owners_count":21363828,"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","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":["module","objective-c","protocol-service","service","swift"],"created_at":"2024-11-09T17:18:40.339Z","updated_at":"2025-04-21T09:33:11.559Z","avatar_url":"https://github.com/DevDragonLi.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ProtocolServiceKit\n\n\u003c/p\u003e\n\u003cp align='center'\u003e\n\n[![Version](https://img.shields.io/cocoapods/v/ProtocolServiceKit.svg?style=flat)](https://cocoapods.org/pods/ProtocolServiceKit)\n[![License](https://img.shields.io/cocoapods/l/ProtocolServiceKit.svg?style=flat)](https://cocoapods.org/pods/ProtocolServiceKit)\n[![Platform](https://img.shields.io/cocoapods/p/ProtocolServiceKit.svg?style=flat)](https://cocoapods.org/pods/ProtocolServiceKit)\n\u003c/p\u003e\n\n\u003c/p\u003e\n\u003cp align='left'\u003e\n\n\u003cimg src=\"http://repo-charts.taroxin.cn/api/repo/starChart?repo=ProtocolServiceKit\u0026owner=DevDragonLi\" width=\"400\" height=\"300\"\u003e      \u003cimg src=\"http://repo-charts.taroxin.cn/api/repo/languageChart?owner=DevDragonLi\u0026repo=ProtocolServiceKit\" width=\"350\" height=\"300\"\u003e\n\n\u003c/p\u003e\n\n### [业界常用的组件通信方案优缺点对比](#业界常用的组件通信方案)\n\n-  [URL Router](#url-router)\n-  [Target-Action](#target-action)\n-  [Protocol-Class](#protocol-class)\n\n### [ProtocolServiceKit](#protocolservicekit)\n\n\u003e  `东半球`最高效的 Protocol\u003c=\u003eService 中间件，解决中间件的占用内存问题。\n\n- Support:ObjC/Swift项目(纯OC/Swift/混编)均可使用\n- Map \u0026\u0026 Cache\n- SafeMode \n\n### [Installation](#installation)\n\n### [ObjC/Swift Example](#OCExample)\n\n\u003e 完善的中间件组件化示例工程\n\n- Use Example【Swift /ObjC】\n\n\n### Q\u0026A\n\n- 引入此中间件，如何**完全解耦**？(见下文)\n- 使用此组件，是否可以代码提醒函数功能？\n\n## 业界常用的组件通信方案\n\n### URL Router\n\n\u003e 蘑菇街路线\n\n-   优点：方案成熟，极高的动态性，能解决组件依赖 ，易于适配 URL Scheme\n    -   可多端复用 （方便地统一管理多平台的路由规则） ✅\n-   缺点：需要注册维护URL表，编译阶段无法发现潜在BUG 🙅‍♂️\n    -   注册存在内存占用问题\n    -   传参方式有限，并且**无法利用编译器进行参数类型检查**，因此所有的参数都只能从字符串中转换而来\n        -   参数可以通过 protocol\n            直接传递，能够利用编译器检查参数类型，并且在 ZIKRouter\n            中，能通过路由声明和编译检查。\n    -   要让 view controller 支持\n        url，需要为其新增初始化方法，因此需要对模块做出修改\n        -   不支持 storyboard\n    -   无法明确声明模块提供的接口，只能依赖于接口文档，重构时无法确保修改正确\n    -   无法保证所使用的模块一定存在\n    -   解耦能力有限，url\n        的\"注册\"、\"实现\"、\"使用\"必须用相同的字符规则，一旦任何一方做出修改都会导致其他方的代码失效，并且重构难度大\n\n### Target-Action\n\n-   优点：不需要注册和内存占用 ✅\n\n-   缺点：编译阶段无法发现潜在BUG，必须遵守命名规则 🙅‍♂️\n\n    -   在 category\n        中仍然引入了字符串硬编码，内部使用字典传参，一定程度上也存在和\n        URL 路由相同的问题\n        -   字典传参的问题\n    -   无法保证所使用的模块一定存在，target\n        模块在修改后，使用者只有在运行时才能发现错误\n    -   过于依赖 runtime 特性，无法应用到纯 Swift 上。在 Swift 中扩展\n        mediator 时，无法使用纯 Swift 类型的参数\n    -   可能会创建过多的 target 类\n    -   使用 runtime\n        相关的接口调用任意类的任意方法，需要注意别被苹果的审核误伤。\n\n### Protocol-Class\n\n-   优点：接口与实现分离，编译阶段就能发现问题 ✅\n\n-   缺点：需要注册和内存占用。\n    - 由框架来创建所有对象，创建方式有限，例如不支持外部传入参数，再调用自定义初始化方法\n    - 只做了 protocol 和 class 的匹配，不支持更复杂的创建方式和依赖注入\n    - 无法保证所使用的 protocol 一定存在对应的模块，也无法直接判断某个\n        protocol 是否能用于获取模块。\n\n## ProtocolServiceKit\n\n\u003e 基于Protocol-Class方案\n\n-  优点：同上`Protocol-Class`方案，但移除了注册逻辑，解决占用内存问题及Protocol对应ServiceClass存在性安全校验。\n\n- **Map** ✅\n\t- 1.2.0 + Support\n\t- 可以不按照约定规则来提供ServiceClass，项目初始化/**需要处**提供map表机制，解决**强制**命名类规范问题，可自由自定义IMPClass。\n\n-  **Cache** ✅ \n\t- 1.3.0 + Support。\n   - 建议项目最常用到组件`Protocol`和`Service`使用。\n\t- Kit内部维护一张表，快速查找到对应的ServiceClass。\n\n- 安全模式\n\n\t\u003e  对外业务能力如果未实现，运行期调用会触发断言处，便于发现问题  ✅\n\n\t- 1.6.0 + (推荐2.1.0+) Support ✅\n\t- 可选择关闭。但**不推荐忽略安全模式手动关闭**，便于及时发现问题。\n\n- 部分缺点同上`Protocol-Class`\n\n## Installation\n\n\u003e ProtocolServiceKit is available through [cocoapods](https://cocoapods.org/pods/ProtocolServiceKit). To install it, simply add the following line to your Podfile:\n\n```ruby\n\n// OC项目建议参考下行\npod 'ProtocolServiceKit',\"~\u003e2.2.1\"  # Default is ObjC\n\n// Swift项目建议参考下行\npod 'ProtocolServiceKit/Swift',\"~\u003e2.2.1\" \n\n// deprecate\npod 'ProtocolServiceManger',\"~\u003e1.0.0\"\n\n```\n\n## ObjC Example\n\n\u003e To run the example/SwiftExample project, clone the repo, and run `pod install` from the Example directory first.\n\n\u003e 为了方便理解，AccountBusiness和PlayBusiness 已部署为远端示例组件，仅暴露Protocol文件\n\n### Main API\n\n\n```objc\n\n- (Class)serviceClassWithProtocol:(Protocol *)aProtocol;\n\n- (Class)serviceClassWithCachedProtocol:(Protocol *)cachedProtocol;\n\n- (void)configProtocolServiceMapsWithDic:(NSDictionary \u003c NSString * ,NSString *\u003e *)mapDics;\n\n```\n\n-  AccountBusiness \u003c=\u003e PlayBusiness Example \n\n```objc\n\n// VIP和播放业务复杂后，只公开Protocol文件决定业务对外能力\n// ServiceWithCachedProtocol 缓存使用\n\nClass \u003cLFLVipProtocol\u003e vipService = ServiceWithProtocol(LFLVipProtocol);\n\n// 不直接使用对应账户类  [LFLAccountTool isUserVipStatus];\n\nBOOL isVip = [vipService isCurrentUserVipStatus];\n\nif (vipService \u0026\u0026 isVip) {\n   [ServiceWithCachedProtocol(LFLPlayProtocol) playMiniVideo];\n} else {\n   NSLog(@\"Error:LFLVipProtocol notfound service Class\");\n}\n\n```\n\n```ObjC\n/// Default  Value NO 【setting YES，ignore All Error Type 】\n@property (nonatomic,assign)BOOL ignoreSafeMode;\n\n```\n\n-  recommended convention\n\n    -   `XXX`Service\n\n    -   `XXX`Protocol\n\n-  项目已存在或者想自由风格命名：**Map解决方案**\n\n```objc\n\nNSDictionary *mapDic = @{\n    @\"LFLUnRuleProtocol\":@\"LFLTestRuleIMP\"\n};\n[[ProService sharedManger] configProtocolServiceMapsWithDic:mapDic];\n\n```\n\n## Swift Example  \n\n- ProtocolServiceKit.ProService\n\n- Swift版本有代码提示！\n\n```Swift \n\nimport ProtocolServiceKit.ProService\n\n func ProtocolServiceNormalDemo() {\n        \n        // 1.1 use\n        let normalService : AnyClass = ProtocolService.serviceClass(aProtocol: SwiftNormalProtocol.self)\n        \n        // 1.2 Xcode can tip functions\n        \n        normalService.normalFunction()\n        \n        // 1.3 cache Service Class\n        let normalCacheServiceDemo : AnyClass = ProtocolService.serviceCacheClass(aProtocol: SwiftNormalProtocol.self)\n        normalCacheServiceDemo.normalFunction()\n        \n        // 2.0 recommend most use Example\n        let normalCacheService : AnyClass = ProtocolService.serviceCacheClass(aProtocol: SwiftNormalProtocol.self)\n        \n        normalCacheService.normalFunction()\n        \n    }\n    \n    func ProtocolServiceMapDemo() {\n        ProtocolService.configProtocolServiceMaps(mapDic: [\"MapExampleProtocol\":\"MapUnRuleClass\"], nameSpace: \"SwiftExample\")\n        \n        let mapCacheService : AnyClass = ProtocolService.serviceCacheClass(aProtocol: MapExampleProtocol.self)\n        mapCacheService.MapExampleFunction()\n    }\n\n```\n\n### Q\u0026A\n\n####  建议组件之间通过明确的API接口交互，如果需要完全隔离（Example：A组件和B组件交互，不可以相互引用）\n- 对于接口层可以单独为轻量级组件（A和B业务组件均存在对应附属的协议层组件A-Protocol，B-Protocol）即可\n\n####  使用此组件，是否可以代码提醒函数功能？\n\n\u003e 一般不建议使用此版本 [ branch Function]\n\n- 支持对象函数调用提醒\n\n```\n// OC项目建议参考下行\npod 'ProtocolServiceKit', :git =\u003e 'https://github.com/DevDragonLi/ProtocolServiceKit.git', :branch =\u003e 'tipFunction' # Default is ObjC\n// Swift项目建议参考下行\npod 'ProtocolServiceKit/Swift', :git =\u003e 'https://github.com/DevDragonLi/ProtocolServiceKit.git', :branch =\u003e 'tipFunction' \n\n```\n\n#### 扩展：对于协议类可抽象出基类协议解决异常Case【视项目而定】\n- 无实现的API最终兜底方案。\n\nAuthor\n------\n\nDevdragonLi, dragonLi_52171@163.com\n\nLicense\n-------\n\nProtocolServiceKit is available under the MIT license. See the LICENSE\nfile for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevdragonli%2Fprotocolservicekit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevdragonli%2Fprotocolservicekit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevdragonli%2Fprotocolservicekit/lists"}