iOS design mode-policy mode

Source: Internet
Author: User

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

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.