iOS中策略模式初运用

文章转载自:http://blog.csdn.net/iukey/article/details/8011200

前段时间项目中涉及到输入验证比较多,有简单的是否为纯数字输入的验证,是否为纯字母输入的验证,也有复杂的正则检查验证。

偶尔翻阅去年买的一本设计模式的书,看到了一种设计模式:策略模式。

运用策略模式,把输入验证抽象出来,写成一个单独的类,在需要的地方调用岂不是很方便。

下面是实现的过程:

一、设计基类

抽象出一个基类,把不同的验证写成子类,这样在所有地方就可以调用同一个接口,大大降低使用者的复杂度。

基类的设计先看代码:

[java]  view plain copy print ?
  1. #import <Foundation/Foundation.h>  
  2. static NSString* const InputValidationErrorDomain = @"InputValidationErrorDomain";  
  3. @interface InputValidator : NSObject  
  4.   
  5. -(BOOL) validateInput:(UITextField*)input error:(NSError**)error;  
  6. @end  

可以看到我们仅仅提供了一个实例方法把要验证的对象传进去,然后传一个填充error的指针。返回验证结果为BOOL型。

再来看实现:

[java]  view plain copy print ?
  1. #import "InputValidator.h"  
  2.   
  3. @implementation InputValidator  
  4. -(BOOL) validateInput:(UITextField *)input error:(NSError **)error{  
  5.     if (error) {  
  6.         *error=nil;  
  7.     }  
  8.     return NO;  
  9. }  
  10. @end  

基类就这么简单。

然后,就是根据需求去具体实现子类。

二、设计子类

子类是根据我们的需求具体去设计及实现。我这里提供两个简单的验证:纯数字验证、纯字母验证。

先看纯数字的验证:

[java]  view plain copy print ?
  1. #import "InputValidator.h"  
  2.   
  3. @interface NumericInputValidator : InputValidator  
  4.   
  5. - (BOOL) validateInput:(UITextField *)input error:(NSError **)error;  
  6. @end  

可以看到我们继承了基类,然后需要重载唯一的实例方法。再看实现:

[java]  view plain copy print ?
  1. #import "NumericInputValidator.h"  
  2.   
  3. @implementation NumericInputValidator  
  4. - (BOOL) validateInput:(UITextField *)input error:(NSError **)error{  
  5.     NSError* regError = nil;  
  6.     NSRegularExpression* regex =[ NSRegularExpression regularExpressionWithPattern:@"^[0-9]*$" options:NSRegularExpressionAnchorsMatchLines error:®Error];  
  7.       
  8.     NSUInteger numberOfMatches = [regex numberOfMatchesInString:[input text] options:NSMatchingAnchored range:NSMakeRange(0, [[input text]length])];  
  9.       
  10.     if (numberOfMatches==0) {  
  11.         if (error != nil) {  
  12.             NSString* description = NSLocalizedString(@"Input Validation Failed", @"");  
  13.             NSString* reason = NSLocalizedString(@"The input can contain only numerical values", @"");  
  14.               
  15.             NSArray* objArray = [NSArray arrayWithObjects:description,reason, nil];  
  16.             NSArray* keyArray = [NSArray arrayWithObjects:NSLocalizedDescriptionKey,NSLocalizedDescriptionKey, nil];  
  17.               
  18.             NSDictionary* userInfo = [NSDictionary dictionaryWithObjects:objArray forKeys:keyArray];  
  19.             *error = [NSError errorWithDomain:InputValidationErrorDomain code:1001 userInfo:userInfo];  
  20.         }  
  21.         return NO;  
  22.     }  
  23.     return  YES;  
  24. }  
  25. @end  

可以看到实现就丰满起来了。


再看纯字母验证的:

头文件:

[java]  view plain copy print ?
  1. #import "InputValidator.h"  
  2.   
  3. @interface AlphaInputValidator : InputValidator{  
  4.       
  5. }  
  6. - (BOOL) validateInput:(UITextField *)input error:(NSError **)error;  
  7. @end  

实现:

[java]  view plain copy print ?
  1. #import "AlphaInputValidator.h"  
  2.   
  3. @implementation AlphaInputValidator  
  4. - (BOOL) validateInput:(UITextField *)input error:(NSError **)error{  
  5.     NSError* regError = nil;  
  6.     NSRegularExpression* regex =[ NSRegularExpression regularExpressionWithPattern:@"^[a-zA-Z]*$" options:NSRegularExpressionAnchorsMatchLines error:®Error];  
  7.       
  8.     NSUInteger numberOfMatches = [regex numberOfMatchesInString:[input text] options:NSMatchingAnchored range:NSMakeRange(0, [[input text]length])];  
  9.       
  10.     if (numberOfMatches==0) {  
  11.         if (error != nil) {  
  12.             NSString* description = NSLocalizedString(@"Input Validation Failed", @"");  
  13.             NSString* reason = NSLocalizedString(@"The input can contain only letters", @"");  
  14.               
  15.             NSArray* objArray = [NSArray arrayWithObjects:description,reason, nil];  
  16.             NSArray* keyArray = [NSArray arrayWithObjects:NSLocalizedDescriptionKey,NSLocalizedDescriptionKey, nil];  
  17.               
  18.             NSDictionary* userInfo = [NSDictionary dictionaryWithObjects:objArray forKeys:keyArray];  
  19.             *error = [NSError errorWithDomain:InputValidationErrorDomain code:1002 userInfo:userInfo];  
  20.         }  
  21.         return NO;  
  22.     }  
  23.     return  YES;  
  24. }  
  25.   
  26. @end  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值