forked from peterdocter/python-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-builder.html
More file actions
681 lines (307 loc) · 19.4 KB
/
python-builder.html
File metadata and controls
681 lines (307 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
<!DOCTYPE HTML>
<html lang="" >
<head>
<title>Builder 模式 · GitBook</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.0">
<link rel="stylesheet" href="gitbook/style.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="python-object-pool.html" />
<link rel="prev" href="python-borg.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="header">Python 设计模式</li>
<li class="chapter " data-level="1.1" data-path="./">
<a href="./">
Introduction
</a>
</li>
<li class="chapter " data-level="1.2" >
<span>
创建型设计模式
</span>
<ul class="articles">
<li class="chapter " data-level="1.2.1" data-path="python-abstract-factory.html">
<a href="python-abstract-factory.html">
抽象工厂模式
</a>
</li>
<li class="chapter " data-level="1.2.2" data-path="python-borg.html">
<a href="python-borg.html">
Borg 模式
</a>
</li>
<li class="chapter active" data-level="1.2.3" data-path="python-builder.html">
<a href="python-builder.html">
Builder 模式
</a>
</li>
<li class="chapter " data-level="1.2.4" data-path="python-object-pool.html">
<a href="python-object-pool.html">
对象池模式
</a>
</li>
<li class="chapter " data-level="1.2.5" data-path="python-prototype.html">
<a href="python-prototype.html">
原型模式
</a>
</li>
<li class="chapter " data-level="1.2.6" data-path="python-singleton.html">
<a href="python-singleton.html">
单例模式
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.3" >
<span>
结构型设计模式
</span>
<ul class="articles">
<li class="chapter " data-level="1.3.1" data-path="python-adapter.html">
<a href="python-adapter.html">
Adapter 模式
</a>
</li>
<li class="chapter " data-level="1.3.2" data-path="python-bridge.html">
<a href="python-bridge.html">
桥接模式
</a>
</li>
<li class="chapter " data-level="1.3.3" data-path="python-composite.html">
<a href="python-composite.html">
组合模式
</a>
</li>
<li class="chapter " data-level="1.3.4" data-path="python-decorator.html">
<a href="python-decorator.html">
装饰器模式
</a>
</li>
<li class="chapter " data-level="1.3.5" data-path="python-facade.html">
<a href="python-facade.html">
外观模式
</a>
</li>
<li class="chapter " data-level="1.3.6" data-path="python-flyweight.html">
<a href="python-flyweight.html">
享元模式
</a>
</li>
<li class="chapter " data-level="1.3.7" data-path="python-proxy.html">
<a href="python-proxy.html">
代理模式
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4" >
<span>
行为型设计模式
</span>
<ul class="articles">
<li class="chapter " data-level="1.4.1" data-path="python-chain.html">
<a href="python-chain.html">
责任链模式
</a>
</li>
<li class="chapter " data-level="1.4.2" data-path="python-command.html">
<a href="python-command.html">
调度模式
</a>
</li>
<li class="chapter " data-level="1.4.3" data-path="python-memento.html">
<a href="python-memento.html">
备忘录模式
</a>
</li>
<li class="chapter " data-level="1.4.4" data-path="python-observer.html">
<a href="python-observer.html">
观察者模式
</a>
</li>
<li class="chapter " data-level="1.4.5" data-path="python-state.html">
<a href="python-state.html">
状态模式
</a>
</li>
<li class="chapter " data-level="1.4.6" data-path="python-strategy.html">
<a href="python-strategy.html">
策略模式
</a>
</li>
<li class="chapter " data-level="1.4.7" data-path="python-template.html">
<a href="python-template.html">
模板方法模式
</a>
</li>
<li class="chapter " data-level="1.4.8" data-path="python-visitor.html">
<a href="python-visitor.html">
访问者模式
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5" >
<span>
其他
</span>
<ul class="articles">
<li class="chapter " data-level="1.5.1" data-path="python-null.html">
<a href="python-null.html">
Null 模式
</a>
</li>
</ul>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href="." >Builder 模式</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h1 id="python设计模式之builder模式"><a href="http://dongweiming.github.io/python-builder.html"Permalink to python设计模式之Builder模式"" target="_blank">python设计模式之Builder模式</a></h1><h2 id="builder模式">Builder模式</h2><p>Builder模式帮你把一个不同类的大工程分开成不同的部分,最后在把这些合并起来</p><h2 id="python的例子">python的例子</h2><p>制造一辆车的工序是很繁琐和浩大的,我们可以定一个车辆的描述</p><pre><code>class Vehicle(object):
def __init__(self, type_name):
self.type = type_name
self.wheels = None
self.doors = None
self.seats = None
# 不同的车子类型,齿轮,车门数,座位数都不尽相同
def view(self):
print(
"This vehicle is a " +
self.type +
" with; " +
str(self.wheels) +
" wheels, " +
str(self.doors) +
" doors, and " +
str(self.seats) +
" seats."
)</code></pre>
<p>这里我要把制造齿轮,车门,座位的工序抽象出来,用不同的制造部门去做</p><pre><code>class VehicleBuilder(object):
__metadata__ = abc.ABCMeta
# 装饰器定义抽象方法(只能用在 metaclass 为 abc.ABCMeta 或其子类的 class 中)
# 这里其实当然可以用pass,或者raise一个基类未实现的异常
@abc.abstractmethod
def make_wheels(self):
raise
@abc.abstractmethod
def make_doors(self):
raise
@abc.abstractmethod
def make_seats(self):
raise</code></pre>
<p>然后设想我要制造汽车和自行车, 每个都要继承这个VehicleBuilder基类,但是齿轮,车门,座位都是不一样的</p><pre><code>class CarBuilder(VehicleBuilder):
def __init__(self):
self.vehicle = Vehicle("Car ")
def make_wheels(self):
self.vehicle.wheels = 4
def make_doors(self):
self.vehicle.doors = 3
def make_seats(self):
self.vehicle.seats = 5
class BikeBuilder(VehicleBuilder):
def __init__(self):
self.vehicle = Vehicle("Bike")
def make_wheels(self):
self.vehicle.wheels = 2
def make_doors(self):
self.vehicle.doors = 0
def make_seats(self):
self.vehicle.seats = 2</code></pre>
<p>下面是最重要的环节:制造的Director</p><pre><code>class VehicleManufacturer(object):
def __init__(self):
self.builder = None
# 根据传进来的builder制造它提供的产品规格
def create(self):
assert not self.builder is None, "No defined builder"
self.builder.make_wheels()
self.builder.make_doors()
self.builder.make_seats()
return self.builder.vehicle</code></pre>
<p>看起来是不是有点和以前的模式不好分辨? 主要下面的调用方式</p><pre><code>if __name__ == "__main__":
manufacturer = VehicleManufacturer()
# 重要的是这样的调用方式,每次修改builder方法传入不同的要求,然后在create里面去制造,
# 整个过程都是在VehicleManufacturer完成
manufacturer.builder = CarBuilder()
car = manufacturer.create()
car.view()
manufacturer.builder = BikeBuilder()
bike = manufacturer.create()
bike.view()</code></pre>
</section>
</div>
<div class="search-results">
<div class="has-results">
<h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
<ul class="search-results-list"></ul>
</div>
<div class="no-results">
<h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
</div>
</div>
</div>
</div>
</div>
</div>
<a href="python-borg.html" class="navigation navigation-prev " aria-label="Previous page: Borg 模式">
<i class="fa fa-angle-left"></i>
</a>
<a href="python-object-pool.html" class="navigation navigation-next " aria-label="Next page: 对象池模式">
<i class="fa fa-angle-right"></i>
</a>
</div>
<script>
var gitbook = gitbook || [];
gitbook.push(function() {
gitbook.page.hasChanged({"page":{"title":"Builder 模式","level":"1.2.3","depth":2,"next":{"title":"对象池模式","level":"1.2.4","depth":2,"path":"python-object-pool.md","ref":"python-object-pool.md","articles":[]},"previous":{"title":"Borg 模式","level":"1.2.2","depth":2,"path":"python-borg.md","ref":"python-borg.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"python-builder.md","mtime":"2016-10-06T01:54:58.000Z","type":"markdown"},"gitbook":{"version":"3.2.0","time":"2016-10-06T01:56:16.113Z"},"basePath":".","book":{"language":""}});
});
</script>
</div>
<script src="gitbook/gitbook.js"></script>
<script src="gitbook/theme.js"></script>
<script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
<script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
<script src="gitbook/gitbook-plugin-search/search.js"></script>
<script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
<script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
<script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
<script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
</body>
</html>