这个系统,感觉思路清爽,,相信多练练,多思考,就会熟悉的。。
http://www.jianshu.com/p/2450b785c329
#!/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 SimplePizzaFactory(object): def create_pizza(self, type): pizza = None if type == "cheese": pizza = CheesePizza() elif type == "clam": pizza = ClamPizza() elif type == "veggie": pizza = VeggiePizza() return pizza class PizzaStore(object): def __init__(self, factory): self.factory = factory def order_pizza(self, type): pizza = self.factory.create_pizza(type) pizza.prepare(type) pizza.bake(type) pizza.cut(type) pizza.box(type) return pizza if __name__ == '__main__': store = PizzaStore(SimplePizzaFactory()) pizza = store.order_pizza('cheese') print pizza.name pizza = store.order_pizza('clam') print pizza.name pizza = store.order_pizza('veggie') print pizza.name
时间: 2024-11-05 16:42:25