首页 > 编程语言 >Python: Decorator Pattern

Python: Decorator Pattern

时间:2022-10-21 23:03:34浏览次数:39  
标签:coffee get Python Pattern self def decorated myCoffee Decorator

 

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

  

 

标签:coffee,get,Python,Pattern,self,def,decorated,myCoffee,Decorator
From: https://www.cnblogs.com/geovindu/p/16815018.html

相关文章

  • Selenium+Python系列(三) - 常见浏览器操作
    写在前面上篇文章为大家分享了自动化测试中,常见元素定位的操作。今天再次读文章,居然忘记了大家特别喜欢的CSS和Xpath定位操作分享,这怎么能行呢?马上安利,感兴趣的同学去参......
  • Python RabbitMQ pika的安装及work消息模型的使用
    RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件,RabbitMQ服务器是用Erlang语言编写的,而集群和故障转移是构建在开放电信平台框架上的。所有主要的编程语言均有......
  • Python pandas 通过时间计算统计每月数据记录数
    在Python中进行数据统计时,有些数据我们可能需要统计每月或指时间范围的数据记录数,本文主要介绍Pythonpandas中通过时间计算统计每月数据记录数的方法,以及相关的示例代码。......
  • Python: Composite Pattern
    DuComposite.py#组合模式CompositePatternfrom__future__importannotationsfromabcimportABC,abstractmethodfromtypingimportListclassComponent(......
  • 3.Python 注释和函数使用
    注释三总:单行注释直接#+内容多行注释三个单引号括起来的内容指定编码注释可以指定文件的中文编码例:#作者:咸瑜#代码时间:2022/10/1715:57''''多行注......
  • 备战python蓝桥杯等级考试系列1.输出hello world
    ​ ​编辑​编辑获取​python从初级到高级学习资料......
  • Python学习:列表和字典练习题
    找出列表list中大于100的值,给字典dic的k1键,小于等于100的值,给字典dic的k2键'''提示:创建字典的两种方式ex:'''v1=[2,3,4,5,]v2=88dic1={'k1':v1,'k2':v2,}......
  • python系列归并排序图文详解
    ​ 算法原理:      改归并排序将序列折半分成两个子序列,然后继续拆分,直到每个序列只有一个数据时,再将各个子序列排序后合并叠加。直到所有子序列都合并,排序完成。......
  • 使用vscode创建我的第一个python程序
    软件配置:首先打开vscode,点击红色方框按钮:  在搜索栏输入【python】,并下载python插件:   如果觉得软件是英文不好理解可以下一个中文插件:   创建 第......
  • Python教程Day06-字符串
    一、认识字符串字符串是Python中最常用的数据类型。我们一般使用引号来创建字符串。创建字符串很简单,只要为变量分配一个值即可。a='helloworld'b="abcdefg"print(......