策略模式
原文鏈接 : 鏈接
定義一系列的算法, 并且將每一個(gè)算法封裝起來(lái), 算法之間還可以相互替換
可以看下圖來(lái)體會(huì)
圖1.png
demo演示
需求簡(jiǎn)單做一個(gè)只接收字母, 只接收數(shù)字的demo, 驗(yàn)證登錄
如下圖所示:
圖2.png
基本步驟
那么我們可以這樣寫(xiě)--->( 此時(shí)全部在控制器中,并沒(méi)有進(jìn)行抽取 )
定義
@property (weak, nonatomic) IBOutlet UITextField *letterInput;//字母輸入
@property (weak, nonatomic) IBOutlet UITextField *numberInput;//數(shù)字輸入
算法
#pragma mark -驗(yàn)證輸入
- (NSString*)letterInput:(UITextField *)textField{
if(textField.text.length == 0){
return nil;
}
//從開(kāi)頭到結(jié)尾,有效字符集合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 = @"請(qǐng)重新輸入";
}else{
outLetter = @"輸入正確";
}
return outLetter;
}
- (NSString*)numberInput:(UITextField *)textField{
if(textField.text.length == 0){
return nil;
}
//從開(kāi)頭到結(jié)尾,有效字符集合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 = @"請(qǐng)重新輸入";
}else{
outLetter = @"輸入正確";
}
return outLetter;
}
代理
#pragma mark -代理
- (void)textFieldDidEndEditing:(UITextField *)textField{
if(textField == self.letterInput){
//驗(yàn)證輸入值
NSString *outPut = [self letterInput:textField];
if (outPut) {
NSLog(@"--%@",outPut);
}else{
NSLog(@"未輸入");
}
}else if(textField == self.numberInput){
//驗(yàn)證是數(shù)字
NSString *outPut = [self numberInput:textField];
if (outPut) {
NSLog(@"--%@",outPut);
}else{
NSLog(@"未輸入");
}
}
}
此時(shí)并沒(méi)有進(jìn)行抽取
策略模式進(jìn)行抽取
圖1.png
首先我們來(lái)根據(jù)上圖的思路來(lái)創(chuàng)建一個(gè)抽象類---InputTextField類
聲明
//策略輸入 YES 通過(guò)
//NO 不通過(guò)
- (BOOL)inputTextField:(UITextField *)textField;
@property (nonatomic,copy)NSString *attributeInputStr;//屬性字符
抽象方法
- (BOOL)inputTextField:(UITextField *)textField{
return NO;
}
場(chǎng)景類---CustomTextField
同樣我們來(lái)聲明一個(gè)BOOL類型驗(yàn)證方法, 并將抽象類導(dǎo)入, 之前屬于一個(gè)聚合的關(guān)系
@property (nonatomic,strong)InputTextField *inputTextField;//抽象策略類
//驗(yàn)證方法
- (BOOL)isOK;
實(shí)現(xiàn)
- (BOOL)isOK{
BOOL result = [self.inputTextField inputTextField:self];
if(!result){
NSLog(@"--%@",self.inputTextField.attributeInputStr);
}
return result;
}
實(shí)現(xiàn)類---LetterInput, ---NumberInput, 這兩個(gè)類全部是繼承于抽象類的
此時(shí)我們開(kāi)始寫(xiě)實(shí)現(xiàn)
- (BOOL)inputTextField:(UITextField *)textField{
if(textField.text.length == 0){
self.attributeInputStr = @"字母輸入為空";
return nil;
}
//從開(kāi)頭到結(jié)尾,有效字符集合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 = @"請(qǐng)重新輸入";
}else{
self.attributeInputStr = @"輸入正確";
}
return self.attributeInputStr == nil ? YES : NO;
}
- (BOOL)inputTextField:(UITextField *)textField{
if(textField.text.length == 0){
self.attributeInputStr = @"數(shù)字輸入為空";
return nil;
}
//從開(kāi)頭到結(jié)尾,有效字符集合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 = @"請(qǐng)重新輸入";
}else{
self.attributeInputStr = @"輸入正確";
}
return self.attributeInputStr == nil ? YES : NO;
}
控制器中實(shí)現(xiàn)
父類指針指向子類對(duì)象
self.letterInput.inputTextField = [LetterInput new];
self.numberInput.inputTextField = [NumberInput new];
調(diào)用
- (void)textFieldDidEndEditing:(UITextField *)textField{
if ([textField isKindOfClass:[CustomTextField class]]) {
[(CustomTextField *)textField inputTextField];
}
}
總結(jié)
假如說(shuō)我們又多了一個(gè)策略, 只需要再次增加一個(gè)類, 增加一個(gè)算法直接調(diào)用, 這樣的話就在Controller中僅僅創(chuàng)建一個(gè)類就可以了, 對(duì)于后期的代碼維護(hù)是不是方便了許多呢?
好了, 給大家這個(gè)簡(jiǎn)單demo, 當(dāng)然在代碼中也寫(xiě)了注釋, 可以去我的git下載, 歡迎star
下載鏈接 : demo地址
技術(shù)交流q群150731459