• 创建类设计模式-工厂类相关模式


    """
    一、快餐点餐系统
    点餐系统,有三类可选择产品,主餐,小食,饮料。我们可以任意选择,下单,支付完成之后,订单完成。
    工厂模式-生成这些食物的逻辑主题
    
    
    """
    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.anme = "chickenwings"
            self.pric = 12.0
    class Beverage():
        name = ""
        price = 0.0
        type = "BEVERAGE"
    
        def getPrice(self):
            return self.price
    
        def setPrice(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
    """
    以上的Burger,Snack,Beverage,都可以认为是该快餐店的产品,由于只提供了抽象方法,我们把它们叫抽象产品类,
    而cheese burger等6个由抽象产品类衍生出的子类,叫作具体产品类。
    接下来,“工厂”就要出现了。
    
    """
    class FoodFactory():
        type = ""
    
        def createFood(self, foodClass):
            print(self.type, " factory produce 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 bevergeFactory(FoodFactory):
    
        def __init__(self):
            self.type = "BEVERAGE"
    
    
    """
    同样,foodFactory为抽象的工厂类,
    而burgerFactory,snackFactory,beverageFactory为具体的工厂类。
    
    """
    if __name__=="__main__":
        burgerF=burgerFactory()
        snackF=snackFactory()
        beverage_factory = bevergeFactory()
        cheese_burger = beverage_factory.createFood(cheeseBurger)
        print(cheese_burger.getName(), cheese_burger.getPrice())
        chicken_wings = snackF.createFood(chickenwings)
        print(chicken_wings.getName(), chicken_wings.getPrice())
        coke_drink = beverage_factory.createFood(coke)
        print(coke_drink.getName(),coke_drink.getPrice())
    BEVERAGE  factory produce a instance.
    cheese burger 10.0
    SNACK  factory produce a instance.
     0.0
    BEVERAGE  factory produce a instance.
    coke 4.0
  • 相关阅读:
    js 右击
    javascript 中的函数,控制流, 事件委托
    javascript 的 this , js 线程
    javascript 中的var : 隐含变量 全局变量 变量提升
    明日
    ajax循环json 中的 for(var prop in data) 与 hasProperty()
    js继承机制
    callee, caller,toString(),String()
    解决eclipse中jsp下无代码提示问题
    商业模式
  • 原文地址:https://www.cnblogs.com/Xingtxx/p/12879651.html
Copyright © 2020-2023  润新知