原型设计模式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

总结

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

相关推荐
在未来等你40 分钟前
AI Agent设计模式 Day 1:ReAct模式:推理与行动的完美结合
设计模式·llm·react·ai agent·plan-and-execute
钢门狂鸭10 小时前
go开发规范指引
开发语言·驱动开发·golang
脚踏实地的大梦想家11 小时前
【Go】P19 Go语言并发编程核心(三):从 Channel 安全到互斥锁
开发语言·安全·golang
乐悠小码12 小时前
Java设计模式精讲---03建造者模式
java·设计模式·建造者模式
Tony Bai12 小时前
Go GUI 开发的“绝境”与“破局”:2025 年现状与展望
开发语言·后端·golang
豆浆whisky12 小时前
Go分布式追踪实战:从理论到OpenTelemetry集成|Go语言进阶(15)
开发语言·分布式·golang
Tony Bai12 小时前
【Go模块构建与依赖管理】08 深入 Go Module Proxy 协议
开发语言·后端·golang
QX_hao12 小时前
【Go】--文件和目录的操作
开发语言·c++·golang
_院长大人_17 小时前
设计模式-代理模式
设计模式·代理模式
guangzan18 小时前
TypeScript 中的单例模式
设计模式