ProtocolServiceKit 2.2.2

ProtocolServiceKit 2.2.2

dragonLi维护。



  • 作者
  • DevdragonLi

ProtocolServiceKit

Version License Platform

业界常用的组件通信方案优缺点对比

ProtocolServiceKit

东半球最高效的 Protocol<=>Service 中间件,解决中间件的占用内存问题。

  • 支持:OC/Swift/混合OC+Swift
  • 映射 && 缓存
  • 安全模式

安装

ObjC/Swift 示例

完善的中间件组件化示例工程

  • 使用示例【Swift /ObjC】

行业常用的组件通信方案

URL 路由器

蘑菇街路由

  • 优点:方案成熟,极高的动态性,能解决组件依赖,易于适配 URL 规划
    • 可多端复用(方便地统一管理多平台的路由规则)
  • 缺点:需要注册维护 URL 表,编译阶段无法发现潜在错误🙅‍♂️
    • 注册存在内存占用问题
    • 传参方式有限,并且**无法利用编译器进行参数类型检查**,因此所有的参数都只能从字符串中转换而来
      • 参数可以通过 protocol 直接传递,能够利用编译器检查参数类型,并且在 ZIKRouter 中,能通过路由声明和编译检查。
    • 要让 view controller 支持 url,需要为其新增初始化方法,因此需要对模块做出修改
      • 不支持 storyboard
    • 无法明确声明模块提供的接口,只能依赖于接口文档,重构时无法确保修改正确
    • 无法保证所使用的模块一定存在
    • 解耦能力有限,url 的"注册"、"实现"、"使用"必须用相同的字符规则,一旦任何一方做出修改都会导致其他方的代码失效,并且重构难度大

目标-操作

  • 优点:不需要注册和内存占用

  • 缺点:编译阶段无法发现潜在错误,必须遵守命名规则🙅‍♂️

    • 在 category 中仍然引入了字符串硬编码,内部使用字典传参,一定程度上也存在和 URL 路由相同的问题
      • 字典传参的问题
    • 无法保证所使用的模块一定存在,target 模块在修改后,使用者只有在运行时才能发现错误
    • 过于依赖 runtime 特性,无法应用到纯 Swift 上。在 Swift 中扩展 mediator 时,无法使用纯 Swift 类型的参数
    • 可能会创建过多的 target 类
    • 使用 runtime 相关的接口调用任意类的任意方法,需要注意别被苹果的审核误伤。

协议-类

  • 优点:接口与实现分离,编译阶段就能发现问题

  • 缺点:需要注册和内存占用。

    • 由框架来创建所有对象,创建方式有限,例如不支持外部传入参数,再调用自定义初始化方法
    • 只做了 protocol 和 class 的匹配,不支持更复杂的创建方式和依赖注入
    • 无法保证所使用的 protocol 一定存在对应的模块,也无法直接判断某个 protocol 是否能用于获取模块。

ProtocolServiceKit

基于Protocol-Class方案

  • 优点:与上述`Protocol-Class`方案相同,但移除了注册逻辑,解决占用内存问题及Protocol对应ServiceClass存在性安全校验。

  • Map

    • 1.2.0 + 支持功能
    • 可以不按照约定规则来提供ServiceClass,项目初始化/需要处提供map表机制,解决强制命名类规范问题,可自由自定义IMPClass。
  • Cache

    • 1.3.0 + 支持功能
    • 建议项目最常用到组件ProtocolService使用。
    • Kit内部维护一张表,快速查找到对应的ServiceClass。
  • 安全模式

    对外业务能力如果未实现,运行期调用会触发断言处,便于发现问题

    • 1.6.0 + (推荐2.1.0+) 支持功能
    • 可选择关闭。但**不推荐忽略安全模式手动关闭**,便于及时发现问题。
  • 部分缺点同上Protocol-Class

安装

ProtocolServiceKit可以通过cocoapods获取。要安装它,只需将以下行添加到您的Podfile中

// recommended 
pod 'ProtocolServiceKit',"~>2.1.0"  

// deprecate
pod 'ProtocolServiceManger',"~>1.0.0"

ObjC 示例

要运行例子中的/SwiftExample项目,请先克隆repo,然后从Example目录运行pod install

为了方便理解,AccountBusiness和PlayBusiness已部署为远程示例组件,仅暴露Protocol文件

主要API

- (Class)serviceClassWithProtocol:(Protocol *)aProtocol;

- (Class)serviceClassWithCachedProtocol:(Protocol *)cachedProtocol;

- (void)configProtocolServiceMapsWithDic:(NSDictionary < NSString * ,NSString *> *)mapDics;
  • AccountBusiness <=> PlayBusiness 示例
// 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;
  • 建议规范

    • XXXService

    • XXXProtocol

  • 项目已存在或想自由命名:Map解决方案

NSDictionary *mapDic = @{
    @"LFLUnRuleProtocol":@"LFLTestRuleIMP"
};
[[ProService sharedManger] configProtocolServiceMapsWithDic:mapDic];

Swift 示例

  • 在大中型项目中,可以内部新建一个Pod ServiceRouter,可参考SwiftDemo中的ServiceRouter.Swift
// 1.1 use

let normalService : AnyClass = ServiceRouter.serviceClass(aProtocol: SwiftNormalProtocol.self)

// 1.2 Xcode can tip functions
normalService.normalFunction()

// 1.3 cache Service Class
let normalCacheServiceDemo : AnyClass = ServiceRouter.serviceCacheClass(aProtocol: SwiftNormalProtocol.self)
normalCacheServiceDemo.normalFunction()


File:SwiftNormalProtocol.swift

@objc public protocol SwiftNormalProtocol {
    
   static func normalFunction()
}


import Foundation

class SwiftNormalService:SwiftNormalProtocol {
    
  static  public func normalFunction() {
      print("SwiftNormalService")
    }
    
}

// user example 

func normalDemo() {
    
    let normalService : AnyClass = ServiceRouter.serviceClass(with:SwiftNormalProtocol.self)
    
    // can tip functions
    normalService.normalFunction()
}

作者

DevdragonLi, [email protected]

授权

ProtocolServiceKit依据MIT授权协议发布。更多信息请查阅LICENSE文件。