• Python与设计模式--工厂模式


    快餐点餐系统

    想必大家一定见过类似于麦当劳自助点餐台一类的点餐系统吧。在一个大的触摸显示屏上,有3类可以选择的上餐品:汉堡等主餐、小食、饮料。当我们选择好自己需要的食物,支付完成后,订单就生成了。下面,我们用今天的 主角--工厂模式--来生成这些食物的逻辑主体。

    首先我们主餐的生成(仅以两种汉堡为例)

    # 工厂模式
    
    class Burger():
        name = ""
        price = 0.0
        def getPrice(self):
            return self.price
    
        def setPrice(self,price):
            self.price = price
    
        def getName(self):
            return self.name
    
    class cheeseBurger(Burger):
        def __init__(self):
            self.name = "cheese burger"
            self.price = 10.0
    
    class spicyChickenBurger(Burger):
        def __init__(self):
            self.name = "spicy chicken burger"
            self.price = 15.0
    
    class Snack():
        name = ""
        price = 0.0
        type = "SNACK"
        def getPrice(self):
            return self.price
        def setPrice(self,price):
            self.price = price
        def getName(self):
            return self.name
    
    class chips(Snack):
        def __init__(self):
            self.name = "chips"
            self.price = 6.0
    
    class chickenWings(Snack):
        def __init__(self):
            self.name = "chicken wings"
            self.price = 12.0
    
    
    class Beverage():
        name = ""
        price = 0.0
        type = "BEVERAGE"
        def getPrice(self):
            return self.price
        def sefPrice(self,price):
            self.price = price
        def getName(self):
            return self.name
    
    class coke(Beverage):
        def __init__(self):
            self.name = "coke"
            self.price = 4.0
    
    class milk(Beverage):
        def __init__(self):
            self.name = "milk"
            self.price = 5.0
    
    class foodFactory():
        type = ""
        def createFood(self,foodClass):
            print(self.type,"factory product a instance.")
            foodIns = foodClass()
            return foodIns
    
    class burgerFactory(foodFactory):
        def __init__(self):
            self.type = "BURGER"
    
    class snackFactory(foodFactory):
        def __init__(self):
            self.type = "SNACK"
    
    class beverageFactory(foodFactory):
        def __init__(self):
            self.type = "BEVERAGE"
    
    if __name__ == '__main__':
        burger_factory = burgerFactory()
        snack_factory = snackFactory()
        beverage_factory = beverageFactory()
        cheese_burger = burger_factory.createFood(cheeseBurger)
        print(cheese_burger.getName(),cheese_burger.getPrice())
        chicken_wings = snack_factory.createFood(chickenWings)
        print(chicken_wings.getName(),chicken_wings.getPrice())
        coke_drink = beverage_factory.createFood(coke)
        print(coke_drink.getName(),coke_drink.getPrice())

    工厂模式的定义如下:定义一个用于创建对象的接口,让子类决定实例化哪一个类。

  • 相关阅读:
    Jenkins-在windows上配置自动化部署(Jenkins+Gitblit)
    GIT-Linux(CentOS7)系统部署GitLab服务器
    VMware 安装Linux系统 CentOS
    GIT-windows系统下Gitblit的使用方式
    SQL 显示所有表所占存储空间
    通过JavaScript更新UpdatePanel备忘
    Entity Framework调用表值函数实现全文检索?
    转载:javascript 拖拽排序,简洁示例备忘
    c++ 读写注册表
    MSSQL中建立分区表(转载备忘)
  • 原文地址:https://www.cnblogs.com/LoganChen/p/11580857.html
Copyright © 2020-2023  润新知