模块在修改后,使用者只有在运行时才能发现错误 ProtocolServiceKit
Protocol-Class
优点:接口与实现分离,编译阶段就能发现问题
缺点:需要注册和内存占用。
ProtocolServiceKit
基于Protocol-Class方案
优点:同上
Protocol-Class
方案,但移除了注册逻辑,解决占用内存问题及Protocol对应ServiceClass存在性安全校验。
Map
Cache
Protocol
和
Service
使用。
对外业务能力如果未实现,运行期调用会触发断言处,便于发现问题
部分缺点同上
Protocol-Class
Installation
ProtocolServiceKit is available through cocoapods . To install it, simply add the following line to your Podfile:
// recommended
pod 'ProtocolServiceKit',"~>2.1.0"
// 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;
// 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
XXX
Service
XXX
Protocol
@"LFLUnRuleProtocol":@"LFLTestRuleIMP"
[[ProService sharedManger] configProtocolServiceMapsWithDic:mapDic];
Swift Example
大中型项目,可以内部新建一个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()
Author