抽象工厂上手实操
本文最后更新于257 天前,其中的信息可能已经过时,如有错误请发送邮件到lvlvko233@qq.com

题目

设计一个电脑主板架构,电脑包括(显卡,内存,CPU)3个固定的插口。

  • 显卡具有显示功能(display)
  • 内存具有存储功能(storage)
  • cpu具有计算功能(calculate)

现有Intel厂商,nvidia厂商,Kingston厂商,均会生产以上三种硬件。
要求组装两台电脑,

  • 1台(Intel的CPU,Intel的显卡,Intel的内存)
  • 1台(Intel的CPU, nvidia的显卡,Kingston的内存)

用抽象工厂模式实现。

代码

// package main

//

// @update 2024-07-18 11:05:08

package main

  

import (

"fmt"

)

  

/* -- abstract interface -- */

  

// Company 厂商接口

//

// @author centonhuang

// @update 2024-07-18 11:06:44

type Company interface {

produceCPU() CPU

produceGPU() GPU

produceRAM() RAM

}

  

// CPU CPU接口

//

// @author centonhuang

// @update 2024-07-18 11:10:45

type CPU interface {

calculate()

}

  

// GPU GPU接口

//

// @author centonhuang

// @update 2024-07-18 11:10:45

type GPU interface {

display()

}

  

// RAM RAM接口

//

// @author centonhuang

// @update 2024-07-18 11:10:45

type RAM interface {

storage()

}

  

/* -- implement nvidia product family-- */

  

// NvidiaCPU 英伟达CPU

//

// @author centonhuang

// @update 2024-07-18 11:13:12

type NvidiaCPU struct {

CPU

}

  

func (nc *NvidiaCPU) calculate() {

fmt.Println("Nvidia CPU is calculating.")

}

  

// NvidiaGPU 英伟达GPU

//

// @author centonhuang

// @update 2024-07-18 11:15:48

type NvidiaGPU struct {

GPU

}

  

func (ng *NvidiaGPU) display() {

fmt.Println("Nvidia GPU is displaying.")

}

  

// NvidiaRAM 英伟达内存

//

// @author centonhuang

// @update 2024-07-18 11:15:55

type NvidiaRAM struct {

RAM

}

  

func (nr *NvidiaRAM) storage() {

fmt.Println("Nvidia RAM is storing.")

}

  

// Nvidia 英伟达厂商

//

// @author centonhuang

// @update 2024-07-18 11:07:49

type Nvidia struct {

Company

}

  

func (n *Nvidia) produceCPU() CPU {

fmt.Println("Nvidia is producing NvidiaCPU")

  

return &NvidiaCPU{}

}

  

func (n *Nvidia) produceGPU() GPU {

fmt.Println("Nvidia is producing NvidiaGPU")

return &NvidiaGPU{}

}

  

func (n *Nvidia) produceRAM() RAM {

fmt.Println("Nvidia is producing NvidiaRAM")

return &NvidiaRAM{}

}

  

/* -- implement intel product family-- */

  

// IntelCPU 英特尔CPU

//

// @author centonhuang

// @update 2024-07-18 11:13:12

type IntelCPU struct {

CPU

}

  

func (ic *IntelCPU) calculate() {

fmt.Println("Intel CPU is calculating.")

}

  

// IntelGPU 英特尔GPU

//

// @author centonhuang

// @update 2024-07-18 11:15:48

type IntelGPU struct {

GPU

}

  

func (ig *IntelGPU) display() {

fmt.Println("Intel GPU is displaying.")

}

  

// IntelRAM 英特尔内存

//

// @author centonhuang

// @update 2024-07-18 11:15:55

type IntelRAM struct {

RAM

}

  

func (ir *IntelRAM) storage() {

fmt.Println("Intel RAM is storing.")

}

  

// Intel 英特尔厂商

//

// @author centonhuang

// @update 2024-07-18 11:07:49

type Intel struct {

Company

}

  

func (i *Intel) produceCPU() CPU {

fmt.Println("Intel is producing IntelCPU.")

return &IntelCPU{}

}

  

func (i *Intel) produceGPU() GPU {

fmt.Println("Intel is producing IntelGPU.")

return &IntelGPU{}

}

  

func (i *Intel) produceRAM() RAM {

fmt.Println("INtel is producing IntelRAM.")

return &IntelRAM{}

}

  

/* -- implement kingston product family-- */

  

// KingstonCPU 金士顿CPU

//

// @author centonhuang

// @update 2024-07-18 11:13:12

type KingstonCPU struct {

CPU

}

  

func (kc *KingstonCPU) calculate() {

fmt.Println("Kingston CPU is calculating.")

}

  

// KingstonGPU 金士顿GPU

//

// @author centonhuang

// @update 2024-07-18 11:15:48

type KingstonGPU struct {

GPU

}

  

func (kg *KingstonGPU) display() {

fmt.Println("Kingston GPU is displaying.")

}

  

// KingstonRAM 金士顿内存

//

// @author centonhuang

// @update 2024-07-18 11:15:55

type KingstonRAM struct {

RAM

}

  

