iOS架构:AOP实现局部板块化

利用 AOP 板块化细节业务,的确有趣。由于我们通常情况下说起 AOP,都会想起比方“埋点”、“method swizzing”等字眼,角度比较宏观,起到理解耦的作用;本文从另一个角度出发,使用 AOP 思想对细节业务做板块分离的工作。
AOP简介
面向切面编程(也叫面向方面):Aspect Oriented Programming(AOP),是目前软件开发中的一个热点。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
主要的功能是:日志记录,性能统计,安全控制,事务解决,异常解决等等。
主要的用意是:将日志记录,性能统计,安全控制,事务解决,异常解决等代码从业务逻辑代码中划分出来,通过对这些行为的分离,我们希望可以将它们独立到非指导业务逻辑的方法中,进而改 变这些行为的时候不影响业务逻辑的代码。

AOP实际是GoF设计模式的延续,设计模式孜孜不倦追求的是调用者和被调用者之间的解耦,AOP可以说也是这种目标的一种实现。
在实际业务需求中,出场率很高的是UITalbeView和UICollecitonView等需要用大量代理商方法配置的视图,当然这是苹果程序设计的惯例。当UI界面很复杂,业务逻辑相当多的时候,尽管把网络请求、数据解决、视图封装等都板块分离出去了,但是配置代理商里面的逻辑太多,我们想要每一个类解决一部分代理商方法。
首先,创立实现 UITableView 代理商的三个类:
@implementation TestTableViewDigitConfig- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 20;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 80;}@end@implementation TestTableViewClickConfig- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"click -- section:%ld, row:%ld", indexPath.section, indexPath.row);}@end@implementation TestTableViewCellConfig- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(UITableViewCell.class)]; if (!cell) { cell.selectionStyle = UITableViewCellSelectionStyleNone; cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:NSStringFromClass(UITableViewCell.class)]; } cell.textLabel.text = [NSString stringWithFormat:@"第%ld行", indexPath.row]; return cell;}@end如代码所见,这里将 tableView 的代理商用三个类来分别实现,而后在 UIViewController 里面只要要写这些代码:
@interface TestVC ()@property (nonatomic, strong) UITableView *tableView;@property (nonatomic, strong) YBAOPManager *aopManager;@property (nonatomic, strong) TestTableViewDigitConfig *digitConfig;@property (nonatomic, strong) TestTableViewClickConfig *clickConfig;@property (nonatomic, strong) TestTableViewCellConfig *cellConfig;@end@implementation TestVC#pragma mark life cycle- (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.tableView];}#pragma mark getter- (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain]; _tableView.tableFooterView = [UIView new]; _digitConfig = [TestTableViewDigitConfig new]; _clickConfig = [TestTableViewClickConfig new]; _cellConfig = [TestTableViewCellConfig new]; _aopManager = [YBAOPManager new]; [_aopManager addTarget:_digitConfig]; [_aopManager addTarget:_clickConfig]; [_aopManager addTarget:_cellConfig]; _tableView.delegate = _aopManager; _tableView.dataSource = _aopManager; } return _tableView;}@end核心代码就是将 YBAOPManager 类的使用:
当你需要使用多个对象(target)来承接少量方法的实现,初始化 YBAOPManager 实例,将这些对象实例增加到
YBAOPManager 实例中(addTarget),最后将 YBAOPManager 实例作为这些方法的第一承接者。剩下的方法分发工作就交给该类了。
晓雯给大家推荐一个iOS技术交流群:691040931群内提供数据结构与算法、底层进阶、swift、逆向、底层面试题整合文档等免费资料!
喜欢晓雯就关注晓雯哟!!晓雯微信:Pingwen20
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » iOS架构:AOP实现局部板块化