koa的Middleware--经典的洋葱造型,在执行一个操作之前会先一层层的执行配置好的中间件,操作执行结束后再一层层的回溯。
在日常的iOS开发中,我们总是会遇这样的少量场景:1.前往某个页面,但是之前需要做多种的校验;2. 执行某个操作,但是需要先调用不同的方法去准备少量数据等等。无论是这些校验或者者数据的准备假如都放在目标方法或者者模块内都不是很合适,由于这些和目标的业务无关而且很有可能变化,比方去掉或者添加一种校验。这样我们就需要使用Middleware来实现这种自由的组合场景。
Middleware的一个主要思想就是需要一个Compose来保存所有的Middleware,每一个Middleware会有一个- (void)invoke:(id)context next:(NoneParamBlock)next方法,当Middleware开始执行的时候会调用这个方法,这个方法接受外面传进来的公共参数和一个next()回调用来移交执行权到下一个Middleware
MathewWang/WWMiddleware
先创立三个Middleware的filter子类
#import <Cocoa/Cocoa.h>#import "ORMiddleWare.h"@interface FirstFilter : ORMiddleWare@end#import "FirstFilter.h"@implementation FirstFilter- (void)invoke:(id)context next:(NoneParamBlock)next{ NSLog(@"first filter doing work with context: %@", context); //执行权移交 next(); NSLog(@"first filter doing cleanup with context: %@", context);}
#import <Cocoa/Cocoa.h>#import "ORMiddleWare.h"@interface SecondFilter : ORMiddleWare@end#import "SecondFilter.h"@implementation SecondFilter- (void)invoke:(id)context next:(NoneParamBlock)next{ NSLog(@"second filter doing work with context: %@", context); //执行权移交 next(); NSLog(@"second filter doing cleanup with context: %@", context);}@end
#import <Cocoa/Cocoa.h>#import "ORMiddleWare.h"@interface ThirdFilter : ORMiddleWare@end#import "ThirdFilter.h"@implementation ThirdFilter- (void)invoke:(id)context next:(NoneParamBlock)next{ NSLog(@"third filter doing work with context: %@", context); //执行权移交 next(); NSLog(@"third filter doing cleanup with context: %@", context);}@end
最后将三个中间件组合,并执行业务代码
#import <Foundation/Foundation.h>#import "ORCompose.h"#import "FirstFilter.h"#import "SecondFilter.h"#import "ThirdFilter.h"int main(int argc, const char * argv[]) { @autoreleasepool { [ORCompose compose:@[[FirstFilter new], [SecondFilter new], [ThirdFilter new]]] .run(@"context", ^{ NSLog(@"doing business"); }); } return 0;}//结果//first filter doing work with context: context//second filter doing work with context: context//third filter doing work with context: context//doing business//third filter doing cleanup with context: context//second filter doing cleanup with context: context//first filter doing cleanup with context: context
结束,简单好用。
低功耗蓝牙BLE5.0灯控模块Mesh组网透传模组TB-04-Kit/开发板
双频WiFi+低功耗蓝牙5.0模块 基于RTL8720DN BW16-Kit/开发板
【YourCee】ESP-LAUNCHER 2.4GHz WiFi开发板模块ESP8266EX芯片
【YourCee】ESP32-DevKitM-1开发板模块搭载ESP32-MINI-1模组
ESP32-LyraTD-MSC WiFi&蓝牙音频开发板 智能音箱AI应用
【YourCee】ESP32-LyraTD-SYNA 音频开发板模块 语音识别唤醒