美文网首页
设计模式-策略模式

设计模式-策略模式

作者: 江湖闹士 | 来源:发表于2018-04-13 10:54 被阅读42次

使用的条件:
1、输入是固定的
2、if..else switch..case 语句

优点:简化代码,使内容看起来更清晰明了
缺点:创建类比较多

策略模式的案例:不同的排列风格.jpg

举个简单的例子,需求如下:


验证输入框

接下来看下代码索引


代码.png
A为抽象类,A1,A2为A的继承类,实现A中的方法,方法中根据不同的情况进行实现代码操作。
B是具体的使用类,里面创建属性A,以A的不同子类情况来操作B。这里说不清楚,看代码会好理解点。
//InputTextFieldValidate.h中

#import <UIKit/UIKit.h>

@interface InputTextFieldValidate : NSObject

- (BOOL)validateInputTextField:(UITextField *)textField;

@property (nonatomic, strong) NSString *attributeInputStr;

@end

//InputTextFieldValidate.m中
#import "InputTextFieldValidate.h"

@implementation InputTextFieldValidate

- (BOOL)validateInputTextField:(UITextField *)textField {
    return NO;  //抽象类中不用实现方法,在子类中实现
}

@end

//LatterTextFieldValidate.h中
#import "InputTextFieldValidate.h"
@interface LatterTextFieldValidate : InputTextFieldValidate

@end

//LatterTextFieldValidate.m中
#import "LatterTextFieldValidate.h"

@implementation LatterTextFieldValidate

//实现父类(抽象类)中的方法
- (BOOL)validateInputTextField:(UITextField *)textField {
    
    if(textField.text.length == 0) {
        self.attributeInputStr = @"数值空的";
        return self.attributeInputStr;
    }
    
    // 正则
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[a-zA-Z]*$" options:NSRegularExpressionAnchorsMatchLines error:nil];
    
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:[textField text] options:NSMatchingAnchored range:NSMakeRange(0, [[textField text] length])];
    
    if (numberOfMatches == 0) {
        self.attributeInputStr = @"字母,输入有问题";
    } else {
        self.attributeInputStr = @"输入正确";
    }
    return self.attributeInputStr == nil ? YES : NO;
}

@end
//NumberTextFieldValidate.h中
#import "InputTextFieldValidate.h"

@interface NumberTextFieldValidate : InputTextFieldValidate

@end

//NumberTextFieldValidate.m中
#import "NumberTextFieldValidate.h"

@implementation NumberTextFieldValidate

//实现父类(抽象类)中的方法
- (BOOL)validateInputTextField:(UITextField *)textField {
    if(textField.text.length == 0) {
        self.attributeInputStr = @"数值空的";
        return self.attributeInputStr;
    }
    
    // 正则验证
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9]*$" options:NSRegularExpressionAnchorsMatchLines error:nil];
    
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:[textField text] options:NSMatchingAnchored range:NSMakeRange(0, [[textField text] length])];
    
    if (numberOfMatches == 0) {
        self.attributeInputStr = @"数字,输入有问题";
    } else {
        self.attributeInputStr = @"输入正确";
    }
    return self.attributeInputStr == nil ? YES : NO;
}

@end
//CustomTextField.h中
#import <UIKit/UIKit.h>
#import "InputTextFieldValidate.h"

@interface CustomTextField : UITextField

@property (nonatomic, strong) InputTextFieldValidate *inputValidate;

// 验证是否符合要求
- (BOOL)validate;

@end

//CustomTextField.m中
#import "CustomTextField.h"

@implementation CustomTextField

- (BOOL)validate {
    BOOL result = [self.inputValidate validateInputTextField:self];
    
    if (!result) {
        NSLog(@"----%@", self.inputValidate.attributeInputStr);
    } else {
        NSLog(@"----%@", self.inputValidate.attributeInputStr);
    }
    
    return result;
}
@end

在viewcontroller调用的时候

#import "ViewController.h"
#import "CustomTextField.h"

#import "LatterTextFieldValidate.h"
#import "NumberTextFieldValidate.h"


@interface ViewController () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet CustomTextField *letterInput; /**< 字母输入 */
@property (weak, nonatomic) IBOutlet CustomTextField *numberInput; /**< 数字输入 */
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.letterInput.delegate = self;
    self.numberInput.delegate = self;
    
    // 初始化
    self.letterInput.inputValidate = [LatterTextFieldValidate new];
    self.numberInput.inputValidate = [NumberTextFieldValidate new];
    
}

- (IBAction)btnClick:(id)sender {
    [self.view endEditing:YES];
}

#pragma mark - UITextFieldDelegate实现
- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    if ([textField isKindOfClass:[CustomTextField class]]) {
        
        [(CustomTextField *)textField validate];
    }
}
@end

总结:刚看是有点懵😳,最主要的是掌握这种设计模式,在脑子有这种印象,在项目中试着用用,熟能生巧!

相关文章

网友评论

      本文标题:设计模式-策略模式

      本文链接:https://www.haomeiwen.com/subject/hwtckftx.html