带你读《2022技术人的百宝黑皮书》——淘宝iOS扫一扫架构升级 - 设计模式的应用(2)https://developer.aliyun.com/article/1338304?groupCode=taobaotech
实现代码示例如下
实现代码示例如下
@implementation TBPipelineManager - (TBPipelineManager *(^)(id<TBPipelineDataCreatorDelegate> dataCreator))addDataCreator { @weakify return ^(id<TBPipelineDataCreatorDelegate> dataCreator) { @strongify if (dataCreator) { [self.dataGenArr addObject:dataCreator]; } return self; }; } - (TBPipelineManager *(^)(id<TBPipelineDelegate> pipeline))addPipeline { @weakify return ^(id<TBPipelineDelegate> pipeline) { @strongify if (pipeline) { [self.pipelineArr addObject:pipeline]; 20 //每一次add的同时,我们做链式标记(通过runtime给每个处理加Next) if (self.pCurPipeline) { NSObject *cur = (NSObject *)self.pCurPipeline; cur.tb_nextPipeline = pipeline; } self.pCurPipeline = pipeline; } return self; }; } - (void)setThrowDataBlock:(void (^)(id _Nonnull))throwDataBlock { _throwDataBlock = throwDataBlock; 34 @weakify //Creator的数组,依次对 Block 回调进行赋值,当业务方调用此 Block 时,就是开始处理数据的时候 [self.dataGenArr enumerateObjectsUsingBlock:^(id<TBPipelineDataCreatorDelegate> _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { obj.generateDataBlock = ^(id<TBPipelineBaseDataProtocol> data, NSInteger dataId) { @strongify data.dataId = dataId; //开始递归处理数据 [self handleData:data]; - (void)handleData:(id)data { [self recurPipeline:self.pipelineArr.firstObject data:data]; } - (void)recurPipeline:(id<TBPipelineDelegate>)pipeline data:(id)data { if (!pipeline) { return; } //递归让pipeline处理数据@weakify [pipeline receiveData:data throwDataBlock:^(id _Nonnull throwData) { @strongify NSObject *cur = (NSObject *)pipeline; if (cur.tb_nextPipeline) { [self recurPipeline:cur.tb_nextPipeline data:throwData]; } else { !self.throwDataBlock?:self.throwDataBlock(throwData); } }]; } @end
处理单元 Pipeline 包含的功能和特点:
1. 因为数据是基于业务的,所以它只被声明为一个 Protocol ,由上层实现。
API 代码示例如下
1 @protocol TBPipelineDelegate <NSObject> 2 //如果有错误,直接抛出 3 - (void)receiveData:(id)data throwDataBlock:(void(^)(id data))block; 4 @end
上层业务代码示例如下
//以A类型码码处理单元为例@implementation TBGen3Pipeline - (void)receiveData:(id <TBCodeDataDelegate>)data throwDataBlock:(void (^)(id data))block { TBScanResult *result = data.scanResult; NSString *scanType = result.resultType; NSString *scanData = result.data; if ([scanType isEqualToString:TBScanResultTypeA]) { //跳转逻辑 ... //可以处理,终止递归BlockInPipeline(); } else { //不满足处理条件,继续递归:由下一个 Pipeline 继续处理PassNextPipeline(data); } } @end
业务层调用
设置标签
有了上述的框架和上层实现,生成一个码处理管理就很容易且能达到解耦的目的,代码示例如下
- (void)setupPipeline { //创建 manager 和 creator self.manager = TBPipelineManager.new; self.dataCreator = TBDataCreator.new; //创建 pipeline TBCodeTypeAPipelie *codeTypeAPipeline = TBCodeTypeAPipelie.new; TBCodeTypeBPipelie *codeTypeBPipeline = TBCodeTypeBPipelie.new; //... TBCodeTypeFPipelie *codeTypeFPipeline = TBCodeTypeFPipelie.new;
11 |
|
|
12 |
|
//往 manager 中链式添加 creator 和 pipeline |
13 |
|
@weakify |
14 |
|
self.manager |
15 |
|
.addDataCreator(self.dataCreator) |
16 |
|
.addPipeline(codeTypeAPipeline) |
17 |
|
.addPipeline(codeTypeBPipeline) |
18 |
|
.addPipeline(codeTypeFPipeline) |
19 |
|
.throwDataBlock = ^(id data) { |
20 |
|
@strongify |
21 |
|
if ([self.proxyImpl respondsToSelector:@selector(scanResultDidFailedProcess:)]) { |
22 |
|
[self.proxyImpl scanResultDidFailedProcess:data]; |
23 |
|
} |
24 |
|
}; |
25 |
} |
|
带你读《2022技术人的百宝黑皮书》——淘宝iOS扫一扫架构升级 - 设计模式的应用(4)https://developer.aliyun.com/article/1338239?groupCode=taobaotech