首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Python: Decorator Pattern

Python: Decorator Pattern

作者头像
geovindu
发布2026-06-19 09:55:56
发布2026-06-19 09:55:56
100
举报

DuDecorator.py

代码语言:javascript
复制
# 装饰模式 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

调用:

代码语言:javascript
复制
# 装饰模式 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()))

输出:

代码语言:javascript
复制
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
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2026-06-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档