iOS链式、函数式和响应式编程

2019-05-05  本文已影响0人  00after

原文
编程思想-iOS链式、函数式和响应式编程

在了解链式、函数式和响应式编程前,我们需要回顾下Block,它在下面的编程中起着核心作用。

Block

block表达式语法:
^返回值类型(参数列表){表达式}

例如:
^int (int count) {
  return count + 1;
};

声明类型变量的语法
返回值类型(^变量名)(参数列表) = block表达式

例如:
int (^sum)(int count) = (^int count) {
  return count + 1;
};

作为函数参数的语法
- (void)func(int (^)(int))block {

}

定义block简写
typedef int (^Sumblock)(int);

- (void)func(Sumblock)block {

}

作为返回值的语法,相当于get方法,不允许带参数
- (int (^)(int))func {

   return ^(int count) {

      return count ++;
    };
}

定义block简写
typedef int (^Sumblock)(int);
- (Sumblock)func {  

  return ^(int count) {

    return count ++;
  };
}

总结

链式编程

链式编程思想特点:方法的返回值必须是方法的调用者

下面是我们的普通的写法


@interface Person : NSObject

- (void)eat;
- (void)sleep;

@end

@implementation Person

- (void)eat
{
    NSLog(@"%s", __FUNCTION__);
}

- (void)sleep
{
    NSLog(@"%s", __FUNCTION__);
}

@end

ViewController.m
Person *person = [[Person alloc] init];
调用时必须单个调用,而且不能任意组合顺序
  /** 普通的调用方式  */
    [person eat];
    [person sleep];

链式写法:方法增加一个返回值,且返回值为调用者本身

// 链式写法
Person.h
- (Person *)eat;
- (Person *)sleep;

Person.m
- (Person *)eat
{
    NSLog(@"%s", __FUNCTION__);
    return self;
}

- (Person *)sleep
{
    NSLog(@"%s", __FUNCTION__);
    return self;
}

ViewController.m
    Person *person = [[Person alloc] init];
  /** 链式写法,这样不仅可以无限调用,而且可以控制顺序 */
    [[person eat] sleep];
    [[person sleep] eat];
    [[person eat] eat];

    /** 通过”点”语法,将需要执行的代码块连续的书写下去,就是链式编程.它能使代码简单易读,书写方便 */
    person.eat.sleep.eat.sleep.sleep;

链式编程带参数的写法:将block作为返回值

Person.h
- (Person *(^)(NSString *food))eat3;
- (Person *(^)(NSString *where))sleep3;

Person.m
- (Person *(^)(NSString *food))eat3
{
    return ^(NSString *food) {

        NSLog(@"吃:%@ ",food);

        return self;
    };
}

- (Person *(^)(NSString *where))sleep3
{
    return ^(NSString *where) {

        NSLog(@"睡在:%@上",where);

        return self;
    };
}

ViewController.m
    Person *person = [[Person alloc] init];

    /** 链式 + 函数式写法 */
    person.sleep3(@"床").eat3(@"苹果").eat3(@"香蕉").sleep3(@"沙发");

    返回值block不带参数,()不传参即可
    person.sleep3().eat3().eat3().sleep3();

函数式编程

函数式编程思想:是将操作尽可能写在一起!嵌套的函数!!
本质:就是往方法里面传入Block,方法中嵌套Block调用.

Person.h
@property (nonatomic, assign) NSInteger result;
- (Person *)calculator:(NSInteger(^)(NSInteger result))block;

Person.m
/** 返回调用者本身,获取其它属性和方法 */
- (Person *)calculator:(NSInteger(^)(NSInteger result))block
{
    _result = block(_result);

    return self;
}

ViewController.m
   Person *person = [[Person alloc] init];
/** 计算器 */
   Person *calculatPerson = [person calculator:^NSInteger(NSInteger result) {

        result = result + 10;
        result = result*10;
        return result;
    }];

    NSLog(@"%ld", calculatPerson.result);

函数+链式编程

若将函数和链式编程结合,我们的程序将会爆发出艺术的火花
典型案例:Masonry界面布局框架

   //创建一个View
    UIView * redView = [[UIView alloc]init];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    //链式编程思想特点:方法返回值必须要有方法调用者!!

    //添加约束  --  make约束制造者!!
    [redView mas_makeConstraints:^(MASConstraintMaker *make) {
        //设置约束 每次调用left\top就是将约束添加到数组中!
        /*
         MASConstraint * (^block)(id)  = make.left.top.equalTo;
         MASConstraint * mk = block(@10);
         mk.top;
        */
        make.left.top.equalTo(@10);

        make.right.bottom.equalTo(@-10);
        /**
            mas_makeConstraints执行流程:
            1.创建约束制造者MASConstraintMaker,并且绑定控件,生成一个保存所有约束的数组
            2.执行mas_makeConstraints传入的Block
            3.让约束制造者安装约束!
                * 1.清空之前的所有约束
                * 2.遍历约束数组,一个一个安装
         */

    }];

我们可以模仿Masonry 写一个简单的加法计算器

addCalculator.h
@property (nonatomic, assign) NSInteger sumresult;
- (addCalculator * (^)(NSInteger sumresult))add;

addCalculator.m
- (addCalculator * (^)(NSInteger sumresult))add
{
    return ^(NSInteger sumresult) {

        _sumresult += sumresult;

        return self;
    };
}

Person.h
// 函数链式编程
@property (nonatomic, assign) NSInteger result;
- (Person *)makecalculator:(void (^)(addCalculator *addcalculator))block;

Person.m
- (Person *)makecalculator:(void (^)(addCalculator *addcalculator))block
{
    addCalculator *add = [[addCalculator alloc] init];
    if (block) {
        block(add);
    }

    self.result = add.sumresult;
    return self;
}

    /** 函数链式编程 */
ViewController.m
    Person *person = [[Person alloc] init];
    [person makecalculator:^(addCalculator *addcalculator) {

        addcalculator.add(10).add(30);
    }];

    NSLog(@"person : %ld", person.result);

响应式编程

解释:在程序开发中:a = b + c赋值之后 b 或者 c 的值变化后,a 的值不会跟着变化;
响应式编程目标就是,如果 b 或者 c 的数值发生变化,a 的数值会同时发生变化;
响应编程的经典案例:KVO
响应式编程框架:ReactiveCocoa(RAC)
RAC学习可参考SkyHarute 简书博客

源码下载


作者:Yochi
链接:https://www.jianshu.com/p/9cf7ecafc756
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

上一篇 下一篇

猜你喜欢

热点阅读