原型设计模式go实现尝试

文章目录


前言

本文章尝试使用go实现"原型"。


代码

go 复制代码
package main

import (
	"fmt"
)

// 不同原型标志枚举
type Type int

const (
	PROTOTYPE_1 Type = iota
	PROTOTYPE_2
)

// 原型接口
type IPrototype interface {
	Clone() IPrototype
	Method(value int)
	Print()
}

// 具体原型1
type ConcretePrototype1 struct {
	name  string
	value int
}

// 构造函数
func NewConcretePrototype1ByFields(name string, value int) *ConcretePrototype1 {
	return &ConcretePrototype1{
		name,
		value,
	}
}

func NewConcretePrototype1ByObject(cp *ConcretePrototype1) *ConcretePrototype1 {
	return &ConcretePrototype1{
		name:  cp.name,
		value: cp.value,
	}
}

// 接口方法
func (cp *ConcretePrototype1) Clone() IPrototype {
	return NewConcretePrototype1ByObject(cp)
}

func (cp *ConcretePrototype1) Method(value int) {
	cp.value = value
}

func (cp *ConcretePrototype1) Print() {
	fmt.Println("Call Method1 from ", cp.name, " with field : ", cp.value)
}

// 具体原型2
type ConcretePrototype2 struct {
	name  string
	value int
}

// 构造函数
func NewConcretePrototype2ByFields(name string, value int) *ConcretePrototype2 {
	return &ConcretePrototype2{
		name,
		value,
	}
}

func NewConcretePrototype2ByObject(cp *ConcretePrototype2) *ConcretePrototype2 {
	return &ConcretePrototype2{
		name:  cp.name,
		value: cp.value,
	}
}

// 接口方法
func (cp *ConcretePrototype2) Clone() IPrototype {
	return NewConcretePrototype2ByObject(cp)
}

func (cp *ConcretePrototype2) Method(value int) {
	cp.value = value
}

func (cp *ConcretePrototype2) Print() {
	fmt.Println("Call Method2 from ", cp.name, " with field : ", cp.value)
}

// 原型工厂
type PrototypeFactory struct {
	prototypes map[Type]IPrototype
}

func NewPrototypeFactory() *PrototypeFactory {
	return &PrototypeFactory{
		prototypes: map[Type]IPrototype{
			PROTOTYPE_1: NewConcretePrototype1ByFields("PROTOTYPE_1 ", 1),
			PROTOTYPE_2: NewConcretePrototype2ByFields("PROTOTYPE_2 ", 2),
		},
	}
}

func (p *PrototypeFactory) CreatePrototype(t Type) IPrototype {
	return p.prototypes[t].Clone()
}

// 客户端代码
func clientCode(p *PrototypeFactory) {
	fmt.Println("Let's create a Prototype 1")

	prototype1 := p.CreatePrototype(PROTOTYPE_1)
	prototype2 := p.CreatePrototype(PROTOTYPE_1)
	prototype1.Method(3)
	prototype2.Method(4)
	prototype1.Print()
	prototype2.Print()

	fmt.Println()

	fmt.Println("Let's create a Prototype 2")

	prototype1 = p.CreatePrototype(PROTOTYPE_2)
	prototype2 = p.CreatePrototype(PROTOTYPE_2)
	prototype1.Method(5)
	prototype2.Method(6)
	prototype1.Print()
	prototype2.Print()
}

func main() {
	clientCode(NewPrototypeFactory())
}

结果

复制代码
Let's create a Prototype 1
Call Method1 from  PROTOTYPE_1   with field :  3
Call Method1 from  PROTOTYPE_1   with field :  4

Let's create a Prototype 2
Call Method2 from  PROTOTYPE_2   with field :  5
Call Method2 from  PROTOTYPE_2   with field :  6

总结

新人设计模式理解,望大家多多指点。

相关推荐
欧先生^_^5 小时前
rust语言,与c,go语言一样也是编译成二进制文件吗?
c语言·golang·rust
背帆16 小时前
go的interface接口底层实现
开发语言·后端·golang
敲代码的 蜡笔小新18 小时前
【行为型之命令模式】游戏开发实战——Unity可撤销系统与高级输入管理的架构秘钥
unity·设计模式·架构·命令模式
m0_5557629018 小时前
D-Pointer(Pimpl)设计模式(指向实现的指针)
设计模式
小Mie不吃饭18 小时前
【23种设计模式】分类结构有哪些?
java·设计模式·设计规范
阑梦清川21 小时前
关于Go语言的开发环境的搭建
开发语言·后端·golang
言之。21 小时前
Makefile 在 Go 项目中的实践
开发语言·elasticsearch·golang
Clown951 天前
go-zero(十八)结合Elasticsearch实现高效数据检索
开发语言·elasticsearch·golang
君鼎1 天前
C++设计模式——单例模式
c++·单例模式·设计模式
敲代码的 蜡笔小新1 天前
【行为型之中介者模式】游戏开发实战——Unity复杂系统协调与通信架构的核心秘诀
unity·设计模式·c#·中介者模式