• 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())

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

  • 相关阅读:
    c++内存管理5-虚拟内存4区结构图
    C++内存管理5-处理new分配内存失败情况(转)
    C++内存管理4-Windows编程中的堆管理(转)
    C++内存管理3-探讨C++内存和回收
    C++内存管理2-内存泄漏
    VS2015远程调试
    C++内存管理1-64位系统运行32位软件会占用更多的内存吗?
    ffmpeg安装步骤
    golang字符串拼接
    如何严格设置php中session过期时间
  • 原文地址:https://www.cnblogs.com/LoganChen/p/11580857.html
走看看 - 开发者的网上家园