原文链接 : 链接 jianshu 地址 : 链接 定义一系列的算法, 并且将每一个算法封装起来, 算法之间还可以相互替换 可以看下图来体会
如下图所示:
那么我们可以这样写 --->(此时全部在控制器中, 并没有进行抽取) 定义
- @property (weak, nonatomic) IBOutlet UITextField *letterInput;//字母输入
- @property (weak, nonatomic) IBOutlet UITextField *numberInput;//数字输入
算法
- #pragma mark - 验证输入 - (NSString * ) letterInput: (UITextField * ) textField {
- if (textField.text.length == 0) {
- return nil;
- }
- //从开头到结尾,有效字符集合a-zA-Z或者更多
- NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern: @"^[a-zA-Z]*$"options: NSRegularExpressionAnchorsMatchLines error: nil];
- NSUInteger numberOfMateches = [regex numberOfMatchesInString: [textField text] options: NSMatchingAnchored range: NSMakeRange(0, [textField text].length)];
- NSString * outLetter = nil;
- //判断,匹配不符合为0
- if (numberOfMateches == 0) {
- outLetter = @"请重新输入";
- } else {
- outLetter = @"输入正确";
- }
- return outLetter;
- } - (NSString * ) numberInput: (UITextField * ) textField {
- if (textField.text.length == 0) {
- return nil;
- }
- //从开头到结尾,有效字符集合0-9或者更多
- NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern: @"^[0-9]*$"options: NSRegularExpressionAnchorsMatchLines error: nil];
- NSUInteger numberOfMateches = [regex numberOfMatchesInString: [textField text] options: NSMatchingAnchored range: NSMakeRange(0, [textField text].length)];
- NSString * outLetter = nil;
- //判断,匹配不符合为0
- if (numberOfMateches == 0) {
- outLetter = @"请重新输入";
- } else {
- outLetter = @"输入正确";
- }
- return outLetter;
- }
代理
- #pragma mark - 代理 - (void) textFieldDidEndEditing: (UITextField * ) textField {
- if (textField == self.letterInput) {
- //验证输入值
- NSString * outPut = [self letterInput: textField];
- if (outPut) {
- NSLog(@"--%@", outPut);
- } else {
- NSLog(@"未输入");
- }
- } else if (textField == self.numberInput) {
- //验证是数字
- NSString * outPut = [self numberInput: textField];
- if (outPut) {
- NSLog(@"--%@", outPut);
- } else {
- NSLog(@"未输入");
- }
- }
- }
此时并没有进行抽取
声明
- //策略输入 YES 通过
- //NO 不通过
- - (BOOL) inputTextField: (UITextField * ) textField;@property(nonatomic, copy) NSString * attributeInputStr; //属性字符
抽象方法
- - (BOOL) inputTextField: (UITextField * ) textField {
- return NO;
- }
同样我们来声明一个 BOOL 类型验证方法, 并将抽象类导入, 之前属于一个聚合的关系
- @property(nonatomic, strong) InputTextField * inputTextField; //抽象策略类
- //验证方法
- - (BOOL) isOK;
实现
- - (BOOL) isOK {
- BOOL result = [self.inputTextField inputTextField: self];
- if (!result) {
- NSLog(@"--%@", self.inputTextField.attributeInputStr);
- }
- return result;
- }
此时我们开始写实现
- - (BOOL) inputTextField: (UITextField * ) textField {
- if (textField.text.length == 0) {
- self.attributeInputStr = @"字母输入为空";
- return nil;
- }
- //从开头到结尾,有效字符集合0-9或者更多
- NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern: @"^[a-zA-Z]*$"options: NSRegularExpressionAnchorsMatchLines error: nil];
- NSUInteger numberOfMateches = [regex numberOfMatchesInString: [textField text] options: NSMatchingAnchored range: NSMakeRange(0, [textField text].length)];
- //判断,匹配不符合为0
- if (numberOfMateches == 0) {
- self.attributeInputStr = @"请重新输入";
- } else {
- self.attributeInputStr = @"输入正确";
- }
- return self.attributeInputStr == nil ? YES: NO;
- }
- - (BOOL) inputTextField: (UITextField * ) textField {
- if (textField.text.length == 0) {
- self.attributeInputStr = @"数字输入为空";
- return nil;
- }
- //从开头到结尾,有效字符集合0-9或者更多
- NSRegularExpression * regex = [NSRegularExpression regularExpressionWithPattern: @"^[0-9]*$"options: NSRegularExpressionAnchorsMatchLines error: nil];
- NSUInteger numberOfMateches = [regex numberOfMatchesInString: [textField text] options: NSMatchingAnchored range: NSMakeRange(0, [textField text].length)];
- //判断,匹配不符合为0
- if (numberOfMateches == 0) {
- self.attributeInputStr = @"请重新输入";
- } else {
- self.attributeInputStr = @"输入正确";
- }
- return self.attributeInputStr == nil ? YES: NO;
- }
父类指针指向子类对象
- self.letterInput.inputTextField = [LetterInput new];
- self.numberInput.inputTextField = [NumberInput new];
调用
- - (void)textFieldDidEndEditing:(UITextField *)textField{
- if ([textField isKindOfClass:[CustomTextField class]]) {
- [(CustomTextField *)textField inputTextField];
- }
- }
假如说我们又多了一个策略, 只需要再次增加一个类, 增加一个算法直接调用, 这样的话就在 Controller 中仅仅创建一个类就可以了, 对于后期的代码维护是不是方便了许多呢? 好了, 给大家这个简单 demo, 当然在代码中也写了注释, 可以去我的 git 下载, 欢迎 star 下载链接 : demo 地址 技术交流 q 群 150731459
来源: https://juejin.im/post/5a30ed226fb9a0452b493ab2