iOS设计模式-策略模式

    在写程序的时候,我们经常会碰到这样的场景:把一堆算法塞到同一段代码中,然后使用if-else或switch-case条件语句来决定要使用哪个算法?这些算法可能是一堆相似的类函数或方法,用以解决相关的问题。比如,一个验证输入数据的例程,数据本身可以是任何数据类型(如NSString、CGFloat等),每种数据类型需要不同的验证算法。如果能把每个算法封装成一个对象,那么就能消除根据数据类型决定使用什么算法的一堆if-else或switch-case语句。


    我们把相关算法分离为不同的类,称为策略模式。策略模式:定义一系列算法,把它们一个个封装起来,并且使它们可相互替换。本模式使得算法可独立于使用它的客户端而变化。

    在以下情形下,我们应该考虑使用策略模式。
    @:一个类在其操作中,使用多个条件语句来定义许多行为,我们可以把相关的条件分支移到它们自己的策略类中。
    @:需要算法的各种变体。
    @:需要避免把复杂的、与算法相关的数据结构暴漏给客户端。

    我们用一个简单的例子来说明以下,策略模式是怎么使用的。假设有两个UITextField,一个UITextField只能输入字母,另一个UITextField只能输入数字,为了确保输入的有效性,我们需要在用户结束文本框的编辑时做下验证。我们把数据验证放在代理方法textFieldDidEndEdting中。

    如果不使用策略模式,我们的代码会写成这样:

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == self.numberTF) {
        // 验证其值只包含数字
       
    }else if (textField == self.alphaTF) {
        // 验证其值只包含字母
       
    }
}

    要是有更多不同类型的文本框,条件语句还会继续下去。如果能去掉这些条件语句,代码会更容易管理,将来对代码的维护也会容易许多。

    现在的目标是把这些验证检查提到各种策略类中,这样他们就能在代理方法和其他方法之中重用。每个验证都从文本框取出输入值,然后根据所H需的策略进行验证,最后返回一个BOOL值。如果返回失败,还会返回一个NSError实例。返回的NSError可以解释失败的原因。

    我们设计一个抽象基类InputValidator,里面有一个validateInput:input error:error方法。分别有两个子类NumberInputValidator、AlphaInputValidator。具体的代码如下所示:

    InputValidator.h中抽象InputValidator的类声明

static NSString *const InputValidationErrorDomain = @"InputValidationErrorDomain";

@interface InputValidator : NSObject
/**
 *  实际验证策略的存根方法
 */
- (BOOL)validateInput:(UITextField *)input error:(NSError *__autoreleasing *)error;

@end

    这个方法还有一个NSError指针的引用,当有错误发生时(即验证失败),方法会构造一个NSError实例,并赋值给这个指针,这样使用验证的地方就能做详细的错误处理。

    InputValidator.m中抽象InputValidator的默认实现

#import "InputValidator.h"

@implementation InputValidator

- (BOOL)validateInput:(UITextField *)input error:(NSError *__autoreleasing *)error {
    if (error) {
        *error = nil;
    }
    return NO;
}

@end

    我们已经定义了输入验证器的行为,然后我们要编写真正的输入验证器了,先来写数值型的,如下:

    NumberInputValidator.h中NumberInputValidator的类定义

#import "InputValidator.h"

@interface NumberInputValidator : InputValidator

/**
 *  这里重新声明了这个方法,以强调这个子类实现或重载了什么,这不是必须的,但是是个好习惯。
 */
- (BOOL)validateInput:(UITextField *)input error:(NSError *__autoreleasing *)error;
@end

    NumberInputValidator.m中NumberInputValidator的实现

#import "NumberInputValidator.h"

@implementation NumberInputValidator

- (BOOL)validateInput:(UITextField *)input error:(NSError *__autoreleasing *)error {
    NSError *regError = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9]*$" options:NSRegularExpressionAnchorsMatchLines error:&regError];
    
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:input.text options:NSMatchingAnchored range:NSMakeRange(0, input.text.length)];
    // 如果没有匹配,就会错误和NO.
    if (numberOfMatches == 0) {
        if (error != nil) {
            // 先判断error对象是存在的
            NSString *description = NSLocalizedString(@"验证失败", @"");
            NSString *reason = NSLocalizedString(@"输入仅能包含数字", @"");
            NSArray *objArray = [NSArray arrayWithObjects:description, reason, nil];
            NSArray *keyArray = [NSArray arrayWithObjects:NSLocalizedDescriptionKey, NSLocalizedFailureReasonErrorKey, nil];
            
            NSDictionary *userInfo = [NSDictionary dictionaryWithObjects:objArray forKeys:keyArray];
            //错误被关联到定制的错误代码1001和在InputValidator的头文件中。
            *error = [NSError errorWithDomain:InputValidationErrorDomain code:1001 userInfo:userInfo]; 
        }
        
        return NO;
    }
    
    return YES;
}

