|
|
|
|
@ -0,0 +1,391 @@
|
|
|
|
|
"""
|
|
|
|
|
建造者模式 (Builder Pattern)
|
|
|
|
|
|
|
|
|
|
意图:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
|
|
|
|
|
|
|
|
|
|
应用场景:
|
|
|
|
|
- 当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时
|
|
|
|
|
- 当构造过程必须允许被构造的对象有不同的表示时
|
|
|
|
|
|
|
|
|
|
结构:
|
|
|
|
|
- Builder: 指定创建产品各部分的抽象接口
|
|
|
|
|
- ConcreteBuilder: 实现Builder的接口,构造和装配产品的各个部分
|
|
|
|
|
- Director: 构造一个使用Builder接口的对象
|
|
|
|
|
- Product: 表示被构造的复杂对象
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from abc import ABC, abstractmethod
|
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Product:
|
|
|
|
|
"""产品类,由多个部件组成"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.parts = []
|
|
|
|
|
|
|
|
|
|
def add(self, part: str) -> None:
|
|
|
|
|
"""添加部件
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
part: 部件名称
|
|
|
|
|
"""
|
|
|
|
|
self.parts.append(part)
|
|
|
|
|
|
|
|
|
|
def list_parts(self) -> str:
|
|
|
|
|
"""列出所有部件
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
str: 部件列表字符串
|
|
|
|
|
"""
|
|
|
|
|
return f"产品部件: {', '.join(self.parts)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Builder(ABC):
|
|
|
|
|
"""建造者抽象类"""
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def product(self) -> Product:
|
|
|
|
|
"""获取产品"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def build_part_a(self) -> None:
|
|
|
|
|
"""构建部件A"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def build_part_b(self) -> None:
|
|
|
|
|
"""构建部件B"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def build_part_c(self) -> None:
|
|
|
|
|
"""构建部件C"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ConcreteBuilder1(Builder):
|
|
|
|
|
"""具体建造者1"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
"""初始化建造者,创建一个新的产品实例"""
|
|
|
|
|
self.reset()
|
|
|
|
|
|
|
|
|
|
def reset(self) -> None:
|
|
|
|
|
"""重置产品实例"""
|
|
|
|
|
self._product = Product()
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def product(self) -> Product:
|
|
|
|
|
"""获取产品,同时重置建造者以准备构建下一个产品"""
|
|
|
|
|
result = self._product
|
|
|
|
|
self.reset()
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
def build_part_a(self) -> None:
|
|
|
|
|
self._product.add("部件A1")
|
|
|
|
|
|
|
|
|
|
def build_part_b(self) -> None:
|
|
|
|
|
self._product.add("部件B1")
|
|
|
|
|
|
|
|
|
|
def build_part_c(self) -> None:
|
|
|
|
|
self._product.add("部件C1")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ConcreteBuilder2(Builder):
|
|
|
|
|
"""具体建造者2"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.reset()
|
|
|
|
|
|
|
|
|
|
def reset(self) -> None:
|
|
|
|
|
self._product = Product()
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def product(self) -> Product:
|
|
|
|
|
result = self._product
|
|
|
|
|
self.reset()
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
def build_part_a(self) -> None:
|
|
|
|
|
self._product.add("部件A2")
|
|
|
|
|
|
|
|
|
|
def build_part_b(self) -> None:
|
|
|
|
|
self._product.add("部件B2")
|
|
|
|
|
|
|
|
|
|
def build_part_c(self) -> None:
|
|
|
|
|
self._product.add("部件C2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Director:
|
|
|
|
|
"""指挥者类,负责按照特定顺序调用建造步骤"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self._builder = None
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def builder(self) -> Builder:
|
|
|
|
|
return self._builder
|
|
|
|
|
|
|
|
|
|
@builder.setter
|
|
|
|
|
def builder(self, builder: Builder) -> None:
|
|
|
|
|
"""设置建造者
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
builder: 建造者实例
|
|
|
|
|
"""
|
|
|
|
|
self._builder = builder
|
|
|
|
|
|
|
|
|
|
def build_minimal_viable_product(self) -> None:
|
|
|
|
|
"""构建最小可行产品"""
|
|
|
|
|
self._builder.build_part_a()
|
|
|
|
|
|
|
|
|
|
def build_full_featured_product(self) -> None:
|
|
|
|
|
"""构建完整功能产品"""
|
|
|
|
|
self._builder.build_part_a()
|
|
|
|
|
self._builder.build_part_b()
|
|
|
|
|
self._builder.build_part_c()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 扩展:流式接口风格的建造者模式(更现代的实现方式)
|
|
|
|
|
class Computer:
|
|
|
|
|
"""计算机类,作为产品"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.cpu = None
|
|
|
|
|
self.ram = None
|
|
|
|
|
self.storage = None
|
|
|
|
|
self.gpu = None
|
|
|
|
|
self.os = None
|
|
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
|
components = []
|
|
|
|
|
if self.cpu:
|
|
|
|
|
components.append(f"CPU: {self.cpu}")
|
|
|
|
|
if self.ram:
|
|
|
|
|
components.append(f"RAM: {self.ram}")
|
|
|
|
|
if self.storage:
|
|
|
|
|
components.append(f"Storage: {self.storage}")
|
|
|
|
|
if self.gpu:
|
|
|
|
|
components.append(f"GPU: {self.gpu}")
|
|
|
|
|
if self.os:
|
|
|
|
|
components.append(f"OS: {self.os}")
|
|
|
|
|
|
|
|
|
|
return "计算机配置:\n" + "\n".join(components)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ComputerBuilder:
|
|
|
|
|
"""计算机建造者,使用流式接口"""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.reset()
|
|
|
|
|
|
|
|
|
|
def reset(self) -> 'ComputerBuilder':
|
|
|
|
|
"""重置构建过程
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
ComputerBuilder: 返回自身以支持链式调用
|
|
|
|
|
"""
|
|
|
|
|
self._computer = Computer()
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
def set_cpu(self, cpu: str) -> 'ComputerBuilder':
|
|
|
|
|
"""设置CPU
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
cpu: CPU型号
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
ComputerBuilder: 返回自身以支持链式调用
|
|
|
|
|
"""
|
|
|
|
|
self._computer.cpu = cpu
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
def set_ram(self, ram: str) -> 'ComputerBuilder':
|
|
|
|
|
"""设置内存
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
ram: 内存容量
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
ComputerBuilder: 返回自身以支持链式调用
|
|
|
|
|
"""
|
|
|
|
|
self._computer.ram = ram
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
def set_storage(self, storage: str) -> 'ComputerBuilder':
|
|
|
|
|
"""设置存储
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
storage: 存储容量和类型
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
ComputerBuilder: 返回自身以支持链式调用
|
|
|
|
|
"""
|
|
|
|
|
self._computer.storage = storage
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
def set_gpu(self, gpu: str) -> 'ComputerBuilder':
|
|
|
|
|
"""设置GPU
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
gpu: GPU型号
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
ComputerBuilder: 返回自身以支持链式调用
|
|
|
|
|
"""
|
|
|
|
|
self._computer.gpu = gpu
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
def set_os(self, os: str) -> 'ComputerBuilder':
|
|
|
|
|
"""设置操作系统
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
os: 操作系统名称
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
ComputerBuilder: 返回自身以支持链式调用
|
|
|
|
|
"""
|
|
|
|
|
self._computer.os = os
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
def build(self) -> Computer:
|
|
|
|
|
"""构建计算机
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Computer: 构建好的计算机实例
|
|
|
|
|
"""
|
|
|
|
|
result = self._computer
|
|
|
|
|
self.reset()
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 预定义的计算机配置
|
|
|
|
|
class ComputerConfig:
|
|
|
|
|
"""计算机配置类,提供预定义的配置方案"""
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def build_basic_computer(builder: ComputerBuilder) -> Computer:
|
|
|
|
|
"""构建基础计算机
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
builder: 计算机建造者
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Computer: 基础配置的计算机
|
|
|
|
|
"""
|
|
|
|
|
return (builder
|
|
|
|
|
.set_cpu("Intel Core i3")
|
|
|
|
|
.set_ram("8GB DDR4")
|
|
|
|
|
.set_storage("256GB SSD")
|
|
|
|
|
.set_os("Windows 11 Home")
|
|
|
|
|
.build())
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def build_gaming_computer(builder: ComputerBuilder) -> Computer:
|
|
|
|
|
"""构建游戏计算机
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
builder: 计算机建造者
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Computer: 游戏配置的计算机
|
|
|
|
|
"""
|
|
|
|
|
return (builder
|
|
|
|
|
.set_cpu("Intel Core i9")
|
|
|
|
|
.set_ram("32GB DDR5")
|
|
|
|
|
.set_storage("2TB NVMe SSD")
|
|
|
|
|
.set_gpu("NVIDIA RTX 4090")
|
|
|
|
|
.set_os("Windows 11 Pro")
|
|
|
|
|
.build())
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def build_workstation(builder: ComputerBuilder) -> Computer:
|
|
|
|
|
"""构建工作站
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
builder: 计算机建造者
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Computer: 工作站配置的计算机
|
|
|
|
|
"""
|
|
|
|
|
return (builder
|
|
|
|
|
.set_cpu("AMD Threadripper Pro")
|
|
|
|
|
.set_ram("128GB DDR5")
|
|
|
|
|
.set_storage("4TB NVMe SSD + 8TB HDD")
|
|
|
|
|
.set_gpu("NVIDIA RTX A6000")
|
|
|
|
|
.set_os("Windows 11 Enterprise")
|
|
|
|
|
.build())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
"""演示建造者模式"""
|
|
|
|
|
print("=== 建造者模式演示 ===")
|
|
|
|
|
|
|
|
|
|
# 基本建造者模式演示
|
|
|
|
|
print("\n1. 基本建造者模式:")
|
|
|
|
|
director = Director()
|
|
|
|
|
builder1 = ConcreteBuilder1()
|
|
|
|
|
builder2 = ConcreteBuilder2()
|
|
|
|
|
|
|
|
|
|
# 使用建造者1
|
|
|
|
|
print("使用ConcreteBuilder1:")
|
|
|
|
|
director.builder = builder1
|
|
|
|
|
director.build_minimal_viable_product()
|
|
|
|
|
print("最小可行产品:")
|
|
|
|
|
print(builder1.product.list_parts())
|
|
|
|
|
|
|
|
|
|
director.build_full_featured_product()
|
|
|
|
|
print("完整功能产品:")
|
|
|
|
|
print(builder1.product.list_parts())
|
|
|
|
|
|
|
|
|
|
# 使用建造者2
|
|
|
|
|
print("\n使用ConcreteBuilder2:")
|
|
|
|
|
director.builder = builder2
|
|
|
|
|
director.build_full_featured_product()
|
|
|
|
|
print("完整功能产品:")
|
|
|
|
|
print(builder2.product.list_parts())
|
|
|
|
|
|
|
|
|
|
# 不使用指挥者,直接使用建造者(客户端自建流程)
|
|
|
|
|
print("\n客户端自建流程:")
|
|
|
|
|
builder = ConcreteBuilder1()
|
|
|
|
|
builder.build_part_a()
|
|
|
|
|
builder.build_part_c()
|
|
|
|
|
print(builder.product.list_parts())
|
|
|
|
|
|
|
|
|
|
# 流式接口风格的建造者模式演示
|
|
|
|
|
print("\n2. 流式接口风格的建造者模式 - 计算机配置:")
|
|
|
|
|
computer_builder = ComputerBuilder()
|
|
|
|
|
|
|
|
|
|
# 自定义配置
|
|
|
|
|
custom_computer = (computer_builder
|
|
|
|
|
.set_cpu("AMD Ryzen 7")
|
|
|
|
|
.set_ram("16GB DDR4")
|
|
|
|
|
.set_storage("1TB SSD")
|
|
|
|
|
.set_gpu("AMD Radeon RX 6800")
|
|
|
|
|
.set_os("Ubuntu 22.04")
|
|
|
|
|
.build())
|
|
|
|
|
print("\n自定义计算机配置:")
|
|
|
|
|
print(custom_computer)
|
|
|
|
|
|
|
|
|
|
# 使用预定义配置
|
|
|
|
|
basic_computer = ComputerConfig.build_basic_computer(computer_builder)
|
|
|
|
|
gaming_computer = ComputerConfig.build_gaming_computer(computer_builder)
|
|
|
|
|
workstation = ComputerConfig.build_workstation(computer_builder)
|
|
|
|
|
|
|
|
|
|
print("\n预定义配置:")
|
|
|
|
|
print("\n基础计算机:")
|
|
|
|
|
print(basic_computer)
|
|
|
|
|
|
|
|
|
|
print("\n游戏计算机:")
|
|
|
|
|
print(gaming_computer)
|
|
|
|
|
|
|
|
|
|
print("\n工作站:")
|
|
|
|
|
print(workstation)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|