• 欢迎光临~

抽象工厂模式 Golang

开发技术 开发技术 2022-09-29 次浏览

参考链接
练习:
设计一个电脑主板架构,电脑包括(显卡,内存,CPU)3个固定的插口,显卡具有显示功能(display,功能实现只要打印出意义即可),内存具有存储功能(storage),cpu具有计算功能(calculate)。
现有Intel厂商,nvidia厂商,Kingston厂商,均会生产以上三种硬件。
要求组装两台电脑,
1台(Intel的CPU,Intel的显卡,Intel的内存)
1台(Intel的CPU, nvidia的显卡,Kingston的内存)
用抽象工厂模式实现。

package main

import "fmt"

// ===========抽象层===========
type AbstractGPU interface {
	display()
}
type AbstractCPU interface {
	calculate(a, b int)
}
type AbstractMemory interface {
	storage(data string)
}

// 抽象工厂
type AbstractFactory interface {
	CreateGPU() AbstractGPU
	CreateCPU() AbstractCPU
	CreateMemory() AbstractMemory
}

//============实现层=============
// Intel intel 产品族
type IntelMemory struct {
	memory string
}

func (i *IntelMemory) storage(data string) {
	//TODO implement me
	i.memory = data
	fmt.Printf("%s stroage successful!n", i.memory)
}

type IntelCPU struct {
}

func (i *IntelCPU) calculate(a, b int) {
	//TODO implement me
	fmt.Printf("calc %d + %d is %dn", a, b, a+b)
}

type IntelGPU struct {
}

func (i *IntelGPU) display() {
	//TODO implement me
	fmt.Println("i am intel")
}

type IntelFactory struct {
}

func (i *IntelFactory) CreateGPU() AbstractGPU {
	var GPU AbstractGPU
	GPU = new(IntelGPU)
	return GPU
	//return &IntelGPU{}
}
func (i *IntelFactory) CreateCPU() AbstractCPU {
	return &IntelCPU{}
}
func (i *IntelFactory) CreateMemory() AbstractMemory {
	return &IntelMemory{}
}

// Nvidia nvidia 产品族
type NvidiaMemory struct {
}

func (i *NvidiaMemory) storage(data string) {
	//TODO implement me
	fmt.Printf("%s stroage successful!n", data)
}

type NvidiaCPU struct {
}

func (i *NvidiaCPU) calculate(a, b int) {
	//TODO implement me
	fmt.Printf("calc %d + %d is %dn", a, b, a+b)
}

type NvidiaGPU struct {
}

func (i *NvidiaGPU) display() {
	//TODO implement me
	fmt.Println("i am Nvidia")
}

type NvidiaFactory struct {
}

func (i *NvidiaFactory) CreateGPU() AbstractGPU {
	var GPU AbstractGPU
	GPU = new(NvidiaGPU)
	return GPU
	//return &NvidiaGPU{}
}

func (i *NvidiaFactory) CreateCPU() AbstractCPU {
	return &NvidiaCPU{}
}
func (i *NvidiaFactory) CreateMemory() AbstractMemory {
	return &NvidiaMemory{}
}

// Kingston kingston 产品族
type KingstonMemory struct {
}

func (i *KingstonMemory) storage(data string) {
	//TODO implement me
	fmt.Printf("%s stroage successful!n", data)
}

type KingstonCPU struct {
}

func (i *KingstonCPU) calculate(a, b int) {
	//TODO implement me
	fmt.Printf("calc %d + %d is %dn", a, b, a+b)
}

type KingstonGPU struct {
}

func (i *KingstonGPU) display() {
	//TODO implement me
	fmt.Println("i am Kingston")
}

type KingstonFactory struct {
}

func (i *KingstonFactory) CreateGPU() AbstractGPU {
	var GPU AbstractGPU
	GPU = new(KingstonGPU)
	return GPU
	//return &KingstonGPU{}
}

func (i *KingstonFactory) CreateCPU() AbstractCPU {
	return &KingstonCPU{}
}
func (i *KingstonFactory) CreateMemory() AbstractMemory {
	return new(KingstonMemory)
}

type Computer struct {
	cpu    AbstractCPU
	gpu    AbstractGPU
	memory AbstractMemory
}

func (c *Computer) calculate(a, b int) {
	c.cpu.calculate(a, b)
}
func (c *Computer) display() {
	c.gpu.display()
}
func (c *Computer) storage(data string) {
	c.memory.storage(data)
}

func main() {
	//创建Intel nvidia kingston工厂
	intelF := new(IntelFactory)
	nvidiaF := new(NvidiaFactory)
	kingstonF := new(KingstonFactory)

	myworkpc := new(Computer)
	myworkpc.cpu = intelF.CreateCPU()
	myworkpc.gpu = nvidiaF.CreateGPU()
	myworkpc.memory = kingstonF.CreateMemory()

	myworkpc.display()
	myworkpc.calculate(1, 2)
	myworkpc.storage("i love huxue")

	mygamepc := new(Computer)
	mygamepc.cpu = intelF.CreateCPU()
	mygamepc.gpu = nvidiaF.CreateGPU()
	mygamepc.memory = intelF.CreateMemory()

	mygamepc.display()
	mygamepc.calculate(114, 514)
	mygamepc.storage("i love huxue too")
}


程序员灯塔
转载请注明原文链接:抽象工厂模式 Golang
喜欢 (0)