You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
以上是维基百科对AOP的基本解释,主要着重于以下几点
将横切关注点与中体业务的进一步分离。
在现有代码的基础上,通过在切入点增加通知的方式实现。
减少与主体业务没有这么密切的代码对主题代码的入侵。
了解过Javascript 高阶函数的同学,可能见到过以下方式对👆题目需求的实现。
// 注意在执行 after的时候,原函数也会被一并执行Function.prototype.after=function(afterfn){let_self=this;returnfunction(){// 执行原方法letresult=_self.apply(this,arguments);// 额外添加 after 函数的执行afterfn.apply(this,arguments);returnresult;}}
A Python decorator is a function that takes another function, extending the behavior of the latter function without explicitly modifying it.
Python装饰器是一种 能拓展另一个函数行为而不明确地修改原函数 的函数。
Decorators provide a way to add both annotations and a meta-programming syntax for class declarations and members.
装饰器(Decorator)是一种与类(class)相关的语法,用来注释或修改类和类方法。
Decorator
本文的主角是
decorator
,字面意思是装饰器
。前端的同学大概都知道,它当前处于stage 2
阶段(草案原文),可以用babel
进行转码后进行使用。使用过
Angular 2
或者Nest.js
(或者Midway.js
)的同学,一定对@Component
、@Inject
、@ViewChild
和@get()
、@post()
、@provide()
不陌生。了解设计模式的同学,大概还记得
修饰器模式
这东西,也许至今也还分不太清楚它和代理模式
的差别。但这次,我们想要追本溯源,从
AOP
、IOC
和descriptor
这些东西说起,认识一下修饰器
这个熟悉的陌生人。"脑壳疼"de问题
在正文开始之前,我们先来一个需求,我们将陆续用不同阶段的思维去实现这个要求。
AOP
以上是维基百科对
AOP
的基本解释,主要着重于以下几点横切关注点
与中体业务的进一步分离。现有代码的基础上
,通过在切入点
增加通知
的方式实现。与主体业务没有这么密切的代码
对主题代码
的入侵。了解过
Javascript 高阶函数
的同学,可能见到过以下方式对👆题目需求的实现。实现过程本身不做过多解释,主要思维是将
要添加的行为
和目标函数(主函数)
包装到了一起,实现了不对原函数(主函数)
入侵的预期,但写法上仍不够优雅
。Spring AOP
在《Spring实战》第四章中提到了
在
Spring
中的AOP
实现,给调用者的实际已经是经过加工
的对象,开发者表面上调用的是Fun
方法,但其实Spring
为你做的是a + b + c --> Fun -->d + e + f
的调用过程。这里的abcdef
都是函数动态的编入点,也就是定义中描述的pointcut
。我们称这种切入方式为
运行时织入
。Spring AOP 的织入点
IOC 与 DI
以上是来自于维基百科对”控制反转“的基本解释。那么,我们如何实现一个控制反转呢,需要了解以下几个关键步骤。
创建 IOC 容器
所谓IOC容器,它的作用是:在应用初始化的时候自动处理对类的依赖,并且将类进行实例化,在需要的时候,使用者可以随时从容器中去除实力进行使用,而不必关心所使用的的实例何时引入、何时被创建。
绑定对象
有了容器,我们需要将”可能会被用到“的对象类,绑定到容器上去。
需要时取出实例
Javascript 中的 decorator
在
tc 39 - decorator
原文中,笔者没有找到总结性的描述语句。这里分别引用Python
和TS
中对decorator
这一特性的描述。Python - decorator
中可以看出,其着重在extending
与without explicitly modifying it
上,基本上沿用了AOP
的设计思想。类的装饰
、类方法的装饰
。从内向外执行
类的装饰
类方法的装饰
Decorator 实现原理
babel 转码看看
我们把上面这一堆东西扔到
babel
中试了一下,得到以下内容。最后最显眼的地方出现了 Class 的 babel 实现,有兴趣的同学可以看这里的全部源码
decorator 只是个语法糖
从前面的转码实验看出 ,
Decorator
语法转为ES 5
后,其实就是使用Object.defineProperty(target, name, description)
进行的。针对前面的例子,其实就是执行了。
descriptor
细心的你已经发现,
decorator
方法的参数 与Object.defineProperty
一模一样。这是因为Javascript
中的decorator
的设定就是后者的拦截器。首先获取到原对象上的
descriptor
对象属性(非额外添加的那些),然后再执行修饰器自身,实现对原descriptor
添加属性。类似于这样日志模块的构建
针对一开始的问题,我们也写一个
Javascript
版本的解决方案吧装饰模式 与 代理模式
看完了上面的内容,我们只要简单地回想一下代理模式的定义,就能轻松梳理出二者的异同点。
实际应用: 图片代理下载、缓存计算等
实际应用:日志模块、模块鉴权等
区别有以下几点:
更详细的例子,推荐参考这篇文章
日常应用
我们日常开发中,还会有一些功能用Decorator能够优美的实现,比如
类型检查
、单位转换
、字段映射
、方法鉴权
、代替部分注释`等。midway 中的实现
midway.js
封装了许多装饰器,部分是用于实现IOC
,如@provide
与@inject
总结
依赖注入
只是IOC
思维实现的一种表现,而装饰器
只是依赖注入
的一种实现手段。参考文章
[1] �我们来聊聊装饰器 -by 讶羽
[2] JS 装饰器实战 -by 芋头
[3] ES6 教程 -by 阮一峰
[4] ES7 Decorator 装饰器 | 淘宝前端团队
[5] 什么是面向切面编程AOP? - 柳树的回答 - 知乎
[6] 什么是面向切面编程AOP? - 夏昊的回答 - 知乎
[7] tc39 - decorator 原文
The text was updated successfully, but these errors were encountered: