AOP 及应用

2018-02-07  本文已影响0人  nix1024

什么是 AOP

Wikipedia 上的 AOP 定义:

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.

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

一些相关术语:

应用

来看一个例子。

现在要记录所有 view controller 展示的日志。

可以想到的方法有:

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

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

利用 Aspect,想要做到在所有的 view controller 显示时记录日志只需要这样:

[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];

这样的好处很明显:

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

参考

上一篇 下一篇

猜你喜欢

热点阅读