乐趣区

关于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):
    # 工作的调配,种类、[期货](https://www.gendan5.com/futures.html) 分量、价格、包装形式
    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. 葡萄 \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()
退出移动版