12-抽象工廠方法模式(Easy搞定Golang設(shè)計(jì)模式)

package main
import "fmt"
//-----抽象層-----
type CPU interface { //cpu生產(chǎn)類接口
? ? Calculate()
}
type MEM interface { //內(nèi)存生產(chǎn)類接口
? ? Storage()
}
type CARD interface { //顯卡生產(chǎn)類接口
? ? DisPlay()
}
type Producer interface { //總生產(chǎn)接口(抽象工廠)
? ? CreatCPU()
? ? CreatMemory()
? ? CreatCard()
}
//-----實(shí)現(xiàn)層------
type CpuInter struct{}
type CpuNvdia struct{}
type CpuKing struct{}
func (cpu *CpuInter) Calculate() {
? ? fmt.Println("CpuInter")
}
func (cpu *CpuNvdia) Calculate() {
? ? fmt.Println("CpuNvdia")
}
func (cpu *CpuKing) Calculate() {
? ? fmt.Println("CpuKing")
}
type MemInter struct{}
type MemNvdia struct{}
type MemKing struct{}
func (mem *MemInter) Storage() {
? ? fmt.Println("MemInter")
}
func (mem *MemNvdia) Storage() {
? ? fmt.Println("MemNvdia")
}
func (mem *MemKing) Storage() {
? ? fmt.Println("MemKing")
}
type CardInter struct{}
type CardNvdia struct{}
type CardKing struct{}
func (card *CardInter) DisPlay() {
? ? fmt.Println("CardInter")
}
func (card *CardNvdia) DisPlay() {
? ? fmt.Println("CardNvdia")
}
func (card *CardKing) DisPlay() {
? ? fmt.Println("CardKing")
}
type Inter struct{}
type Nvidia struct{}
type King struct{}
func (abs *Inter) CreatCPU() CPU {
? ? cpu := new(CpuInter)
? ? return cpu
}
func (abs *Nvidia) CreatCPU() CPU {
? ? cpu := new(CpuNvdia)
? ? return cpu
}
func (abs *King) CreatCPU() CPU {
? ? cpu := new(CpuKing)
? ? return cpu
}
func (abs *Inter) CreatMemory() MEM {
? ? mem := new(MemInter)
? ? return mem
}
func (abs *Nvidia) CreatMemory() MEM {
? ? mem := new(MemNvdia)
? ? return mem
}
func (abs *King) CreatMemory() MEM {
? ? mem := new(MemKing)
? ? return mem
}
func (abs *Inter) CreatCard() CARD {
? ? card := new(CardInter)
? ? return card
}
func (abs *Nvidia) CreatCard() CARD {
? ? card := new(CardNvdia)
? ? return card
}
func (abs *King) CreatCard() CARD {
? ? card := new(CardKing)
? ? return card
}
//------邏輯層------
func main() {
? ? inter := new(Inter)
? ? cpu := inter.CreatCPU()
? ? cpu.Calculate()
? ? king := King{}
? ? mem := king.CreatMemory()
? ? mem.Storage()
}