策略模式
定義一系列的算法,將每一個算法封裝起來,并且這些算法之間可以相互替換。
抽象策略類,為所有支持或相關算法聲明共同接口,用策略接口來實現相關算法的具體策略類。場景類對象使用策略接口多態調用具體策略類對應的方法。
策略類是對象的一部分。應用,適用場景
已知策略,用驗證策略驗證郵箱、電話號碼格式。
驗證策略模式
//
// InputValidator.h
// LearnStrategy
//
// Created by 印林泉 on 2017/3/3.
// Copyright ? 2017年 yiq. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface InputValidator : NSObject
///策略的輸入
- (BOOL)validateInput:(UITextField *)input;
@property (strong, nonatomic)NSString *errorMessage;
@end
//
// InputValidator.m
// LearnStrategy
//
// Created by 印林泉 on 2017/3/3.
// Copyright ? 2017年 yiq. All rights reserved.
//
#import "InputValidator.h"
@implementation InputValidator
- (BOOL)validateInput:(UITextField *)input {
return NO;
}
@end
郵箱驗證策略模式
//
// EmailValidator.h
// LearnStrategy
//
// Created by 印林泉 on 2017/3/3.
// Copyright ? 2017年 yiq. All rights reserved.
//
#import "InputValidator.h"
@interface EmailValidator : InputValidator
///重載了父類的驗證方法
- (BOOL)validateInput:(UITextField *)input;
@end
//
// EmailValidator.m
// LearnStrategy
//
// Created by 印林泉 on 2017/3/3.
// Copyright ? 2017年 yiq. All rights reserved.
//
#import "EmailValidator.h"
#ifndef DisableRegExCategoriesMacros
#define RX(pattern) [[NSRegularExpression alloc] initWithPattern:pattern]
#endif
@implementation EmailValidator
- (BOOL)validateInput:(UITextField *)input {
if (input.text.length <= 0) {
self.errorMessage = @"沒有輸入";
} else {
BOOL isMatch = [input.text isEqualToString:@"1546310515@qq.com"];
if (isMatch == NO) {
self.errorMessage = @"請輸入正確的郵箱";
} else {
self.errorMessage = nil;
}
}
return self.errorMessage == nil ? YES : NO;
}
@end
電話號碼驗證策略模式
//
// PhoneNumberValidator.h
// LearnStrategy
//
// Created by 印林泉 on 2017/3/3.
// Copyright ? 2017年 yiq. All rights reserved.
//
#import "InputValidator.h"
@interface PhoneNumberValidator : InputValidator
///重載了父類的驗證方法
- (BOOL)validateInput:(UITextField *)input;
@end
//
// PhoneNumberValidator.m
// LearnStrategy
//
// Created by 印林泉 on 2017/3/3.
// Copyright ? 2017年 yiq. All rights reserved.
//
#import "PhoneNumberValidator.h"
@implementation PhoneNumberValidator
- (BOOL)validateInput:(UITextField *)input {
if (input.text.length <= 0) {
self.errorMessage = @"沒有輸入";
} else {
BOOL isMatch = [input.text isEqualToString:@"18721409352"];
if (isMatch == NO) {
self.errorMessage = @"請輸入正確的手機號碼";
} else {
self.errorMessage = nil;
}
}
return self.errorMessage == nil ? YES : NO;
}
@end
自定義輸入框
//
// CustomTextField.h
// LearnStrategy
//
// Created by 印林泉 on 2017/3/3.
// Copyright ? 2017年 yiq. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "InputValidator.h"http:///輸入驗證器
@interface CustomTextField : UITextField
///抽象的策略
@property (strong, nonatomic) InputValidator *validator;
///初始化
- (instancetype)initWithFrame:(CGRect)frame;
///驗證輸入合法性
- (BOOL)validate;
@end
//
// CustomTextField.m
// LearnStrategy
//
// Created by 印林泉 on 2017/3/3.
// Copyright ? 2017年 yiq. All rights reserved.
//
#import "CustomTextField.h"
@implementation CustomTextField
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (void)setup {
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, self.frame.size.height)];
self.leftView = leftView;
self.leftViewMode = UITextFieldViewModeAlways;
self.font = [UIFont fontWithName:@"Avenir-Book" size:12.f];
self.layer.borderWidth = 0.5f;
}
- (BOOL)validate {
return [self.validator validateInput:self];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
使用
//
// ViewController.m
// LearnStrategy
//
// Created by 印林泉 on 2017/3/3.
// Copyright ? 2017年 yiq. All rights reserved.
//
#import "ViewController.h"
#import "CustomTextField.h"
#import "EmailValidator.h"
#import "PhoneNumberValidator.h"
#define Width [UIScreen mainScreen].bounds.size.width
@interface ViewController ()<UITextFieldDelegate>
///輸入郵箱的驗證
@property (nonatomic, strong) CustomTextField *emailTextField;
///輸入電話號碼的驗證框
@property (nonatomic, strong) CustomTextField *phoneNumberTextField;
/////驗證email地址
//- (NSString *)validateEmailInput:(UITextField *)input;
/////驗證電話號碼
//- (NSString *)validatePhoneNumberInput:(UITextField *)input;
//
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
///初始化按鈕
[self initButton];
///初始化驗證框
[self initCustomTextFields];
}
- (void)initCustomTextFields {
self.emailTextField = [[CustomTextField alloc] initWithFrame:CGRectMake(30, 80, Width - 60, 30)];
self.emailTextField.placeholder = @"請輸入郵箱";
self.emailTextField.delegate = self;
self.emailTextField.validator = [EmailValidator new];
[self.view addSubview:self.emailTextField];
self.phoneNumberTextField = [[CustomTextField alloc] initWithFrame:CGRectMake(30, 80 + 40, Width - 60, 30)];
self.phoneNumberTextField.placeholder = @"請輸入電話號碼";
self.phoneNumberTextField.delegate = self;
self.phoneNumberTextField.validator = [PhoneNumberValidator new];
[self.view addSubview:self.phoneNumberTextField];
}
#pragma mark - 初始化按鈕以及按鈕事件
- (void)initButton {
UIButton *button = [[UIButton alloc] initWithFrame:(CGRect){0, 30, 90, 30}];
[button setTitle:@"back" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonsEvent:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonsEvent:(UIButton *)button {
[self.view endEditing:YES];
}
#pragma mark - 文本框代理
- (void)textFieldDidEndEditing:(UITextField *)textField {
CustomTextField *customTextField = (CustomTextField *)textField;
if ([customTextField validate] == NO) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:customTextField.validator.errorMessage preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:alertAction];
[self presentViewController:alertController animated:YES completion:nil];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
抽象父類方法延遲到子類實現。