责任链vs金融登录

金融app相对普通app而言,出于安全考虑,其安全校验方式比较多,以某些银行app为例,手机号登录成功后,会增加指纹、手势、OCR人脸等验证!这些安全项的校验,会根据用户的风险等级有不同的校验优先级,例如A客户的校验顺序是先指纹->手势->OCR,B客户的校验顺序可能是先手势->OCR,其验证顺序不固定,故此处用责任链的模式来设计更为合理:

demo地址:https://download.csdn.net/download/denggun12345/88104411?spm=1001.2014.3001.5503

//基类BusinessObject

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@class BusinessObject;

typedef void (^CompletionBlock)(BOOL handled);

typedef void (^ResultBlock)(BusinessObject *handler, BOOL handled);

@interface BusinessObject : NSObject

@property (nonatomic ,strong) UILabel *LB;

//下一个响应者

@property (nonatomic ,strong) BusinessObject *nextBusiness;

//响应者的处理方法

-(void)handle:(ResultBlock)result;

//各个业务在该方法当中做时机业务处理

-(void)handleBusiness:(CompletionBlock)completion;

@end

NS_ASSUME_NONNULL_END

#import "BusinessObject.h"

@implementation BusinessObject

//责任链入口方法

-(void)handle:(ResultBlock)result{

CompletionBlock completion = ^(BOOL handled){

//当前业务处理掉了,上抛结果

if(handled){

result(self,handled);

}else{

//沿着责任链,指派给下一个业务处理

if (self.nextBusiness){

**self**.nextBusiness handle:result\]; }**else**{ //没有业务处理,上抛 result(**nil** ,**NO**); } } }; //当前业务进行处理 \[**self** handleBusiness:completion\]; } -(**void**)handleBusiness:(CompletionBlock)completion{ //业务逻辑处理 //如不网络请求、本地照片查询等 } **@end** 模拟业务类,即继承基类的类,用来模拟指纹、手势、ocr的业务类 #import "BusinessObjectA.h" **@implementation** BusinessObjectA -(**void**)handleBusiness:(CompletionBlock)completion{ NSLog(@"BusinessObjectA"); **if** (\[**self**.LB.text isEqualToString:@"0"\]) { completion(**YES**); }**else**{ completion(**NO**); } } **@end** #import "BusinessObjectB.h" **@implementation** BusinessObjectB -(**void**)handleBusiness:(CompletionBlock)completion{ NSLog(@"BusinessObjectB"); **if** (\[**self**.LB.text isEqualToString:@"0"\]) { completion(**YES**); }**else**{ completion(**NO**); } } **@end** #import "BusinessObjectC.h" **@implementation** BusinessObjectC -(**void**)handleBusiness:(CompletionBlock)completion{ NSLog(@"BusinessObjectC"); **if** (\[**self**.LB.text isEqualToString:@"0"\]) { completion(**YES**); }**else**{ completion(**NO**); } } **@end** //模拟处理逻辑优先级 #import "ViewController.h" #import "BusinessObjectA.h" #import "BusinessObjectB.h" #import "BusinessObjectC.h" **@interface** ViewController () //背景变红色 **@property** (**nonatomic** ,**assign** ) **BOOL** isOuShu; **@property** (**weak** , **nonatomic** ) **IBOutlet** UILabel \*LBOne; **@property** (**weak** , **nonatomic** ) **IBOutlet** UILabel \*LBTwo; **@property** (**weak** , **nonatomic** ) **IBOutlet** UILabel \*LBThree; **@property** (**nonatomic** ,**strong**) NSArray \*testArray; **@property** (**weak** , **nonatomic** ) **IBOutlet** UIButton \*testBtn; **@end** **@implementation** ViewController -(NSArray \*)testArray{ **if**(!_testArray){ _testArray = @\[@1,@2,@3\]; } **return** _testArray; } - (**void**)viewDidLoad { \[**super** viewDidLoad\]; // Do any additional setup after loading the view.} - (**IBAction** )ClickAction:(**id**)sender { **int** x = arc4random() % 3;//0 1 2 模拟责任链中密码、手势、指纹的优先级 **self**.LBOne.text = \[NSString stringWithFormat:@"%d",x\]; **self**.LBTwo.text = \[NSString stringWithFormat:@"%d",x-1\]; **self**.LBThree.text = \[NSString stringWithFormat:@"%d",x-2\]; //开始责任链 //a指纹 BusinessObjectA \*objA = \[BusinessObjectA new\]; objA.LB = **self**.LBOne; \[objA handle:\^(BusinessObject \* **_Nonnull** handler, **BOOL** handled) { **if**(handled){ NSLog(@"a_yes_handler_test:%@",handler.LB.text); \[**self**.testBtn setTitle:@"LBOne" forState:UIControlStateNormal\]; }**else**{ NSLog(@"a_no_handler_test:%@",handler.LB.text); } }\]; //b手势 BusinessObjectB \*objB = \[BusinessObjectB new\]; objB.LB = **self**.LBTwo; objA.nextBusiness = objB; \[objB handle:\^(BusinessObject \* **_Nonnull** handler, **BOOL** handled) { **if**(handled){ NSLog(@"b_yes_handler_test:%@",handler.LB.text); \[**self**.testBtn setTitle:@"LBTwo" forState:UIControlStateNormal\]; }**else**{ NSLog(@"b_no_handler_test:%@",handler.LB.text); } }\]; //cOCR BusinessObjectC \*objC = \[BusinessObjectC new\]; objC.LB = **self**.LBThree; objB.nextBusiness = objC; \[objC handle:\^(BusinessObject \* **_Nonnull** handler, **BOOL** handled) { **if**(handled){ \[**self**.testBtn setTitle:@"LBThree" forState:UIControlStateNormal\]; NSLog(@"c_yes_handler_test:%@",handler.LB.text); }**else**{ NSLog(@"c_no_handler_test:%@",handler.LB.text); } }\]; } **@end**

相关推荐
AirDroid_cn2 小时前
在 iOS 18 中,如何将锁屏底部的 “手电筒” 替换为其他应用?
ios
极客数模6 小时前
2025年(第六届)“大湾区杯”粤港澳金融数学建模竞赛准备!严格遵循要求,拿下大奖!
大数据·python·数学建模·金融·分类·图论·boosting
芝麻开门-新起点9 小时前
Android 和 iOS 系统版本及开发适配
android·ios·cocoa
2501_915918419 小时前
iOS描述文件功能解析
android·macos·ios·小程序·uni-app·cocoa·iphone
NewsMash11 小时前
金泽通 打造数字金融与商业融合新模式
金融
necessary65311 小时前
从工行“余额归零”事件看CAP定理:当金融系统在一致性与可用性之间做出选择
分布式·金融·wpf·可用性测试
汉秋13 小时前
SwiftUI动画之使用 navigationTransition(.zoom) 实现 Hero 动画
ios·swiftui
加油乐18 小时前
解决 iOS 端输入框聚焦时页面上移问题
前端·javascript·ios
电话交换机IPPBX-3CX1 天前
电话交换机软件 3CX iOS 应用 V5.4 Beta 更新
ios·软件更新·ip pbx·电话交换机
初遇你时动了情1 天前
uniapp/flutter中实现苹果IOS 26 毛玻璃效果、跟随滑动放大动画
flutter·ios·uni-app