iOS design mode-policy
Effect
Description
1. The algorithm that solves the same problem is abstracted into a strategy (the same problem refers to the same input parameters, but different output parameters will vary depending on the algorithm)
2. The policy is encapsulated within the object (part of the object's content), and the policy changes the content of the object. If the behavior of an object is extended externally, it is not called the policy mode, but the adornment mode.
3. Strategy mode simplifies complex judgment logic (If-else)
4. This tutorial becomes difficult to understand if you are unfamiliar with object-oriented fundamentals and the fundamentals of design patterns.
Source
Https://github.com/YouXianMing/StrategyPattern
////InputValidator.h//Strategypattern////Created by youxianming on 15/7/26.//Copyright (c) 2015 youxianming. All rights reserved.//#import<Foundation/Foundation.h>#import<UIKit/UIKit.h>#defineEmpty_input @ "Input is empty"@interfaceInputvalidator:nsobject/** Abstract Strategy * * @param input Current TextField * * @return Input verification is legal*/-(BOOL) ValidateInput: (Uitextfield *) input;/** * error message*/@property (nonatomic, strong) NSString*errormessage;@end
////INPUTVALIDATOR.M//Strategypattern////Created by youxianming on 15/7/26.//Copyright (c) 2015 youxianming. All rights reserved.//#import "InputValidator.h"@implementationInputvalidator-(BOOL) ValidateInput: (Uitextfield *) Input {returnNO;}@end
////CustomField.h//Strategypattern////Created by youxianming on 15/7/26.//Copyright (c) 2015 youxianming. All rights reserved.//#import<UIKit/UIKit.h>#import "InputValidator.h"@interfaceCustomfield:uitextfield/** * Abstract Strategy*/@property (nonatomic, Strong,ReadOnly) Inputvalidator *Inputvalidator;/** * Initialize TextField * * @param frame * @param inputvalidator validation Policy * * @return Instance Object*/-(Instancetype) initWithFrame: (CGRect) frame withinputvalidator: (Inputvalidator *) Inputvalidator;/** * corresponds to the return value of the specific policy * * @return Eligibility*/-(BOOL) validate;@end
////CUSTOMFIELD.M//Strategypattern////Created by youxianming on 15/7/26.//Copyright (c) 2015 youxianming. All rights reserved.//#import "CustomField.h"@interfaceCustomField ()/** * Abstract Strategy*/@property (nonatomic, strong) Inputvalidator*Inputvalidator;@end@implementationCustomField#pragmaMark-Initialize-(instancetype) initWithFrame: (CGRect) frame withinputvalidator: (Inputvalidator *) Inputvalidator { self=[Super Initwithframe:frame]; if(self) {[self setup]; //hold InputvalidatorSelf.inputvalidator =Inputvalidator; } returnSelf ;}- (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 A. f]; Self.layer.borderWidth=0.5f;}-(BOOL) Validate {return[Self.inputvalidator validateinput:self];}@end
Analysis
Policy mode comparison (abstract class vs. concrete implementation)
Policies are encapsulated in objects (part of the object's content), and the policy changes the contents of the object
Simplified If-else operation (according to the previous wording, there are a few TextField judgment, you have to write a few if-else, also, the implementation details exposed, maintenance difficulties-_-!!)
iOS design mode-policy