Python: Decorator Pattern

这是一个关于Python装饰器模式的实现示例,展示了如何使用抽象基类和six库创建咖啡装饰器,用于增加咖啡的价格和配料。通过实例化不同装饰器(如糖、牛奶和香草),逐步修改咖啡的成本和配料列表,最后输出了不同装饰后的咖啡详情。
摘要由CSDN通过智能技术生成

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值