溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python設(shè)計模式之抽象工廠模式怎么實現(xiàn)

發(fā)布時間:2022-03-29 16:17:53 來源:億速云 閱讀:129 作者:iii 欄目:移動開發(fā)

本篇內(nèi)容主要講解“python設(shè)計模式之抽象工廠模式怎么實現(xiàn)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“python設(shè)計模式之抽象工廠模式怎么實現(xiàn)”吧!

實現(xiàn)的代碼如下:

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):
        # 任務(wù)的分配,品種、重量、價格、包裝方式
        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):
        # 返回相關(guān)的購買信息
        return self.name, self.weight, self.variety, self.pack


if __name__ == "__main__":
    # 創(chuàng)建顧客
    buyer = Consumer()
    # 拿到顧客的購買信息
    buy_info = buyer.request()
    # 使用水果工廠,傳達指令至旗下的子工廠并執(zhí)行購買操作
    buy_res = FruitFactory(int(buy_info[0]), int(buy_info[1]), int(buy_info[2]), int(buy_info[3]))
    # 購買信息的展示
    buy_res.show_info()

到此,相信大家對“python設(shè)計模式之抽象工廠模式怎么實現(xiàn)”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI