iOS 策略模式附Demo

2,471 阅读2分钟

策略模式

原文链接 : 链接 jianshu地址 : 链接 定义一系列的算法, 并且将每一个算法封装起来, 算法之间还可以相互替换 可以看下图来体会

demo演示

需求简单做一个只接收字母, 只接收数字的demo, 验证登录

如下图所示:

基本步骤

那么我们可以这样写--->( 此时全部在控制器中,并没有进行抽取 ) 定义

@property (weak, nonatomic) IBOutlet UITextField *letterInput;//字母输入
@property (weak, nonatomic) IBOutlet UITextField *numberInput;//数字输入

算法

#pragma mark -验证输入
- (NSString*)letterInput:(UITextField *)textField{
if(textField.text.length == 0){
return nil;
}
//从开头到结尾,有效字符集合a-zA-Z或者更多
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[a-zA-Z]*$" options:NSRegularExpressionAnchorsMatchLines error:nil];
NSUInteger numberOfMateches = [regex numberOfMatchesInString:[textField text] options:NSMatchingAnchored range:NSMakeRange(0, [textField text].length)];
NSString *outLetter = nil;
//判断,匹配不符合为0
if(numberOfMateches == 0){
outLetter = @"请重新输入";
}else{
outLetter = @"输入正确";
}
return outLetter;
}
- (NSString*)numberInput:(UITextField *)textField{
if(textField.text.length == 0){
return nil;
}
//从开头到结尾,有效字符集合0-9或者更多
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9]*$" options:NSRegularExpressionAnchorsMatchLines error:nil];
NSUInteger numberOfMateches = [regex numberOfMatchesInString:[textField text] options:NSMatchingAnchored range:NSMakeRange(0, [textField text].length)];
NSString *outLetter = nil;
//判断,匹配不符合为0
if(numberOfMateches == 0){
outLetter = @"请重新输入";
}else{
outLetter = @"输入正确";
}
return outLetter;
}

代理

#pragma mark -代理
- (void)textFieldDidEndEditing:(UITextField *)textField{
if(textField == self.letterInput){
//验证输入值
NSString *outPut = [self letterInput:textField];
if (outPut) {
NSLog(@"--%@",outPut);
}else{
NSLog(@"未输入");
}
}else if(textField == self.numberInput){
//验证是数字
NSString *outPut = [self numberInput:textField];
if (outPut) {
NSLog(@"--%@",outPut);
}else{
NSLog(@"未输入");
}
}
}

此时并没有进行抽取

策略模式进行抽取

首先我们来根据上图的思路来创建一个抽象类---InputTextField类

声明

//策略输入 YES 通过
//NO 不通过
- (BOOL)inputTextField:(UITextField *)textField;
@property (nonatomic,copy)NSString *attributeInputStr;//属性字符

抽象方法

- (BOOL)inputTextField:(UITextField *)textField{
return NO;
}

场景类---CustomTextField

同样我们来声明一个BOOL类型验证方法, 并将抽象类导入, 之前属于一个聚合的关系

@property (nonatomic,strong)InputTextField *inputTextField;//抽象策略类
//验证方法
- (BOOL)isOK;

实现

- (BOOL)isOK{
BOOL result = [self.inputTextField inputTextField:self];
if(!result){
NSLog(@"--%@",self.inputTextField.attributeInputStr);
}
return result;
}

实现类---LetterInput, ---NumberInput, 这两个类全部是继承于抽象类的

此时我们开始写实现

- (BOOL)inputTextField:(UITextField *)textField{
if(textField.text.length == 0){
self.attributeInputStr = @"字母输入为空";
return nil;
}
//从开头到结尾,有效字符集合0-9或者更多
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[a-zA-Z]*$" options:NSRegularExpressionAnchorsMatchLines error:nil];
NSUInteger numberOfMateches = [regex numberOfMatchesInString:[textField text] options:NSMatchingAnchored range:NSMakeRange(0, [textField text].length)];

//判断,匹配不符合为0
if(numberOfMateches == 0){
self.attributeInputStr = @"请重新输入";
}else{
self.attributeInputStr = @"输入正确";
}
return self.attributeInputStr == nil ? YES : NO;
}
- (BOOL)inputTextField:(UITextField *)textField{
if(textField.text.length == 0){
self.attributeInputStr = @"数字输入为空";
return nil;
}
//从开头到结尾,有效字符集合0-9或者更多
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9]*$" options:NSRegularExpressionAnchorsMatchLines error:nil];
NSUInteger numberOfMateches = [regex numberOfMatchesInString:[textField text] options:NSMatchingAnchored range:NSMakeRange(0, [textField text].length)];

//判断,匹配不符合为0
if(numberOfMateches == 0){
self.attributeInputStr = @"请重新输入";
}else{
self.attributeInputStr = @"输入正确";
}
return self.attributeInputStr == nil ? YES : NO;
}

控制器中实现

父类指针指向子类对象

self.letterInput.inputTextField = [LetterInput new];
self.numberInput.inputTextField = [NumberInput new];

调用

- (void)textFieldDidEndEditing:(UITextField *)textField{
if ([textField isKindOfClass:[CustomTextField class]]) {
[(CustomTextField *)textField inputTextField];
}
}

总结

假如说我们又多了一个策略, 只需要再次增加一个类, 增加一个算法直接调用, 这样的话就在Controller中仅仅创建一个类就可以了, 对于后期的代码维护是不是方便了许多呢? 好了, 给大家这个简单demo, 当然在代码中也写了注释, 可以去我的git下载, 欢迎star 下载链接 : demo地址 技术交流q群150731459