• Python: Decorator Pattern


    DuDecorator.py

    1. # 装饰模式 Decorator Pattern
    2. import six # https://pypi.org/project/six/
    3. from abc import ABCMeta
    4. @six.add_metaclass(ABCMeta)
    5. class Abstract_Coffee(object):
    6. def get_cost(self):
    7. pass
    8. def get_ingredients(self):
    9. pass
    10. def get_tax(self):
    11. return 0.1 * self.get_cost()
    12. class Concrete_Coffee(Abstract_Coffee):
    13. def get_cost(self):
    14. return 1.00
    15. def get_ingredients(self):
    16. return '咖啡'
    17. @six.add_metaclass(ABCMeta)
    18. class Abstract_Coffee_Decorator(Abstract_Coffee):
    19. def __init__(self, decorated_coffee):
    20. self.decorated_coffee = decorated_coffee
    21. def get_cost(self):
    22. return self.decorated_coffee.get_cost()
    23. def get_ingredients(self):
    24. return self.decorated_coffee.get_ingredients()
    25. class Sugar(Abstract_Coffee_Decorator):
    26. def __init__(self, decorated_coffee):
    27. Abstract_Coffee_Decorator.__init__(self, decorated_coffee)
    28. def get_cost(self):
    29. return self.decorated_coffee.get_cost()
    30. def get_ingredients(self):
    31. return self.decorated_coffee.get_ingredients() + ', 糖果'
    32. class Milk(Abstract_Coffee_Decorator):
    33. def __init__(self, decorated_coffee):
    34. Abstract_Coffee_Decorator.__init__(self, decorated_coffee)
    35. def get_cost(self):
    36. return self.decorated_coffee.get_cost() + 0.25
    37. def get_ingredients(self):
    38. return self.decorated_coffee.get_ingredients() + ', 牛奶'
    39. class Vanilla(Abstract_Coffee_Decorator):
    40. def __init__(self, decorated_coffee):
    41. Abstract_Coffee_Decorator.__init__(self, decorated_coffee)
    42. def get_cost(self):
    43. return self.decorated_coffee.get_cost() + 0.75
    44. def get_ingredients(self):
    45. return self.decorated_coffee.get_ingredients() + ', 香草'

    main.py

    调用:

    1. # 装饰模式 Decorator Pattern
    2. myCoffee = DuDecorator.Concrete_Coffee()
    3. print('Geovin Du买材料: '+myCoffee.get_ingredients()+
    4. '; geovindu付费用: '+str(myCoffee.get_cost())+'; 涂聚文交营业税 = '+str(myCoffee.get_tax()))
    5. myCoffee = DuDecorator.Milk(myCoffee)
    6. print('Geovin Du买材料: '+myCoffee.get_ingredients()+
    7. '; geovindu付费用: '+str(myCoffee.get_cost())+'; 涂聚文交营业税 = '+str(myCoffee.get_tax()))
    8. myCoffee = DuDecorator.Vanilla(myCoffee)
    9. print('Geovin Du买材料: '+myCoffee.get_ingredients()+
    10. '; geovindu付费用: '+str(myCoffee.get_cost())+'; 涂聚文交营业税 = '+str(myCoffee.get_tax()))
    11. myCoffee = DuDecorator.Sugar(myCoffee)
    12. print('Geovin Du买材料: '+myCoffee.get_ingredients()+
    13. '; geovindu付费用: '+str(myCoffee.get_cost())+'; 涂聚文交营业税 = '+str(myCoffee.get_tax()))

    输出:

    1. Geovin Du买材料: 咖啡; geovindu付费用: 1.0; 涂聚文交营业税 = 0.1
    2. Geovin Du买材料: 咖啡, 牛奶; geovindu付费用: 1.25; 涂聚文交营业税 = 0.125
    3. Geovin Du买材料: 咖啡, 牛奶, 香草; geovindu付费用: 2.0; 涂聚文交营业税 = 0.2
    4. Geovin Du买材料: 咖啡, 牛奶, 香草, 糖果; geovindu付费用: 2.0; 涂聚文交营业税 = 0.2

     

  • 相关阅读:
    2023-2028年中国硫氰酸胍(GTC)市场研究及前景投资预测报告
    jenkins配置
    抖音矩阵系统,抖音矩阵系统,抖音矩阵系统。
    408 | 【计网】第一章 计算机网络体系结构 回顾
    Java开发:注解Annotation
    【图像版权】论文阅读:CRMW 图像隐写术+压缩算法
    通过此文让你全面了解Thread线程的基本操作
    musescore 4.0编译
    SD、SDIO和MMC接口基础和规范介绍
    从0到1图文教你如何将spring boot项目部署到minikube中去
  • 原文地址:https://blog.csdn.net/geovindu/article/details/127459199