在 python 中实现构建器模式
Implementing the builder pattern in python
我一直在研究设计模式。我正在努力寻找可以为该场景实施构建器模式的场景。有人在 python 中有任何例子吗?
在这段代码中,我们将为我们的 AppleFactory 构建一台计算机,它看起来像下面没有设计模式(在本例中为构建器模式)的代码。
MINI14 = '1.4GHz Mac mini'
class AppleFactory:
class MacMini14:
def __init__(self):
self.memory = 4 # in gigabytes
self.hdd = 500 # in gigabytes
self.gpu = 'Intel HD Graphics 5000'
def __str__(self):
info = (f'Model: {MINI14}',
f'Memory: {self.memory}GB',
f'Hard Disk: {self.hdd}GB',
f'Graphics Card: {self.gpu}')
return '\n'.join(info)
def build_computer(self, model):
if model == MINI14:
return self.MacMini14()
else:
msg = f"I don't know how to build {model}"
print(msg)
if __name__ == '__main__':
afac = AppleFactory()
mac_mini = afac.build_computer(MINI14)
print(mac_mini)
让我们使用 Builder 设计模式来解耦上面的代码。
class Computer:
def __init__(self, serial_number):
self.serial = serial_number
self.memory = None # in gigabytes
self.hdd = None # in gigabytes
self.gpu = None
def __str__(self):
info = (f'Memory: {self.memory}GB',
f'Hard Disk: {self.hdd}GB',
f'Graphics Card: {self.gpu}')
return '\n'.join(info)
class ComputerBuilder:
def __init__(self):
self.computer = Computer('AG23385193')
def configure_memory(self, amount):
self.computer.memory = amount
def configure_hdd(self, amount):
self.computer.hdd = amount
def configure_gpu(self, gpu_model):
self.computer.gpu = gpu_model
class HardwareEngineer:
def __init__(self):
self.builder = None
def construct_computer(self, memory, hdd, gpu):
self.builder = ComputerBuilder()
steps = (self.builder.configure_memory(memory),
self.builder.configure_hdd(hdd),
self.builder.configure_gpu(gpu))
[step for step in steps]
@property
def computer(self):
return self.builder.computer
def main():
engineer = HardwareEngineer()
engineer.construct_computer(hdd=500,memory=8,gpu='GeForce GTX 650 Ti')
computer = engineer.computer
print(computer)
if __name__ == '__main__':
main()
在这里,我们将代码分离为 3 个 classes - Computer、ComputerBuilder 和 HardwareEngineer,这样任何人都可以调用 HardwareEngineer class 并构建。
我一直在研究设计模式。我正在努力寻找可以为该场景实施构建器模式的场景。有人在 python 中有任何例子吗?
在这段代码中,我们将为我们的 AppleFactory 构建一台计算机,它看起来像下面没有设计模式(在本例中为构建器模式)的代码。
MINI14 = '1.4GHz Mac mini'
class AppleFactory:
class MacMini14:
def __init__(self):
self.memory = 4 # in gigabytes
self.hdd = 500 # in gigabytes
self.gpu = 'Intel HD Graphics 5000'
def __str__(self):
info = (f'Model: {MINI14}',
f'Memory: {self.memory}GB',
f'Hard Disk: {self.hdd}GB',
f'Graphics Card: {self.gpu}')
return '\n'.join(info)
def build_computer(self, model):
if model == MINI14:
return self.MacMini14()
else:
msg = f"I don't know how to build {model}"
print(msg)
if __name__ == '__main__':
afac = AppleFactory()
mac_mini = afac.build_computer(MINI14)
print(mac_mini)
让我们使用 Builder 设计模式来解耦上面的代码。
class Computer:
def __init__(self, serial_number):
self.serial = serial_number
self.memory = None # in gigabytes
self.hdd = None # in gigabytes
self.gpu = None
def __str__(self):
info = (f'Memory: {self.memory}GB',
f'Hard Disk: {self.hdd}GB',
f'Graphics Card: {self.gpu}')
return '\n'.join(info)
class ComputerBuilder:
def __init__(self):
self.computer = Computer('AG23385193')
def configure_memory(self, amount):
self.computer.memory = amount
def configure_hdd(self, amount):
self.computer.hdd = amount
def configure_gpu(self, gpu_model):
self.computer.gpu = gpu_model
class HardwareEngineer:
def __init__(self):
self.builder = None
def construct_computer(self, memory, hdd, gpu):
self.builder = ComputerBuilder()
steps = (self.builder.configure_memory(memory),
self.builder.configure_hdd(hdd),
self.builder.configure_gpu(gpu))
[step for step in steps]
@property
def computer(self):
return self.builder.computer
def main():
engineer = HardwareEngineer()
engineer.construct_computer(hdd=500,memory=8,gpu='GeForce GTX 650 Ti')
computer = engineer.computer
print(computer)
if __name__ == '__main__':
main()
在这里,我们将代码分离为 3 个 classes - Computer、ComputerBuilder 和 HardwareEngineer,这样任何人都可以调用 HardwareEngineer class 并构建。