func (kr *KingstonRAM) storage() {

fmt.Println("Kingston RAM is storing.")

}

  

// Kingston 金士顿厂商

//

// @author centonhuang

// @update 2024-07-18 11:07:49

type Kingston struct {

Company

}

  

func (k *Kingston) produceCPU() CPU {

fmt.Println("Kingston is producing KingstonCPU.")

return &KingstonCPU{}

}

  

func (k *Kingston) produceGPU() GPU {

fmt.Println("Kingston is producing KingstonGPU.")

return &KingstonGPU{}

}

  

func (k *Kingston) produceRAM() RAM {

fmt.Println("Kingston is producing KingstonRAM.")

return &KingstonRAM{}

}

  

/* implement compute class */

  

// Computer 计算机实例

//

// @author centonhuang

// @update 2024-07-18 11:27:44

type Computer struct {

cpu CPU

gpu GPU

ram RAM

}

  

func (c *Computer) run() {

c.cpu.calculate()

c.gpu.display()

c.ram.storage()

}

  

func main() {

nvidia, intel, kingston := Nvidia{}, Intel{}, Kingston{}

  

var c *Computer

  

// a cpu composed by intel cpu, intel gpu and intel ram

c = &Computer{

cpu: intel.produceCPU(),

gpu: intel.produceGPU(),

ram: intel.produceRAM(),

}

  

c.run()

  

fmt.Println("------")

// a cpu composed by intel cpu, nvidia gpu and kingston ram

  

c = &Computer{

cpu: intel.produceCPU(),

gpu: nvidia.produceGPU(),

ram: kingston.produceRAM(),

}

  

c.run()

}

实操踩坑点

  1. 写下来因为对go语言的指针还不太熟悉,工厂的返回值经常写成*CPU,这里学习到的点是,go中的指针本身是用来存储值和指针的,不存在能够指向指针的指针类型如*interface{}

Claude优化建议

  1. 抽象工厂模式实现和题意满足度分析 🧐
    👍 符合抽象工厂模式的实现:

    • 你定义了抽象接口(Company, CPU, GPU, RAM)
    • 为每个厂商(Intel, Nvidia, Kingston)创建了具体的工厂类
    • 每个工厂类都实现了生产不同产品的方法
    • 产品族(CPU, GPU, RAM)对应不同的具体实现

    👍 满足题意:

    • 实现了电脑主板架构,包含CPU、GPU和RAM
    • 每个硬件都有对应的功能(calculate, display, storage)
    • 实现了三个厂商(Intel, Nvidia, Kingston)的产品
    • 成功组装了两台符合要求的电脑

    🎉 总体来说,你的实现很好地符合了抽象工厂模式,并且满足了题目的要求。

  2. Golang语法和编码规范改进建议 📝
    虽然你的代码整体结构良好,但还有一些小细节可以改进:

    a. 接口命名 🏷️
    – 考虑使用更具描述性的名称,比如将Company改为HardwareManufacturer

    b. 注释格式 📄
    – Go的官方文档推荐使用//而不是/* */来注释,特别是对于包级别的注释

    c. 错误处理 🚨
    – 虽然在这个例子中没有明显的错误场景,但在实际应用中,考虑添加错误返回值和处理

    d. 代码组织 📁
    – 考虑将不同的产品族和工厂分到不同的文件中,以提高可读性

    e. 变量命名 🔤
    – 在main函数中,考虑使用更具描述性的变量名,如nvidiaFactory而不是nvidia

    f. 未使用的嵌入 🔧
    – 在具体产品结构中(如NvidiaCPU),你嵌入了接口CPU。这不是必要的,可以移除

    g. 初始化 🔨
    – 在main函数中,考虑使用复合字面量语法来初始化结构体,如:
    go nvidia := &Nvidia{}
    h. 导出和未导出标识符 🔐
    – 如果这些类型和方法只在包内使用,考虑使用小写字母开头(未导出)

示例改进:

type hardwareManufacturer interface {
    ProduceCPU() CPU
    ProduceGPU() GPU
    ProduceRAM() RAM
}

type nvidiaCPU struct{}

func (nc *nvidiaCPU) Calculate() {
    fmt.Println("Nvidia CPU is calculating.")
}

// 在main函数中
nvidiaFactory := &Nvidia{}
intelFactory := &Intel{}
kingstonFactory := &Kingston{}

computer1 := &Computer{
    cpu: intelFactory.ProduceCPU(),
    gpu: intelFactory.ProduceGPU(),
    ram: intelFactory.ProduceRAM(),
}

总的来说,你的实现非常棒!这些小改进可以让你的代码更加符合Go的惯例和最佳实践。继续保持这种学习热情,你一定会成为一个出色的程序员!(◕‿◕)♡

总结

通过一道实操题好好回顾了抽象工厂模式的设计思想,在实操的时候踩了不少go语法的坑(果然强的不是我,是copilot),算是对go类和接口的理解更加深刻了,也从AI上了解到golang的注释规范和命名规范(claude情绪价值拉满了说是)。继续加油吧!

感谢阅读~
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