PYTHON设计模式,创建型之工厂方法模式

我感觉和上一个差不多,可能不要动最要的地方吧。。。

 

PYTHON设计模式,创建型之工厂方法模式
#!/usr/bin/evn python
#coding:utf8

class Pizza(object):
    def prepare(self, type):
        print 'prepare {type} pizza'.format(type=type)

    def bake(self, type):
        print 'bake {type} pizza'.format(type=type)

    def cut(self, type):
        print 'cut {type} pizza'.format(type=type)

    def box(self, type):
        print 'box {type} pizza'.format(type=type)

class CheesePizza(Pizza):
    def __init__(self):
        self.name = "cheese pizza"

class ClamPizza(Pizza):
    def __init__(self):
        self.name = "clam pizza"

class VeggiePizza(Pizza):
    def __init__(self):
        self.name = "viggie pizza"

class PizzaFactory(object):
    
    def create_pizza(self, type):
        raise NotImplementedError
    
    def order_pizza(self, type):
        pizza = self.create_pizza(type)
        pizza.prepare(type)
        pizza.bake(type)
        pizza.cut(type)
        pizza.box(type)
        return pizza


class PizzaStore(PizzaFactory):
    def create_pizza(self, type):
        pizza = None

        if type == "cheese":
            pizza =  CheesePizza()
        if type == "clam":
            pizza =  ClamPizza()
        if type == "veggie":
            pizza =  VeggiePizza()
        return pizza
        

if __name__ == '__main__':
    store = PizzaStore()
    pizza = store.order_pizza('cheese')
    print pizza.name
    store = PizzaStore()
    pizza = store.order_pizza('clam')
    print pizza.name
    store = PizzaStore()
    pizza = store.order_pizza('veggie')
    print pizza.name
PYTHON设计模式,创建型之工厂方法模式

 

上一篇:python设计模式(十五):责任链模式


下一篇:修改MS SQL忽略大小写