iOS design mode-policy

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.