Python版

https://github.com/faif/python-patterns/blob/master/structural/decorator.py

#!/usr/bin/env python
# -*- coding: utf-8 -*- """
*What is this pattern about?
The Decorator pattern is used to dynamically add a new feature to an
object without changing its implementation. It differs from
inheritance because the new feature is added only to that particular
object, not to the entire subclass. *What does this example do?
This example shows a way to add formatting options (boldface and
italic) to a text by appending the corresponding tags (<b> and
<i>). Also, we can see that decorators can be applied one after the other,
since the original text is passed to the bold wrapper, which in turn
is passed to the italic wrapper. *Where is the pattern used practically?
The Grok framework uses decorators to add functionalities to methods,
like permissions or subscription to an event:
http://grok.zope.org/doc/current/reference/decorators.html *References:
https://sourcemaking.com/design_patterns/decorator *TL;DR80
Adds behaviour to object without affecting its class.
""" from __future__ import print_function class TextTag(object):
"""Represents a base text tag"""
def __init__(self, text):
self._text = text def render(self):
return self._text class BoldWrapper(TextTag):
"""Wraps a tag in <b>"""
def __init__(self, wrapped):
self._wrapped = wrapped def render(self):
return "<b>{}</b>".format(self._wrapped.render()) class ItalicWrapper(TextTag):
"""Wraps a tag in <i>"""
def __init__(self, wrapped):
self._wrapped = wrapped def render(self):
return "<i>{}</i>".format(self._wrapped.render()) if __name__ == '__main__':
simple_hello = TextTag("hello, world!")
special_hello = ItalicWrapper(BoldWrapper(simple_hello))
print("before:", simple_hello.render())
print("after:", special_hello.render()) ### OUTPUT ###
# before: hello, world!
# after: <i><b>hello, world!</b></i>

Python转载版

【编程思想】【设计模式】【结构模式Structural】装饰模式decorator的更多相关文章

  1. 九、结构模式之装饰(Decorator)模式

    装饰模式又叫包装模式,装饰模式以客户端透明的方式扩展对象的功能,是继承关系的一个替代方案.装饰模式可以在不使用创造更多的子类的情况下,将对象的功能加以扩展. 装饰模式结构图如下: 其包含的角色就分为: ...

  2. 【java设计模式】【结构模式Structural Pattern】装饰模式Decorator Pattern

    public class Client { public static void main(String[] args) { Component component=new ConcreteCompo ...

  3. 【编程思想】【设计模式】【结构模式Structural】代理模式Proxy

    Python版 https://github.com/faif/python-patterns/blob/master/structural/proxy.py #!/usr/bin/env pytho ...

  4. 【编程思想】【设计模式】【结构模式Structural】MVC

    Python版 https://github.com/faif/python-patterns/blob/master/structural/mvc.py #!/usr/bin/env python ...

  5. 【编程思想】【设计模式】【结构模式Structural】front_controller

    Python版 https://github.com/faif/python-patterns/blob/master/structural/front_controller.py #!/usr/bi ...

  6. 【编程思想】【设计模式】【结构模式Structural】享元模式flyweight

    Python版 https://github.com/faif/python-patterns/blob/master/structural/flyweight.py #!/usr/bin/env p ...

  7. 【编程思想】【设计模式】【结构模式Structural】门面模式/外观模式Facade

    Python版 https://github.com/faif/python-patterns/blob/master/structural/facade.py #!/usr/bin/env pyth ...

  8. 【编程思想】【设计模式】【结构模式Structural】组合模式composite

    Python版 https://github.com/faif/python-patterns/blob/master/structural/composite.py #!/usr/bin/env p ...

  9. 【编程思想】【设计模式】【结构模式Structural】桥梁模式/桥接模式bridge

    Python版 https://github.com/faif/python-patterns/blob/master/structural/bridge.py #!/usr/bin/env pyth ...

随机推荐

  1. Webshell 一句话木马

    Webshell介绍 什么是 WebShell webshell就是以asp.php.jsp或者cgj等网页文件形式存在的一种命令执行环境,也可以将其称做为一种网页后门 由于 webshell其大多是 ...

  2. Spring @Component生成bean名称的规则(当类的名字是以两个或以上的大写字母开头的话,bean的名字会与类名保持一致

    正确注入方式: @Autowired private TFeeMapper TFeeMapper; 错误注入方式 @Autowired private TFeeMapper tFeeMapper; 这 ...

  3. jQuery css()选择器使用说明

    css选择器只是jquery中的一个功能罢了,下面我来给各位朋友详细介绍jQuery css()选择器使用方法与说明详解,有需要了解学习的同学可参考. CSS操作有一个重要的方法:CSS() CSS( ...

  4. 13-Semi-supervised Learning

    半监督学习(semi-supervised learning) 1.introduction 2.Semi-supervised Learning for Generative Model 3.Low ...

  5. puts()_C语言

    puts()函数用来向标准输出设备, scanf函数是格式输入函数,即按用户指定的格式从键盘上把数据输入到指定的变量之中. puts就是输出字符串啊.int puts(    const char* ...

  6. [loj574]黄金矿工

    记$dep_{x}$为1到$x$的边权和,当$x$上的矿工挖了$y$上的黄金时($y$在$x$子树内),显然$\sum_{e}c_{e}=dep_{y}-dep_{x}$ 由此,对于$u$上权值为$v ...

  7. [loj3504]支配

    令$S_{x}$表示$x$支配的节点集合,可以暴力枚举$x$并求出$S_{x}$(删去$x$后从1开始dfs,复杂度为$o(nm)$),进而反过来即可求出受支配集$D_{x}$ 结论1:若$z\in ...

  8. Linux学习 - 树莓派4b的U-Boot的初识

    Linux学习 - 树莓派4b的U-Boot的初识 初识U-Boot 学习书籍:<[正点原子]I.MX6U嵌入式Linux驱动开发指南V1.5.1> 章节:第三十章 学习内容: 书中介绍u ...

  9. 『学了就忘』Linux用户管理 — 52、用户组管理相关命令

    目录 1.添加用户组 2.删除用户组 3.把用户添加进组或从组中删除 4.有效组(了解) 1.添加用户组 添加用户组的命令是groupadd. 命令格式如下: [root@localhost ~]# ...

  10. 联盛德 HLK-W806 (二): Win10下的开发环境配置, 编译和烧录说明

    目录 联盛德 HLK-W806 (一): Ubuntu20.04下的开发环境配置, 编译和烧录说明 联盛德 HLK-W806 (二): Win10下的开发环境配置, 编译和烧录说明 联盛德 HLK-W ...