美文网首页
2018-11-04iOS链式响应编程-加减乘除

2018-11-04iOS链式响应编程-加减乘除

作者: 窗内户外 | 来源:发表于2018-11-04 22:54 被阅读0次
    一、什么是链式编程?
    链式编程,是指将多个方法用点语法链接起来。
    1)让代码更加简洁;
    2)可读性更强;
    3)编程性强;
    4)对程序员的业务能力要求高;
    5)不太利于代码调试。 
    
    二、调用代码,如下:
    //
    //  ViewController.m
    //  链式编程Demo
    //
    //  Created by ghk on 2018/11/4.
    //  Copyright © 2018年 ghk. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "Tool.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        float result = [Tool make:^(Tool *tool) {
    
            tool.add(1).add(2).add(3).subtract(1).ride(5).divide(4);
        }];
    
        NSLog(@"%f",result);
    }
    
    
    @end
    
    
    三、Tool实现代码,如下:
    //
    //  Tool.h
    //  链式编程Demo
    //
    //  Created by ghk on 2018/11/4.
    //  Copyright © 2018年 ghk. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface Tool : NSObject
    
    typedef Tool * (^Block)(NSInteger number);
    
    /** 加法 */
    @property (nonatomic, copy, readonly) Block add;
    /** 减法 */
    @property (nonatomic, copy, readonly) Block subtract;
    /** 乘法 */
    @property (nonatomic, copy, readonly) Block ride;
    /** 除法 */
    @property (nonatomic, copy, readonly) Block divide;
    
    + (float)make:(void (^)(Tool *tool))block;
    
    @end
    
    //
    //  Tool.m
    //  链式编程Demo
    //
    //  Created by ghk on 2018/11/4.
    //  Copyright © 2018年 ghk. All rights reserved.
    //
    
    #import "Tool.h"
    
    @interface Tool ()
    
    @property (nonatomic, assign) float result;
    
    @end
    
    @implementation Tool
    
    - (void)dealloc
    {
        NSLog(@"%s",__func__);
    }
    
    + (float )make:(void (^)(Tool *tool))block {
    
        if (block) {
    
            Tool *tool = [[Tool alloc] init];
            block(tool);
            return tool.result;
        }
        return 0;
    }
    
    - (Block)add {
    
        return ^(NSInteger number) {
    
            float newResult = self.result;
            newResult += number;
            NSLog(@" %f + %zi = %f",self.result,number,newResult);
            self.result = newResult;
    
            return self;
        };
    }
    
    - (Block)subtract {
    
        return ^(NSInteger number) {
    
            float newResult = self.result;
            newResult -= number;
            NSLog(@" %f - %zi = %f",self.result,number,newResult);
            self.result = newResult;
    
            return self;
        };
    }
    
    - (Block)ride {
    
        return ^(NSInteger number) {
            float newResult = self.result;
            newResult *= number;
            NSLog(@" %f × %zi = %f",self.result,number,newResult);
            self.result = newResult;
            return self;
        };
    }
    
    - (Block)divide {
    
        return ^(NSInteger number) {
            float newResult = self.result;
            newResult /= number;
            NSLog(@" %f ÷ %zi = %f",self.result,number,newResult);
            self.result = newResult;
            return self;
        };
    }
    
    @end
    
    欢迎评价!!!!!!
    感谢支持!!!!!!

    相关文章

      网友评论

          本文标题:2018-11-04iOS链式响应编程-加减乘除

          本文链接:https://www.haomeiwen.com/subject/kjndxqtx.html