装饰器模式
# 注意
装饰器模式不等于装饰器,本文中可能概念存在混淆,需要进一步整理概念,为避免误导他人,请自己学习相关知识!
# 作用
Adds behaviour to object without affecting its class. Decorator 模式用于动态地向对象添加新特性,而不更改其原来的代码实现。它与继承不同,新特性只绑定到特定对象,而不会添加到整个子类。
# 优缺点
# 优点
- 你无需创建新子类即可扩展对象的行为。
- 你可以在运行时添加或删除对象的功能。
- 你可以用多个装饰封装对象来组合几种行为。
- 单一职责原则。 你可以将实现了许多不同行为的一个大类拆分为多个较小的类。
# 缺点
- 在封装器栈中删除特定封装器比较困难。
- 实现行为不受装饰栈顺序影响的装饰比较困难。
- 各层的初始化配置代码看上去可能会很糟糕。
# 代码
# 入门
- 函数运行时间记录
import time
def time_it(func):
def wrap_func(*args, **kwargs):
start_time = time.time()
ret_result = func(*args, **kwargs)
end_time = time.time()
print('The function **{0}** takes {1} time.'.format(func.__name__, end_time - start_time))
return ret_result
return wrap_func
@time_it
def foo():
pass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 进阶
- 装饰器带参数
def deco_with_args(name):
def decorator(func):
def wrapper(*args,**kwargs):
if name=='foo':
print('hello')
else:
print('Bonjour!')
ret = func(*args,**kwargs)
return ret
return wrapper
return decorator
@deco_with_args('foo')
def foo():
print('this is foo')
@deco_with_args('bar')
def bar():
print('this is bar')
foo()
print('---------------')
bar()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 类装饰器
- 不带参数
基于类装饰器的实现,必须实现
__call__
和__init__
两个内置函数。 其中__init__
用于接收被装饰函数,__call__
实现装饰逻辑。
编辑 (opens new window)
上次更新: 2024-02-28, 07:52:01