• Python: Decorator Pattern


    DuDecorator.py

    # 装饰模式 Decorator  Pattern
    import six  # https://pypi.org/project/six/
    from abc import ABCMeta
    
    
    @six.add_metaclass(ABCMeta)
    class Abstract_Coffee(object):
    
        def get_cost(self):
            pass
    
        def get_ingredients(self):
            pass
    
        def get_tax(self):
            return 0.1 * self.get_cost()
    
    
    class Concrete_Coffee(Abstract_Coffee):
    
        def get_cost(self):
            return 1.00
    
        def get_ingredients(self):
            return '咖啡'
    
    
    @six.add_metaclass(ABCMeta)
    class Abstract_Coffee_Decorator(Abstract_Coffee):
    
        def __init__(self, decorated_coffee):
            self.decorated_coffee = decorated_coffee
    
        def get_cost(self):
            return self.decorated_coffee.get_cost()
    
        def get_ingredients(self):
            return self.decorated_coffee.get_ingredients()
    
    
    class Sugar(Abstract_Coffee_Decorator):
    
        def __init__(self, decorated_coffee):
            Abstract_Coffee_Decorator.__init__(self, decorated_coffee)
    
        def get_cost(self):
            return self.decorated_coffee.get_cost()
    
        def get_ingredients(self):
            return self.decorated_coffee.get_ingredients() + ', 糖果'
    
    
    class Milk(Abstract_Coffee_Decorator):
    
        def __init__(self, decorated_coffee):
            Abstract_Coffee_Decorator.__init__(self, decorated_coffee)
    
        def get_cost(self):
            return self.decorated_coffee.get_cost() + 0.25
    
        def get_ingredients(self):
            return self.decorated_coffee.get_ingredients() + ', 牛奶'
    
    
    class Vanilla(Abstract_Coffee_Decorator):
    
        def __init__(self, decorated_coffee):
            Abstract_Coffee_Decorator.__init__(self, decorated_coffee)
    
        def get_cost(self):
            return self.decorated_coffee.get_cost() + 0.75
    
        def get_ingredients(self):
            return self.decorated_coffee.get_ingredients() + ', 香草'
    

      

    main.py

    调用:

    # 装饰模式 Decorator  Pattern
    myCoffee = DuDecorator.Concrete_Coffee()
    print('Geovin Du买材料: '+myCoffee.get_ingredients()+
       '; geovindu付费用: '+str(myCoffee.get_cost())+'; 涂聚文交营业税 = '+str(myCoffee.get_tax()))
    
    myCoffee = DuDecorator.Milk(myCoffee)
    print('Geovin Du买材料: '+myCoffee.get_ingredients()+
       '; geovindu付费用: '+str(myCoffee.get_cost())+'; 涂聚文交营业税 = '+str(myCoffee.get_tax()))
    
    myCoffee = DuDecorator.Vanilla(myCoffee)
    print('Geovin Du买材料: '+myCoffee.get_ingredients()+
       '; geovindu付费用: '+str(myCoffee.get_cost())+'; 涂聚文交营业税 = '+str(myCoffee.get_tax()))
    
    myCoffee = DuDecorator.Sugar(myCoffee)
    print('Geovin Du买材料: '+myCoffee.get_ingredients()+
       '; geovindu付费用: '+str(myCoffee.get_cost())+'; 涂聚文交营业税 = '+str(myCoffee.get_tax()))
    

      

    输出:

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

      

  • 相关阅读:
    HDU 4734 F(x) 2013 ACM/ICPC 成都网络赛
    VC++中的头文件包含问题
    php调用com组件配置 以openoffice为例
    Android 进程和线程
    文件队列 QueueFile
    Android zip文件压缩解压缩
    Internet Explorer 11(IE11)无法切换第三方输入法
    非递归实现快速排序
    class_create()
    字符设备 register_chrdev_region()、alloc_chrdev_region() 和 register_chrdev()
  • 原文地址:https://www.cnblogs.com/geovindu/p/16815018.html
Copyright © 2020-2023  润新知