In fact, there are many types of iOS design patterns, in the actual application we should be able to use some design patterns to standardize and enrich our code ~ ~ Hey
In fact, the strategy pattern is to abstract the complex logic of our code into an object to deal with, which will greatly reduce the amount of code, and to make our logic look more organized.
Take if else as an example to write a complex judgment logic into an abstract base class in which to judge and then use the principle of inheritance and polymorphism to execute different business logic, which will reduce the amount of code in the controller
Controller code: Control execution logic by passing in judgment conditions
////VIEWCONTROLLER.M//Strategydemo////Created by Sansing on 16/2/8.//copyright©2016 year Sansing. All rights reserved.//#import "ViewController.h"#import "StrategyClass.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.NSLog (@"\ nthe first time printing:%@\n second time printing:%@", [Strategyclass type:type_1],[strategyclass type:type_2]);}@end
Code in abstract class:
////STRATEGYCLASS.M//Strategydemo////Created by Sansing on 16/2/8.//copyright©2016 year Sansing. All rights reserved.//#import "StrategyClass.h"#import "Strategyclasstype_1.h"#import "strategyclasstype_2.h"@implementationStrategyclass+ (ID) Type: (fxtype) type{if(Type = =type_1) { return[Strategyclasstype_1 Showstr]; }Else if(Type = =type_2) { return[strategyclasstype_2 Showstr]; }Else{ return @""; }}@end
Okay, all done.
iOS design mode-policy mode