block与delegate在代码组织上的特点

代码组织是影响代码可读性的重要因素之一,block与delegate对代码的组织提供了两个不同的方向:
- block:让代码更集中
- delegate:让代码更有层次
举例说明:
我封装了一个倒计时button,提供了block与delegate两种回调方式:
- block
__weak typeof(self) weakSelf = self;[self.countDownButton configDuration:10 buttonClicked:^{ //========== 按钮点击 ==========// [weakSelf.countDownButton startCountDown];} countDownStart:^{ //========== 倒计时开始 ==========// NSLog(@"倒计时开始");} countDownUnderway:^(NSInteger restCountDownNum) { //========== 倒计时进行中 ==========// NSString *title = [NSString stringWithFormat:@"%ld秒后重试", restCountDownNum]; [weakSelf.countDownButton setTitle:title forState:UIControlStateNormal];} countDownCompletion:^{ //========== 倒计时结束 ==========// [weakSelf.countDownButton setTitle:@"点击获取验证码" forState:UIControlStateNormal]; NSLog(@"倒计时结束");}];- delegate
self.countDownButton.dataSource = self;self.countDownButton.delegate = self;#pragma mark - CQCountDownButton DataSource// 设置倒计时总时间- (NSInteger)startCountDownNumOfCountDownButton:(CQCountDownButton *)countDownButton { return 10;}#pragma mark - CQCountDownButton Delegate// 倒计时按钮点击时回调- (void)countDownButtonDidClick:(CQCountDownButton *)countDownButton { [self.countDownButton startCountDown];}// 倒计时开始的回调- (void)countDownButtonDidStartCountDown:(CQCountDownButton *)countDownButton { NSLog(@"倒计时开始");}// 倒计时进行中的回调- (void)countDownButtonDidCountDown:(CQCountDownButton *)countDownButton withRestCountDownNum:(NSInteger)restCountDownNum { NSString *title = [NSString stringWithFormat:@"%ld秒后重试", restCountDownNum]; [self.countDownButton setTitle:title forState:UIControlStateNormal];}// 倒计时结束时的回调- (void)countDownButtonDidEndCountDown:(CQCountDownButton *)countDownButton { [self.countDownButton setTitle:@"点击获取验证码" forState:UIControlStateNormal]; NSLog(@"倒计时结束");}可以看到,采用block的方式,可以将几个回调集中在一个方法里,而delegate,每个回调都需要一个代理商方法。
似乎看来,从代码组织层面上讲,block比delegate要好很多。
假如你这样想就真的太年轻了。delegate的特点是让代码组织更有层次,只有当代码量比较大的时候才能表现出分层的优势。比方,还是拿这个倒计时按钮举例,现在添加一个需求:点击按钮时先获取验证码,获取成功后再开始倒计时。
- 现在block里的代码是这样的:
__weak typeof(self) weakSelf = self;[self.countDownButton configDuration:10 buttonClicked:^{ //========== 按钮点击 ==========// [SVProgressHUD showWithStatus:@"正在获取验证码..."]; // 模拟数据请求 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ int a = arc4random() % 2; if (a == 0) { // 获取成功 [SVProgressHUD showSuccessWithStatus:@"验证码已发送"]; // 获取到验证码后开始倒计时 [weakSelf.countDownButton startCountDown]; } else { // 获取失败 [SVProgressHUD showErrorWithStatus:@"获取失败,请重试"]; [weakSelf.countDownButton endCountDown]; } });} countDownStart:^{ //========== 倒计时开始 ==========// NSLog(@"倒计时开始");} countDownUnderway:^(NSInteger restCountDownNum) { //========== 倒计时进行中 ==========// NSString *title = [NSString stringWithFormat:@"%ld秒后重试", restCountDownNum]; [weakSelf.countDownButton setTitle:title forState:UIControlStateNormal];} countDownCompletion:^{ //========== 倒计时结束 ==========// [weakSelf.countDownButton setTitle:@"点击获取验证码" forState:UIControlStateNormal]; NSLog(@"倒计时结束");}];是不是感觉多了一大坨代码瞬间可读性就降低了很多?假如每个block里都是一大坨代码呢?你或者许会说老子把那坨代码抽离出去弄个新方法不就OK了?当然OK,不过这个时候你去看看delegate,用它来解决是不是好看多了?
总结
当代码量比较小且将多个回调聚集在一起可读性较高时,block是不错的选择;
当代码量比较大且需要较好的区分各个回调时,delegate是很好的选择。
上一篇 目录 下一篇
说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » block与delegate在代码组织上的特点
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » block与delegate在代码组织上的特点