ProtocolServiceKit

东半球最高效的 Protocol<=>Service 中间件,解决中间件的占用内存问题。
- Support:ObjC/Swift项目(纯OC/Swift/混编)均可使用
- Map && Cache
- SafeMode
完善的中间件组件化示例工程
Q&A
- 引入此中间件,如何完全解耦?(见下文)
- 使用此组件,是否可以代码提醒函数功能?
业界常用的组件通信方案
URL Router
蘑菇街路线
- 优点:方案成熟,极高的动态性,能解决组件依赖 ,易于适配 URL Scheme
- 可多端复用 (方便地统一管理多平台的路由规则) ✅
- 缺点:需要注册维护URL表,编译阶段无法发现潜在BUG 🙅♂️
- 注册存在内存占用问题
- 传参方式有限,并且无法利用编译器进行参数类型检查,因此所有的参数都只能从字符串中转换而来
- 参数可以通过 protocol
直接传递,能够利用编译器检查参数类型,并且在 ZIKRouter
中,能通过路由声明和编译检查。
- 要让 view controller 支持
url,需要为其新增初始化方法,因此需要对模块做出修改
- 无法明确声明模块提供的接口,只能依赖于接口文档,重构时无法确保修改正确
- 无法保证所使用的模块一定存在
- 解耦能力有限,url
的"注册"、"实现"、"使用"必须用相同的字符规则,一旦任何一方做出修改都会导致其他方的代码失效,并且重构难度大
Target-Action
Protocol-Class
优点:接口与实现分离,编译阶段就能发现问题 ✅
缺点:需要注册和内存占用。
- 由框架来创建所有对象,创建方式有限,例如不支持外部传入参数,再调用自定义初始化方法
- 只做了 protocol 和 class 的匹配,不支持更复杂的创建方式和依赖注入
- 无法保证所使用的 protocol 一定存在对应的模块,也无法直接判断某个
protocol 是否能用于获取模块。
ProtocolServiceKit
基于Protocol-Class方案
Installation
ProtocolServiceKit is available through cocoapods. To install it, simply add the following line to your Podfile:
// OC项目建议参考下行
pod 'ProtocolServiceKit',"~>2.2.1" # Default is ObjC
// Swift项目建议参考下行
pod 'ProtocolServiceKit/Swift',"~>2.2.1"
// deprecate
pod 'ProtocolServiceManger',"~>1.0.0"
ObjC Example
To run the example/SwiftExample project, clone the repo, and run pod install from the Example directory first.
为了方便理解,AccountBusiness和PlayBusiness 已部署为远端示例组件,仅暴露Protocol文件
Main API
- (Class)serviceClassWithProtocol:(Protocol *)aProtocol;
- (Class)serviceClassWithCachedProtocol:(Protocol *)cachedProtocol;
- (void)configProtocolServiceMapsWithDic:(NSDictionary < NSString * ,NSString *> *)mapDics;
- AccountBusiness <=> PlayBusiness Example
// VIP和播放业务复杂后,只公开Protocol文件决定业务对外能力
// ServiceWithCachedProtocol 缓存使用
Class <LFLVipProtocol> vipService = ServiceWithProtocol(LFLVipProtocol);
// 不直接使用对应账户类 [LFLAccountTool isUserVipStatus];
BOOL isVip = [vipService isCurrentUserVipStatus];
if (vipService && isVip) {
[ServiceWithCachedProtocol(LFLPlayProtocol) playMiniVideo];
} else {
NSLog(@"Error:LFLVipProtocol notfound service Class");
}
/// Default Value NO 【setting YES,ignore All Error Type 】
@property (nonatomic,assign)BOOL ignoreSafeMode;
recommended convention
项目已存在或者想自由风格命名:Map解决方案
NSDictionary *mapDic = @{
@"LFLUnRuleProtocol":@"LFLTestRuleIMP"
};
[[ProService sharedManger] configProtocolServiceMapsWithDic:mapDic];
Swift Example
import ProtocolServiceKit.ProService
func ProtocolServiceNormalDemo() {
// 1.1 use
let normalService : AnyClass = ProtocolService.serviceClass(aProtocol: SwiftNormalProtocol.self)
// 1.2 Xcode can tip functions
normalService.normalFunction()
// 1.3 cache Service Class
let normalCacheServiceDemo : AnyClass = ProtocolService.serviceCacheClass(aProtocol: SwiftNormalProtocol.self)
normalCacheServiceDemo.normalFunction()
// 2.0 recommend most use Example
let normalCacheService : AnyClass = ProtocolService.serviceCacheClass(aProtocol: SwiftNormalProtocol.self)
normalCacheService.normalFunction()
}
func ProtocolServiceMapDemo() {
ProtocolService.configProtocolServiceMaps(mapDic: ["MapExampleProtocol":"MapUnRuleClass"], nameSpace: "SwiftExample")
let mapCacheService : AnyClass = ProtocolService.serviceCacheClass(aProtocol: MapExampleProtocol.self)
mapCacheService.MapExampleFunction()
}
Q&A
建议组件之间通过明确的API接口交互,如果需要完全隔离(Example:A组件和B组件交互,不可以相互引用)
- 对于接口层可以单独为轻量级组件(A和B业务组件均存在对应附属的协议层组件A-Protocol,B-Protocol)即可
使用此组件,是否可以代码提醒函数功能?
一般不建议使用此版本 [ branch Function]
// OC项目建议参考下行
pod 'ProtocolServiceKit', :git => 'https://github.com/DevDragonLi/ProtocolServiceKit.git', :branch => 'tipFunction' # Default is ObjC
// Swift项目建议参考下行
pod 'ProtocolServiceKit/Swift', :git => 'https://github.com/DevDragonLi/ProtocolServiceKit.git', :branch => 'tipFunction'
扩展:对于协议类可抽象出基类协议解决异常Case【视项目而定】
Author
DevdragonLi, dragonLi_52171@163.com
License
ProtocolServiceKit is available under the MIT license. See the LICENSE
file for more info.