设计模式-python-建造者模式
·
简述
建造者模式也是用于创建对象的场景。举个例子说明:

如上图,盒装椰奶和罐装椰奶的生产大致流程, ==>
-
角色分配:
(1)指令发布者,他决定生产每种椰奶的数量
(2)罐装椰奶生产机和盒装椰奶生产机:用来生产成品,也就是我们喝的椰奶
(3)椰奶:用于标识各种类型的椰奶 -
生产流程:
指令发布者 =(参数)> 罐装椰奶机 ——> 罐装椰奶
||
(参数)
||
==> 盒装椰奶机 ——> 盒装椰奶
与工厂模式的比较
我个人理解,建造者模式适用于大场面,而工厂模式适用于小场面。 还是用椰奶来说:
- 椰奶工厂生产椰奶的场景,椰奶对象高度可配置(有很多入参),创造椰奶的设备也会有很多流程,这种场景适合用建造者模式
- 小卖铺买椰奶的场景,椰奶对象简单(只有一个或者两个入参),创造椰奶的设备流程较少,这种场景适合用工厂模式
建造者模式举例
基本上要准备三种对象:
- 产品对象: 对应上面的椰奶
- 生产者:对应上面的盒装生产机以及罐装生产机
- 指挥者:整个流程的发起者,校验指令,给生产者传参,产出产品
"""
@author: hananmin
@time: 2022/4/2 15:26
"""
# 椰奶对象
class CoconutMilk:
def __init__(self, picture="", volume="", ex_date="", packaging=""):
# 包装
self.packaging = packaging
# 图片
self.picture = picture
# 体积
self.volume = volume
# 保质期
self.ex_date = ex_date
@property
def drinks(self):
return f"椰奶: 包装=>{self.packaging} 图片=>{self.picture} 体积=>{self.volume} 保质期=>{self.ex_date}"
# 铁罐生成机器
class CanProducer:
def __init__(self, nums, picture, ex_date, volume):
self.nums = nums
self.packaging = "铁罐"
self.picture = picture
self.ex_date = ex_date
self.volume = volume
def process_milk(self):
print(f"生产牛奶 {self.volume}")
return "200ml"
def inject_container(self, semi_product):
print(f"装入容器, 容器是 {self.packaging}")
semi_product.packaging = self.packaging
def make_labels(self, semi_product):
print(f"外包装 保质期{self.ex_date} 图片是 {self.picture} 体积是 {self.volume}")
semi_product.ex_date = self.ex_date
semi_product.picture = self.picture
semi_product.volume = self.volume
def run(self):
all_products = []
print(f"==>{self.packaging} {'*'*10} 开始生产 {'*'*10}")
for part in range(0, self.nums):
tmp_pro = CoconutMilk()
self.process_milk()
self.inject_container(tmp_pro)
self.make_labels(tmp_pro)
all_products.append(tmp_pro)
print(f"{'*' * 10} 生产结束 {'*' * 10}")
return all_products
# 盒装生产机器
class BoxProducer:
def __init__(self, nums, picture, ex_date, volume):
self.nums = nums
self.packaging = "纸盒"
self.picture = picture
self.ex_date = ex_date
self.volume = volume
def process_milk(self):
print(f"生产牛奶 {self.volume}")
return "200ml"
def inject_container(self, semi_product):
print(f"装入容器, 容器是 {self.packaging}")
semi_product.packaging = self.packaging
def make_labels(self, semi_product):
print(f"外包装 保质期{self.ex_date} 图片是 {self.picture} 体积是 {self.volume}")
semi_product.ex_date = self.ex_date
semi_product.picture = self.picture
semi_product.volume = self.volume
def run(self):
all_products = []
print(f"==>{self.packaging} {'*'*10} 开始生产 {'*'*10}")
for part in range(0, self.nums):
tmp_pro = CoconutMilk()
self.process_milk()
self.inject_container(tmp_pro)
self.make_labels(tmp_pro)
all_products.append(tmp_pro)
print(f"{'*' * 10} 生产结束 {'*' * 10}")
return all_products
# 指挥者
class Director:
def __init__(self, command):
self.command = command
# 对命令进行校验
def verify_command(self):
if len(self.command) != 2:
return False
print(f"指令检验通过")
return True
# 生产牛奶
def product_milk(self):
if self.verify_command():
cans = CanProducer(**self.command[0]).run()
boxes = BoxProducer(**self.command[1]).run()
print("====>尝一口:")
print(cans[0].drinks)
print(boxes[0].drinks)
if __name__ == "__main__":
command = [
{"nums": 1, "picture": "大哥女人", "ex_date": "3022年", "volume": "200ml"},
{"nums": 1, "picture": "大哥女人", "ex_date": "3011年", "volume": "100ml"},
]
Director(command).product_milk()

更多推荐



所有评论(0)