Python实现的建造者模式示例
作者:初行 时间:2023-06-02 07:17:00
本文实例讲述了Python实现的建造者模式。分享给大家供大家参考,具体如下:
#!/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
标签:Python,建造者模式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python可视化hdf5文件的操作
2022-11-16 16:35:48
![](https://img.aspxhome.com/file/2023/4/105054_0s.jpg)
Python实现方便使用的级联进度信息实例
2021-04-22 12:31:18
YOLOv5车牌识别实战教程(五)字符分割与识别
2022-04-07 07:38:25
![](https://img.aspxhome.com/file/2023/9/97189_0s.png)
详解JavaScript中操作符和表达式
2024-06-17 21:14:30
mysql 通配符(sql 高级过滤)
2024-01-24 17:15:39
PyCharm关闭碍眼的波浪线图文详解
2023-07-27 08:47:53
![](https://img.aspxhome.com/file/2023/7/118027_0s.png)
Pytorch加载数据集的方式总结及补充
2023-02-15 17:14:37
“尊重”设计师?
2009-03-23 18:14:00
Django框架自定义session处理操作示例
2022-02-11 02:56:19
Python标准库os常用函数和属性详解
2022-05-17 21:40:03
![](https://img.aspxhome.com/file/2023/7/135067_0s.jpg)
MySql模糊查询json关键字检索方案示例
2024-01-16 13:57:44
![](https://img.aspxhome.com/file/2023/8/111418_0s.png)
asp如何刪除客户端的Cookies?
2010-05-18 18:25:00
python Django框架实现自定义表单提交
2021-01-04 14:52:42
正确理解SQL Server四类数据仓库建模方法
2008-12-23 15:20:00
AJAX在GET中文的时候解决乱码的方法
2007-11-04 13:04:00
实现div可编辑的常见方法
2007-11-06 12:02:00
python3.9.1环境安装的方法(图文)
2023-06-26 15:58:26
![](https://img.aspxhome.com/file/2023/5/135035_0s.jpg)
python中windows链接linux执行命令并获取执行状态的问题小结
2022-05-18 12:56:55
JSP 获取spring容器中bean的两种方法总结
2023-06-15 07:27:46
python爬虫模拟登录之图片验证码实现详解
2022-06-30 00:07:29
![](https://img.aspxhome.com/file/2023/9/130679_0s.png)