建造者模式
# 产品类,由多个组件组成 class Product(object): def __init__(self): self.L = [] def Add(self,part): self.L.append(part) def show(self): print("产品 创建 -----") for i in self.L: print(i) # 建造者类接口 class Builder(object): def partA(self): pass def partB(self): pass def get_result(self): pass # 具体产品,具体建造者类 class Concrete1(Builder): def __init__(self): self.product = Product() def partA(self): self.product.Add("部件A") def partB(self): self.product.Add("部件B") def get_result(self): return self.product class Concrete2(Builder): def __init__(self): self.product = Product() def partA(self): self.product.Add("部件X") def partB(self): self.product.Add("部件Y") def get_result(self): return self.product # 指挥者 class Director(object): def construct(self,builder): builder.partA() builder.partB() director = Director() b1 = Concrete1() b2 = Concrete2() director.construct(b1) p1 = b1.get_result() p1.show() director.construct(b2) p2 = b2.get_result() p2.show() 建造者