欢迎光临散文网 会员登陆 & 注册

12-抽象工厂方法模式(Easy搞定Golang设计模式)

2023-06-18 22:37 作者:吃馒头啊啊啊  | 我要投稿

package main


import "fmt"


type CPU interface {

Calculate()

}


type GPU interface {

Display()

}


type Memory interface {

Storage()

}


type VendorFactory interface {

VendorCPU()

VendorGPU()

VendorMemory()

}


type IntelCPU struct {

}


func (ic *IntelCPU) Calculate() {

fmt.Println("Intel Cpu Calculating...")

}


type IntelGPU struct {

}


func (ig *IntelGPU) Display() {

fmt.Println("Intel Gpu Displaying...")

}


type IntelMemory struct {

}


func (im *IntelMemory) Storage() {

fmt.Println("Intel Memory Storage...")

}


type IntelFactory struct {

}


func (ifac *IntelFactory) VendorCPU() CPU {

return &IntelCPU{}

}


func (ifac *IntelFactory) VendorGPU() GPU {

return &IntelGPU{}

}


func (ifac *IntelFactory) VendorMemory() Memory {

return &IntelMemory{}

}


type NvidiaCPU struct {

}


func (nc *NvidiaCPU) Calculate() {

fmt.Println("Nvidia Cpu Calculating...")

}


type NvidiaGPU struct {

}


func (nv *NvidiaGPU) Display() {

fmt.Println("Nvidia Gpu Displaying...")

}


type NvidiaMemory struct {

}


func (im *NvidiaMemory) Storage() {

fmt.Println("Nvidia Memory Storage...")

}


type NvidiaFactory struct {

}


func (ifac *NvidiaFactory) VendorCPU() CPU {

return &NvidiaCPU{}

}


func (ifac *NvidiaFactory) VendorGPU() GPU {

return &NvidiaGPU{}

}


func (ifac *NvidiaFactory) VendorMemory() Memory {

return &NvidiaMemory{}

}


type KingstonCPU struct {

}


func (ic *KingstonCPU) Calculate() {

fmt.Println("Kingston Cpu Calculating...")

}


type KingstonGPU struct {

}


func (ig *KingstonGPU) Display() {

fmt.Println("Kingston Gpu Displaying...")

}


type KingstonMemory struct {

}


func (im *KingstonMemory) Storage() {

fmt.Println("Kingston Memory Storage...")

}


type KingstonFactory struct {

}


func (ifac *KingstonFactory) VendorCPU() CPU {

return &IntelCPU{}

}


func (ifac *KingstonFactory) VendorGPU() GPU {

return &IntelGPU{}

}


func (ifac *KingstonFactory) VendorMemory() Memory {

return &IntelMemory{}

}


func main() {


intel := IntelFactory{}

nvidia := NvidiaFactory{}

kingston := KingstonFactory{}


fmt.Println("Install first pc:")

intel.VendorCPU().Calculate()

intel.VendorGPU().Display()

intel.VendorMemory().Storage()


fmt.Println("Install second pc:")

intel.VendorCPU().Calculate()

nvidia.VendorGPU().Display()

kingston.VendorMemory().Storage()

}

12-抽象工厂方法模式(Easy搞定Golang设计模式)的评论 (共 条)

分享到微博请遵守国家法律