点击上方”python宝典”,关注获取python全套视频,
技术文章第一时间送达!
建造者模式
1、内容
将一个复杂对象的构建与它表示分离,使得同样的构建过程可以创建不同的表示
2、角色
建造者模式与抽象工厂模式相似,也用来创建复杂对象。主要区别是建造者模式着重一步步构造一个复杂对象,而抽象工厂模式着重于多个系列的产品对象。
3、优点
4、缺点
5、使用场景
6、代码示例
import random
from abc import abstractmethod, ABCMeta
#------产品------
class Player:
def __init__(self, face=None, body=None, arm=None, leg=None):
self.face = face
self.arm = arm
self.leg = leg
self.body = body
def __str__(self):
return "%s, %s, %s, %s" % (self.face, self.arm, self.body, self.leg)
#------建造者------
class PlayerBuilder(metaclass=ABCMeta):
@abstractmethod
def build_face(self):
pass
@abstractmethod
def build_arm(self):
pass
@abstractmethod
def build_leg(self):
pass
@abstractmethod
def build_body(self):
pass
@abstractmethod
def get_player(self):
pass
class BeautifulWomanBuilder(PlayerBuilder):
def __init__(self):
self.player = Player()
def build_face(self):
self.player.face = "漂亮脸蛋"
def build_arm(self):
self.player.arm="细胳膊"
def build_body(self):
self.player.body="细腰"
def build_leg(self):
self.player.leg="长腿"
def get_player(self):
return self.player
class RandomPlayerBuilder(PlayerBuilder):
def __init__(self):
self.player = Player()
def build_face(self):
self.player.face = random.choice(["瓜子脸","西瓜子脸"])
def build_arm(self):
self.player.arm=random.choice(["长胳膊","短胳膊"])
def build_body(self):
self.player.body=random.choice(["苗条","胖"])
def build_leg(self):
self.player.leg=random.choice(["长腿","短腿"])
def get_player(self):
return self.player
class PlayerDirector:
def __init__(self, builder):
self.builder = builder
# 控制组装顺序
def build_player(self):
self.builder.build_body()
self.builder.build_face()
self.builder.build_arm()
self.builder.build_leg()
return self.builder.get_player()
pd = PlayerDirector(RandomPlayerBuilder())
p = pd.build_player()
print(p)
识别图中二维码,欢迎关注python宝典