zoukankan      html  css  js  c++  java
  • 设计模式之建造者模式

    设计模式之建造者模式

    import random
    from abc import ABCMeta,abstractmethod
    class CellPhone:
        # 产品端
        def __init__(self,screen=None,cpu=None,camera=None,cell=None,name=None):
            #手机的四个属性:屏幕、处理器、摄像头、电池
            self.screen = None
            self.cpu = None
            self.camera = None
            self.cell = None
            self.name = name
        def __str__(self):
            info = "手机:%s,  配置信息--------->屏幕:%s,处理器:%s,摄像头:%s,电池:%s"%(self.name,self.screen,self.cpu,self.camera,self.cell)
            return info
    
    # ----------------创建一个手机的接口(规则类)
    class CellPhoneBuilder:
        # 制造手机的接口
        @abstractmethod
        def name(self, name):
            pass
        @abstractmethod
        def produce_screen(self,screen):
            pass
        @abstractmethod
        def produce_cpu(self,cpu):
            pass
        @abstractmethod
        def produce_camera(self,camera):
            pass
        @abstractmethod
        def produce_cell(self,cell):
            pass
        @abstractmethod
        def get_cellphone(self):
            pass
    
    
    # ------------------此类用于组装某一类手机
    class XiaoMiBuilder(CellPhoneBuilder):
        # 组装一个手机
        def __init__(self):
            self.cellphone = CellPhone()
        def name(self, name='MIX2S'):
            self.cellphone.name = name
        def produce_screen(self,screen = 'LCD屏'):
            self.cellphone.screen = screen
        def produce_cpu(self,cpu = '骁龙845'):
            self.cellphone.cpu = cpu
        def produce_camera(self,camera = '双1200万像素'):
            self.cellphone.camera = camera
        def produce_cell(self,cell='3400mAh'):
            self.cellphone.cell = cell
        def get_cellphone(self):
            return self.cellphone
    
    
    # ------------------此类用于组装某一类手机
    class HuaWeiBuilder(CellPhoneBuilder):
        # 组装一个手机
        def __init__(self):
            self.cellphone = CellPhone()
        def name(self, name='华为P20'):
            self.cellphone.name = name
        def produce_screen(self,screen = 'LCD屏'):
            self.cellphone.screen = screen
        def produce_cpu(self,cpu = '麒麟970'):
            self.cellphone.cpu = cpu
        def produce_camera(self,camera = '2000万+1200万'):
            self.cellphone.camera = camera
        def produce_cell(self,cell='3400mAh'):
            self.cellphone.cell = cell
        def get_cellphone(self):
            return self.cellphone
    
    
    # ------------------此类用于组装非限定范围的手机
    class RandomCellPhoneBuilder(CellPhoneBuilder):
        # 随机组装一个手机
        def __init__(self):
            self.cellphone = CellPhone()
        def name(self, name='随机创建手机'):
            self.cellphone.name = name
        def produce_screen(self,screen=None):
            screens = ['LCD屏', 'NOVA屏', 'AMOLED屏']
            if screen:
                screens.append(screen)
            self.cellphone.screen = random.choice(screens)
        def produce_cpu(self,cpu=None):
            cpus = ['骁龙845','麒麟970','苹果A11']
            if cpu:
                cpus.append(cpu)
            self.cellphone.cpu = random.choice(cpus)
        def produce_camera(self,camear=None):
            cameras = ['双1200万像素','2000万+1200万','双1200万像素']
            if camear:
                cameras.append(camear)
            self.cellphone.camera = random.choice(cameras)
        def produce_cell(self,cell=None):
            cells = ['2716mAh','3400mAh','3400mAh']
            if cell:
                cells.append(cell)
            self.cellphone.cell = random.choice(cells)
        def get_cellphone(self):
            return self.cellphone
    
    
    # ------------------负责调度产线来生产手机
    class CellPhoneDirector:
        # 负责调度手机类进行手机的生产(制造)
        def __init__(self,builder):
            self.builder = builder
        def build_cellphone(self):
            self.builder.name()
            self.builder.produce_screen()
            self.builder.produce_cpu()
            self.builder.produce_camera()
            self.builder.produce_cell()
            return self.builder.get_cellphone()
    
    
    # ------------------测试
    random_cellphone = RandomCellPhoneBuilder()
    xiaomi = XiaoMiBuilder()
    huawei = HuaWeiBuilder()
    xm = CellPhoneDirector(xiaomi)
    hw = CellPhoneDirector(huawei)
    rd = CellPhoneDirector(random_cellphone)
    print(xm.build_cellphone())
    print(hw.build_cellphone())
    print(rd.build_cellphone())
  • 相关阅读:
    HDU1864--01背包
    HDU4508--完全背包
    HDU5410--01背包+完全背包
    HDU1284--完全背包
    HDU1248--完全背包
    HDU2191--多重背包(二进制分解+01背包)
    HDU2186--水
    PAT乙级--1003
    51Nod--1006 lcs
    51Nod--1008
  • 原文地址:https://www.cnblogs.com/aadmina/p/8976159.html
Copyright © 2011-2022 走看看