美文网首页程序员
iOS 策略模式

iOS 策略模式

作者: a437e8f87a81 | 来源:发表于2017-12-13 16:31 被阅读0次

策略模式

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

图1.png

demo演示

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

如下图所示:


图2.png

基本步骤

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

@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(@"未输入");
}
}
}

此时并没有进行抽取

策略模式进行抽取

图1.png

首先我们来根据上图的思路来创建一个抽象类---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

相关文章

  • IOS策略模式和多态

    策略模式是一种常见的软件设计模式,这里简单得介绍一下策略模式并用IOS简单实现一下。所谓的策略模式,顾名思义是要采...

  • 设计模式

    iOS设计模式(5)策略模式 iOS适配器设计模式其实就是对某个控件上的各个部分,用一个model来统一赋值,而在...

  • iOS - 策略模式

    定义 策略模式是一种比较简单的模式,也叫政策模式。定义一系列的算法,把它们一个个封装起来,并且使它们可相互替代。 ...

  • iOS 策略模式

    策略模式 原文链接 : 链接定义一系列的算法, 并且将每一个算法封装起来, 算法之间还可以相互替换可以看下图来...

  • iOS策略模式

    用处:让算法和对象分开来,使得算法可以独立于使用它的客户而变化。 例子:出行旅游,我们 可以有几个策略可以考虑,可...

  • iOS 策略模式

    策略模式定义一系列的算法,将每一个算法封装起来,并且这些算法之间可以相互替换。抽象策略类,为所有支持或相关算法声明...

  • 2020-04-16

    iOS设计模式-策略模式 面向对象的设计模式中,我们可以把相关的算法分离为不同的类,成为策略。与这种做法相关的一种...

  • 《iOS开发》--------常用的设计模式

    关于iOS开发中的设计模式,当下有集中最常用的设计模式:代理模式、观察者模式、MVC模式、单例模式、策略模式、工厂...

  • iOS设计模式-策略模式

    简介 策略模式就是根据不同的程序场景来使用不同的策略,一般用在有if 判断或switch case中,可以避免把不...

  • iOS设计模式-策略模式

    策略模式 将一系列的算法单独封装起来,使调用者在使用的时候,可以互相替换。 企鹅案例 这里我们需要几个企鹅,那这几...

网友评论

    本文标题:iOS 策略模式

    本文链接:https://www.haomeiwen.com/subject/qkhzixtx.html