• 装饰器模式学习


    转自:https://www.runoob.com/design-pattern/decorator-pattern.html

    1.介绍

    • 饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。就增加功能来说,装饰器模式相比生成子类更为灵活
    • 这种模式创建了一个装饰类,用来包装原有的类,并在保持类方法签名完整性的前提下,提供了额外的功能。通过下面的实例来演示装饰器模式的用法。其中,我们将把一个形状装饰上不同的颜色,同时又不改变形状类。

    优点:装饰类和被装饰类可以独立发展,不会相互耦合,装饰模式是继承的一个替代模式,装饰模式可以动态扩展一个实现类的功能。

    缺点:多层装饰比较复杂。

    2.例子

    上述链接中给的例子很好。组件类为图形类,有一个抽象类Shape,两个图形子类Circle和Rect,那么抽象装饰类继承了Shape,并且有一个shape类对象,可以实现set不同的类对象,从而实现对不同shape的装饰;实现了装饰红色Red的具体装饰类,并且都实现了draw方法,这样在draw时具体装饰类可以做出不同的动作。

    3.Python的装饰器例子

    https://www.runoob.com/w3cnote/python-func-decorators.html

    from functools import wraps
     
    def a_new_decorator(a_func):
        @wraps(a_func)
        def wrapTheFunction():
            print("I am doing some boring work before executing a_func()")
            a_func()
            print("I am doing some boring work after executing a_func()")
        return wrapTheFunction
     
    @a_new_decorator
    def a_function_requiring_decoration():
        """Hey yo! Decorate me!"""
        print("I am the function which needs some decoration to "
              "remove my foul smell")
     
    print(a_function_requiring_decoration.__name__)
    # Output: a_function_requiring_decoration

    那么这样在调用a_function_requiring_decoration()前,会先调用a_new_decorator(),wraps函数接受一个函数来进行装饰,并加入了复制函数名称、注释文档、参数列表等等的功能。这可以让我们在装饰器里面访问在装饰之前的函数的属性

    3.1 使用场景

    授权:

    装饰器能有助于检查某个人是否被授权去使用一个web应用的端点(endpoint)。它们被大量使用于Flask和Django web框架中。这里是一个例子来使用基于装饰器的授权:

    from functools import wraps
     
    def requires_auth(f):
        @wraps(f)
        def decorated(*args, **kwargs):
            auth = request.authorization
            if not auth or not check_auth(auth.username, auth.password):
                authenticate()
            return f(*args, **kwargs)
        return decorated

    日志,通过带参数的装饰器,写入到不同的文件中。

  • 相关阅读:
    [iOS 主要框架的总结]
    [无线传感器 网络中的节点定位技术]
    [JAVA 多种方式读取文件]
    [IOS swift对比oc]
    [IOS 静态库]
    [U3D 导出Xcode工程包,用Xcode给U3D脚本传递参数]
    [U3D 添加大地、天空,用第一视角看看自己做的世界]
    [U3D 画起重机,绑脚本和控制它运动的基本操作]
    Flutter 国际化适配
    Error:Unable to resolve dependency for ':app@releaseUnitTest/compileClasspath': Coul完美解决
  • 原文地址:https://www.cnblogs.com/BlueBlueSea/p/15383754.html
Copyright © 2020-2023  润新知