美文网首页
函数式编程/链式编程思想

函数式编程/链式编程思想

作者: smile_frank | 来源:发表于2019-03-12 17:40 被阅读0次

    传统的函数调用方式

        Fouction *aFouction = [[Fouction alloc]init];
        [aFouction dosomething];
        [aFouction working];
    

    Fouction.h

    /**
     传统写法
     */
    -(void)dosomething;
    -(void)working;
    
    

    Fouction.m

    -(void)dosomething{
        NSLog(@"dosomething ");
    }
    -(void)working{
        NSLog(@"working ");
    }
    

    链式编程调用

    Fouction *aFouction = [[Fouction alloc]init];
    [[[aFouction dosomething2] working2] dosomething2];
    

    Fouction.h

    /**
     链式编程方法
     @return 返回当前对象
     */
    -(Fouction *)dosomething2;
    -(Fouction *)working2;
    

    Fouction.m

    -(Fouction *)dosomething2{
        
        //执行操作办法
        NSLog(@"dosomething ");
        return  self;
    }
    
    -(Fouction *)working2{
        
        //执行操作办法
        NSLog(@"working");
        return  self;
    }
    
    

    函数式编程调用

      Fouction *aFouction = [[Fouction alloc]init];
      aFouction.dosomething4(@"工作").working4(@"休息");
    

    Fouction.h

    /**
     函数式编程 无参数
     */
    
    -(Fouction *(^)(void))dosomething3;
    -(Fouction *(^)(void))working3;
    
    /**
      函数式编程  有参数
     */
    -(Fouction *(^)(NSString *str))dosomething4;
    -(Fouction *(^)(NSString * str))working4;
    
    

    Fouction.m

    -(Fouction *(^)(void))dosomething3{
        Fouction *(^runBlock)(void) = ^ () {
            NSLog(@"执行了不带参数的函数方法");
            return self;
        };
        return runBlock;
        
     //   改进写法
    //    return ^(){
    //        NSLog(@"Block Run2");
    //        return self;
    //    };
    }
    -(Fouction *(^)(void))working3 {
        return ^() {
            NSLog(@"执行了无参数的方法");
            return self;
        };
    }
    
    -(Fouction *(^)(NSString *))working4 {
        Fouction *(^myBlock)(NSString *str) = ^(NSString *str) {
             NSLog(@"执行了传值过来的参数方法--%@",str);
            return self;
        };
        return myBlock;
    
    //    return ^(NSString *str) {
    //        NSLog(@"执行了传值过来的参数方法--%@",str);
    //        return self;
    //    };
    }
    
    -(Fouction *(^)(NSString *))dosomething4 {
        return ^(NSString *str) {
            NSLog(@"执行了传值过来的参数方法--%@",str);
            return self;
        };
    }
    

    源码git地址:https://gitee.com/WangBaofeng/hanshushibiancheng.git

    相关文章

      网友评论

          本文标题:函数式编程/链式编程思想

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