Python 设计模式中的创建型建造者模式
作者:范桂飓 时间:2021-05-23 02:41:50
一、建造者模式
建造者模式,顾名思义类似于建筑工人,他们按照有条理的施工顺序(e.g. 打桩 => 浇筑框架 => 砌墙 => 装修)来进行建筑的修建。对于千差万别的建筑,都可以复用同样的施工流程。因为不同的材料、不同设计,可以有不同的表现。
建造者模式,与抽象工厂模式同样用于实例化复杂的对象,主要区别在于:
抽象工厂模式侧重于实例化多个系列的复杂对象。
建造者模式侧重于一步步有序地构造一个复杂对象。
二、代码示例
按照有序的步骤来组装(建造)一个复杂对象。
实体角色:
抽象建造者(
Builder
)具体建造者(
Concrete Builder
)指挥者(
Director
)产品(
Product
)
import abc
class Robot:
def __init__(self, head=None, body=None, arms=None, legs=None):
self.head = head
self.body = body
self.arms = arms
self.legs = legs
def __str__(self):
return f"我是一个机器人:{self.head, self.body, self.arms, self.legs}"
# 抽象建造者
class RobotBuilder(metaclass=abc.ABCMeta):
@abc.abstractmethod
def build_head(self):
print("第二步:安装头")
pass
@abc.abstractmethod
def build_body(self):
print("第一步:安装身体")
pass
@abc.abstractmethod
def build_arms(self):
print("第三步:安装手臂")
pass
@abc.abstractmethod
def build_legs(self):
print("第四步:安装脚")
pass
# 具体建造者 Doraemon
class Doraemon(RobotBuilder):
def __init__(self):
self.robot = Robot()
def build_head(self):
super().build_head()
self.robot.head = "蓝色的狸猫头"
def build_body(self):
super().build_body()
self.robot.body = "带有百宝袋的身体"
def build_arms(self):
super().build_arms()
self.robot.arms = "圆手"
def build_legs(self):
super().build_legs()
self.robot.legs = "短腿"
# 具体建造者 Gundam
class Gundam(RobotBuilder):
def __init__(self):
self.robot = Robot()
def build_head(self):
super().build_head()
self.robot.head = "白色的机械头"
def build_body(self):
super().build_body()
self.robot.body = "钢铁般的身体"
def build_arms(self):
super().build_arms()
self.robot.arms = "带有巨炮的机械手"
def build_legs(self):
super().build_legs()
self.robot.legs = "带有推进器的机械腿"
# 指挥者,确定建造顺序
class BuildDirector:
def build(self, builder):
builder.build_body()
builder.build_head()
builder.build_arms()
builder.build_legs()
return builder.robot
if __name__ == "__main__":
director = BuildDirector()
doraemon = Doraemon()
print(director.build(doraemon))
gundam = Gundam()
print(director.build(gundam))
来源:https://is-cloud.blog.csdn.net/article/details/122922317
标签:Python,建造者,模式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Linux系统下导出ORACLE数据库出现Exporting questionable statistics.错误 处理
2010-07-16 13:27:00
python 采用paramiko 远程执行命令及报错解决
2021-01-23 19:17:16
python实现AES加密与解密
2022-12-08 08:18:40
Python教程之基本运算符的使用(上)
2023-12-16 23:11:40
Python爬取科目四考试题库的方法实现
2023-02-14 00:29:39
![](https://img.aspxhome.com/file/2023/0/70220_0s.jpg)
Python之Numpy的超实用基础详细教程
2021-12-05 19:06:23
web版Photoshop来了
2008-04-21 13:39:00
![](https://img.aspxhome.com/file/UploadPic/20084/21/2008421134249782s.jpg)
激发你的灵感:50个优秀的Favicons设计
2007-10-21 19:52:00
![](https://img.aspxhome.com/file/UploadPic/200710/21/20071021211019426s.gif)
Ajax+asp应用实例 注册模块,表单提交
2011-04-08 10:47:00
python求最大连续子数组的和
2022-05-12 07:23:24
python对json的相关操作实例详解
2022-03-27 20:15:06
![](https://img.aspxhome.com/file/2023/2/97112_0s.png)
Linux下MySQL整个数据库的备份与还原
2008-12-29 13:20:00
Win10下Python3.7.3安装教程图解
2023-09-02 18:16:54
![](https://img.aspxhome.com/file/2023/6/62316_0s.jpg)
WEB2.0网页制作标准教程(9)第一个CSS布局实例
2008-02-19 19:05:00
![](https://img.aspxhome.com/file/UploadPic/20082/19/2008219191629508s.gif)
python根据距离和时长计算配速示例
2022-10-13 14:30:36
python 读取txt,json和hdf5文件的实例
2022-02-18 10:43:34
Python中对象迭代与反迭代的技巧总结
2023-06-08 04:25:33
用Python实现给Word文档盖章
2021-07-08 21:18:00
![](https://img.aspxhome.com/file/2023/3/93463_0s.jpg)
如何利用pyecharts画好看的饼状图
2021-04-19 09:07:19
![](https://img.aspxhome.com/file/2023/4/111934_0s.png)
Tensorflow 2.4 搭建单层和多层 Bi-LSTM 模型
2021-03-30 16:25:06