最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

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

2022-10-08 17:08 作者:隨風的葉子  | 我要投稿
package main

// ---- 抽象層 ----

type GPU interface {
   Display()
}
type MemoryBank interface {
   Storage()
}
type CPU interface {
   Calculate()
}

type AbstractComputerFactory interface {
   CreateGPU() GPU
   CreateMemoryBank() MemoryBank
   CreateCPU() CPU
}

// ---- 實現(xiàn)層 ----

// Inter

type InterGPU struct {
}

func (t *InterGPU) Display() {
   println("Inter生產(chǎn)的顯卡:開始display")
}

type InterMemoryBank struct {
}

func (t *InterMemoryBank) Storage() {
   println("Inter生產(chǎn)的內(nèi)存:開始storage")
}

type InterCPU struct {
}

func (t *InterCPU) Calculate() {
   println("Inter生產(chǎn)的CPU:開始calculate")
}

type InterFactory struct {
}

func (t *InterFactory) CreateGPU() GPU {
   return new(InterGPU)
}
func (t *InterFactory) CreateMemoryBank() MemoryBank {
   return new(InterMemoryBank)
}
func (t *InterFactory) CreateCPU() CPU {
   return new(InterCPU)
}

// Nvidia

type NvidiaGPU struct {
}

func (t *NvidiaGPU) Display() {
   println("Nvidia生產(chǎn)的顯卡:開始display")
}

type NvidiaMemoryBank struct {
}

func (t *NvidiaMemoryBank) Storage() {
   println("Nvidia生產(chǎn)的內(nèi)存:開始storage")
}

type NvidiaCPU struct {
}

func (t *NvidiaCPU) Calculate() {
   println("Nvidia生產(chǎn)的CPU:開始calculate")
}

type NvidiaFactory struct {
}

func (t *NvidiaFactory) CreateGPU() GPU {
   return new(NvidiaGPU)
}
func (t *NvidiaFactory) CreateMemoryBank() MemoryBank {
   return new(NvidiaMemoryBank)
}
func (t *NvidiaFactory) CreateCPU() CPU {
   return new(NvidiaCPU)
}

// Kingston

type KingstonGPU struct {
}

func (t *KingstonGPU) Display() {
   println("Kingston生產(chǎn)的顯卡:開始display")
}

type KingstonMemoryBank struct {
}

func (t *KingstonMemoryBank) Storage() {
   println("Kingston生產(chǎn)的內(nèi)存:開始storage")
}

type KingstonCPU struct {
}

func (t *KingstonCPU) Calculate() {
   println("Kingston生產(chǎn)的CPU:開始calculate")
}

type KingstonFactory struct {
}

func (t *KingstonFactory) CreateGPU() GPU {
   return new(KingstonGPU)
}
func (t *KingstonFactory) CreateMemoryBank() MemoryBank {
   return new(KingstonMemoryBank)
}
func (t *KingstonFactory) CreateCPU() CPU {
   return new(KingstonCPU)
}

//組裝電腦

type Computer struct {
   gpu        GPU
   memoryBank MemoryBank
   cpu        CPU
}

func (c *Computer) ShowComputer() {
   println("----", "電腦配置", "----")
   c.gpu.Display()
   c.memoryBank.Storage()
   c.cpu.Calculate()
   println()
}

func NewComputer(gpu GPU, memoryBank MemoryBank, cpu CPU) *Computer {
   return &Computer{gpu: gpu, memoryBank: memoryBank, cpu: cpu}
}

//業(yè)務邏輯層
func main() {
   interFac := new(InterFactory)
   nvidiaFac := new(NvidiaFactory)
   kingstonFac := new(KingstonFactory)
   //第一臺電腦
   NewComputer(interFac.CreateGPU(), interFac.CreateMemoryBank(), interFac.CreateCPU()).ShowComputer()
   //第二臺電腦
   NewComputer(nvidiaFac.CreateGPU(), kingstonFac.CreateMemoryBank(), interFac.CreateCPU()).ShowComputer()
}


12-抽象工廠方法模式(Easy搞定Golang設計模式)的評論 (共 條)

分享到微博請遵守國家法律
甘孜| 分宜县| 进贤县| 新野县| 无锡市| 溧水县| 潜山县| 苍南县| 寻乌县| 虎林市| 织金县| 乐平市| 株洲县| 黑龙江省| 顺义区| 凌云县| 乳山市| 东平县| 米脂县| 齐齐哈尔市| 商洛市| 黄浦区| 安泽县| 绥阳县| 澄城县| 米脂县| 沿河| 依兰县| 隆化县| 雅安市| 廉江市| 沂南县| 夏邑县| 松滋市| 安福县| 克什克腾旗| 鸡东县| 霍州市| 任丘市| 章丘市| 唐海县|