源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

举例讲解iOS应用开发中对设计模式中的策略模式的使用

  • 时间:2022-01-14 00:19 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:举例讲解iOS应用开发中对设计模式中的策略模式的使用
策略模式是一种常见的软件设计模式,这里简单得介绍一下策略模式并用IOS简单实现一下。 所谓的策略模式,顾名思义是要采用不同的策略的。一般来说,在不同的情况下,处理某一个问题的方法也不一样。比如说对字符串的排序和对数字的排序,虽然用的都是快排,但是显然不可能使用一段通用的代码。有人说java里面的compareTo可以做到,但如果考虑这么一个问题:同样是出门旅行,老年人身体虚弱,需要大量的休息,而孩子则是精力充沛,希望玩到更多的景点。如何在同一模式下表达以上信息、采用合理的设计模式进行封装而不是大量重写类似的代码,就需要学习并采用策略模式。 [b]例子 [/b]该例子主要利用策略模式来判断UITextField是否满足输入要求,比如输入的只能是数字,如果只是数字就没有提示,如果有其他字符则提示出错。验证字母也是一样。 首先,我们先定义一个抽象的策略类IputValidator。代码如下: InputValidator.h
[u]复制代码[/u] 代码如下:
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> static NSString * const InputValidationErrorDomain = @"InputValidationErrorDomain"; @interface InputValidator : NSObject //实际验证策略的存根方法 -(BOOL)validateInput:(UITextField *)input error:(NSError **)error; @end
InputValidator.m
[u]复制代码[/u] 代码如下:
#import "InputValidator.h" @implementation InputValidator -(BOOL)validateInput:(UITextField *)input error:(NSError **)error {     if (error) {         *error = nil;     }     return NO; } @end
这个就是一个策略基类,然后我们去创建两个子类NumericInputValidator和AlphaInputValidator。具体代码如下: NumericIputValidator.h
[u]复制代码[/u] 代码如下:
#import "InputValidator.h" @interface NumericInputValidator : InputValidator -(BOOL)validateInput:(UITextField *)input error:(NSError **)error; @end
NumericIputValidator.m
[u]复制代码[/u] 代码如下:
#import "NumericInputValidator.h" @implementation NumericInputValidator -(BOOL)validateInput:(UITextField *)input error:(NSError **)error {     NSError *regError = nil;     //使用配置的NSRegularExpression对象,检查文本框中数值型的匹配次数。     //^[0-9]*$:意思是从行的开头(表示为^)到结尾(表示为$)应该有数字集(标示为[0-9])中的0或者更多个字符(表示为*)     NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9]*$" options:NSRegularExpressionAnchorsMatchLines error:®Error];             NSUInteger numberOfMatches = [regex numberOfMatchesInString:[input text] options:NSMatchingAnchored range:NSMakeRange(0, [[input text] length])];         //如果没有匹配,就返回错误和NO     if (numberOfMatches==0) {         if (error != nil) {             NSString *description = NSLocalizedString(@"Input Validation Faild", @"");                         NSString *reason = NSLocalizedString(@"The input can contain only numerical values", @"");                                     NSArray *objArray = [NSArray arrayWithObjects:description,reason, nil];                         NSArray *keyArray = [NSArray arrayWithObjects:NSLocalizedDescriptionKey,NSLocalizedFailureReasonErrorKey ,nil];                         NSDictionary *userInfo = [NSDictionary dictionaryWithObjects:objArray forKeys:keyArray];                         *error = [NSError errorWithDomain:InputValidationErrorDomain code:1001 userInfo:userInfo];         }         return NO;     }     return YES; } @end
AlphaInputValidator.h
[u]复制代码[/u] 代码如下:
#import "InputValidator.h" @interface AlphaInputValidator : InputValidator - (BOOL)validateInput:(UITextField *)input error:(NSError **)error; @end
AlphaInputValidator.m
[u]复制代码[/u] 代码如下:
#import "AlphaInputValidator.h" @implementation AlphaInputValidator -(BOOL)validateInput:(UITextField *)input error:(NSError **)error {     NSError *regError = nil;     //使用配置的NSRegularExpression对象,检查文本框中数值型的匹配次数。     //^[0-9]*$:意思是从行的开头(表示为^)到结尾(表示为$)应该有数字集(标示为[0-9])中的0或者更多个字符(表示为*)     NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[a-zA-Z]*$" options:NSRegularExpressionAnchorsMatchLines error:®Error];             NSUInteger numberOfMatches = [regex numberOfMatchesInString:[input text] options:NSMatchingAnchored range:NSMakeRange(0, [[input text] length])];         //如果没有匹配,就返回错误和NO     if (numberOfMatches==0) {         if (error != nil) {             NSString *description = NSLocalizedString(@"Input Validation Faild", @"");                         NSString *reason = NSLocalizedString(@"The input can contain only letters ", @"");                                     NSArray *objArray = [NSArray arrayWithObjects:description,reason, nil];                         NSArray *keyArray = [NSArray arrayWithObjects:NSLocalizedDescriptionKey,NSLocalizedFailureReasonErrorKey ,nil];                         NSDictionary *userInfo = [NSDictionary dictionaryWithObjects:objArray forKeys:keyArray];                         *error = [NSError errorWithDomain:InputValidationErrorDomain code:1002 userInfo:userInfo];         }         return NO;     }     return YES; } @end
他们两个都是InputValidator的子类。然后再定义一个CustomTextField: CustomTextField.h
[u]复制代码[/u] 代码如下:
#import <UIKit/UIKit.h> @class InputValidator; @interface CustomTextField : UITextField @property(nonatomic,strong)InputValidator *inputValidator; -(BOOL)validate; @end
CustomTextField.m
[u]复制代码[/u] 代码如下:
#import "CustomTextField.h" #import "InputValidator.h" @implementation CustomTextField -(BOOL)validate {     NSError *error = nil;     BOOL validationResult = [_inputValidator validateInput:self error:&error];             if (!validationResult) {         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[error localizedDescription] message:[error localizedFailureReason] delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", @"") otherButtonTitles: nil];                 [alertView show];     }     return validationResult; } @end
最后在ViewController中测试是否完成验证 ViewController.m
[u]复制代码[/u] 代码如下:
#import "ViewController.h" #import "CustomTextField.h" #import "NumericInputValidator.h" #import "AlphaInputValidator.h" @interface ViewController () @end
[u]复制代码[/u] 代码如下:
@implementation ViewController - (void)viewDidLoad {     [super viewDidLoad];     _numberTextField.inputValidator = [NumericInputValidator new];     _letterTextField.inputValidator = [AlphaInputValidator new];     // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning {     [super didReceiveMemoryWarning];     // Dispose of any resources that can be recreated. } #pragma mark - ValidButtonMehtod - (IBAction)validNumAction:(id)sender {     [_numberTextField validate]; } - (IBAction)validLetterAction:(id)sender {     [_letterTextField validate]; } @end
结果:当我们输入的不满足条件的时候就会显示提示信息,而满足条件就不会有任何提示。 [b]优点 [/b] [list] [*]策略模式是一种定义一系列算法的方法,从概念上来看,所有这些算法完成的都是相同的工作,只是实现不同,它可以以相同的方式调用所有的算法,减少了各种算法类与使用算法类之间的耦合。[/*] [*]策略模式的Stategy类层次为Context定义了一些列的可供重用的算法或行为。继承有助于析取出算法中的公共功能。[/*] [*]策略模式的优点是简化了单元测试,因为每个算法都有自己的类,可以通过自己的接口单独测试。[/*] [/list] [b]使用场景 [/b] [list] [*]一个类在其操作中使用多个条件语句来定义许多行为。我们可以把相关的条件分支移到他们自己的策略类中[/*] [*]需要算法的各种变体[/*] [*]需要避免把复杂的、与算法相关的数据结构暴露给客户端[/*] [/list] [b]总结 [/b]再总结一下策略方法的实现,本质上就是需要完成一个事情(出行),但是并不清楚需要使用怎样的策略,所以封装出一个函数,能够把需要的策略(young OR old)作为参数传递进来,并且使用相应的策略完成这个事件的处理。 最后简单谈一谈个人对于策略模式和面向对象中多态的思想的理解,首先多态是高层次,高度抽象的概念,独立于语言之外,是面向对象思想的精髓,而策略模式只是一种软件设计模式,相对而言更加具体,而且具体实现依赖于具体的编程语言,比如OC和java的实现方法并不相同,是language-dependent的。其次,多态更多强调的是,不同的对象调用同一个方法会得到不同的结果,而策略模式更多强调的是,同一个对象(事实上这个对象本身并不重要)在不同情况下执行不同的方法,而他们的实现方式又是高度类似的,即共享同一个父类并且各自重写父类的方法。 以上观点纯属个人愚见,欢迎大牛指正,互相交流。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部