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

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

2023-04-07 17:14 作者:轻风莫染尘  | 我要投稿

package main


import "fmt"


//-----抽象层-----

type CPU interface { //cpu生产类接口

    Calculate()

}

type MEM interface { //内存生产类接口

    Storage()

}

type CARD interface { //显卡生产类接口

    DisPlay()

}

type Producer interface { //总生产接口(抽象工厂)

    CreatCPU()

    CreatMemory()

    CreatCard()

}


//-----实现层------


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()

}


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

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