PYTHON设计模式,创建型之简单工厂模式

这个系统,感觉思路清爽,,相信多练练,多思考,就会熟悉的。。

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

PYTHON设计模式,创建型之简单工厂模式

上一篇:win10 + cuda8.0 + caffe SSD + vs2015 + python3


下一篇:字符集和字符编码(Charset & Encoding)