Implementing the decorator pattern in Python

后端 未结 7 1583
梦谈多话
梦谈多话 2020-12-02 09:59

I want to implement the decorator pattern in Python, and I wondered if there is a way to write a decorator that just implements the function it wants to modify, without writ

7条回答
  •  再見小時候
    2020-12-02 10:42

    The UML diagram in the linked Wikipedia article is wrong and so is your code.

    If you follow the "decorator pattern", the decorator class is derived from the base decorated class. (In the UML diagram an inheritance arrow from the WindowDecorator to Window is missing).

    with

    class foo_decorator(foo):
    

    you don't need to implement undecorated methods.

    BTW: In strong typed languages there is one more reason, why the decorator must be derived from the decorated class: Otherwise you wouldnt be able to chain decorators.

提交回复
热议问题