抽象工厂模式(Abstract Factory Pattern):
属于创建型模式,它提供了一种创建对象的最佳方式,以一个超级工厂创建其他工厂。
在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类,每个生成的工厂都能按照工厂模式提供对象。
抽象工厂模式将具体产品的创建延迟到具体工厂的子类中,这样将对象的创建封装起来,可以减少客户端与具体产品类之间的依赖,从而使系统耦合度低,这样更有利于后期的维护和扩展;
与简单工厂模式的区别:
工厂方法模式针对的是一个产品等级结构;而抽象工厂模式则是针对的多个产品等级结构
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
主要解决接口选择的问题。
系统的产品有多于一个的产品族,而系统只消费其中某一族的产品。
在一个产品族里面,定义多个产品。
在一个工厂里聚合多个同类产品。
比如:
消费者(买水果的人),
水果工厂(卖水果的人),
水果工厂旗下的子工厂(品种工厂、价格工厂、包装工厂、称重工厂消费者主要向水果工厂传达“购买需求”的相关信息(水果的种类、包装、重量等…);
水果工厂主要针对消费者的不同“购买需求”将任务分派给子工厂进行执行;
品种工厂负责管理水果的种类,
价格工厂负责管理水果的价格,
称重工厂负责水果的称重,
包装工厂负责水果的包装方式。
class FruitClass: # 1品种工厂 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 # 品种类1 -实现不一样= 橘子类、哈密瓜类、葡萄类 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: # 2称重工厂--实现都一样 def __init__(self, weight): self.weight = float(weight) def print_weight(self): print("该水果的重量为:%.2f千克" % self.weight) class FruitPrice: # 3价格工厂 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 # 价格类3 -价格实现不一样= 橘子价格类、哈密瓜价格类、葡萄价格类 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: # 4包装工厂 --实现都一样 , 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("-*-" * 10) class Consumer: # 消费者类 def __init__(self): print("-*-" * 10) # 输入原始的“购买需求”信息 self.name = input("请输入你要购买的水果名称:0.橘子 1.哈密瓜 2.葡萄\n") self.weight = input("请输入你要购买水果的重量(kg):\n") self.variety = input("如果您购买橘子,我们有2种橘子:0.不买橘子 1.甘橘 2.砂糖橘\n") self.pack = input("请您选择该水果的包装方式:1.散称 2.盒装\n") 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()
class FruitClass: # 1品种工厂 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 # 品种类1 -实现不一样= 橘子类、哈密瓜类、葡萄类 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: # 2称重工厂--实现都一样 def __init__(self, weight): self.weight = float(weight) def print_weight(self): print("该水果的重量为:%.2f千克" % self.weight) class FruitPrice: # 3价格工厂 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 # 价格类3 -价格实现不一样= 橘子价格类、哈密瓜价格类、葡萄价格类 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: # 4包装工厂 --实现都一样 , 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("-*-" * 10) class Consumer: # 消费者类 def __init__(self): print("-*-" * 10) # 输入原始的“购买需求”信息 self.name = input("请输入你要购买的水果名称:0.橘子 1.哈密瓜 2.葡萄\n") self.weight = input("请输入你要购买水果的重量(kg):\n") self.variety = input("如果您购买橘子,我们有2种橘子:0.不买橘子 1.甘橘 2.砂糖橘\n") self.pack = input("请您选择该水果的包装方式:1.散称 2.盒装\n") 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()
class FruitClass: # 1品种工厂 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 # 品种类1 -实现不一样= 橘子类、哈密瓜类、葡萄类 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: # 2称重工厂--实现都一样 def __init__(self, weight): self.weight = float(weight) def print_weight(self): print("该水果的重量为:%.2f千克" % self.weight) class FruitPrice: # 3价格工厂 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 # 价格类3 -价格实现不一样= 橘子价格类、哈密瓜价格类、葡萄价格类 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: # 4包装工厂 --实现都一样 , 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("-*-" * 10) class Consumer: # 消费者类 def __init__(self): print("-*-" * 10) # 输入原始的“购买需求”信息 self.name = input("请输入你要购买的水果名称:0.橘子 1.哈密瓜 2.葡萄\n") self.weight = input("请输入你要购买水果的重量(kg):\n") self.variety = input("如果您购买橘子,我们有2种橘子:0.不买橘子 1.甘橘 2.砂糖橘\n") self.pack = input("请您选择该水果的包装方式:1.散称 2.盒装\n") 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()
class FruitClass: # 1品种工厂 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 # 品种类1 -实现不一样= 橘子类、哈密瓜类、葡萄类 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: # 2称重工厂--实现都一样 def __init__(self, weight): self.weight = float(weight) def print_weight(self): print("该水果的重量为:%.2f千克" % self.weight) class FruitPrice: # 3价格工厂 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 # 价格类3 -价格实现不一样= 橘子价格类、哈密瓜价格类、葡萄价格类 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: # 4包装工厂 --实现都一样 , 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("-*-" * 10) class Consumer: # 消费者类 def __init__(self): print("-*-" * 10) # 输入原始的“购买需求”信息 self.name = input("请输入你要购买的水果名称:0.橘子 1.哈密瓜 2.葡萄\n") self.weight = input("请输入你要购买水果的重量(kg):\n") self.variety = input("如果您购买橘子,我们有2种橘子:0.不买橘子 1.甘橘 2.砂糖橘\n") self.pack = input("请您选择该水果的包装方式:1.散称 2.盒装\n") 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()
【输出】
请输入你要购买的水果名称:0.橘子 1.哈密瓜 2.葡萄
1
请输入你要购买水果的重量(kg):
10
如果您购买橘子,我们有2种橘子:0.不买橘子 1.甘橘 2.砂糖橘
0
请您选择该水果的包装方式:1.散称 2.盒装
1
-*–*–*–*–*–*–*–*–*–*–*–*–*–*–*–*–*–*–*–*-
您购买的水果为:哈密瓜
该水果的重量为:10.00千克
该水果的价格为:24.30元/千克
该水果的打包方式为:散称
需要支付的金额共计为:243.00元
原文地址:http://www.cnblogs.com/BetterDo/p/16926962.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性