python设计模式(外观模式)

来源:互联网 发布:非洲的基础网络情况 编辑:程序博客网 时间:2024/05/22 13:13

学习版本3.5.2

外观模式(Facade Pattern)向客户端提供一个可以访问系统的接口,隐藏了系统的复杂性。

比如说,生产a产品需要abc三个流水线生产的零件,我只想管a产品的生产。

class ProductionLineA(object):    def create(self):        print("create component a")class ProductionLineB(object):    def create(self):        print("create component b")class ProductionLineC(object):    def create(self):        print("create component c")class AssembleProductA(object):    def assemble(self):        print("use component a,b and c to assemble product a")class YieldProduct(object):    def __init__(self):        self.pla = ProductionLineA()        self.plb = ProductionLineB()        self.plc = ProductionLineC()        self.apa = AssembleProductA()    def product(self):        self.pla.create()        self.plb.create()        self.plc.create()        self.apa.assemble()if __name__ == "__main__":    yp = YieldProduct()    yp.product()

运行结果

create component acreate component bcreate component cuse component a,b and c to assemble product a