我感觉和上一个差不多,可能不要动最要的地方吧。。。
#!/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