@end

    现在,我们来编写字母验证的实现,代码如下:

    AlphaInputValidator.h中AlphaInputValidator的类定义

#import "InputValidator.h"
@interface AlphaInputValidator : InputValidator

- (BOOL)validateInput:(UITextField *)input error:(NSError *__autoreleasing *)error;

@end

    AlphaInputValidator.m中AlphaInputValidator的实现:

#import "AlphaInputValidator.h"

@implementation AlphaInputValidator

- (BOOL)validateInput:(UITextField *)input error:(NSError *__autoreleasing *)error {
    NSError *regError = nil;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[a-zA-Z]*$" options:NSRegularExpressionAnchorsMatchLines error:&regError];
    
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:input.text options:NSMatchingAnchored range:NSMakeRange(0, input.text.length)];
    // 如果没有匹配,就会错误和NO.
    if (numberOfMatches == 0) {
        if (error != nil) {
            // 先判断error对象是存在的
            NSString *description = NSLocalizedString(@"验证失败", @"");
            NSString *reason = NSLocalizedString(@"输入仅能包字母", @"");
            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]; //错误被关联到定制的错误代码1002和在InputValidator的头文件中。
        }
        
        return NO;
    }
    
    return YES;
}

@end

    AlphaInputValidator也是实现了validateInput方法的InputValidator类型。它的代码结构和算法跟NumberInputValidator相似,只是使用了不同的正则表达式,不同错误代码和消息。可以看到两个版本的代码有很多重复。两个算法结构相同,我们可以把这个结构,我们可以把这个结构重构成抽象父类的模板方法(将在下一篇博客中,来进行实现)。

    至此,我们已经写好了输入验证器,可以在客户端来使用了,但是UITextField不认识它们,所以我们需要自己的UITextField版本。我们要创建UITextField的子类,其中有一个InputValidator的引用,以及一个方法validate。代码如下:

CustomTextField.h中CustomTextField的类声明

#import <UIKit/UIKit.h>
#import "InputValidator.h"
@interface CustomTextField : UITextField

@property (nonatomic, strong) InputValidator *inputValidator; //用一个属性保持对InputValidator的引用。

- (BOOL)validate;

@end

    CustomTextField有一个属性保持着对InputValidator的引用。当调用它的validate方法时,它会使用这个InputValidator引用,开始进行实际的验证过程。

    CustomTextField.m中CustomTextField的实现

#import "CustomTextField.h"

@implementation CustomTextField

- (BOOL)validate {
    NSError *error = nil;
    BOOL validationResult = [_inputValidator validateInput:self error:&error];
    
    if (!validationResult) {
        // 通过这个例子也让自己明白了,NSError的具体用法。
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:[error localizedDescription] message:[error localizedFailureReason] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
    }
    
    return validationResult;
}

@end

    validate方法向inputValidator引用发送了[_inputValidator validateInput:self error:&error]消息。CustomTextField无需知道使用的是什么类型的InputValidator以及算法的任何细节,这就是策略模式的好处。对于客户端使用来说,只需要调用validate方法就可以了。因此在将来如果添加了新的InputValidator,客户端不需要做任何的改动的。

    下面,我们看下客户端是怎么使用的,代码如下。

#import "ViewController.h"
#import "CustomTextField.h"
#import "InputValidator.h"
#import "NumberInputValidator.h"
#import "AlphaInputValidator.h"
@interface ViewController () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet CustomTextField *numberTF;
@property (weak, nonatomic) IBOutlet CustomTextField *alphaTF;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    InputValidator *numberValidator = [[NumberInputValidator alloc] init];
    InputValidator *alphaValidator = [[AlphaInputValidator alloc] init];
    
    _numberTF.inputValidator = numberValidator;
    _alphaTF.inputValidator = alphaValidator;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITextFieldDelegate

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([textField isKindOfClass:[CustomTextField class]]) {
        [(CustomTextField *)textField validate];
    }
}

@end

    可以看出,我们不需要那些条件语句了,相反,我们使用一条简洁得多的语句,实现同样的数据验证。除了上面多了一条确保textField对象的类型是CustomField的额外检查之外,不应再有任何复杂的东西。


这就是策略模式。

转载于:https://my.oschina.net/daguoshi/blog/486688

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值