通过chatgpt练习设计模式--工厂模式
Easy搞定Golang设计模式:https://www.yuque.com/aceld/lfhu8y/ckdht9
class CPU: def calculate(self): # 实现计算功能 pass class GraphicsCard: def display(self): # 实现显示功能 pass class Memory: def storage(self): # 实现存储功能 pass class Intel: def produce_cpu(self): return CPU() def produce_graphics_card(self): return GraphicsCard() def produce_memory(self): return Memory() class Nvidia: def produce_graphics_card(self): return GraphicsCard() class Kingston: def produce_memory(self): return Memory() class Computer: def __init__(self, cpu, graphics_card, memory): self.cpu = cpu self.graphics_card = graphics_card self.memory = memory # 组装两台电脑 intel_computer = Computer(Intel().produce_cpu(), Intel().produce_graphics_card(), Intel().produce_memory()) nvidia_kingston_computer = Computer(Intel().produce_cpu(), Nvidia().produce_graphics_card(), Kingston().produce_memory())
package main import "fmt" // CPU 类 type CPU struct{} func (c *CPU) calculate() { fmt.Println("CPU is calculating.") } // GraphicsCard 类 type GraphicsCard struct{} func (g *GraphicsCard) display() { fmt.Println("GraphicsCard is displaying.") } // Memory 类 type Memory struct{} func (m *Memory) storage() { fmt.Println("Memory is storing.") } // Intel 厂商 type Intel struct{} func (i *Intel) produceCPU() *CPU { return &CPU{} } func (i *Intel) produceGraphicsCard() *GraphicsCard { return &GraphicsCard{} } func (i *Intel) produceMemory() *Memory { return &Memory{} } // Nvidia 厂商 type Nvidia struct{} func (n *Nvidia) produceGraphicsCard() *GraphicsCard { return &GraphicsCard{} } // Kingston 厂商 type Kingston struct{} func (k *Kingston) produceMemory() *Memory { return &Memory{} } // Computer 类 type Computer struct { cpu *CPU graphicsCard *GraphicsCard memory *Memory } // 组装电脑的方法 func assembleComputer(cpuProducer, graphicsCardProducer, memoryProducer interface{}) *Computer { return &Computer{ cpu: cpuProducer.(*CPU), graphicsCard: graphicsCardProducer.(*GraphicsCard), memory: memoryProducer.(*Memory), } } func main() { // 组装两台电脑 intelComputer := assembleComputer(&Intel{}, &Intel{}, &Intel{}) nvidiaKingstonComputer := assembleComputer(&Intel{}, &Nvidia{}, &Kingston{}) // 测试电脑功能 intelComputer.cpu.calculate() intelComputer.graphicsCard.display() intelComputer.memory.storage() nvidiaKingstonComputer.cpu.calculate() nvidiaKingstonComputer.graphicsCard.display() nvidiaKingstonComputer.memory.storage() }
type SoundCard struct {} func (s *SoundCard) playSound() { fmt.Println("SoundCard is playing sound.") } type NetworkCard struct {} func (n *NetworkCard) connect() { fmt.Println("NetworkCard is connecting to the network.") }
intelSoundCardComputer := assembleComputer(&Intel{}, &Intel{}, &SoundCard{}) nvidiaNetworkCardComputer := assembleComputer(&Intel{}, &Nvidia{}, &NetworkCard{})
package main import "fmt" // Abstract Factory type HardwareFactory interface { ProduceCPU() CPU ProduceGraphicsCard() GraphicsCard ProduceMemory() Memory } // Concrete Factories type IntelFactory struct{} func (i IntelFactory) ProduceCPU() CPU { return CPU{Brand: "Intel"} } func (i IntelFactory) ProduceGraphicsCard() GraphicsCard { return GraphicsCard{Brand: "Intel"} } func (i IntelFactory) ProduceMemory() Memory { return Memory{Brand: "Intel"} } type NvidiaFactory struct{} func (n NvidiaFactory) ProduceGraphicsCard() GraphicsCard { return GraphicsCard{Brand: "Nvidia"} } type KingstonFactory struct{} func (k KingstonFactory) ProduceMemory() Memory { return Memory{Brand: "Kingston"} } // Product type CPU struct { Brand string } type GraphicsCard struct { Brand string } type Memory struct { Brand string } // Director type ComputerDirector struct { Factory HardwareFactory } func (cd ComputerDirector) BuildComputer() Computer { cpu := cd.Factory.ProduceCPU() graphicsCard := cd.Factory.ProduceGraphicsCard() memory := cd.Factory.ProduceMemory() return Computer{ CPU: cpu, GraphicsCard: graphicsCard, Memory: memory, } } // Builder type ComputerBuilder interface { BuildCPU() BuildGraphicsCard() BuildMemory() GetComputer() Computer } // Concrete Builder type IntelComputerBuilder struct { computer Computer } func (b *IntelComputerBuilder) BuildCPU() { b.computer.CPU = CPU{Brand: "Intel"} } func (b *IntelComputerBuilder) BuildGraphicsCard() { b.computer.GraphicsCard = GraphicsCard{Brand: "Intel"} } func (b *IntelComputerBuilder) BuildMemory() { b.computer.Memory = Memory{Brand: "Intel"} } func (b *IntelComputerBuilder) GetComputer() Computer { return b.computer } // Product type Computer struct { CPU CPU GraphicsCard GraphicsCard Memory Memory } func main() { // 使用抽象工厂模式 intelFactory := IntelFactory{} nvidiaFactory := NvidiaFactory{} kingstonFactory := KingstonFactory{} intelComputer := buildComputer(intelFactory) nvidiaKingstonComputer := buildComputer(nvidiaFactory, kingstonFactory) fmt.Println(intelComputer) fmt.Println(nvidiaKingstonComputer) // 使用建造者模式 intelBuilder := &IntelComputerBuilder{} director := ComputerDirector{Factory: intelFactory} director.BuildComputerWithBuilder(intelBuilder) intelComputer2 := intelBuilder.GetComputer() fmt.Println(intelComputer2) } func buildComputer(factories ...HardwareFactory) Computer { computerDirector := ComputerDirector{} for _, factory := range factories { computerDirector.Factory = factory return computerDirector.BuildComputer() } return Computer{} }
package main import "fmt" // Abstract Product type CPU struct { Brand string Model string } // Abstract Factory type HardwareFactory interface { ProduceCPU() CPU ProduceGraphicsCard() GraphicsCard ProduceMemory() Memory } // Concrete Factories type IntelFactory struct{} func (i IntelFactory) ProduceCPU() CPU { return CPU{Brand: "Intel", Model: "Core i7"} // 默认型号 } func (i IntelFactory) ProduceGraphicsCard() GraphicsCard { return GraphicsCard{Brand: "Intel"} } func (i IntelFactory) ProduceMemory() Memory { return Memory{Brand: "Intel"} } type NvidiaFactory struct{} func (n NvidiaFactory) ProduceGraphicsCard() GraphicsCard { return GraphicsCard{Brand: "Nvidia"} } type KingstonFactory struct{} func (k KingstonFactory) ProduceMemory() Memory { return Memory{Brand: "Kingston"} } // Director type ComputerDirector struct { Factory HardwareFactory } func (cd ComputerDirector) BuildComputer() Computer { cpu := cd.Factory.ProduceCPU() graphicsCard := cd.Factory.ProduceGraphicsCard() memory := cd.Factory.ProduceMemory() return Computer{ CPU: cpu, GraphicsCard: graphicsCard, Memory: memory, } } // Builder type ComputerBuilder interface { BuildCPU() BuildGraphicsCard() BuildMemory() GetComputer() Computer } // Concrete Builder type IntelComputerBuilder struct { computer Computer } func (b *IntelComputerBuilder) BuildCPU() { b.computer.CPU = CPU{Brand: "Intel", Model: "Core i7"} // 默认型号 } func (b *IntelComputerBuilder) BuildGraphicsCard() { b.computer.GraphicsCard = GraphicsCard{Brand: "Intel"} } func (b *IntelComputerBuilder) BuildMemory() { b.computer.Memory = Memory{Brand: "Intel"} } func (b *IntelComputerBuilder) GetComputer() Computer { return b.computer } func main() { // 使用抽象工厂模式 intelFactory := IntelFactory{} nvidiaFactory := NvidiaFactory{} kingstonFactory := KingstonFactory{} intelComputer := buildComputer(intelFactory) nvidiaKingstonComputer := buildComputer(nvidiaFactory, kingstonFactory) fmt.Println(intelComputer) fmt.Println(nvidiaKingstonComputer) } func buildComputer(factories ...HardwareFactory) Computer { computerDirector := ComputerDirector{} for _, factory := range factories { computerDirector.Factory = factory return computerDirector.BuildComputer() } return Computer{} }
package main import "fmt" // Abstract Product type CPU struct { Brand string Model string } // Abstract Factory type HardwareFactory interface { ProduceCPU(model string) CPU ProduceGraphicsCard() GraphicsCard ProduceMemory() Memory } // Concrete Factories type IntelFactory struct{} func (i IntelFactory) ProduceCPU(model string) CPU { return CPU{Brand: "Intel", Model: model} } func (i IntelFactory) ProduceGraphicsCard() GraphicsCard { return GraphicsCard{Brand: "Intel"} } func (i IntelFactory) ProduceMemory() Memory { return Memory{Brand: "Intel"} } type NvidiaFactory struct{} func (n NvidiaFactory) ProduceGraphicsCard() GraphicsCard { return GraphicsCard{Brand: "Nvidia"} } type KingstonFactory struct{} func (k KingstonFactory) ProduceMemory() Memory { return Memory{Brand: "Kingston"} } // Director type ComputerDirector struct { Factory HardwareFactory } func (cd ComputerDirector) BuildComputer(cpuModel string) Computer { cpu := cd.Factory.ProduceCPU(cpuModel) graphicsCard := cd.Factory.ProduceGraphicsCard() memory := cd.Factory.ProduceMemory() return Computer{ CPU: cpu, GraphicsCard: graphicsCard, Memory: memory, } } // Builder type ComputerBuilder interface { BuildCPU(model string) BuildGraphicsCard() BuildMemory() GetComputer() Computer } // Concrete Builder type IntelComputerBuilder struct { computer Computer } func (b *IntelComputerBuilder) BuildCPU(model string) { b.computer.CPU = CPU{Brand: "Intel", Model: model} } func (b *IntelComputerBuilder) BuildGraphicsCard() { b.computer.GraphicsCard = GraphicsCard{Brand: "Intel"} } func (b *IntelComputerBuilder) BuildMemory() { b.computer.Memory = Memory{Brand: "Intel"} } func (b *IntelComputerBuilder) GetComputer() Computer { return b.computer } func main() { // 使用抽象工厂模式 intelFactory := IntelFactory{} nvidiaFactory := NvidiaFactory{} kingstonFactory := KingstonFactory{} intelComputer := buildComputer(intelFactory, "Core i7") nvidiaKingstonComputer := buildComputer(nvidiaFactory, kingstonFactory, "Ryzen 5") fmt.Println(intelComputer) fmt.Println(nvidiaKingstonComputer) } func buildComputer(factory HardwareFactory, cpuModel string) Computer { computerDirector := ComputerDirector{Factory: factory} return computerDirector.BuildComputer(cpuModel) }
package main import "fmt" // Abstract Product type CPU struct { Brand string Model string } type GraphicsCard struct { Brand string Model string } type Memory struct { Brand string Model string } // Abstract Factory type HardwareFactory interface { ProduceCPU(model string) CPU ProduceGraphicsCard(model string) GraphicsCard ProduceMemory(model string) Memory } // Concrete Factories type IntelFactory struct{} func (i IntelFactory) ProduceCPU(model string) CPU { return CPU{Brand: "Intel", Model: model} } func (i IntelFactory) ProduceGraphicsCard(model string) GraphicsCard { return GraphicsCard{Brand: "Intel", Model: model} } func (i IntelFactory) ProduceMemory(model string) Memory { return Memory{Brand: "Intel", Model: model} } type NvidiaFactory struct{} func (n NvidiaFactory) ProduceGraphicsCard(model string) GraphicsCard { return GraphicsCard{Brand: "Nvidia", Model: model} } type KingstonFactory struct{} func (k KingstonFactory) ProduceMemory(model string) Memory { return Memory{Brand: "Kingston", Model: model} } // Director type ComputerDirector struct { Factory HardwareFactory } func (cd ComputerDirector) BuildComputer(cpuModel, graphicsCardModel, memoryModel string) Computer { cpu := cd.Factory.ProduceCPU(cpuModel) graphicsCard := cd.Factory.ProduceGraphicsCard(graphicsCardModel) memory := cd.Factory.ProduceMemory(memoryModel) return Computer{ CPU: cpu, GraphicsCard: graphicsCard, Memory: memory, } } // Builder type ComputerBuilder interface { BuildCPU(model string) BuildGraphicsCard(model string) BuildMemory(model string) GetComputer() Computer } // Concrete Builder type IntelComputerBuilder struct { computer Computer } func (b *IntelComputerBuilder) BuildCPU(model string) { b.computer.CPU = CPU{Brand: "Intel", Model: model} } func (b *IntelComputerBuilder) BuildGraphicsCard(model string) { b.computer.GraphicsCard = GraphicsCard{Brand: "Intel", Model: model} } func (b *IntelComputerBuilder) BuildMemory(model string) { b.computer.Memory = Memory{Brand: "Intel", Model: model} } func (b *IntelComputerBuilder) GetComputer() Computer { return b.computer } func main() { // 使用抽象工厂模式 intelFactory := IntelFactory{} nvidiaFactory := NvidiaFactory{} kingstonFactory := KingstonFactory{} intelComputer := buildComputer(intelFactory, "Core i7", "IntelGraphics", "IntelMemory") nvidiaKingstonComputer := buildComputer(nvidiaFactory, "Ryzen 5", "NvidiaGraphics", "KingstonMemory") fmt.Println(intelComputer) fmt.Println(nvidiaKingstonComputer) } func buildComputer(factory HardwareFactory, cpuModel, graphicsCardModel, memoryModel string) Computer { computerDirector := ComputerDirector{Factory: factory} return computerDirector.BuildComputer(cpuModel, graphicsCardModel, memoryModel) }
package main import "fmt" // ... (前面的代码保持不变) // 新增一个接口,用于定义电脑的组装过程 type ComputerAssembler interface { AssembleCPU(cpu CPU) string AssembleGraphicsCard(graphicsCard GraphicsCard) string AssembleMemory(memory Memory) string } // 具体的组装公司A type CompanyAAssembler struct{} func (a CompanyAAssembler) AssembleCPU(cpu CPU) string { return fmt.Sprintf("Assembling CPU %s by Company A", cpu.Model) } func (a CompanyAAssembler) AssembleGraphicsCard(graphicsCard GraphicsCard) string { return fmt.Sprintf("Assembling Graphics Card %s by Company A", graphicsCard.Model) } func (a CompanyAAssembler) AssembleMemory(memory Memory) string { return fmt.Sprintf("Assembling Memory %s by Company A", memory.Model) } // Director 中引入新的接口 type ComputerDirector struct { Factory HardwareFactory Assembler ComputerAssembler // 引入组装接口 } func (cd ComputerDirector) BuildComputer(cpuModel, graphicsCardModel, memoryModel string) Computer { cpu := cd.Factory.ProduceCPU(cpuModel) graphicsCard := cd.Factory.ProduceGraphicsCard(graphicsCardModel) memory := cd.Factory.ProduceMemory(memoryModel) // 使用组装接口完成组装过程 cpuAssembly := cd.Assembler.AssembleCPU(cpu) graphicsCardAssembly := cd.Assembler.AssembleGraphicsCard(graphicsCard) memoryAssembly := cd.Assembler.AssembleMemory(memory) fmt.Println(cpuAssembly) fmt.Println(graphicsCardAssembly) fmt.Println(memoryAssembly) return Computer{ CPU: cpu, GraphicsCard: graphicsCard, Memory: memory, } } func main() { intelFactory := IntelFactory{} companyAAssembler := CompanyAAssembler{} // 新的组装公司 // 使用抽象工厂模式和组装公司A intelComputerByCompanyA := buildComputer(intelFactory, companyAAssembler, "Core i7", "IntelGraphics", "IntelMemory") fmt.Println(intelComputerByCompanyA) } func buildComputer(factory HardwareFactory, assembler ComputerAssembler, cpuModel, graphicsCardModel, memoryModel string) Computer { computerDirector := ComputerDirector{ Factory: factory, Assembler: assembler, } return computerDirector.BuildComputer(cpuModel, graphicsCardModel, memoryModel) }
package main import "fmt" // Abstract Product type CPU struct { Brand string Model string } type GraphicsCard struct { Brand string Model string } type Memory struct { Brand string Model string } // Abstract Factory type HardwareFactory interface { ProduceCPU(model string) CPU ProduceGraphicsCard(model string) GraphicsCard ProduceMemory(model string) Memory } // Concrete Factories type IntelFactory struct{} func (i IntelFactory) ProduceCPU(model string) CPU { return CPU{Brand: "Intel", Model: model} } func (i IntelFactory) ProduceGraphicsCard(model string) GraphicsCard { return GraphicsCard{Brand: "Intel", Model: model} } func (i IntelFactory) ProduceMemory(model string) Memory { return Memory{Brand: "Intel", Model: model} } // 新增一个接口,用于定义电脑的组装过程 type ComputerAssembler interface { AssembleCPU(cpu CPU) string AssembleGraphicsCard(graphicsCard GraphicsCard) string AssembleMemory(memory Memory) string } // 具体的组装公司A type CompanyAAssembler struct{} func (a CompanyAAssembler) AssembleCPU(cpu CPU) string { return fmt.Sprintf("Assembling CPU %s by Company A", cpu.Model) } func (a CompanyAAssembler) AssembleGraphicsCard(graphicsCard GraphicsCard) string { return fmt.Sprintf("Assembling Graphics Card %s by Company A", graphicsCard.Model) } func (a CompanyAAssembler) AssembleMemory(memory Memory) string { return fmt.Sprintf("Assembling Memory %s by Company A", memory.Model) } // Director 中引入新的接口 type ComputerDirector struct { Factory HardwareFactory Assembler ComputerAssembler // 引入组装接口 } func (cd ComputerDirector) BuildComputer(cpuModel, graphicsCardModel, memoryModel string) Computer { cpu := cd.Factory.ProduceCPU(cpuModel) graphicsCard := cd.Factory.ProduceGraphicsCard(graphicsCardModel) memory := cd.Factory.ProduceMemory(memoryModel) // 使用组装接口完成组装过程 cpuAssembly := cd.Assembler.AssembleCPU(cpu) graphicsCardAssembly := cd.Assembler.AssembleGraphicsCard(graphicsCard) memoryAssembly := cd.Assembler.AssembleMemory(memory) fmt.Println(cpuAssembly) fmt.Println(graphicsCardAssembly) fmt.Println(memoryAssembly) return Computer{ CPU: cpu, GraphicsCard: graphicsCard, Memory: memory, } } // Builder type ComputerBuilder interface { BuildCPU(model string) BuildGraphicsCard(model string) BuildMemory(model string) GetComputer() Computer } // Concrete Builder type IntelComputerBuilder struct { computer Computer } func (b *IntelComputerBuilder) BuildCPU(model string) { b.computer.CPU = CPU{Brand: "Intel", Model: model} } func (b *IntelComputerBuilder) BuildGraphicsCard(model string) { b.computer.GraphicsCard = GraphicsCard{Brand: "Intel", Model: model} } func (b *IntelComputerBuilder) BuildMemory(model string) { b.computer.Memory = Memory{Brand: "Intel", Model: model} } func (b *IntelComputerBuilder) GetComputer() Computer { return b.computer } // Product type Computer struct { CPU CPU GraphicsCard GraphicsCard Memory Memory } func main() { intelFactory := IntelFactory{} companyAAssembler := CompanyAAssembler{} // 新的组装公司 // 使用抽象工厂模式和组装公司A intelComputerByCompanyA := buildComputer(intelFactory, companyAAssembler, "Core i7", "IntelGraphics", "IntelMemory") fmt.Println(intelComputerByCompanyA) } func buildComputer(factory HardwareFactory, assembler ComputerAssembler, cpuModel, graphicsCardModel, memoryModel string) Computer { computerDirector := ComputerDirector{ Factory: factory, Assembler: assembler, } return computerDirector.BuildComputer(cpuModel, graphicsCardModel, memoryModel) }
package main import "fmt" // 新增声卡相关的抽象产品 type SoundCard struct { Brand string Model string } // 新增声卡相关的抽象工厂接口 type HardwareFactory interface { ProduceCPU(model string) CPU ProduceGraphicsCard(model string) GraphicsCard ProduceMemory(model string) Memory ProduceSoundCard(model string) SoundCard // 新增生产声卡的方法 } // 具体工厂中实现声卡的生产逻辑 type IntelFactory struct{} func (i IntelFactory) ProduceCPU(model string) CPU { return CPU{Brand: "Intel", Model: model} } func (i IntelFactory) ProduceGraphicsCard(model string) GraphicsCard { return GraphicsCard{Brand: "Intel", Model: model} } func (i IntelFactory) ProduceMemory(model string) Memory { return Memory{Brand: "Intel", Model: model} } func (i IntelFactory) ProduceSoundCard(model string) SoundCard { return SoundCard{Brand: "Intel", Model: model} } // 修改组装接口,添加声卡的组装逻辑 type ComputerAssembler interface { AssembleCPU(cpu CPU) string AssembleGraphicsCard(graphicsCard GraphicsCard) string AssembleMemory(memory Memory) string AssembleSoundCard(soundCard SoundCard) string // 新增组装声卡的方法 } // 具体组装公司中实现声卡的组装逻辑 type CompanyAAssembler struct{} func (a CompanyAAssembler) AssembleCPU(cpu CPU) string { return fmt.Sprintf("Assembling CPU %s by Company A", cpu.Model) } func (a CompanyAAssembler) AssembleGraphicsCard(graphicsCard GraphicsCard) string { return fmt.Sprintf("Assembling Graphics Card %s by Company A", graphicsCard.Model) } func (a CompanyAAssembler) AssembleMemory(memory Memory) string { return fmt.Sprintf("Assembling Memory %s by Company A", memory.Model) } func (a CompanyAAssembler) AssembleSoundCard(soundCard SoundCard) string { return fmt.Sprintf("Assembling Sound Card %s by Company A", soundCard.Model) } // 修改 Director,添加声卡的构建逻辑 type ComputerDirector struct { Factory HardwareFactory Assembler ComputerAssembler } func (cd ComputerDirector) BuildComputer(cpuModel, graphicsCardModel, memoryModel, soundCardModel string) Computer { cpu := cd.Factory.ProduceCPU(cpuModel) graphicsCard := cd.Factory.ProduceGraphicsCard(graphicsCardModel) memory := cd.Factory.ProduceMemory(memoryModel) soundCard := cd.Factory.ProduceSoundCard(soundCardModel) // 新增构建声卡的逻辑 cpuAssembly := cd.Assembler.AssembleCPU(cpu) graphicsCardAssembly := cd.Assembler.AssembleGraphicsCard(graphicsCard) memoryAssembly := cd.Assembler.AssembleMemory(memory) soundCardAssembly := cd.Assembler.AssembleSoundCard(soundCard) // 新增组装声卡的逻辑 fmt.Println(cpuAssembly) fmt.Println(graphicsCardAssembly) fmt.Println(memoryAssembly) fmt.Println(soundCardAssembly) return Computer{ CPU: cpu, GraphicsCard: graphicsCard, Memory: memory, SoundCard: soundCard, // 新增声卡属性 } } // ... (其他代码保持不变)
package main import ( "encoding/json" "fmt" "io/ioutil" "reflect" ) // HardwareConfig 定义硬件配置 type HardwareConfig struct { Type string `json:"type"` Model string `json:"model"` } // Config 定义整体配置 type Config struct { HardwareConfigs []HardwareConfig `json:"hardwareConfigs"` } // HardwareProducer 定义硬件生产者接口 type HardwareProducer interface { Produce(model string) interface{} } // HardwareAssembler 定义硬件组装者接口 type HardwareAssembler interface { Assemble(hardware interface{}) string } // ComputerDirector 构建电脑的 Director type ComputerDirector struct { Producer HardwareProducer Assembler HardwareAssembler } // BuildComputer 根据配置构建电脑 func (cd ComputerDirector) BuildComputer(config HardwareConfig) interface{} { hardware := cd.Producer.Produce(config.Model) assemblyResult := cd.Assembler.Assemble(hardware) fmt.Println(assemblyResult) return hardware } // ConcreteHardwareProducer 具体的硬件生产者 type ConcreteHardwareProducer struct { Brand string } func (p ConcreteHardwareProducer) Produce(model string) interface{} { switch p.Brand { case "Intel": return CPU{Brand: "Intel", Model: model} case "Nvidia": return GraphicsCard{Brand: "Nvidia", Model: model} // 可以根据需要添加其他硬件类型的处理逻辑 default: return nil } } // ConcreteHardwareAssembler 具体的硬件组装者 type ConcreteHardwareAssembler struct { Brand string } func (a ConcreteHardwareAssembler) Assemble(hardware interface{}) string { switch a.Brand { case "CompanyA": switch h := hardware.(type) { case CPU: return fmt.Sprintf("Assembling CPU %s by Company A", h.Model) case GraphicsCard: return fmt.Sprintf("Assembling Graphics Card %s by Company A", h.Model) // 可以根据需要添加其他硬件类型的处理逻辑 default: return "Unknown hardware type" } // 可以根据需要添加其他组装公司的处理逻辑 default: return "Unknown brand" } } func main() { // 读取配置文件 configData, err := ioutil.ReadFile("hardware_config.json") if err != nil { fmt.Println("Error reading configuration file:", err) return } // 解析配置文件 var config Config if err := json.Unmarshal(configData, &config); err != nil { fmt.Println("Error parsing configuration:", err) return } // 创建硬件生产者和组装者 producer := ConcreteHardwareProducer{Brand: "Intel"} assembler := ConcreteHardwareAssembler{Brand: "CompanyA"} // 创建 Director director := ComputerDirector{Producer: producer, Assembler: assembler} // 根据配置文件构建电脑 for _, hardwareConfig := range config.HardwareConfigs { director.BuildComputer(hardwareConfig) } }
package main import ( "encoding/json" "fmt" "io/ioutil" ) // HardwareConfig 定义硬件配置 type HardwareConfig struct { Type string `json:"type"` Model string `json:"model"` } // Config 定义整体配置 type Config struct { HardwareConfigs []HardwareConfig `json:"hardwareConfigs"` } // HardwareProducer 定义硬件生产者接口 type HardwareProducer interface { Produce(model string) interface{} } // HardwareAssembler 定义硬件组装者接口 type HardwareAssembler interface { Assemble(hardware interface{}) string } // ComputerDirector 构建电脑的 Director type ComputerDirector struct { Producer HardwareProducer Assembler HardwareAssembler } // BuildComputer 根据配置构建电脑 func (cd ComputerDirector) BuildComputer(config HardwareConfig) interface{} { hardware := cd.Producer.Produce(config.Model) assemblyResult := cd.Assembler.Assemble(hardware) fmt.Println(assemblyResult) return hardware } // ConcreteHardwareProducer 具体的硬件生产者 type ConcreteHardwareProducer struct { Brand string } func (p ConcreteHardwareProducer) Produce(model string) interface{} { switch p.Brand { case "Intel": return CPU{Brand: "Intel", Model: model} case "Nvidia": return GraphicsCard{Brand: "Nvidia", Model: model} // 可以根据需要添加其他硬件类型的处理逻辑 default: return nil } } // ConcreteHardwareAssembler 具体的硬件组装者 type ConcreteHardwareAssembler struct { Brand string } func (a ConcreteHardwareAssembler) Assemble(hardware interface{}) string { switch a.Brand { case "CompanyA": switch h := hardware.(type) { case CPU: return fmt.Sprintf("Assembling CPU %s by Company A", h.Model) case GraphicsCard: return fmt.Sprintf("Assembling Graphics Card %s by Company A", h.Model) // 可以根据需要添加其他硬件类型的处理逻辑 default: return "Unknown hardware type" } // 可以根据需要添加其他组装公司的处理逻辑 default: return "Unknown brand" } } func main() { // 读取配置文件 configData, err := ioutil.ReadFile("hardware_config.json") if err != nil { fmt.Println("Error reading configuration file:", err) return } // 解析配置文件 var config Config if err := json.Unmarshal(configData, &config); err != nil { fmt.Println("Error parsing configuration:", err) return } // 创建硬件生产者和组装者 producer := ConcreteHardwareProducer{Brand: "Intel"} assembler := ConcreteHardwareAssembler{Brand: "CompanyA"} // 创建 Director director := ComputerDirector{Producer: producer, Assembler: assembler} // 根据配置文件构建电脑 for _, hardwareConfig := range config.HardwareConfigs { director.BuildComputer(hardwareConfig) } }
package main import ( "encoding/json" "fmt" "io/ioutil" ) // Abstract Product type CPU struct { Brand string Model string } type GraphicsCard struct { Brand string Model string } type Memory struct { Brand string Model string } // Abstract Factory type HardwareFactory interface { ProduceCPU(brand, model string) CPU ProduceGraphicsCard(brand, model string) GraphicsCard ProduceMemory(brand, model string) Memory } // Concrete Factories type IntelFactory struct{} func (i IntelFactory) ProduceCPU(brand, model string) CPU { return CPU{Brand: brand, Model: model} } func (i IntelFactory) ProduceGraphicsCard(brand, model string) GraphicsCard { return GraphicsCard{Brand: brand, Model: model} } func (i IntelFactory) ProduceMemory(brand, model string) Memory { return Memory{Brand: brand, Model: model} } // 新增一个接口,用于定义电脑的组装过程 type ComputerAssembler interface { AssembleCPU(cpu CPU) string AssembleGraphicsCard(graphicsCard GraphicsCard) string AssembleMemory(memory Memory) string } // 具体的组装公司A type CompanyAAssembler struct{} func (a CompanyAAssembler) AssembleCPU(cpu CPU) string { return fmt.Sprintf("Assembling CPU %s by Company A", cpu.Model) } func (a CompanyAAssembler) AssembleGraphicsCard(graphicsCard GraphicsCard) string { return fmt.Sprintf("Assembling Graphics Card %s by Company A", graphicsCard.Model) } func (a CompanyAAssembler) AssembleMemory(memory Memory) string { return fmt.Sprintf("Assembling Memory %s by Company A", memory.Model) } // Director 中引入新的接口 type ComputerDirector struct { Factory HardwareFactory Assembler ComputerAssembler // 引入组装接口 } func (cd ComputerDirector) BuildComputer(brand, cpuModel, graphicsCardModel, memoryModel string) (Computer, error) { cpu := cd.Factory.ProduceCPU(brand, cpuModel) graphicsCard := cd.Factory.ProduceGraphicsCard(brand, graphicsCardModel) memory := cd.Factory.ProduceMemory(brand, memoryModel) // 使用组装接口完成组装过程 cpuAssembly := cd.Assembler.AssembleCPU(cpu) graphicsCardAssembly := cd.Assembler.AssembleGraphicsCard(graphicsCard) memoryAssembly := cd.Assembler.AssembleMemory(memory) fmt.Println(cpuAssembly) fmt.Println(graphicsCardAssembly) fmt.Println(memoryAssembly) return Computer{ CPU: cpu, GraphicsCard: graphicsCard, Memory: memory, }, nil } // Builder type ComputerBuilder interface { BuildCPU(model string) BuildGraphicsCard(model string) BuildMemory(model string) GetComputer() Computer } // Concrete Builder type IntelComputerBuilder struct { computer Computer } func (b *IntelComputerBuilder) BuildCPU(model string) { b.computer.CPU = CPU{Brand: "Intel", Model: model} } func (b *IntelComputerBuilder) BuildGraphicsCard(model string) { b.computer.GraphicsCard = GraphicsCard{Brand: "Intel", Model: model} } func (b *IntelComputerBuilder) BuildMemory(model string) { b.computer.Memory = Memory{Brand: "Intel", Model: model} } func (b *IntelComputerBuilder) GetComputer() Computer { return b.computer } // Product type Computer struct { CPU CPU GraphicsCard GraphicsCard Memory Memory } func main() { intelFactory := IntelFactory{} companyAAssembler := CompanyAAssembler{} // 新的组装公司 // 使用抽象工厂模式和组装公司A intelComputerByCompanyA, err := buildComputer(intelFactory, companyAAssembler, "Intel", "Core i7", "IntelGraphics", "IntelMemory") if err != nil { fmt.Println("Error building computer:", err) return } fmt.Println(intelComputerByCompanyA) } func buildComputer(factory HardwareFactory, assembler ComputerAssembler, brand, cpuModel, graphicsCardModel, memoryModel string) (Computer, error) { computerDirector := ComputerDirector{ Factory: factory, Assembler: assembler, } return computerDirector.BuildComputer(brand, cpuModel, graphicsCardModel, memoryModel) }
算法基本型 文章被收录于专栏
算法基本型感悟