iOS AOP编程思想及实践

什么是 AOP

Wikipedia 上的 AOP 定义:git

In computing, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding additional behavior to existing code (an advice) without modifying the code itself, instead separately specifying which code is modified via a “pointcut” specification, such as “log all function calls when the function’s name begins with ‘set’”. This allows behaviors that are not central to the business logic (such as logging) to be added to a program without cluttering the code core to the functionality. AOP forms a basis for aspect-oriented software development.github

AOP(Aspect Oriented Programming,国内多译做「面向切面编程」),它是在不修改原有代码的前提下给程序添加功能的一种技术。这样能够将一些重复而又与主要业务逻辑无关的代码抽离出来,减小了耦合性,增长了代码复用性。编程

一些相关术语:app

  • Cross-cutting concerns:多个业务逻辑共有的非核心的逻辑(好比记录日志)。
  • Advice:具体的须要插入的非核心逻辑代码(写日志逻辑)。
  • Pointcut:核心业务逻辑中的 Advice 插入点。
  • Aspect:Advice 与 Pointcut 合起来被称为 Aspect。

应用

来看一个例子。spa

如今要记录全部 view controller 展现的日志。.net

能够想到的方法有:日志

  • 最原始的方法:在每一个 view controller 的 viewWillAppear: 方法里面增长写日志的代码。
  • 稍微改进的代码:写一个 base view controller 添加记录日志的方法,全部的 view controller 都继承它。
  • 使用 method swizzling 将全部的 viewWillAppear: 方法替换成原有逻辑加上写日志的逻辑——也就是 AOP 的方式。

显然,对比一下这三种方式,毫无疑问最后一种方式不须要修改已有代码,而且后期维护、增长新逻辑更方便。code

iOS 有一个著名的开源库:Aspects,它利用 method swizzling 封装出 API 让你能够在一个类的方法执行以前、以后(Pointcut)执行附加的逻辑(Advice)。orm

利用 Aspect,想要作到在全部的 view controller 显示时记录日志只须要这样:继承

1
2
3
[UIViewController aspect_hookSelector:@selector(viewWillAppear:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo, BOOL animated) {
NSLog(@"View Controller %@ will appear animated: %tu", aspectInfo.instance, animated);
} error:NULL];

这样的好处很明显:

  • 下降了程序的耦合度:你不须要修改业务代码就能够增长功能。
  • 增长了代码的复用性:在全部的 pointcut 增长功能,只须要实现一次。

固然,上面只是一种实现方式而已,重要的是 AOP 的思想。

参考

相关文章
相关标签/搜索