您好,登錄后才能下訂單哦!
本篇內容主要講解“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設計模式之抽象工廠模式怎么實現”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。