iOS AOP编程思想及实践

本文详细介绍了面向切面编程(AOP)的概念及其在iOS开发中的应用。通过实例展示了如何利用AOP降低程序耦合度并提高代码复用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

什么是 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 显示时记录日志只需要这样:

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 的思想。

参考

转载于:https://www.cnblogs.com/junhuawang/p/7602465.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值