The strategy pattern

The jianshu link defines a series of algorithms, and encapsulates each algorithm. Algorithms can also be replaced with each other to get an idea

The demo presentation

A simple demo that accepts only letters and numbers is required to verify login

As shown below:

Basic steps

So we can write –>(all in the controller at this point, no extraction) definition

@property (weak, nonatomic) IBOutlet UITextField *letterInput;// Enter letters
@property (weak, nonatomic) IBOutlet UITextField *numberInput;// Digital input
Copy the code

algorithm

#pragma mark - validate input - (NSString*)letterInput:(UITextField *)textField{
if(textField.text.length == 0) {return nil;
}
// The set of valid characters a-za-z or more from beginning to end
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;
// Check that the match does not match 0
if(numberOfMateches == 0){
outLetter = @"Please re-enter";
}else{
outLetter = @"Input correctly";
}
return outLetter;
}
- (NSString*)numberInput:(UITextField *)textField{
if(textField.text.length == 0) {return nil;
}
// The set of valid characters is 0-9 or more from beginning to end
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;
// Check that the match does not match 0
if(numberOfMateches == 0){
outLetter = @"Please re-enter";
}else{
outLetter = @"Input correctly";
}
return outLetter;
}
Copy the code

The agent

#pragma mark - proxy - (void)textFieldDidEndEditing:(UITextField *)textField{
if(textField == self.letterInput){
// Validate the input value
NSString *outPut = [self letterInput:textField];
if (outPut) {
NSLog(@"-- % @",outPut);
}else{
NSLog(@"Not entered"); }}else if(textField == self.numberInput){
// The validation is a number
NSString *outPut = [self numberInput:textField];
if (outPut) {
NSLog(@"-- % @",outPut);
}else{
NSLog(@"Not entered"); }}}Copy the code

There is no extraction at this point

The strategy pattern is extracted

First let’s create an abstract class, InputTextField, based on the idea above

The statement

// Policy enter YES
/ / NO not through
- (BOOL)inputTextField:(UITextField *)textField;
@property (nonatomic,copy)NSString *attributeInputStr;// Attribute character
Copy the code

Abstract methods

- (BOOL)inputTextField:(UITextField *)textField{
return NO;
}
Copy the code

Class – CustomTextField scene

Let’s also declare a BOOL validation method and import the abstract class that was previously an aggregation

@property (nonatomic,strong)InputTextField *inputTextField;// Abstract policy class
// Verify the method
- (BOOL)isOK;
Copy the code

implementation

- (BOOL)isOK{
BOOL result = [self.inputTextField inputTextField:self];
if(! result){NSLog(@"-- % @".self.inputTextField.attributeInputStr);
}
return result;
}
Copy the code

Implement classes LetterInput and NumberInput, both of which inherit from abstract classes

Now let’s start writing the implementation

- (BOOL)inputTextField:(UITextField *)textField{
if(textField.text.length == 0) {self.attributeInputStr = @"Letter input is empty";
return nil;
}
// The set of valid characters is 0-9 or more from beginning to end
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)];

// Check that the match does not match 0
if(numberOfMateches == 0) {self.attributeInputStr = @"Please re-enter";
}else{
self.attributeInputStr = @"Input correctly";
}
return self.attributeInputStr == nil ? YES : NO;
}
Copy the code
- (BOOL)inputTextField:(UITextField *)textField{
if(textField.text.length == 0) {self.attributeInputStr = @"Digital input is empty";
return nil;
}
// The set of valid characters is 0-9 or more from beginning to end
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^ [0-9] * $" options:NSRegularExpressionAnchorsMatchLines error:nil];
NSUInteger numberOfMateches = [regex numberOfMatchesInString:[textField text] options:NSMatchingAnchored range:NSMakeRange(0, [textField text].length)];

// Check that the match does not match 0
if(numberOfMateches == 0) {self.attributeInputStr = @"Please re-enter";
}else{
self.attributeInputStr = @"Input correctly";
}
return self.attributeInputStr == nil ? YES : NO;
}
Copy the code

Implementation in controller

Superclass Pointers point to subclass objects

self.letterInput.inputTextField = [LetterInput new];
self.numberInput.inputTextField = [NumberInput new];
Copy the code

call

- (void)textFieldDidEndEditing:(UITextField *)textField{
if ([textField isKindOfClass:[CustomTextField class]]) {[(CustomTextField*)textField inputTextField]; }}Copy the code

conclusion

If we add another policy, we just need to add another class, add a direct algorithm call, so that just create a class in the Controller, will it be much easier to maintain the code later? Ok, give you this simple demo, of course, in the code also wrote a note, you can go to my git download, welcome star download link: demo address technical exchange Q group 150731459