The Builder Pattern of Design Patterns

The Builder Pattern of Design Patterns

import random
 from abc import ABCMeta,abstractmethod
 class CellPhone: #Product
     side def 
    __init__  ( self,screen=None,cpu=None,camera=None,cell=None,name= None):
         #Four attributes of the phone: screen, processing device, camera, battery 
        self.screen = None
        self.cpu = None
        self.camera = None
        self.cell = None
        self.name = name
    def __str__(self):
        info = " Phone: %s, Configuration Information--------->Screen: %s, Processor: %s, Camera: %s, Battery: %s " % (self.name,self.screen ,self.cpu,self.camera,self.cell)
         return info

# ----------------Create a mobile phone interface (rule class) 
class CellPhoneBuilder: #Create
     a mobile phone interface 
    @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


# ------------------This class is used to assemble a certain type of mobile phone 
class XiaoMiBuilder(CellPhoneBuilder): #Assemble
     a mobile phone 
    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 = ' double 12 million pixels ' ):
        self.cellphone.camera = camera
    def produce_cell(self,cell='3400mAh'):
        self.cellphone.cell = cell
    def get_cellphone(self):
        return self.cellphone


# ------------------This class is used to assemble a certain type of mobile phone 
class HuaWeiBuilder(CellPhoneBuilder): #Assemble
     a mobile phone 
    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


# ------------------This class is used to assemble an unrestricted range of mobile phones 
class RandomCellPhoneBuilder(CellPhoneBuilder): #Randomly
     assemble a mobile phone 
    def  __init__ (self):
        self.cellphone = CellPhone()
     def name(self, name= ' Randomly create a phone ' ):
        self.cellphone.name = name
    def produce_screen(self,screen=None):
        screens = [ ' LCD screen ' , ' NOVA screen ' , ' AMOLED screen ' ]
         if screen:
            screens.append(screen)
        self.cellphone.screen = random.choice(screens)
    def produce_cpu(self,cpu=None):
        cpus = [ ' Snapdragon 845 ' , ' Kirin 970 ' , ' Apple A11 ' ]
         if cpu:
            cpus.append(cpu)
        self.cellphone.cpu = random.choice(cpus)
    def produce_camera(self,camear=None):
        cameras = [ ' Dual 12MP ' , ' 20MP+12MP ' , ' Dual 12MP ' ]
         if camera:
            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


# ------------------ Responsible for scheduling production lines to produce mobile phones 
class CellPhoneDirector:
     # Responsible for scheduling mobile phone classes for mobile phone production (manufacturing) 
    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()


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

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325088484&siteId=291194637