iOS 策略模式附Demo

策略模式

原文連接 : 連接 jianshu地址 : 連接 定義一系列的算法, 而且將每個算法封裝起來, 算法之間還能夠相互替換 能夠看下圖來體會 git

demo演示

需求簡單作一個只接收字母, 只接收數字的demo, 驗證登陸

以下圖所示: github

基本步驟

那麼咱們能夠這樣寫--->( 此時所有在控制器中,並無進行抽取 ) 定義算法

@property (weak, nonatomic) IBOutlet UITextField *letterInput;//字母輸入
@property (weak, nonatomic) IBOutlet UITextField *numberInput;//數字輸入
複製代碼

算法swift

#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;
}
複製代碼

代理atom

#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(@"未輸入");
}
}
}
複製代碼

此時並無進行抽取spa

策略模式進行抽取

首先咱們來根據上圖的思路來建立一個抽象類---InputTextField類

聲明代理

//策略輸入 YES 經過
//NO 不經過
- (BOOL)inputTextField:(UITextField *)textField;
@property (nonatomic,copy)NSString *attributeInputStr;//屬性字符
複製代碼

抽象方法指針

- (BOOL)inputTextField:(UITextField *)textField{
return NO;
}
複製代碼

場景類---CustomTextField

一樣咱們來聲明一個BOOL類型驗證方法, 並將抽象類導入, 以前屬於一個聚合的關係code

@property (nonatomic,strong)InputTextField *inputTextField;//抽象策略類
//驗證方法
- (BOOL)isOK;
複製代碼

實現cdn

- (BOOL)isOK{
BOOL result = [self.inputTextField inputTextField:self];
if(!result){
NSLog(@"--%@",self.inputTextField.attributeInputStr);
}
return result;
}
複製代碼

實現類---LetterInput, ---NumberInput, 這兩個類所有是繼承於抽象類的

此時咱們開始寫實現

- (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

相關文章
相關標籤/搜索