python设计模式第八天【装饰器模式】

时间:2022-01-05 13:23:12

1.定义

使用包装的释放扩展类的功能,但是不使用继承

2.使用场景

3.代码实现

#!/usr/bin/env python
#! _*_ coding:UTF-8 _*_

def MyDecorator(before_func, after_func):
    def decorator(main_func):
        def wrapper(request, kargs):

            before_result = before_func(request, kargs)
            if before_result is not None:
                return before_result

            main_result = main_func(request, kargs)
            if main_result is not None:
                return main_result

            after_result = after_func(request, kargs)
            if after_result is not None:
                return after_result

        return wrapper
    return decorator

@MyDecorator(AccountFilter.Before, AccountFilter.After)
def foo(request, kargs):
    pass