• 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

     

  • 相关阅读:
    基于速度伺服波浪补偿器的模糊自适应算法设计
    app小程序手机端Python爬虫实战09通过U2实现移动设备九宫格解锁
    openresty定时任务
    音视频常见问题(六):视频黑边或放大
    【ES6.0】- 扩展运算符(...)
    Java多线程
    深入探索OCR技术:前沿算法与工业级部署方案揭秘
    gpf_maskf分配掩码描述
    Java StringBuffer类与StringBuilder类总结(方法清单/面试题)短小精悍
    PyTorch中的CUDA操作
  • 原文地址:https://blog.csdn.net/geovindu/article/details/127459199