抽象工厂模式 Golang

参考链接
练习:
设计一个电脑主板架构,电脑包括(显卡,内存,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 %d\n", 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 %d\n", 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 %d\n", 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")
}


posted @   Notomato  阅读(54)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
点击右上角即可分享
微信分享提示