iOS中策略模式初运用
2013-06-18 15:05
471 查看
文章转载自:http://blog.csdn.net/iukey/article/details/8011200
前段时间项目中涉及到输入验证比较多,有简单的是否为纯数字输入的验证,是否为纯字母输入的验证,也有复杂的正则检查验证。
偶尔翻阅去年买的一本设计模式的书,看到了一种设计模式:策略模式。
运用策略模式,把输入验证抽象出来,写成一个单独的类,在需要的地方调用岂不是很方便。
下面是实现的过程:
一、设计基类
抽象出一个基类,把不同的验证写成子类,这样在所有地方就可以调用同一个接口,大大降低使用者的复杂度。
基类的设计先看代码:
[java] view
plaincopyprint?
#import <Foundation/Foundation.h>
static NSString* const InputValidationErrorDomain = @"InputValidationErrorDomain";
@interface InputValidator : NSObject
-(BOOL) validateInput:(UITextField*)input error:(NSError**)error;
@end
可以看到我们仅仅提供了一个实例方法把要验证的对象传进去,然后传一个填充error的指针。返回验证结果为BOOL型。
再来看实现:
[java] view
plaincopyprint?
#import "InputValidator.h"
@implementation InputValidator
-(BOOL) validateInput:(UITextField *)input error:(NSError **)error{
if (error) {
*error=nil;
}
return NO;
}
@end
基类就这么简单。
然后,就是根据需求去具体实现子类。
二、设计子类
子类是根据我们的需求具体去设计及实现。我这里提供两个简单的验证:纯数字验证、纯字母验证。
先看纯数字的验证:
[java] view
plaincopyprint?
#import "InputValidator.h"
@interface NumericInputValidator : InputValidator
- (BOOL) validateInput:(UITextField *)input error:(NSError **)error;
@end
可以看到我们继承了基类,然后需要重载唯一的实例方法。再看实现:
[java] view
plaincopyprint?
#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
可以看到实现就丰满起来了。
再看纯字母验证的:
头文件:
[java] view
plaincopyprint?
#import "InputValidator.h"
@interface AlphaInputValidator : InputValidator{
}
- (BOOL) validateInput:(UITextField *)input error:(NSError **)error;
@end
实现:
[java] view
plaincopyprint?
#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
前段时间项目中涉及到输入验证比较多,有简单的是否为纯数字输入的验证,是否为纯字母输入的验证,也有复杂的正则检查验证。
偶尔翻阅去年买的一本设计模式的书,看到了一种设计模式:策略模式。
运用策略模式,把输入验证抽象出来,写成一个单独的类,在需要的地方调用岂不是很方便。
下面是实现的过程:
一、设计基类
抽象出一个基类,把不同的验证写成子类,这样在所有地方就可以调用同一个接口,大大降低使用者的复杂度。
基类的设计先看代码:
[java] view
plaincopyprint?
#import <Foundation/Foundation.h>
static NSString* const InputValidationErrorDomain = @"InputValidationErrorDomain";
@interface InputValidator : NSObject
-(BOOL) validateInput:(UITextField*)input error:(NSError**)error;
@end
可以看到我们仅仅提供了一个实例方法把要验证的对象传进去,然后传一个填充error的指针。返回验证结果为BOOL型。
再来看实现:
[java] view
plaincopyprint?
#import "InputValidator.h"
@implementation InputValidator
-(BOOL) validateInput:(UITextField *)input error:(NSError **)error{
if (error) {
*error=nil;
}
return NO;
}
@end
基类就这么简单。
然后,就是根据需求去具体实现子类。
二、设计子类
子类是根据我们的需求具体去设计及实现。我这里提供两个简单的验证:纯数字验证、纯字母验证。
先看纯数字的验证:
[java] view
plaincopyprint?
#import "InputValidator.h"
@interface NumericInputValidator : InputValidator
- (BOOL) validateInput:(UITextField *)input error:(NSError **)error;
@end
可以看到我们继承了基类,然后需要重载唯一的实例方法。再看实现:
[java] view
plaincopyprint?
#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
可以看到实现就丰满起来了。
再看纯字母验证的:
头文件:
[java] view
plaincopyprint?
#import "InputValidator.h"
@interface AlphaInputValidator : InputValidator{
}
- (BOOL) validateInput:(UITextField *)input error:(NSError **)error;
@end
实现:
[java] view
plaincopyprint?
#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
相关文章推荐
- IOS中策略模式初运用
- “第一次亲密接触”——iOS中策略模式初运用
- iOS应用运用设计模式中的Strategy策略模式的开发实例
- “第一次亲密接触”——iOS中策略模式初运用
- iOS应用运用设计模式中的Strategy策略模式的开发实例
- iOS模式分析-策略模式
- iOS策略模式的简单应用
- IOS设计模式学习(19)策略
- 详解Python设计模式编程中观察者模式与策略模式的运用
- iOS开发-策略模式
- iOS设计模式 - 策略
- ios 开发 设计模式之策略模式
- IOS设计模式--策略模式
- 【iOS开发-57】案例改进:block动画、控件的removeFromSuperview、利用layer设置圆角矩形以及代理模式运用
- iOS应用开发中运用设计模式中的组合模式的实例解析
- iOS设计模式 - (4)策略模式
- iOS应用设计模式开发中对简单工厂和工厂方法模式的运用
- IOS设计模式学习(19)策略
- iOS App设计模式开发中策略模式的实现示例