前段时间项目中涉及到输入验证比较多,有简单的是否为纯数字输入的验证,是否为纯字母输入的验证,也有复杂的正则检查验证。
偶尔翻阅去年买的一本设计模式的书,看到了一种设计模式:策略模式。
运用策略模式,把输入验证抽象出来,写成一个单独的类,在需要的地方调用岂不是很方便。
下面是实现的过程:
一、设计基类
抽象出一个基类,把不同的验证写成子类,这样在所有地方就可以调用同一个接口,大大降低使用者的复杂度。
基类的设计先看代码:
#import <Foundation/Foundation.h>
static NSString* const InputValidationErrorDomain = @"InputValidationErrorDomain";
@interface InputValidator : NSObject
-(BOOL) validateInput:(UITextField*)input error:(NSError**)error;
@end
可以看到我们仅仅提供了一个实例方法把要验证的对象传进去,然后传一个填充error的指针。返回验证结果为BOOL型。
再来看实现:
#import "InputValidator.h"
@implementation InputValidator
-(BOOL) validateInput:(UITextField *)input error:(NSError **)error{
if (error) {
*error=nil;
}
return NO;
}
@end
基类就这么简单。
然后,就是根据需求去具体实现子类。
二、设计子类
子类是根据我们的需求具体去设计及实现。我这里提供两个简单的验证:纯数字验证、纯字母验证。
先看纯数字的验证:
#import "InputValidator.h"
@interface NumericInputValidator : InputValidator
- (BOOL) validateInput:(UITextField *)input error:(NSError **)error;
@end
可以看到我们继承了基类,然后需要重载唯一的实例方法。再看实现:
#import "NumericInputValidator.h"
@implementation NumericInputValidator
- (BOOL) validateInput:(UITextField *)input error:(NSError **)error{
NSError* regError = nil;
NSRegularExpression* regex =[ NSRegularExpression regularExpressionWithPattern:@"^[0-9]*$" options:NSRegularExpressionAnchorsMatchLines error:®Error];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:[input text] options:NSMatchingAnchored range:NSMakeRange(0, [[input text]length])];
if (numberOfMatches==0) {
if (error != nil) {
NSString* description = NSLocalizedString(@"Input Validation Failed", @"");
NSString* reason = NSLocalizedString(@"The input can contain only numerical values", @"");
NSArray* objArray = [NSArray arrayWithObjects:description,reason, nil];
NSArray* keyArray = [NSArray arrayWithObjects:NSLocalizedDescriptionKey,NSLocalizedDescriptionKey, nil];
NSDictionary* userInfo = [NSDictionary dictionaryWithObjects:objArray forKeys:keyArray];
*error = [NSError errorWithDomain:InputValidationErrorDomain code:1001 userInfo:userInfo];
}
return NO;
}
return YES;
}
@end
可以看到实现就丰满起来了。
再看纯字母验证的:
头文件:
#import "InputValidator.h"
@interface AlphaInputValidator : InputValidator{
}
- (BOOL) validateInput:(UITextField *)input error:(NSError **)error;
@end
实现:
#import "AlphaInputValidator.h"
@implementation AlphaInputValidator
- (BOOL) validateInput:(UITextField *)input error:(NSError **)error{
NSError* regError = nil;
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])];
if (numberOfMatches==0) {
if (error != nil) {
NSString* description = NSLocalizedString(@"Input Validation Failed", @"");
NSString* reason = NSLocalizedString(@"The input can contain only letters", @"");
NSArray* objArray = [NSArray arrayWithObjects:description,reason, nil];
NSArray* keyArray = [NSArray arrayWithObjects:NSLocalizedDescriptionKey,NSLocalizedDescriptionKey, nil];
NSDictionary* userInfo = [NSDictionary dictionaryWithObjects:objArray forKeys:keyArray];
*error = [NSError errorWithDomain:InputValidationErrorDomain code:1002 userInfo:userInfo];
}
return NO;
}
return YES;
}
@end