策略模式
定義一系列的算法,將每一個(gè)算法封裝起來(lái)香追,并且這些算法之間可以相互替換。
抽象策略類(lèi)西壮,為所有支持或相關(guān)算法聲明共同接口康震,用策略接口來(lái)實(shí)現(xiàn)相關(guān)算法的具體策略類(lèi)谚咬。場(chǎng)景類(lèi)對(duì)象使用策略接口多態(tài)調(diào)用具體策略類(lèi)對(duì)應(yīng)的方法尚辑。
策略類(lèi)是對(duì)象的一部分。應(yīng)用逗载,適用場(chǎng)景
已知策略挚躯,用驗(yàn)證策略驗(yàn)證郵箱、電話號(hào)碼格式。
驗(yàn)證策略模式
//
// 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
郵箱驗(yàn)證策略模式
//
// EmailValidator.h
// LearnStrategy
//
// Created by 印林泉 on 2017/3/3.
// Copyright ? 2017年 yiq. All rights reserved.
//
#import "InputValidator.h"
@interface EmailValidator : InputValidator
///重載了父類(lèi)的驗(yàn)證方法
- (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 = @"沒(méi)有輸入";
} else {
BOOL isMatch = [input.text isEqualToString:@"1546310515@qq.com"];
if (isMatch == NO) {
self.errorMessage = @"請(qǐng)輸入正確的郵箱";
} else {
self.errorMessage = nil;
}
}
return self.errorMessage == nil ? YES : NO;
}
@end
電話號(hào)碼驗(yàn)證策略模式
//
// PhoneNumberValidator.h
// LearnStrategy
//
// Created by 印林泉 on 2017/3/3.
// Copyright ? 2017年 yiq. All rights reserved.
//
#import "InputValidator.h"
@interface PhoneNumberValidator : InputValidator
///重載了父類(lèi)的驗(yàn)證方法
- (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 = @"沒(méi)有輸入";
} else {
BOOL isMatch = [input.text isEqualToString:@"18721409352"];
if (isMatch == NO) {
self.errorMessage = @"請(qǐng)輸入正確的手機(jī)號(hào)碼";
} 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:///輸入驗(yàn)證器
@interface CustomTextField : UITextField
///抽象的策略
@property (strong, nonatomic) InputValidator *validator;
///初始化
- (instancetype)initWithFrame:(CGRect)frame;
///驗(yàn)證輸入合法性
- (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>
///輸入郵箱的驗(yàn)證
@property (nonatomic, strong) CustomTextField *emailTextField;
///輸入電話號(hào)碼的驗(yàn)證框
@property (nonatomic, strong) CustomTextField *phoneNumberTextField;
/////驗(yàn)證email地址
//- (NSString *)validateEmailInput:(UITextField *)input;
/////驗(yàn)證電話號(hào)碼
//- (NSString *)validatePhoneNumberInput:(UITextField *)input;
//
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
///初始化按鈕
[self initButton];
///初始化驗(yàn)證框
[self initCustomTextFields];
}
- (void)initCustomTextFields {
self.emailTextField = [[CustomTextField alloc] initWithFrame:CGRectMake(30, 80, Width - 60, 30)];
self.emailTextField.placeholder = @"請(qǐng)輸入郵箱";
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 = @"請(qǐng)輸入電話號(hào)碼";
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
抽象父類(lèi)方法延遲到子類(lèi)實(shí)現(xiàn)黄锤。