“decorator design” 和 “template design” 在Python中有什么区别?

huangapple go评论57阅读模式
英文:

What is the difference between "decorator design" and "template design" in Python?

问题

我看过一个模板设计的示例,它的说明如下:

适用于存在类似算法结构但某些步骤可能不同的情况。
常用于软件开发中,用于实现具有相似工作流程但细节不同的操作。

然而,我认为这个示例(如下所示)也可以使用装饰器设计

似乎模板设计和装饰器可以实现相同的结果。

我想了解模板设计的应用,但网上的示例或说明对我来说不够清晰。

谁能告诉我模板和装饰器之间的区别?

模板设计

def dots_style(msg):
    msg = msg.capitalize()
    msg = '·' * 10 + msg + '·' * 10
    return msg


def admire_style(msg):
    msg = msg.upper()
    return '!'.join(msg)


def generate_banner(msg, style=dots_style):
    print('-- 横幅开始 --')
    print(style(msg))
    print('-- 横幅结束 --\n\n')


def main():
    msg = '编程快乐'
    [generate_banner(msg, style) for style in (dots_style, admire_style)]

if __name__ == "__main__":
    main()

结果

-- 横幅开始 --
··········编程快乐··········
-- 横幅结束 --


-- 横幅开始 --
编程快乐!编程快乐!编程快乐!编程快乐!编程快乐!编程快乐!编程快乐!编程快乐!编程快乐!编程快乐
-- 横幅结束 --

装饰器设计

from functools import wraps


def generate_banner(style):
    @wraps(style)
    def wrapper(*args, **kwargs):
        print('-- 横幅开始 --')
        print(style(*args, **kwargs))
        print('-- 横幅结束 --\n\n')

    return wrapper


@generate_banner
def dots_style(msg):
    msg = msg.capitalize()
    msg = '·' * 10 + msg + '·' * 10
    return msg


@generate_banner
def admire_style(msg):
    msg = msg.upper()
    return '!'.join(msg)


def main():
    msg = '编程快乐'
    dots_style(msg)
    admire_style(msg)

if __name__ == "__main__":
    main()

结果

-- 横幅开始 --
··········编程快乐··········
-- 横幅结束 --


-- 横幅开始 --
编程快乐!编程快乐!编程快乐!编程快乐!编程快乐!编程快乐!编程快乐!编程快乐!编程快乐!编程快乐
-- 横幅结束 --
英文:

I've seen an example of template design, and its instructions is:

> Applicable to situations where there is a similar algorithm structure but certain steps may vary.
>
>Commonly used in software development to implement operations with similar workflows but differing details.

However, I think this example(as following) can also use "decorator design".

It seems that template design and decorator can achieve the same result.

I want to understand the application of template design, but the examples or instructions on the web didn't make it clear to me.

Who can tell me the difference between template and decorator?

Template Design

def dots_style(msg):
    msg = msg.capitalize()
    msg = '.' * 10 + msg + '.' * 10
    return msg


def admire_style(msg):
    msg = msg.upper()
    return '!'.join(msg)


def generate_banner(msg, style=dots_style):
    print('-- start of banner --')
    print(style(msg))
    print('-- end of banner --\n\n')


def main():
    msg = 'happy coding'
    [generate_banner(msg, style) for style in (dots_style, admire_style)]

if __name__ == "__main__":
    main()

Result

-- start of banner --
..........Happy coding..........
-- end of banner --


-- start of banner --
H!A!P!P!Y! !C!O!D!I!N!G
-- end of banner --

Decorator Design

from functools import wraps


def generate_banner(style):
    @wraps(style)
    def wrapper(*arg, **kwargs):
        print('-- start of banner --')
        print(style(*arg, **kwargs))
        print('-- end of banner --\n\n')

    return wrapper


@generate_banner
def dots_style(msg):
    msg = msg.capitalize()
    msg = '.' * 10 + msg + '.' * 10
    return msg


@generate_banner
def admire_style(msg):
    msg = msg.upper()
    return '!'.join(msg)


def main():
    msg = 'happy coding'
    dots_style(msg)
    admire_style(msg)

if __name__ == "__main__":
    main()

Results

-- start of banner --
..........Happy coding..........
-- end of banner --


-- start of banner --
H!A!P!P!Y! !C!O!D!I!N!G
-- end of banner --

答案1

得分: 1

装饰者设计模式可以动态地为对象/函数添加功能,而模板设计模式定义了具有可在子类中定制步骤的算法骨架。

在你的例子中
模板设计方法允许您定义不同的样式以生成横幅,同时保持整体结构不变。您可以通过将其作为参数传递给generate_banner函数来选择使用哪种样式。另一方面,装饰者设计方法允许您通过使用generate_banner装饰器将横幅功能动态地添加到不同的样式中。

英文:

The decorator design pattern adds functionality to an object / function dynamically, while the template design pattern defines the skeleton of an algorithm with customizable steps in subclasses.

In your example
The template design approach allows you to define different styles for generating banners while keeping the overall structure intact. You can choose which style to use by passing it as a parameter to the generate_banner function. On the other hand, the decorator design approach allows you to dynamically add the banner functionality to different styles by using the generate_banner decorator.

huangapple
  • 本文由 发表于 2023年6月15日 19:26:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481984.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定