编程思想

1. 面向过程

面向过程:处理事情以过程为核心,一步一步的实现。

2. 面向对象

面向对象:万物皆对象。(封装、继承、多态)

3. 链式编程

链式编程:是将多个操作(多行代码)通过点号(.)链接在一起成为一句代码,使代码可读性好。a(1).b(2).c(3)

  • 链式编程特点:方法的返回值是block,block必须有返回值(本身对象),block参数(需要操作的值)
  • 代表:masonry框架。
  • 模仿masonry,写一个加法计算器,练习链式编程思想。
    int iResult = [CaculatorMaker makeCaculators:^(CaculatorMaker * _Nonnull make) {
        make.add123(9).sub123(2).muilt123(3);
    }];
    NSLog(@"--%d--",iResult);
//CaculatorMaker.h

#import 

NS_ASSUME_NONNULL_BEGIN

@interface CaculatorMaker : NSObject

@property (nonatomic, assign) int iResult;

+ (int)makeCaculators:(void(^)(CaculatorMaker *make))caculatorMaker;

//加法
- (CaculatorMaker *(^)(int))add123;

//减法
- (CaculatorMaker *(^)(int))sub123;

//乘法
- (CaculatorMaker *(^)(int))muilt123;

//除法
- (CaculatorMaker *(^)(int))divide123;

@end

NS_ASSUME_NONNULL_END
//CaculatorMaker.m
#import "CaculatorMaker.h"

@implementation CaculatorMaker

+ (int)makeCaculators:(void (^)(CaculatorMaker * _Nonnull))caculatorMaker {
    CaculatorMaker *maker = [[CaculatorMaker alloc] init];
    caculatorMaker(maker);
    return maker.iResult;
}

- (CaculatorMaker * _Nonnull (^)(int))add123 {
    return ^CaculatorMaker *(int value) {
        self->_iResult += value;
        return self;
    };
}

- (CaculatorMaker * _Nonnull (^)(int))sub123 {
    return ^CaculatorMaker *(int value) {
        self->_iResult -= value;
        return self;
    };
}

- (CaculatorMaker * _Nonnull (^)(int))muilt123 {
    return ^CaculatorMaker *(int value) {
        self->_iResult *= value;
        return self;
    };
}

- (CaculatorMaker * _Nonnull (^)(int))divide123 {
    return ^CaculatorMaker *(int value) {
        self->_iResult /= value;
        return self;
    };
}

@end

4. 函数式编程 FP(Functional Programming)

  • 函数式编程特点:每个方法必须有返回值(本身对象),把函数或者Block当做参数,block参数(需要操作的值)block返回值(操作结果)
  • 代表:ReactiveCocoa
  • 用函数式编程实现,写一个加法计算器,并且加法计算器自带判断是否等于某个值.
    Caculator *c = [[Caculator alloc] init];
    BOOL isqule = [[[c caculator:^int(int result) {
        result += 2;
        result *= 5;
        return result;
    }] equle:^BOOL(int result) {
        return result == 10;
    }] isEqule];
    
//    BOOL isqule = [[c caculator:^int(int result) {
//        result += 2;
//        result *= 5;
//        return result;
//    }] equle:^BOOL(int result) {
//        return result == 10;
//    }].isEqule;
    
    NSLog(@"---%d",isqule);
//Caculator.h
#import 

NS_ASSUME_NONNULL_BEGIN

@interface Caculator : NSObject

@property (nonatomic, assign) BOOL isEqule;
@property (nonatomic, assign) int result;

- (Caculator *)caculator:(int(^)(int result))caculator;
- (Caculator *)equle:(BOOL(^)(int result))operation;

@end

NS_ASSUME_NONNULL_END
//Caculator.m
#import "Caculator.h"

@implementation Caculator

- (Caculator *)caculator:(int (^)(int))caculator {
    self.result = caculator(0);
    return self;
}

- (Caculator *)equle:(BOOL (^)(int))operation {
    self.isEqule = operation(self.result);
    return self;
}

@end

5. 响应式编程 RP(Reactive Programming)

你变我也变

  • 代表:KVO运用

6. 函数响应式编程 FRP ReactiveObjC

MVVM + RxSwift


block 返回值 ------链式编程 masonry
block 参数 ------函数式编程

参考

你可能感兴趣的:(编程思想)