本文实例讲述了Python实现的建造者模式。分享给大家供大家参考,具体如下:
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
|
#!/usr/bin/python # -*- coding:utf-8 -*- #建造者基类 class PersonBuilder(): def BuildHead( self ): pass def BuildBody( self ): pass def BuildArm( self ): pass def BuildLeg( self ): pass #胖子 class PersonFatBuilder(PersonBuilder): type = u '胖子' def BuildHead( self ): print u '构建%s的头' % self . type def BuildBody( self ): print u '构建%s的身体' % self . type def BuildArm( self ): print u '构建%s的手' % self . type def BuildLeg( self ): print u '构建%s的脚' % self . type #瘦子 class PersonThinBuilder(PersonBuilder): type = u '瘦子' def BuildHead( self ): print u '构建%s的头' % self . type def BuildBody( self ): print u '构建%s的身体' % self . type def BuildArm( self ): print u '构建%s的手' % self . type def BuildLeg( self ): print u '构建%s的脚' % self . type #指挥者 class PersonDirector(): pb = None ; def __init__( self , pb): self .pb = pb def CreatePereson( self ): self .pb.BuildHead() self .pb.BuildBody() self .pb.BuildArm() self .pb.BuildLeg() def clientUI(): pb = PersonThinBuilder() pd = PersonDirector(pb) pd.CreatePereson() pb = PersonFatBuilder() pd = PersonDirector(pb) pd.CreatePereson() return if __name__ = = '__main__' : clientUI(); |
运行结果:
构建瘦子的头
构建瘦子的身体
构建瘦子的手
构建瘦子的脚
构建胖子的头
构建胖子的身体
构建胖子的手
构建胖子的脚
希望本文所述对大家Python程序设计有所帮助。
原文链接:https://www.cnblogs.com/zxlovenet/p/4073782.html