You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 第二章-构造器模式.md
+39-40Lines changed: 39 additions & 40 deletions
Original file line number
Diff line number
Diff line change
@@ -56,7 +56,7 @@ to return the final object when it needs it [GOF95, page 113], [j.mp/builderpat]
56
56
57
57
The new computer analogy might help to distinguish between a Builder pattern and a Factory pattern. Assume that you want to buy a new computer. If you decide to buy a specific preconfigured computer model, for example, the latest Apple 1.4 GHz Mac mini, you use the Factory pattern. All the hardware specifications are already predefined by the manufacturer, who knows what to do without consulting you. The manufacturer typically receives just a single instruction. Code-wise, this would look like the following (*apple-factory.py*):
58
58
59
-
新计算机的分析有助于区别构造器模式和工厂模式。假设你需要买一台新电脑。如果你决定买预先配置好的电脑,例如最新的Apple 1.4Ghz Mac mini,你使用的是工厂模式。所有硬件规格都已经由厂商预定义了,厂家不用问你也知道该干什么。厂商通畅只会收到一条指令。代码使人明了,其内容如下(*apple-factory.py*):
59
+
新计算机的分析有助于区别构造器模式和工厂模式。假设你需要买一台新电脑。如果你决定买预先配置好的电脑,例如最新的Apple 1.4Ghz Mac mini,你使用的是工厂模式。所有硬件规格都已经由厂商预定义了,厂家不用问你也知道该干什么。厂商通常只会收到一条指令。代码使人明了,其内容如下(*apple-factory.py*):
60
60
61
61
```python
62
62
MINI14='1.4GHz Mac mini'
@@ -183,7 +183,7 @@ not supposed to be instantiated directly. A builder creates an instance of the e
183
183
- To clarify the fact that the end product is typically minimal does not mean that you should never assign it any responsibilities
184
184
- To promote code reuse through composition [GOF95, page 32]
185
185
186
-
- 要说明的是,终端产品通畅是最小化的,但是这并不意味着你从此就不能让它承担任何的职责
186
+
- 要说明的是,终端产品通常是最小化的,但是这并不意味着你从此就不能让它承担任何的职责
187
187
- 通过使用合成来提升代码的复用性
188
188
189
189
```python
@@ -360,7 +360,6 @@ class MargaritaBuilder:
360
360
self.pizza = Pizza('margarita')
361
361
self.progress = PizzaProgress.queued
362
362
self.baking_time =5# in seconds for the sake of the
363
-
example
364
363
365
364
defprepare_dough(self):
366
365
self.progress = PizzaProgress.preparation
@@ -402,53 +401,53 @@ class CreamyBaconBuilder:
402
401
self.pizza = Pizza('creamy bacon')
403
402
self.progress = PizzaProgress.queued
404
403
self.baking_time =7# in seconds for the sake of the
405
-
example
406
404
407
405
defprepare_dough(self):
408
-
self.progress = PizzaProgress.preparation
409
-
self.pizza.prepare_dough(PizzaDough.thick)
406
+
self.progress = PizzaProgress.preparation
407
+
self.pizza.prepare_dough(PizzaDough.thick)
410
408
411
409
defadd_sauce(self):
412
-
print('adding the crème fraîche sauce to your creamy bacon')
413
-
self.pizza.sauce = PizzaSauce.creme_fraiche
414
-
time.sleep(STEP_DELAY)
415
-
print('done with the crème fraîche sauce')
410
+
print('adding the crème fraîche sauce to your creamy bacon')
411
+
self.pizza.sauce = PizzaSauce.creme_fraiche
412
+
time.sleep(STEP_DELAY)
413
+
print('done with the crème fraîche sauce')
416
414
417
415
defadd_topping(self):
418
-
print('adding the topping (mozzarella, bacon, ham, mushrooms, red onion, oregano) to your creamy bacon')
419
-
self.pizza.topping.append([t for t in
420
-
(PizzaTopping.mozzarella, PizzaTopping.bacon,
421
-
PizzaTopping.ham,PizzaTopping.mushrooms,
422
-
PizzaTopping.red_onion, PizzaTopping.oregano)])
423
-
time.sleep(STEP_DELAY)
424
-
print('done with the topping (mozzarella, bacon, ham, mushrooms, red onion, oregano)')
416
+
print('adding the topping (mozzarella, bacon, ham, mushrooms, red onion, oregano) to your creamy bacon')
417
+
self.pizza.topping.append([t for t in
418
+
(PizzaTopping.mozzarella, PizzaTopping.bacon,
419
+
PizzaTopping.ham,PizzaTopping.mushrooms,
420
+
PizzaTopping.red_onion, PizzaTopping.oregano)])
421
+
time.sleep(STEP_DELAY)
422
+
print('done with the topping (mozzarella, bacon, ham, mushrooms, red onion, oregano)')
425
423
426
424
defbake(self):
427
-
self.progress = PizzaProgress.baking
428
-
print('baking your creamy bacon for {} seconds'.format(self.baking_time))
429
-
time.sleep(self.baking_time)
430
-
self.progress = PizzaProgress.ready
431
-
print('your creamy bacon is ready')
425
+
self.progress = PizzaProgress.baking
426
+
print('baking your creamy bacon for {} seconds'.format(self.baking_time))
0 commit comments