在上一篇 python设计模式:抽象工厂模式 中,我们尝试用抽象工厂模式规范化了 Pizza 原材料的供应以及 Pizza 的创建。但是我们忽略了一个问题,那就是每种 Pizza 的烘焙时间依赖于生面团的厚度和使用的配料,它们所需的时间是不一样的。那这时我们改如何处理呢?
- 问题:
Pizza 的制作流程包括:准备(擀面皮、加佐料),然后烘烤、切片、装盒。这些有特定的顺序,不能错乱。
为了保证 生产 Pizza 的步骤不会出错,我们打算指派一个创建者,创建者用于控制 Pizza 的制作流程。
首先我们定义一个 Pizza
- class Pizza:
- def __init__(self, name):
- self.name = name
- self.dough = None
- self.sauce = None
- self.toppings = []
- def prepare_dough(self, dough):
- self.dough = dough
- print(self.dough)
- print('preparing the {} dough of your {}...'.format(self.dough, self))
- time.sleep(STEP_DELAY)
- print('Done with the {} dough'.format(self.dough))
- def __str__(self):
- return self.name
然后我们抽象出一个创建者:
- class PizzaBuilder(object):
- name = None
- def __init__(self):
- self.progress = PIZZA_PROGRESS
- self.baking_time = 5
- def prepare_dough(self):
- raise NotImplementedError()
- def add_sauce(self):
- raise NotImplementedError()
- def add_topping(self):
- raise NotImplementedError()
- def bake(self):
- raise NotImplementedError()
- def cut(self):
- raise NotImplementedError()
- def box(self):
- raise NotImplementedError()
- @property
- def pizza(self):
- return Pizza(self.name)
- class NYStyleCheeseBuilder(PizzaBuilder):
- name = 'NY Style Sauce and Cheese Pizza'
- def prepare_dough(self):
- self.progress = PIZZA_PROGRESS[0]
- self.pizza.prepare_dough('thin')
- def add_sauce(self):
- print('adding the tomato sauce to your pizza..')
- self.pizza.sauce = 'tomato'
- time.sleep(STEP_DELAY)
- print('done with the tomato sauce')
- def add_topping(self):
- print('adding the topping (grated reggiano cheese) to your pizza')
- self.pizza.toppings.append(["Grated", "Reggiano", "Cheese"])
- time.sleep(STEP_DELAY)
- print('done with the topping (grated reggiano cheese)')
- def bake(self):
- self.progress = PIZZA_PROGRESS[1]
- print('baking your pizza for {} seconds'.format(self.baking_time))
- time.sleep(self.baking_time)
- def cut(self):
- self.progress = PIZZA_PROGRESS[2]
- print("Cutting the pizza into diagonal slices")
- def box(self):
- self.progress = PIZZA_PROGRESS[3]
- print("Place pizza in official PizzaStore box")
- class Waiter:
- # 指挥者
- def __init__(self):
- self.builder = None
- def construct_pizza(self, builder):
- self.builder = builder
- # 一旦我们有了一个 pizza,需要做一些准备(擀面皮、加佐料),然后烘烤、切片、装盒
- [step() for step in (builder.prepare_dough, builder.add_sauce,
- builder.add_topping, builder.bake,
- builder.cut, builder.box)]
- @property
- def pizza(self):
- return self.builder.pizza
完整代码参考: python-design-patter-builder
从这个例子我可以看出,建造者模式包含如下角色:
造者模式(Builder Pattern):将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。也可以说,每个产品的建造会遵循同样的流程,不过流程内的每一个步骤都不尽相同。
建造者模式又可以称为生成器模式。
看上边这个例子,你可能会疑惑,为什么明明可以使用工厂方法模式可以解决的问题,要换成建造者模式呢?
通过代码可以看出,建造者模式和工厂方法模式最大的区别是,建造者模式多了一个指挥者的角色。
。
- 建造者负责创建复杂对象的各个组成部分。而指挥者使用一个建造者实例控制建造的过程
,因为对象的创建过程更为复杂,因此将对象的创建过程独立出来组成一个新的类——指挥者类。
- 与工厂模式相比,建造者模式一般用来创建更为复杂的对象
建造者模式通常用于补充工厂模式的不足,尤其是在如下场景中:
最后,感谢女朋友支持。
欢迎关注(April_Louisa) | 请我喝芬达 |
---|---|
来源: http://www.tuicool.com/articles/VBf2MrV