Swift是否支持面向方面的编程? [英] Does Swift support aspect oriented programming?

查看:75
本文介绍了Swift是否支持面向方面的编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名试图学习面向方面编程的iOS开发人员,但Swift是否支持面向方面编程?

I am an iOS developer trying to learn aspect oriented programming but does Swift support aspect oriented programming?

推荐答案

面向方面编程是拦截模式。我们从横切要求开始 - 需要在应用程序的许多部分中发生。然后使用 切入点表达式 ,通过识别应用此要求的所有位置对其进行模块化。这是通过拦截方法调用和编织其他行为来完成的。因此,对于支持AOP的语言,它必须支持拦截模式。

The foundation of Aspect Oriented Programming is the intercept pattern. We start with a crosscutting requirement - something that needs to occur in many parts of the application. And then using a pointcut expression, modularize it, by identifying all of the places this requirement should be applied. This is done by intercepting a method call and weaving in additional behavior. Therefore, for a language to support AOP, it must support the intercept pattern.

现在,根据语言的不同,可以在编译时,运行时或两者中应用方法拦截。 Swift在这方面是一个有趣的案例,因为它支持以下几种方法调度:

Now, depending on the language, method interception can be applied either at compile-time, run-time or both. Swift is an interesting case in this regard, as it supports the following kinds of method dispatch:


  • 静态/ vtable,就像C ++一样(更快:在测试中占用方法调用时间约1.1纳秒或更少)

  • Messaging ,如Objective-C (较慢:在测试中占用方法调用时间约4.9纳秒)。也称为 动态调度 后期绑定

  • Static/vtable, like C++ (faster : in tests accounted for about 1.1 nano-seconds or less of method invocation time)
  • Messaging, like Objective-C (slower : in tests accounted for about 4.9 nano-seconds of method invocation time). Also known as dynamic dispatch or late binding.

如果你扩展NSObject或使用@objc装饰,那么将使用消息传递。否则Swift将恢复为static / vtable方法调用。

If you extend NSObject or use the @objc decoration then messaging will be used. Otherwise Swift will revert to static/vtable method invocations.


  • 使用static / vtable类型的分派,只能进行编译时拦截。在C ++(和Swift)的情况下,这涉及使用在实际编译之前生成新源的预处理器。这是一种有点麻烦的方法,需要更多的努力来开发必要的工具。虽然它确实提供了最佳性能。

  • 使用方法调用的消息传递方式,还可以使用运行时拦截。实际上,Objective-C拦截非常简单,没有正式的AOP框架。它可能是有用的,但原材料非常好,没有人费心去做一个。 Cooca的许多最佳功能都利用了Objective-C的动态调度和拦截方法调用的能力。

摘要:


  • 如果扩展NSObject或使用'@objc'装饰,Swift将支持运行时AOP。这有一些怪癖和限制 - 苹果公司关于在Swift中使用KVO的指南将指出其中大部分内容。

  • 如果你没有扩展Objective-C基础或使用'@objc'装饰,那么只能编译时AOP。目前还没有这样的库提供编译时AOP。此外,编译时AOP的一个缺点是它只适用于您拥有源的类。

NB1:某些语言,例如Java使用静态/ vtable风格的方法调度,仍然支持运行时方法拦截。这是可能的,因为它们依赖于虚拟机,以及 类加载器 ,这是另一个挂钩点。事实上,Java因此而被归类为后期绑定语言。

NB1: Some languages, such as Java uses a static/vtable style of method dispatch and still support runtime method interception. This is possible as they rely on virtual machine, along with a class loader, another hook-in point. In fact Java is still classed as a 'late binding' language because of this.

NB2:技术上可以支持在编译器到机器代码二进制文件的编译时编织,但有一些限制。首先,没有太多工具可以支持这一点,因为实施工作量很大,必须按平台重复。第二是它限制了可用的AOP功能。

NB2: Its technically possible to support provide compile time weaving against compiled-to-machine-code binaries with some limitations. The first is there aren't too many tools to support this because implementation effort is high, and must be repeated per-platform. The second is that it limits the available AOP features.

这篇关于Swift是否支持面向方面的编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