分析Python中设计模式之Decorator装饰器模式的要点
先给出一个四人团对Decorator mode的定义:动态地给一个对象添加一些额外的职责。
再来说说这个模式的好处:认证,权限检查,记日志,检查参数,加锁,等等等等,这些功能和系统业务无关,但又是系统所必须的,说的更明白一点,就是面向方面的编程(AOP)。
在Python中Decorator mode可以按照像其它编程语言如C++, Java等的样子来实现,但是Python在应用装饰概念方面的能力上远不止于此,Python提供了一个语法和一个编程特性来加强这方面的功能。Python提供的语法就是装饰器语法(decorator),如下:
@aoo def foo(): pass def aoo(fn): return fn
装饰模式强调动态地给对象添加额外的功能。 Python内置了很多对装饰器的支持,因此在Python中使用装饰模式是非常容易的,下面是一个典型的例子,给函数增加日志功能:
import functools def log_wrapper(fun): @functools.wraps(fun) def wrapper(*args, **kwargs): print '在函数执行前加日志' ret = fun(*args, **kwargs) print '在函数执行后家日志' return ret return wrapper @log_wrapper def test(): print 'Hello, 世界'
functools.wraps是Python标准库提供的一个特殊的装饰器,用来解决装饰器带来的一些常规问题,如函数名称、doc等的不一致问题。@是Python针对装饰器提供的一个语法糖,上面的@log_wrapper相当于wrap_test = log_rapper(test),用@后,这个步骤由解释器代劳了。
装饰器是Python编程必须掌握的一项技能,在编码过程中经常会用到。
这里只是一个普通的内嵌函数
def foo(x): y = x def foo1 (): a = 1 return a return foo1
而下面boo则是一个闭包
def aoo(a, b): c = a def boo (x): x = b + 1 return x return boo
boo的特殊性在于引用了外部变量b,当aoo返回后,只要返回值(boo)一直存在,则对b的引用就会一直存在。
上面的知识可能需要花些时间消化,如果你觉得已经掌握了这些知识,下面就回归正题,看看这些语言特性是怎样来实现Python中装饰的概念的。
还是让我们先看一个简单的例子,然后逐步深入。这个例子就是加锁,怎样实现加锁的功能?
具体需求是这样的:我有一个对象,实现了某些功能并提供了一些接口供其它模块调用,这个对象是运行在并发的环境中的,因此我需要对接口的调用进行同步,第一版的代码如下:
class Foo(object): def __init__(self, …): self.lock = threading.Lock() def interface1(self, …): self.lock.acquire() try: do something finally: self.lock.release() def interface2(self, …): same as interface1() …
这版代码的问题很明显,那就是每个接口函数都有相同的加锁/解锁代码,重复的代码带来的是更多的键入,更多的阅读,更多的维护,以及更多的修改,最主要的是,程序员本应集中在业务上的的精力被分散了,而且请注意,真正的业务代码在距离函数定义2次缩进处开始,即使你的显示器是宽屏,这也会带来一些阅读上的困难。
你直觉的认为,可以把这些代码收进一个函数中,以达到复用的目的,但是请注意,这些代码不是一个完整同一的代码块,而是在中间嵌入了业务代码。
现在我们用装饰器语法来改进这部分代码,得到第2版代码:
def sync(func): def wrapper(*args, **kv): self = args[0] self.lock.acquire() try: return func(*args, **kv) finally: self.lock.release() return wrapper class Foo(object): def __init__(self, …): self.lock = threading.Lock() @sync def interface1(self, …): do something @sync def interface2(self, …): do something …
一个装饰器函数的第一个参数是所要装饰的那个函数对象,而且装饰器函数必须返回一个函数对象。如sync函数,当其装饰interface1时,参数func的值就是interface1,返回值是wrapper,但类Foo实例的interface1被调用时,实际调用的是wrapper函数,在wrapper函数体中间接调用实际的interface1;当interface2被调用时,也调用的是wrapper函数,不过由于在装饰时func已经变成interface2,所以会间接地调用到实际的interface2函数。
使用装饰器语法的好处:
代码量大大的减少了,更少的代码意味着更少的维护,更少的阅读,更少的键入,好处不一而足(可复用,可维护)
用户基本上将绝大部分精力放在了业务代码上,而且少了加减锁的代码,可读性也提高了
缺点:
业务对象Foo中有一个非业务数据成员lock,很碍眼;
相当程度的耦合,wrapper的第一个参数必须是对象本身,而且被装饰的对象中必须有一个lock对象存在,这给客户对象添加了限制,使用起来不是很舒服。
我们可以更进一步想一想:
lock对象必须要放在Foo中吗?
为每个接口函数都键入@sync还是很烦人的重复性人工工作,如果漏添加一个,还是会造成莫名其妙的运行时错误,为什么不集中处理呢?
为了解决上述的缺点,第3版代码如下:
class DecorateClass(object): def decorate(self): for name, fn in self.iter(): if not self.filter(name, fn): continue self.operate(name, fn) class LockerDecorator(DecorateClass): def __init__(self, obj, lock = threading.RLock()): self.obj = obj self.lock = lock def iter(self): return [(name, getattr(self.obj, name)) for name in dir(self.obj)] def filter(self, name, fn): if not name.startswith('_') and callable(fn): return True else: return False def operate(self, name, fn): def locker(*args, **kv): self.lock.acquire() try: return fn(*args, **kv) finally: self.lock.release() setattr(self.obj, name, locker) class Foo(object): def __init__(self, …): … LockerDecorator(self).decorate() def interface1(self, …): do something def interface2(self, …): do something …
对对象的功能装饰是一个更一般的功能,不仅限于为接口加锁,我用2个类来完成这一功能,DecorateClass是一个基类,只定义了遍历并应用装饰功能的算法代码(template method),LockerDecorator实现了为对象加锁的功能,其中iter是迭代器,定义了怎样遍历对象中的成员(包括数据成员和成员函数),filter是过滤器,定义了符合什么规则的成员才能成为一个接口,operate是执行函数,具体实施了为对象接口加锁的功能。
而在业务类Foo的__init__函数中,只需要在最后添加一行代码:LockerDecorator(self).decorate(),就可以完成为对象加锁的功能。
如果你的对象提供的接口有特殊性,完全可以通过直接改写filter或者继承LockerDecorator并覆盖filter的方式来实现;此外,如果要使用其他的装饰功能,可以写一个继承自DecorateClass的类,并实现iter,filter和operate三个函数即可。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
