装饰者模式:在不改变对象自身的基础上,动态地添加功能代码。
根据描述,装饰者显然比继承等方式更灵活,而且不污染原来的代码,代码逻辑松耦合。
装饰者模式由于松耦合,多用于一开始不确定对象的功能、或者对象功能经常变动的时候。 尤其是在参数检查、参数拦截等场景。
ES6 的装饰器语法规范只是在“提案阶段”,而且不能装饰普通函数或者箭头函数。
下面的代码,addDecorator
可以为指定函数增加装饰器。
其中,装饰器的触发可以在函数运行之前,也可以在函数运行之后。
注意:装饰器需要保存函数的运行结果,并且返回。
const addDecorator = (fn, before, after) => {let isFn = fn => typeof fn === "function";if (!isFn(fn)) {return () => {};}return (...args) => {let result;// 按照顺序执行“装饰函数”isFn(before) && before(...args);// 保存返回函数结果isFn(fn) && (result = fn(...args));isFn(after) && after(...args);// 最后返回结果return result;};};/******************以下是测试代码******************/const beforeHello = (...args) => {console.log(`Before Hello, args are ${args}`);};const hello = (name = "user") => {console.log(`Hello, ${name}`);return name;};const afterHello = (...args) => {console.log(`After Hello, args are ${args}`);};const wrappedHello = addDecorator(hello, beforeHello, afterHello);let result = wrappedHello("godbmw.com");console.log(result);
python 直接提供装饰器的语法支持。用法如下:
# 不带参数def log_without_args(func):def inner(*args, **kw):print("args are %s, %s" % (args, kw))return func(*args, **kw)return inner# 带参数def log_with_args(text):def decorator(func):def wrapper(*args, **kw):print("decorator's arg is %s" % text)print("args are %s, %s" % (args, kw))return func(*args, **kw)return wrapperreturn decorator@log_without_argsdef now1():print('call function now without args')@log_with_args('execute')def now2():print('call function now2 with args')if __name__ == '__main__':now1()now2()
其实 python 中的装饰器的实现,也是通过“闭包”实现的。
以上述代码中的now1
函数为例,装饰器与下列语法等价:
# ....def now1():print('call function now without args')# ...now_without_args = log_without_args(now1) # 返回被装饰后的 now1 函数now_without_args() # 输出与前面代码相同