iOS 策略模式附Demo

策略模式

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

demo演示

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

以下图所示: github

基本步骤

那么咱们能够这样写--->( 此时所有在控制器中,并无进行抽取 ) 定义算法

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

算法swift

#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;
}
复制代码

代理atom

#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(@"未输入");
}
}
}
复制代码

此时并无进行抽取spa

策略模式进行抽取

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

声明代理

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

抽象方法指针

- (BOOL)inputTextField:(UITextField *)textField{
return NO;
}
复制代码

场景类---CustomTextField

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

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

实现cdn

- (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

相关文章
相关标签/搜索