python设计模式之抽象工厂模式怎么实现
本篇内容主要讲解“python设计模式之抽象工厂模式怎么实现”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“python设计模式之抽象工厂模式怎么实现”吧!
创新互联建站是一家集网站建设,藤县企业网站建设,藤县品牌网站建设,网站定制,藤县网站建设报价,网络营销,网络优化,藤县网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
实现的代码如下:
class FruitClass: # 品种工厂 def get_name(self, name_index): if name_index == 0: name_object = OrangeClass() elif name_index == 1: name_object = Hami_MelonClass() elif name_index == 2: name_object = GrapeClass() else: name_object = None return name_object class OrangeClass: # 橘子类 def __init__(self): self.name = "橘子" def print_name(self): print("您购买的水果为:%s" % self.name) class Hami_MelonClass: # 哈密瓜类 def __init__(self): self.name = "哈密瓜" def print_name(self): print("您购买的水果为:%s" % self.name) class GrapeClass: # 葡萄类 def __init__(self): self.name = "葡萄" def print_name(self): print("您购买的水果为:%s" % self.name) class FruitWeight: # 称重工厂 def __init__(self, weight): self.weight = float(weight) def print_weight(self): print("该水果的重量为:%.2f千克" % self.weight) class FruitPrice: # 价格工厂 def get_price(self, name_index, variety): if name_index == 0: price_object = OrangePrice(variety) elif name_index == 1: price_object = Hami_MelonPrice() elif name_index == 2: price_object = GrapePrice() else: price_object = None return price_object class OrangePrice: # 橘子价格类 def __init__(self, variety): self.variety = variety if self.variety == 1: self.price = 8.5 else: self.price = 11.0 def print_price(self): print("该水果的单价为:%.2f元/千克" % self.price) class Hami_MelonPrice: # 哈密瓜价格类 def __init__(self): self.price = 24.3 def print_price(self): print("该水果的价格为:%.2f元/千克" % self.price) class GrapePrice: # 葡萄价格类 def __init__(self): self.price = 16.2 def print_price(self): print("该水果的价格为:%.2f元/千克" % self.price) return self.price class FruitPack: # 包装工厂 def __init__(self, pack): if pack == 1: self.pack = "散称" else: self.pack = "盒装" def print_pack(self): print("该水果的打包方式为:%s" % self.pack) class FruitFactory: def __init__(self, name_index, weight, variety, pack): # 任务的分配,品种、重量、价格、包装方式 self.name_object = FruitClass().get_name(name_index) self.weight_object = FruitWeight(weight) self.price_object = FruitPrice().get_price(name_index, variety) self.pack_object = FruitPack(pack) def print_purchase(self): # 计算购买的金额 money = self.price_object.price * self.weight_object.weight print("需要支付的金额共计为:%.2f元" % money) def show_info(self): # 展示最终的购买信息 self.name_object.print_name() self.weight_object.print_weight() self.price_object.print_price() self.pack_object.print_pack() self.print_purchase() print("-*-" * 20) class Consumer: # 消费者类 def __init__(self): print("-*-" * 20) # 输入原始的“购买需求”信息 self.name = input("请输入你要购买的水果名称:0.橘子 1.哈密瓜 2.葡萄 ") self.weight = input("请输入你要购买水果的重量(kg): ") self.variety = input("如果您购买橘子,我们有2种橘子:0.不买橘子 1.甘橘 2.砂糖橘 ") self.pack = input("请您选择该水果的包装方式:1.散称 2.盒装 ") print("-*-" * 20) def request(self): # 返回相关的购买信息 return self.name, self.weight, self.variety, self.pack if __name__ == "__main__": # 创建顾客 buyer = Consumer() # 拿到顾客的购买信息 buy_info = buyer.request() # 使用水果工厂,传达指令至旗下的子工厂并执行购买操作 buy_res = FruitFactory(int(buy_info[0]), int(buy_info[1]), int(buy_info[2]), int(buy_info[3])) # 购买信息的展示 buy_res.show_info()
到此,相信大家对“python设计模式之抽象工厂模式怎么实现”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
网页名称:python设计模式之抽象工厂模式怎么实现
标题路径:http://scjbc.cn/article/pcpsds.html