码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式之建造者模式

时间:2018-05-01 15:27:19      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:code   华为P20   bst   choice   self   rom   处理器   port   rac   

设计模式之建造者模式

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())

 

设计模式之建造者模式

标签:code   华为P20   bst   choice   self   rom   处理器   port   rac   

原文地址:https://www.cnblogs.com/aadmina/p/8976159.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!