AOP 及应用

栏目: 编程工具 · 发布时间: 6年前

内容简介:AOP 及应用

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

一些相关术语:

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

应用

来看一个例子。

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

可以想到的方法有:

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

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

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

这样的好处很明显:

  • 降低了程序的耦合度:你不需要修改业务代码就可以增加功能。
  • 增加了代码的复用性:在所有的 pointcut 增加功能,只需要实现一次。

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

参考


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Two Scoops of Django

Two Scoops of Django

Daniel Greenfeld、Audrey M. Roy / CreateSpace Independent Publishing Platform / 2013-4-16 / USD 29.95

Two Scoops of Django: Best Practices For Django 1.5 is chock-full of material that will help you with your Django projects. We'll introduce you to various tips, tricks, patterns, code snippets, and......一起来看看 《Two Scoops of Django》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具